bglibs
|
Data Structures | |
struct | ghash |
struct | ghashiter |
Macros | |
#define | ghash_entry_hash(P) (*(adt_hash_t*)(P)) |
#define | ghash_entry_keyptr(P) ((P)+sizeof(adt_hash_t)) |
#define | ghash_entry_dataptr(P, L) ((P)+sizeof(adt_hash_t)+(L)) |
#define | ghash_hashb adt_hashb |
#define | ghash_hashs adt_hashs |
#define | ghash_hashsp adt_hashsp |
#define | GHASH_STRUCT_ENTRY(PREFIX, KTYPE, DTYPE) |
#define | GHASH_KEYOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->key) |
#define | GHASH_DATAOFFSET(PREFIX) ((unsigned long)&((struct PREFIX##_entry*)0)->data) |
#define | GHASH_KEYSIZE(PREFIX) |
#define | GHASH_DECL(PREFIX, KTYPE, DTYPE) |
#define | GHASH_INIT_DEFN(PREFIX, KTYPE, DTYPE, HASHFN, CMP, KCOPY, DCOPY, KFREE, DFREE) |
#define | GHASH_FREE_DEFN(PREFIX) |
#define | GHASH_ADD_DEFN(PREFIX, KTYPE, DTYPE) |
#define | GHASH_SET_DEFN(PREFIX, KTYPE, DTYPE) |
#define | GHASH_GET_DEFN(PREFIX, KTYPE) |
#define | GHASH_REBUILD_DEFN(PREFIX) |
#define | GHASH_REHASH_DEFN(PREFIX) |
#define | GHASH_REMOVE_DEFN(PREFIX, KTYPE) |
#define | GHASH_FOREACH_DEFN(PREFIX) |
#define | GHASH_SEARCH_DEFN(PREFIX) |
#define | GHASH_DEFN(PREFIX, KTYPE, DTYPE, HASHFN, CMPFN, KCOPY, DCOPY, KFREE, DFREE) |
#define | ghashiter_loop(I, G) for(ghashiter_first(I,G);ghashiter_valid(I);ghashiter_next(I)) |
Functions | |
void | ghash_insert (struct ghash *d, void *e) |
void * | ghash_add (struct ghash *d, const void *key, const void *data) |
void * | ghash_set (struct ghash *d, const void *key, const void *data) |
void | ghash_free (struct ghash *d) |
void ** | ghash_find (struct ghash *d, const void *key) |
void * | ghash_get (struct ghash *d, const void *key) |
void | ghash_init (struct ghash *d, unsigned long keysize, unsigned long entrysize, adt_hash_fn *hashfn, adt_cmp_fn *keycmp, adt_copy_fn *keycopy, adt_copy_fn *datacopy, adt_free_fn *keyfree, adt_free_fn *datafree) |
int | ghash_rebuild (struct ghash *d) |
int | ghash_rehash (struct ghash *d) |
int | ghash_remove (struct ghash *d, const void *key) |
void | ghash_foreach (struct ghash *d, void(*fn)(void *entry)) |
void * | ghash_search (struct ghash *d, int(*fn)(const void *entry)) |
void | ghashiter_first (struct ghashiter *, const struct ghash *) |
int | ghashiter_valid (const struct ghashiter *) |
void | ghashiter_next (struct ghashiter *) |
The actual hash table manipulation functions use a simple linear probing algorithm with a dynamic table size. Since many more slots are allocated than are in use at any given time, there are always plenty of empty slots available to make searches short. Since each shot is a single pointer, these extra slots don't consume a significant amount of memory.
The structure at the center of the ghash
implementation contains pointers to the actual data (including all relevant metadata) plus function pointers to the functions needed to manipulate the data.
#define GHASH_ADD_DEFN | ( | PREFIX, | |
KTYPE, | |||
DTYPE | |||
) |
Define a specialized ghash
table add function.
#define GHASH_DATAOFFSET | ( | PREFIX | ) | ((unsigned long)&((struct PREFIX##_entry*)0)->data) |
The offset of the data into a specialized ghash
entry.
#define GHASH_DECL | ( | PREFIX, | |
KTYPE, | |||
DTYPE | |||
) |
Declare all the prototypes for a ghash
table, specialized to particular key and data types.
#define GHASH_DEFN | ( | PREFIX, | |
KTYPE, | |||
DTYPE, | |||
HASHFN, | |||
CMPFN, | |||
KCOPY, | |||
DCOPY, | |||
KFREE, | |||
DFREE | |||
) |
Define all necessary functions for a specialized ghash
table. If either of the copy functions KCOPY
or DCOPY
are NULL
, a simple memcpy is used instead. If either of the free functions KFREE
or DFREE
are NULL, no data freeing is attempted.
#define ghash_entry_dataptr | ( | P, | |
L | |||
) | ((P)+sizeof(adt_hash_t)+(L)) |
The data structure address within an entry P
. The offset parameter L
is the size of the key structure.
Referenced by ghash_add(), ghash_free(), ghash_remove(), and ghash_set().
#define ghash_entry_hash | ( | P | ) | (*(adt_hash_t*)(P)) |
The hash value stored within an entry P
.
Referenced by ghash_add(), ghash_find(), ghash_insert(), and ghash_rehash().
#define ghash_entry_keyptr | ( | P | ) | ((P)+sizeof(adt_hash_t)) |
The key structure address within an entry P
.
Referenced by ghash_add(), ghash_find(), ghash_free(), ghash_rehash(), and ghash_remove().
#define GHASH_FOREACH_DEFN | ( | PREFIX | ) |
#define GHASH_FREE_DEFN | ( | PREFIX | ) |
#define GHASH_GET_DEFN | ( | PREFIX, | |
KTYPE | |||
) |
#define ghash_hashb adt_hashb |
The adt_hashb
function also works for ghash
#define ghash_hashs adt_hashs |
The adt_hashs
function also works for ghash
#define ghash_hashsp adt_hashsp |
The adt_hashsp
function also works for ghash
#define GHASH_INIT_DEFN | ( | PREFIX, | |
KTYPE, | |||
DTYPE, | |||
HASHFN, | |||
CMP, | |||
KCOPY, | |||
DCOPY, | |||
KFREE, | |||
DFREE | |||
) |
Define a specialized ghash
table init function.
#define GHASH_KEYOFFSET | ( | PREFIX | ) | ((unsigned long)&((struct PREFIX##_entry*)0)->key) |
The offset of the key into a specialized ghash
entry.
#define GHASH_KEYSIZE | ( | PREFIX | ) |
The actual size of the key data in a specialized ghash
entry.
#define GHASH_REBUILD_DEFN | ( | PREFIX | ) |
Define a specialized ghash
table rebuild function.
#define GHASH_REHASH_DEFN | ( | PREFIX | ) |
Define a specialized ghash
table rehash function.
#define GHASH_REMOVE_DEFN | ( | PREFIX, | |
KTYPE | |||
) |
Define a specialized ghash
table remove function.
#define GHASH_SEARCH_DEFN | ( | PREFIX | ) |
Define a specialized ghash
table search function.
#define GHASH_SET_DEFN | ( | PREFIX, | |
KTYPE, | |||
DTYPE | |||
) |
Define a specialized ghash
table add function.
#define GHASH_STRUCT_ENTRY | ( | PREFIX, | |
KTYPE, | |||
DTYPE | |||
) |
Prototype for the ghash
entry structure, containing a key of type KTYPE
and data of type DTYPE
.
#define ghashiter_loop | ( | I, | |
G | |||
) | for(ghashiter_first(I,G);ghashiter_valid(I);ghashiter_next(I)) |
A convenience macro which expands to a for
loop using the ghashiter
I to iterate over the table G
.
void* ghash_add | ( | struct ghash * | d, |
const void * | key, | ||
const void * | data | ||
) |
Add an entry into a generic hash table. This function adds a new entry into the given hash table. If the table is too small to provide sufficient slots for efficient access, the table is automatically expanded to the next larger size and all the existing entries are copied over.
References ghash::count, ghash::datacopy, ghash::entrysize, ghash_entry_dataptr, ghash_entry_hash, ghash_entry_keyptr, ghash_insert(), ghash::hashfn, ghash::keycopy, ghash::keyfree, and ghash::keysize.
Referenced by ghash_set().
void** ghash_find | ( | struct ghash * | d, |
const void * | key | ||
) |
Find an entry in a ghash
table.
NULL
if the key
was not found. References ghash_entry_hash, ghash_entry_keyptr, ghash::hashfn, ghash::keycmp, ghash::size, and ghash::table.
Referenced by ghash_get(), and ghash_remove().
void ghash_foreach | ( | struct ghash * | d, |
void(*)(void *entry) | fn | ||
) |
Iterate over a ghash
table, calling a function for each entry.
References ghash::size, and ghash::table.
void ghash_free | ( | struct ghash * | d | ) |
Free all data (and entries) in a ghash
table.
References ghash::datafree, ghash_entry_dataptr, ghash_entry_keyptr, ghash::keyfree, ghash::keysize, ghash::size, and ghash::table.
void* ghash_get | ( | struct ghash * | d, |
const void * | key | ||
) |
Find an entry in a ghash
table.
NULL
if the key
was not found. References ghash_find().
Referenced by ghash_set().
void ghash_init | ( | struct ghash * | d, |
unsigned long | keysize, | ||
unsigned long | entrysize, | ||
adt_hash_fn * | hashfn, | ||
adt_cmp_fn * | keycmp, | ||
adt_copy_fn * | keycopy, | ||
adt_copy_fn * | datacopy, | ||
adt_free_fn * | keyfree, | ||
adt_free_fn * | datafree | ||
) |
Initialize an empty ghash
table.
References ghash::count, ghash::datacopy, ghash::datafree, ghash::entrysize, ghash::hashfn, ghash::keycmp, ghash::keycopy, ghash::keyfree, ghash::keysize, ghash::size, and ghash::table.
void ghash_insert | ( | struct ghash * | d, |
void * | e | ||
) |
Insert an entry into a ghash
table, without resizing it.
References ghash::count, ghash_entry_hash, ghash::size, and ghash::table.
Referenced by ghash_add(), ghash_rebuild(), and ghash_remove().
int ghash_rebuild | ( | struct ghash * | d | ) |
Rebuild the entry pointer table in a ghash
table. This function is used internally after either rehashing the table or removing an entry.
References ghash::count, ghash_insert(), ghash::size, and ghash::table.
Referenced by ghash_rehash().
int ghash_rehash | ( | struct ghash * | d | ) |
Regenerate all the hash values in a ghash
table and then rebuild it. Use this function when any of the keys change value.
References ghash_entry_hash, ghash_entry_keyptr, ghash_rebuild(), ghash::hashfn, ghash::size, and ghash::table.
int ghash_remove | ( | struct ghash * | d, |
const void * | key | ||
) |
Remove an entry from a ghash
table. This function attempts to do the minimum amount of rebuilding necessary to adjust the positions of entries that may fall in the same slot as the newly removed entry.
References ghash::count, ghash::datafree, ghash_entry_dataptr, ghash_entry_keyptr, ghash_find(), ghash_insert(), ghash::keyfree, ghash::keysize, ghash::size, and ghash::table.
void* ghash_search | ( | struct ghash * | d, |
int(*)(const void *entry) | fn | ||
) |
Search for the first entry in the ghash
table for which the given function returns true.
References ghash::size, and ghash::table.
void* ghash_set | ( | struct ghash * | d, |
const void * | key, | ||
const void * | data | ||
) |
Replace or add an entry into a generic hash table.
References ghash::datacopy, ghash::entrysize, ghash_add(), ghash_entry_dataptr, ghash_get(), and ghash::keysize.
Set a ghash
iterator to the first element in the table.
References ghashiter::ghashp.
void ghashiter_next | ( | struct ghashiter * | iter | ) |
Advance the ghash
iterator to the next element in the table.
References ghashiter::index.
int ghashiter_valid | ( | const struct ghashiter * | iter | ) |
Check if the ghash
iterator is still valid.
References ghashiter::ghashp, ghashiter::index, ghash::size, and ghash::table.