A Hash Table data structure definition and functions. Also see Hash Table Example.
s_hash_table_new | Create a new hash table. |
s_hash_table_delete | Delete a hash table. |
s_hash_table_add | Add an element to a hash table. |
s_hash_element_unlink | Unlink the given hash table element from the hash table. |
s_hash_element_delete | Delete the given hash table element. |
s_hash_table_size | Get the number of elements in the hash table. |
s_hash_table_resize | Resize the hash table to a specific size. |
s_hash_table_find | Find a hash table element in the hash table. |
s_hash_table_first | Get the first hash table element of the given hash table. |
s_hash_element_next | Get the next hash table element relative to the given one. |
s_hash_element_key | Get the key of the given hash table element. |
s_hash_element_key_length | Get the key length (bytes) of the given hash table element. |
s_hash_element_get_data | Get the data of the given hash table element. |
s_hash_element_set_data | Set the data of the given hash table element. |
s_hash_table_stats | Print statistics about the hash table to a buffer that is returned. |
s_hash_element_pos | Get the position of the given hash table element in the hash table array. |
Opaque hash table.
The hash table grows automatically, albeit the grow function is slow as elements already in the hash table need to be rehashed. Therefore it is desirable to select the hash table size as optimal as possible during creation with s_hash_table_new.
Opaque hash table element.
The hash table element represents one element in a hash table.
Create a new hash table.
Creates a new hash table and initializes the size of the table to 2 size.
Parameters: |
|
---|---|
Return: | The newly created hash table. |
Delete a hash table.
Delete a hash table and all it’s elements. If the s_hash_table_free_fp is NULL then the elements memory wont be freed.
Parameters: |
|
---|
The s_hash_table element free function callback typedef.
A pointer to a function that frees the dynamically allocated memory of a hash table element.
Parameters: |
|
---|
Add an element to a hash table.
Hash table element keys must be unique.
Parameters: |
|
---|---|
Note: | The newly created hash table element takes hold of the memory of the key. |
Unlink the given hash table element from the hash table.
Parameters: |
|
---|---|
Note: | The caller must take care of the memory of the key and data of the given hash table element before unlinking, otherwise the references to the key and data are lost as the element is freed with this call. |
Delete the given hash table element.
If the s_hash_table_free_fp function pointer is NULL then the memory of the key and data of the element will not be freed.
Parameters: |
|
---|
Get the number of elements in the hash table.
Parameters: |
|
---|---|
Return: | Returns the number of elements of the hash table. |
Resize the hash table to a specific size.
The hash table can be resized to any size as long as it can still contain all the existing elements in it. There are a few outcomes based on the given size and the elements contained in the hash table:
Parameters: |
|
---|---|
Note: | This function is relatively slow, as rehashing a table takes time. However, if there are no elements in the list it is quite fast. |
Find a hash table element in the hash table.
Parameters: |
|
---|---|
Return: | Pointer to the hash table element or NULL if the given key does not have an associated hash table element. |
Get the first hash table element of the given hash table.
Parameters: |
|
---|---|
Return: | Pointer to the first hash table element, or NULL if not found or the hash table is empty. |
Get the next hash table element relative to the given one.
Parameters: |
|
---|---|
Return: | Pointer to next hash table element relative to given one, or NULL if none. |
Get the key of the given hash table element.
Parameters: |
|
---|---|
Return: | The key of the given hash table element. |
Get the key length (bytes) of the given hash table element.
Parameters: |
|
---|---|
Return: | The key length (bytes) of the given hash table element. |
Get the data of the given hash table element.
Parameters: |
|
---|---|
Return: | The data of the given hash table element. |
Set the data of the given hash table element.
Parameters: |
|
---|---|
Note: | If the hash element has some data it will be lost. |
Print statistics about the hash table to a buffer that is returned.
Parameters: |
|
---|---|
Return: | Character buffer with Hash Table stats in. |
Note: | Prints out information as follows:
s_hash_table_stats:
items 0: <number of buckets with 0 items> buckets
items 1: <number of buckets with 1 item> buckets
...
buckets: <number of buckets> items: <number of items> existing: <x>
The caller is responsible for the memory of the returned buffer. |
Get the position of the given hash table element in the hash table array.
Parameters: |
|
---|---|
Return: | The position of the given hash table element in the hash table array. |