Simple hash table implementation.
More...
#include <stddef.h>
#include <string.h>
#include <utils/gp_types.h>
Go to the source code of this file.
Simple hash table implementation.
Definition in file gp_htable.h.
◆ GP_HTABLE_FOREACH
#define GP_HTABLE_FOREACH |
( |
|
table, |
|
|
|
var |
|
) |
| |
Value: for (
struct gp_htable_rec *var = (table)->recs; var < &((table)->recs[(table)->size]); var++) \
if (var->key)
Hash table iterator.
Definition at line 52 of file gp_htable.h.
◆ gp_htable_flags
Flags to change how to deal with the hash table string keys.
Enumerator |
---|
GP_HTABLE_COPY_KEY | The key is copied on insert.
|
GP_HTABLE_FREE_KEY | The key is freed on removal.
|
GP_HTABLE_FREE_SELF | The gp_htable is freed at the end of gp_htable_free()
|
Definition at line 24 of file gp_htable.h.
◆ gp_htable_free()
Frees a hash table.
- Parameters
-
self | The table to be freed. |
◆ gp_htable_get()
void * gp_htable_get |
( |
gp_htable * |
self, |
|
|
const char * |
key |
|
) |
| |
Search for an element given a string key.
- Parameters
-
self | A hash table. |
key | A string key. |
- Returns
- A value if found or NULL.
◆ gp_htable_init()
int gp_htable_init |
( |
gp_htable * |
self, |
|
|
unsigned int |
order, |
|
|
int |
flags |
|
) |
| |
Initializes an hash table embedded in a different structure.
- Parameters
-
self | A pointer to an hash table to be initialized. |
order | Hint of log2(size) for expected number of hash elements, if unsure pass 0. |
flags | See enum gp_htable_flags. |
- Returns
- Zero on success, non-zero on allocation failure.
◆ gp_htable_keys()
static size_t gp_htable_keys |
( |
gp_htable * |
self | ) |
|
|
inlinestatic |
Returns the number of keys in hash table.
- Parameters
-
- Returns
- The number of keys in hash table.
Definition at line 87 of file gp_htable.h.
References gp_htable::used.
◆ gp_htable_new()
gp_htable * gp_htable_new |
( |
unsigned int |
order, |
|
|
int |
flags |
|
) |
| |
Allocates a hash table.
- Parameters
-
order | Hint of log2(size) for expected number of hash elements, if unsure pass 0. |
flags | See enum gp_htable_flags. |
Note that GP_HTABLE_FREE_SELF is added automatically to the flags so that the resulting table is freed on gp_htable_free().
- Returns
- Newly allocated hash table or NULL in a case of a malloc failure.
◆ gp_htable_put()
void gp_htable_put |
( |
gp_htable * |
self, |
|
|
void * |
val, |
|
|
char * |
key |
|
) |
| |
Adds a pointer to a hash table.
- Parameters
-
self | Hash table. |
val | A value. |
key | A string key. |
◆ gp_htable_rem()
void * gp_htable_rem |
( |
gp_htable * |
self, |
|
|
const char * |
key |
|
) |
| |
Removes an entry from a hash table.
- Parameters
-
self | A Hash table. |
key | A string key. |
- Returns
- A value for removed key or NULL if not found.
◆ gp_htable_strcmp()
static int gp_htable_strcmp |
( |
const void * |
key1, |
|
|
const void * |
key2 |
|
) |
| |
|
inlinestatic |
A string matching function.
- Parameters
-
key1 | A string. |
key2 | A string. |
- Returns
- Non-zero if keys are equal.
Definition at line 124 of file gp_htable.h.
◆ gp_htable_strhash()
static size_t gp_htable_strhash |
( |
const void * |
key, |
|
|
size_t |
htable_size |
|
) |
| |
|
inlinestatic |
A string hashing function.
- Parameters
-
key | A string. |
htable_size | A hash table size. |
Definition at line 105 of file gp_htable.h.