2005-09-15 09:58:31 -04:00
|
|
|
#ifndef EL__UTIL_HASH_H
|
|
|
|
#define EL__UTIL_HASH_H
|
|
|
|
|
|
|
|
#include "util/lists.h"
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** This should be hopefully always 32bit at least. I'm not sure what will
|
2005-09-15 09:58:31 -04:00
|
|
|
* happen when this will be of other length, but it should still work ok.
|
|
|
|
* --pasky */
|
|
|
|
typedef unsigned long hash_value_T;
|
|
|
|
typedef hash_value_T (* hash_func_T)(unsigned char *key, unsigned int keylen, hash_value_T magic);
|
|
|
|
|
|
|
|
struct hash_item {
|
|
|
|
LIST_HEAD(struct hash_item);
|
|
|
|
|
|
|
|
unsigned char *key;
|
|
|
|
unsigned int keylen;
|
|
|
|
void *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hash {
|
2007-07-27 05:35:13 -04:00
|
|
|
unsigned int width; /**< Number of bits - hash array must be 2^width long. */
|
2005-09-15 09:58:31 -04:00
|
|
|
hash_func_T func;
|
2007-07-27 05:35:13 -04:00
|
|
|
struct list_head hash[1]; /**< Must be at end ! */
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
2006-05-31 13:17:01 -04:00
|
|
|
struct hash *init_hash8(void);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-05-31 13:33:36 -04:00
|
|
|
void free_hash(struct hash **hashp);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
struct hash_item *add_hash_item(struct hash *hash, unsigned char *key, unsigned int keylen, void *value);
|
|
|
|
struct hash_item *get_hash_item(struct hash *hash, unsigned char *key, unsigned int keylen);
|
|
|
|
void del_hash_item(struct hash *hash, struct hash_item *item);
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates hash */
|
2005-09-15 09:58:31 -04:00
|
|
|
#define foreach_hash_item(item, hash_table, iterator) \
|
2006-05-31 13:17:01 -04:00
|
|
|
for (iterator = 0; iterator < (1 << (hash_table).width); iterator++) \
|
2005-09-15 09:58:31 -04:00
|
|
|
foreach (item, (hash_table).hash[iterator])
|
|
|
|
|
|
|
|
#endif
|