| libdatatypes 0.3.2
    Abstract datatypes for C. | 
Generic hashtable. More...
Go to the source code of this file.
| Data Structures | |
| struct | HashTable | 
| Table containing lists of buckets to create associations between keys and values.  More... | |
| struct | HashTable::_Bucket | 
| Singly-linked list implementation storing keys & values.  More... | |
| struct | HashTable::_HashTablePair | 
| Found key-value pair.  More... | |
| struct | HashTableIter | 
| Structure to iterate over the elements of a HashTable.  More... | |
| Macros | |
| #define | HASHTABLE_AUTO_RESIZE 0 | 
| #define | hashtable_pair_key(p) p->bucket->key | 
| #define | hashtable_pair_value(p) p->bucket->data | 
| #define | hashtable_iter_key(iter) iter.liter->key | 
| #define | hashtable_iter_value(iter) iter.liter->data | 
| Typedefs | |
| typedef struct _HashTablePair | HashTablePair | 
| Enumerations | |
| enum | HashTableInsertResult { HASHTABLE_INSERT_RESULT_NEW , HASHTABLE_INSERT_RESULT_REPLACED , HASHTABLE_INSERT_RESULT_FAILED } | 
| result of hashtable_set() method.  More... | |
Generic hashtable.
Definition in file hashtable.h.
| struct HashTable | 
Table containing lists of buckets to create associations between keys and values.
Definition at line 38 of file hashtable.h.
| Data Fields | ||
|---|---|---|
| struct _Bucket ** | buckets | Array containing pointers to buckets. | 
| EqualFunc | compare_keys | Function to check equality of two keys. | 
| size_t | count | Number of stored elements. | 
| FreeFunc | free_key | Function to free keys. | 
| FreeFunc | free_value | Function to free values. | 
| bool | grow | Enable/disable auto-resize. | 
| HashFunc | hash | Function to create a hash from a value. | 
| struct _HashTablePair | pair | Last found key-value pair. | 
| Pool * | pool | Pool used to create/destroy list elements. | 
| size_t | size | Size of the hash table. | 
| const size_t * | sizeptr | Pointer to a prime number specifying the current table size. | 
| struct HashTable::_Bucket | 
Singly-linked list implementation storing keys & values.
Definition at line 61 of file hashtable.h.
| Data Fields | ||
|---|---|---|
| void * | data | Value of the element. | 
| void * | key | Key of the element. | 
| struct _Bucket * | next | Pointer to next list element or NULL. | 
| struct HashTable::_HashTablePair | 
Found key-value pair.
Definition at line 81 of file hashtable.h.
| Data Fields | ||
|---|---|---|
| struct _Bucket * | bucket | Bucket containing the found key-value pair. | 
| FreeFunc | free_value | Function to free the associated value. | 
| struct HashTableIter | 
Structure to iterate over the elements of a HashTable.
Definition at line 97 of file hashtable.h.
| Data Fields | ||
|---|---|---|
| bool | finished | true if iteration is completed. | 
| struct _Bucket * | liter | Pointer to current list element. | 
| size_t | offset | Index of current bucket. | 
| const HashTable * | table | Pointer to the associated HashTable. | 
| #define HASHTABLE_AUTO_RESIZE 0 | 
HashTable size used to enable auto-resizing.
Definition at line 32 of file hashtable.h.
| #define hashtable_iter_key | ( | iter | ) | iter.liter->key | 
Accesses the key of the current element directly.
Definition at line 270 of file hashtable.h.
| #define hashtable_iter_value | ( | iter | ) | iter.liter->data | 
Accesses the value of the current element directly.
Definition at line 281 of file hashtable.h.
| #define hashtable_pair_key | ( | p | ) | p->bucket->key | 
Accesses the key of a key-value pair directly.
Definition at line 206 of file hashtable.h.
| #define hashtable_pair_value | ( | p | ) | p->bucket->data | 
Accesses the value of a key-value pair directly.
Definition at line 217 of file hashtable.h.
| typedef struct _HashTablePair HashTablePair | 
A found key-value pair.
Definition at line 91 of file hashtable.h.
result of hashtable_set() method.
| Enumerator | |
|---|---|
| HASHTABLE_INSERT_RESULT_NEW | Item has been inserted. | 
| HASHTABLE_INSERT_RESULT_REPLACED | Item has been replaced. | 
| HASHTABLE_INSERT_RESULT_FAILED | Item insertion failed. | 
Definition at line 113 of file hashtable.h.
| void hashtable_clear | ( | HashTable * | table | ) | 
| table | a HashTable | 
Removes all elements from the HashTable.
Definition at line 218 of file hashtable.c.
| size_t hashtable_count | ( | const HashTable * | table | ) | 
| table | a HashTable | 
Gets the number of stored elements.
Definition at line 481 of file hashtable.c.
| void hashtable_destroy | ( | HashTable * | table | ) | 
| table | a HashTable | 
Frees all keys, values and the table pointer.
Definition at line 167 of file hashtable.c.
| void hashtable_free | ( | HashTable * | table | ) | 
| table | a HashTable | 
Frees all keys and values without freeing the table pointer.
Definition at line 208 of file hashtable.c.
| void hashtable_init | ( | HashTable * | table, | 
| size_t | size, | ||
| HashFunc | hash_func, | ||
| EqualFunc | compare_keys, | ||
| FreeFunc | free_key, | ||
| FreeFunc | free_value | ||
| ) | 
| table | a HashTable | 
| size | size of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically | 
| hash_func | function to create hash from a key | 
| compare_keys | function to check equality of two keys | 
| free_key | function to free keys or NULL | 
| free_value | function to free values or NULL | 
Initializes a HashTable.
Definition at line 120 of file hashtable.c.
| void * hashtable_iter_get_key | ( | const HashTableIter * | iter | ) | 
| iter | a HashTableIter | 
Retrieves the key of the current element.
Definition at line 537 of file hashtable.c.
| void * hashtable_iter_get_value | ( | const HashTableIter * | iter | ) | 
| iter | a HashTableIter | 
Retrieves the value of the current element.
Definition at line 545 of file hashtable.c.
| void hashtable_iter_init | ( | const HashTable * | table, | 
| HashTableIter * | iter | ||
| ) | 
| table | a HashTable | 
| iter | an uninitialized HashTableIter | 
Initializes a key/value pair iterator and associates it with the table. Modifying the table while using the iterator might lead to undefined behaviour.
Definition at line 489 of file hashtable.c.
| bool hashtable_iter_next | ( | HashTableIter * | iter | ) | 
| iter | a HashTableIter | 
Goes to next element of the HashTable.
Definition at line 513 of file hashtable.c.
| bool hashtable_key_exists | ( | const HashTable * | table, | 
| const void * | key | ||
| ) | 
| table | a HashTable | 
| key | key to test | 
Checks if a key does exist.
Definition at line 470 of file hashtable.c.
| HashTablePair * hashtable_lookup | ( | HashTable * | table, | 
| const void * | key | ||
| ) | 
| table | a HashTable | 
| key | key to lookup | 
Looks up a key-value pair in the HashTable.
Definition at line 416 of file hashtable.c.
| HashTable * hashtable_new | ( | size_t | size, | 
| HashFunc | hash_func, | ||
| EqualFunc | compare_keys, | ||
| FreeFunc | free_key, | ||
| FreeFunc | free_value | ||
| ) | 
| size | size of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically | 
| hash_func | function to create hash from a key | 
| compare_keys | function to check equality of two keys | 
| free_key | function to free keys or NULL | 
| free_value | function to free values or NULL | 
Creates a new HashTable.
Definition at line 100 of file hashtable.c.
| void * hashtable_pair_get_key | ( | const HashTablePair * | pair | ) | 
| pair | a key-value pair | 
Retrieves the key of a key-value pair.
Definition at line 435 of file hashtable.c.
| void * hashtable_pair_get_value | ( | const HashTablePair * | pair | ) | 
| pair | a key-value pair | 
Retrieves the value of a key-value pair.
Definition at line 445 of file hashtable.c.
| void hashtable_pair_set_value | ( | HashTablePair * | pair, | 
| void * | value | ||
| ) | 
| pair | a HashTablePair | 
| value | new value to set | 
Overwrites the value of a key-value pair.
Definition at line 455 of file hashtable.c.
| void hashtable_remove | ( | HashTable * | table, | 
| const void * | key | ||
| ) | 
| table | a HashTable | 
| key | key of the element to remove | 
Removes an element from the HashTable.
Definition at line 367 of file hashtable.c.
| HashTableInsertResult hashtable_set | ( | HashTable * | table, | 
| void * | key, | ||
| void * | value, | ||
| bool | overwrite_key | ||
| ) | 
| table | a HashTable | 
| key | key to insert | 
| value | the value to associate with the key | 
| overwrite_key | true to overwrite already existing keys | 
Inserts a new key and value in the HashTable. If overwrite_key is set an existing key is freed using the specified free_key function before it gets replaced.
Definition at line 308 of file hashtable.c.