Debian Codegraph @

src/utils/hash.c

raw
1 #include "utils/hash.h"
2 #include <assert.h>
3 #include <stdbool.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <string.h>
7
8 #include "lib/a5hash-5.25.h"
9
10 static uint64_t
11 hash_hkey (hkey_t key)
12 {
13 #if __SIZEOF_POINTER__ == 8
14 return a5hash (key.start, key.len, 0);
15 #else
16 return a5hash32 (key.start, key.len, 0);
17 #endif
18 }
19
20 static bool
21 cmpr_hkey (hkey_t key1, hkey_t key2)
22 {
23 if (key1.len != key2.len)
24 return false;
25 return !memcmp (key1.start, key2.start, key1.len);
26 }
27
28 #define NAME str_table
29 #define KEY_TY hkey_t
30 #define VAL_TY uint16_t
31 #define HASH_FN hash_hkey
32 #define CMPR_FN cmpr_hkey
33 #include "lib/verstable-2.2.1.h"
34
35 str_table hash_table;
36
37 int
38 insert_key (hkey_t *key, uint16_t line_nr)
39 {
40 size_t prev_size = vt_size (&hash_table);
41 str_table_itr itr = vt_get_or_insert (&hash_table, *key, line_nr);
42 if (vt_is_end (itr))
43 return -1;
44 if (prev_size == vt_size (&hash_table))
45 return 1;
46 return 0;
47 }
48
49 uint16_t
50 find_key (hkey_t *key)
51 {
52 str_table_itr itr = vt_get (&hash_table, *key);
53 if (vt_is_end (itr))
54 return -1;
55 return itr.data->val;
56 }
57
58 void
59 init_table (void)
60 {
61 vt_init (&hash_table);
62 }
63
64 void
65 free_table (void)
66 {
67 vt_cleanup (&hash_table);
68 }