grafthistory: support curl
[elinks/elinks-j605.git] / src / util / hash.h
blobb98d0d57e1d64c49b49a9996fbaf964cb1da97e8
1 #ifndef EL__UTIL_HASH_H
2 #define EL__UTIL_HASH_H
4 #include "util/lists.h"
6 /* This should be hopefully always 32bit at least. I'm not sure what will
7 * happen when this will be of other length, but it should still work ok.
8 * --pasky */
9 typedef unsigned long hash_value_T;
10 typedef hash_value_T (* hash_func_T)(unsigned char *key, unsigned int keylen, hash_value_T magic);
12 struct hash_item {
13 LIST_HEAD(struct hash_item);
15 unsigned char *key;
16 unsigned int keylen;
17 void *value;
20 struct hash {
21 unsigned int width; /* Number of bits - hash array must be 2^width long. */
22 hash_func_T func;
23 struct list_head hash[1]; /* Must be at end ! */
26 #define hash_size(n) (1 << (n))
28 struct hash *init_hash(unsigned int width, hash_func_T func);
29 void free_hash(struct hash *hash);
31 struct hash_item *add_hash_item(struct hash *hash, unsigned char *key, unsigned int keylen, void *value);
32 struct hash_item *get_hash_item(struct hash *hash, unsigned char *key, unsigned int keylen);
33 void del_hash_item(struct hash *hash, struct hash_item *item);
34 hash_value_T strhash(unsigned char *k, unsigned int length, hash_value_T initval);
36 #define foreach_hash_item(item, hash_table, iterator) \
37 for (iterator = 0; iterator < hash_size((hash_table).width); iterator++) \
38 foreach (item, (hash_table).hash[iterator])
40 #endif