|
struct hash_table * | hash_table_new (int size) |
| Create an empty hash table. More...
|
|
void | hash_table_free (struct hash_table *ht) |
| Free a hash table. More...
|
|
void * | hash_table_lookup (const struct hash_table *ht, const char *key) |
| Retrieve an entry. More...
|
|
void * | hash_table_update (struct hash_table *ht, const char *key, void *value) |
| Add, update or remove an entry. More...
|
|
void * | hash_table_for_each (struct hash_table *ht, void *(*func)(const char *key, void *value)) |
| Iterate through all entries. More...
|
|
void * | hash_table_for_each2 (struct hash_table *ht, void *(*func)(const char *key, void *value, void *user), void *user) |
| Iterate through all entries with user pointer. More...
|
|
int | hash_table_delete_if (struct hash_table *ht, int(*pred)(const char *key, void *value, void *user), void *user) |
| Delete all entries matching predicate. More...
|
|
void | hash_table_debug_dump (const struct hash_table *ht, struct membuf_t *buf) |
| Debugging: dump contents of hash table to memory buffer. More...
|
|
A generic hash table implementation. The key of the hash table is a null-terminated C string. The hash table is automatically resized when necessary, in order to reduce hash collisions and keep lookup operations as O(1). The maximum number of bins is 2^31, though there is no limit to the number of entries.
This is a wrapper around the Dynamic hash table (memory keys) module.
◆ hash_table_new()
struct hash_table* hash_table_new |
( |
int |
size | ) |
|
Create an empty hash table.
- Parameters
-
size | Initial guess at size of hash table. May be 0. |
- Returns
- Pointer to newly-allocated opaque hash table object.
Creates and initialises an empty hash table object. The initial number of bins to allocate may be specified in size, which is the base-2 logarithm of the total number (i.e. passing size as 4 gets you 16 bins). Passing anything less than 4 will still allocate 16 bins.
◆ hash_table_free()
void hash_table_free |
( |
struct hash_table * |
ht | ) |
|
Free a hash table.
- Parameters
-
Frees a hash table, but does not alter the objects pointed to by the table. In order to free memory associated with entries, call hash_table_for_each(). If ht is 0, no action is taken.
◆ hash_table_lookup()
void* hash_table_lookup |
( |
const struct hash_table * |
ht, |
|
|
const char * |
key |
|
) |
| |
Retrieve an entry.
- Parameters
-
ht | Hash table. |
key | Key of entry to look up. |
- Returns
- Value associated with key.
- Return values
-
0 | if there is no associated value. |
Retrieves the value associated with the entry with key key. If there is no such entry, then 0 is returned.
◆ hash_table_update()
void* hash_table_update |
( |
struct hash_table * |
ht, |
|
|
const char * |
key, |
|
|
void * |
value |
|
) |
| |
Add, update or remove an entry.
- Parameters
-
ht | Hash table. |
key | Key of entry. |
value | Value to store. 0 to delete. |
- Returns
- Previous value.
- Return values
-
0 | if there was no previous value. |
Associates the value value with an entry with key key. If key had a previous value associated with it, this previous value is returned. Passing a value of 0 will clear the entry, freeing the memory associated with the entry. If key had no previous value, 0 will be returned, and the new value will be stored (or no action will be taken if value is 0).
Note that key is copied when creating a new entry, so the memory it points to need not persist past the function call.
◆ hash_table_for_each()
void* hash_table_for_each |
( |
struct hash_table * |
ht, |
|
|
void *(*)(const char *key, void *value) |
func |
|
) |
| |
Iterate through all entries.
- Parameters
-
ht | Hash table. |
func | Callback function for each entry. |
- Return values
-
0 | if func always returned 0. |
- Returns
- First non-zero value returned from func otherwise.
Iterates through each entry in the hash table ht and calls func for each. func takes two arguments: the key and the value. If func ever returns non-zero, the iteration is aborted and the value returned from func is returned.
◆ hash_table_for_each2()
void* hash_table_for_each2 |
( |
struct hash_table * |
ht, |
|
|
void *(*)(const char *key, void *value, void *user) |
func, |
|
|
void * |
user |
|
) |
| |
Iterate through all entries with user pointer.
- Parameters
-
ht | Hash table. |
func | Callback function for each entry. |
user | Opaque user pointer to pass to callback. |
- Return values
-
0 | if func always returned 0. |
- Returns
- First non-zero value returned from func otherwise.
Iterates through each entry in the hash table ht and calls func for each. func takes three arguments: the key, the value and an opaque user pointer user. If func ever returns non-zero, the iteration is aborted and the value returned from func is returned.
◆ hash_table_delete_if()
int hash_table_delete_if |
( |
struct hash_table * |
ht, |
|
|
int(*)(const char *key, void *value, void *user) |
pred, |
|
|
void * |
user |
|
) |
| |
Delete all entries matching predicate.
- Parameters
-
ht | Hash table. |
pred | Predicate function (returning 0 to keep or non-0 to delete). |
user | Opaque pointer to pass to callbacks (may be 0). |
- Returns
- Number of entries removed.
For each entry in the hash table ht, this function calls pred. This callback is similar to that of hash_table_for_each(), except that it returns an integer, with 0 meaning ‘keep element’ and non-0 meaning ‘delete element’.
If any cleanup of the element needs to be done (e.g. freeing of memory), this can be done in pred before it returns. Other hash table functions, with the exception of lookups, should not be called on ht from within the callback.
◆ hash_table_debug_dump()
void hash_table_debug_dump |
( |
const struct hash_table * |
ht, |
|
|
struct membuf_t * |
buf |
|
) |
| |
Debugging: dump contents of hash table to memory buffer.
- Parameters
-
ht | Hash table. |
buf | Memory buffer to dump to. |
A debugging function which will dump the contents of the hash table ht (i.e. its keys and the pointer value associated with them) to the memory buffer buf. The dump is in human-readable form and will consist only of printable characters. It is terminated with a newline.