hash.h revision c23ce1889e343431078015dfa7c84206e7a9aa5f
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch#ifndef __HASH_H
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen#define __HASH_H
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen/* Returns hash code. */
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainentypedef unsigned int hash_callback_t(const void *p);
65988f5a8abed57e9894fec77105941e046d3490Timo Sirainen/* Returns 0 if the pointers are equal. */
72388282bf6718c39af34cfcf51438910f9d62daTimo Sirainentypedef int hash_cmp_callback_t(const void *p1, const void *p2);
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen/* Create a new hash table. If initial_size is 0, the default value is used.
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen If hash_cb or key_compare_cb is NULL, direct hashing/comparing is used.
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen table_pool is used to allocate/free large hash tables, node_pool is used
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainen for smaller allocations and can also be alloconly pool. The pools must not
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen be free'd before hash_destroy() is called. */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenstruct hash_table *
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenhash_create(pool_t table_pool, pool_t node_pool, unsigned int initial_size,
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen hash_callback_t *hash_cb, hash_cmp_callback_t *key_compare_cb);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid _hash_destroy(struct hash_table **table);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen#define hash_destroy(table) _hash_destroy(&(table))
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* Remove all nodes from hash table. If free_collisions is TRUE, the
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen memory allocated from node_pool is freed, or discarded with
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen alloconly pools. */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid hash_clear(struct hash_table *table, bool free_collisions);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid *hash_lookup(const struct hash_table *table, const void *key);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenbool hash_lookup_full(const struct hash_table *table, const void *lookup_key,
79454ba23ef6baf56997cd3cc23123eb69ae4f4cTimo Sirainen void **orig_key, void **value);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* Insert/update node in hash table. The difference is that hash_insert()
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen replaces the key in table to given one, while hash_update() doesnt. */
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainenvoid hash_insert(struct hash_table *table, void *key, void *value);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid hash_update(struct hash_table *table, void *key, void *value);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid hash_remove(struct hash_table *table, const void *key);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenunsigned int hash_size(const struct hash_table *table);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* Iterates through all nodes in hash table. You may safely call hash_*()
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen functions while iterating, but if you add any new nodes, they may or may
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainen not be called for in this iteration. */
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainenstruct hash_iterate_context *hash_iterate_init(struct hash_table *table);
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainenbool hash_iterate(struct hash_iterate_context *ctx,
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainen void **key_r, void **value_r);
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainenvoid hash_iterate_deinit(struct hash_iterate_context *ctx);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* Hash table isn't resized, and removed nodes aren't removed from
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen the list while hash table is freezed. Supports nesting. */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid hash_freeze(struct hash_table *table);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenvoid hash_thaw(struct hash_table *table);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* Copy all nodes from one hash table to another */
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainenvoid hash_copy(struct hash_table *dest, struct hash_table *src);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen/* hash function for strings */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenunsigned int str_hash(const void *p);
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenunsigned int strcase_hash(const void *p);
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainen#endif
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen