1 #ifndef HASH2_H
2 #define HASH2_H
3
4 #include "hash.h"
5
6 struct hash2_iter {
7 struct hash2_value *value, *next_value;
8 unsigned int key_hash;
9 };
10
11 /* Returns hash code for the key. */
12 typedef unsigned int hash2_key_callback_t(const void *key);
13 /* Returns TRUE if the key matches the value. */
14 typedef bool hash2_cmp_callback_t(const void *key, const void *value,
15 void *context);
16
17 /* Create a new hash table. If initial_size is 0, the default value is used. */
18 struct hash2_table *
19 hash2_create(unsigned int initial_size, unsigned int value_size,
20 hash2_key_callback_t *key_hash_cb,
21 hash2_cmp_callback_t *key_compare_cb, void *context) ATTR_NULL(5);
22 void hash2_destroy(struct hash2_table **hash);
23 /* Remove all nodes from hash table. */
24 void hash2_clear(struct hash2_table *hash);
25
26 void *hash2_lookup(const struct hash2_table *hash, const void *key) ATTR_PURE;
27 /* Iterate through all nodes with the given hash. iter must initially be
28 zero-filled. */
29 void *hash2_iterate(const struct hash2_table *hash,
30 unsigned int key_hash, struct hash2_iter *iter);
31 /* Insert node to the hash table and returns pointer to the value that can be
32 written to. Assumes it doesn't already exist (or that a duplicate entry
33 is wanted). */
34 void *hash2_insert(struct hash2_table *hash, const void *key);
35 /* Like hash2_insert(), but insert directly using a hash. */
36 void *hash2_insert_hash(struct hash2_table *hash, unsigned int key_hash);
37 /* Remove a node. */
38 void hash2_remove(struct hash2_table *hash, const void *key);
39 /* Remove the last node iterator returned. Iterating continues from the next
40 node. */
41 void hash2_remove_iter(struct hash2_table *hash, struct hash2_iter *iter);
42 /* Return the number of nodes in hash table. */
43 unsigned int hash2_count(const struct hash2_table *hash) ATTR_PURE;
44
45 /* can be used with string keys */
hash2_strcmp(const void * a,const void * b,void * ctx ATTR_UNUSED)46 static inline bool hash2_strcmp(const void *a, const void *b, void *ctx ATTR_UNUSED)
47 {
48 return strcmp(a, b) == 0;
49 }
50
hash2_str_hash(const void * key)51 static inline unsigned int hash2_str_hash(const void *key)
52 {
53 return str_hash(key);
54 }
55
56 #endif
57