LibRCG
3.1.1
|
Implementation of a hash table. More...
Go to the source code of this file.
Data Structures | |
struct | SHashNode |
Hash table node structure. More... | |
struct | SHashMap |
Hash table structure. More... | |
Typedefs | |
typedef SHashNode * | HashNode |
Hash table node definition. More... | |
typedef SHashMap * | HashMap |
Hash table definition. More... | |
Functions | |
HashMap | newHash (int size, float factor, int(*hash)(void *), int(*equals)(void *, void *)) |
Creates a hash table. More... | |
int | hashSetHash (HashMap hmap, int(*hash)(void *)) |
Sets the hash function of a hash table. More... | |
int | hashSetEquals (HashMap hmap, int(*equals)(void *, void *)) |
Sets the comparison function of a hash table. More... | |
int | hashSetFactor (HashMap hmap, int factor) |
Sets the load factor of a hash table. More... | |
void | hashDelete (HashMap hmap) |
Deletes a hash table. More... | |
int | hashInsert (HashMap hmap, void *key, void *value, int replace) |
Associates a value to a key in a hash table. More... | |
int | hashRemove (HashMap hmap, void *key, void **value, void(*del)(void *)) |
Removes the mapping for a key from a hash table. More... | |
int | hashGet (HashMap hmap, void *key, void **value) |
Provides the mapping for a key from a hash table. More... | |
int | hashSize (HashMap hmap) |
Returns the number of elements present in a hash table. More... | |
Iterator | hashKeys (HashMap hmap) |
Creates an iterator from the keys of a hash table. More... | |
Iterator | hashValues (HashMap hmap) |
Creates an iterator from the values of a hash table. More... | |
Implementation of a hash table.
Provides functions to create and manipulate a hash table.
Collisions are handled using having a list for each buckets. When the number of elements is greater than a specified ratio of the number of buckets the hash table is resized.
To use this hash table you have to provide the following functions for the data type used for keys:
int hash(void *)
Hash function (used by hashInsert
, hashRemove
, and hashGet
). Generates a unique hash to a key. This function can be changed using function hashSetHash
.
E.g.:
int keyEquals(void* key1,void* key2)
Compares two keys (used by hashInsert
, hashRemove
, and hashGet
). It should return 0 if key1
is equal to key2
, and a value different from 0 otherwise. This function can be changed using function hashSetEquals
.
E.g.:
Definition in file hashmap.h.
void hashDelete | ( | HashMap | hmap | ) |
int hashGet | ( | HashMap | hmap, |
void * | key, | ||
void ** | value | ||
) |
Provides the mapping for a key from a hash table.
If there is no mapping for the specified key, it will be put the value NULL
at value
. However, the NULL
value may also say that the mapping for the specified key was NULL
. Check the returned value in order to know whether the key was in the hash table.
value
a pointer to the mapping of the specified key. Changes to this value will affect the value in the hash table.hmap | the hash table |
key | key whose mapping is to be provided |
value | pointer where the mapping value will be put |
int hashInsert | ( | HashMap | hmap, |
void * | key, | ||
void * | value, | ||
int | replace | ||
) |
Associates a value to a key in a hash table.
If the key already had a value, the replace
argument specifies whether the new element should be added (it will be added only if replace!=0
).
hmap | the hash table |
key | the key |
value | the value to be inserted |
replace | specifies whether an old value shall be replaced |
int hashRemove | ( | HashMap | hmap, |
void * | key, | ||
void ** | value, | ||
void(*)(void *) | del | ||
) |
Removes the mapping for a key from a hash table.
Provides the value of the removed element if the value of elem
is not NULL
.
del
.hmap | the hash table |
key | key whose mapping is to be removed |
value | pointer where the removed element shall be put (or NULL ) |
del | function to free the memory used by the key (or NULL ) |
int hashSetEquals | ( | HashMap | hmap, |
int(*)(void *, void *) | equals | ||
) |
int hashSetFactor | ( | HashMap | hmap, |
int | factor | ||
) |
int hashSetHash | ( | HashMap | hmap, |
int(*)(void *) | hash | ||
) |
int hashSize | ( | HashMap | hmap | ) |
HashMap newHash | ( | int | size, |
float | factor, | ||
int(*)(void *) | hash, | ||
int(*)(void *, void *) | equals | ||
) |
Creates a hash table.
The load factor (factor
) specifies when the number of buckets should be increased (it will be increased when size > factor*length
). The load factor must be greater than or equal to 0.1 (otherwise value 0.1 will be used).
size | the initial number of buckets |
factor | the load factor |
hash | the hash function |
equals | the comparison function |
NULL
if an error occurredLibRCG © 2004-2015 Rui Carlos Gonçalves