blob: e2c8c71eb3a212cb95b58a3dec1d1848e9d19ab9 [file] [log] [blame]
Neels Hofmeyr4f0dbac2024-05-23 02:59:53 +02001#include <osmocom/core/linuxlist.h>
2#include <osmocom/core/hashtable.h>
3#include <osmocom/core/jhash.h>
4
5struct item {
6 const char blob[32];
7 struct hlist_node node;
8};
9
10struct item items[] = {
11 { "blob one", },
12 { "blob two and five are the same", },
13 { "third blob", },
14 { "fourth blob", },
15 { "blob two and five are the same", },
16};
17
18uint32_t item_hash(const struct item *item)
19{
20 return osmo_jhash(item->blob, strlen(item->blob), 0);
21}
22
23int main(void)
24{
25 int i;
26 struct item *item;
27
28 DECLARE_HASHTABLE(haystack, 5);
29 hash_init(haystack);
30
31 printf("add:\n");
32 for (i = 0; i < ARRAY_SIZE(items); i++) {
33 uint32_t hash;
34 item = &items[i];
35 hash_add(haystack, &item->node, hash = item_hash(item));
36 printf("- adding items[%d]#%x = %s\n", i, hash, item->blob);
37 }
38
39 printf("list:\n");
40 hash_for_each (haystack, i, item, node)
41 printf("- %s [%d]\n", item->blob, (int)(item - items));
42
43 printf("find:\n");
44 for (i = 0; i < ARRAY_SIZE(items); i++) {
45 uint32_t hash;
46 struct item *needle = &items[i];
47 hash = item_hash(needle);
48 printf("- looking up items[%d]#%x = %s\n", i, hash, needle->blob);
49 hash_for_each_possible (haystack, item, node, hash)
50 printf(" - %s items[%d]\n",
51 (item == needle) ? "found" : "not",
52 (int)(item - items));
53 }
54
55 return 0;
56}