#include <inttypes.h>
#include "linked_list.h"
Go to the source code of this file.
Data Structures | |
| struct | struct_hashtable_t |
| A basic implementation of a hashtable. More... | |
| struct | struct_hashtable_entry_t |
| The structure of a hashtable's entry. More... | |
Defines | |
| #define | _TIFA_HASHTABLE_H_ |
Typedefs | |
| typedef struct struct_hashtable_t | hashtable_t |
Equivalent to struct struct_hashtable_t. | |
|
typedef struct struct_hashtable_entry_t | hashtable_entry_t |
Equivalent to struct struct_hashtable_entry_t. | |
Functions | |
| hashtable_t * | alloc_init_hashtable (uint32_t size, int(*cmp_func)(const void *const key_a, const void *const key_b), uint32_t(*hash_func)(const void *const key)) |
Allocates and returns a new hashtable_t. | |
| void | free_hashtable (hashtable_t *htable) |
Clears a hashtable_t. | |
| void | add_entry_in_hashtable (hashtable_t *const htable, const void *const key, const void *const data) |
Adds an entry in a hashtable_t. | |
| void * | get_entry_in_hashtable (hashtable_t *const htable, const void *const key) |
Gets an entry's data from a hashtable_t. | |
| void * | remove_entry_in_hashtable (hashtable_t *const htable, const void *const key) |
Removes an entry from a hashtable_t. | |
Definition in file hashtable.h.
| #define _TIFA_HASHTABLE_H_ |
Standard include guard.
Definition at line 35 of file hashtable.h.
| void add_entry_in_hashtable | ( | hashtable_t *const | htable, | |
| const void *const | key, | |||
| const void *const | data | |||
| ) |
Adds an entry in a hashtable_t.
Creates an entry with its data field given by the pointer data and its key field given by the pointer key and adds it in a hashtable_t.
key and data but merely copies these references in the hashtable_entry_t structure.| [in] | htable | A pointer to the hashtable_t. |
| [in] | key | A pointer to the key of the new entry. |
| [in] | data | A pointer to the data of the new entry. |
| hashtable_t* alloc_init_hashtable | ( | uint32_t | size, | |
| int(*)(const void *const key_a, const void *const key_b) | cmp_func, | |||
| uint32_t(*)(const void *const key) | hash_func | |||
| ) |
Allocates and returns a new hashtable_t.
Allocates and returns a pointer to a new hashtable_t with size allocated buckets, using the comparison function pointed by cmp_func and the hash function given by hash_func.
size is not a power of two, the lowest power of two greater than size is used instead.| [in] | size | The number of allocated buckets. |
| [in] | cmp_func | A pointer to the comparison function. |
| [in] | hash_func | A pointer to the hash function. |
| void free_hashtable | ( | hashtable_t * | htable | ) |
Clears a hashtable_t.
Clears the hashtable_t pointed by htable.
key and data pointers of the hashtable_entry_t.
Do not call free(htable) in client code after a call to free_hashtable(htable): it would result in an error.
| [in] | htable | A pointer to the hashtable_t to clear. |
Referenced by free_mpzpair_htable().
| void* get_entry_in_hashtable | ( | hashtable_t *const | htable, | |
| const void *const | key | |||
| ) |
Gets an entry's data from a hashtable_t.
Gets the data field of the entry from the hashtable htable whose key is given by key.
| [in] | htable | A pointer to the hashtable_t. |
| [in] | key | A pointer to the key of the entry to read. |
data field of the entry whose key is given by key, if any. NULL if no such entry is found in the hashtable.
| void* remove_entry_in_hashtable | ( | hashtable_t *const | htable, | |
| const void *const | key | |||
| ) |
Removes an entry from a hashtable_t.
Removes the entry from the hashtable htable whose key is given by key and returns its data field.
key field of the removed entry is freed if a matching entry is found. The means that if key is a pointer to a structure containing some other pointers, all the memory may not be freed since the real type of key is not known by the hashtable. This is why it is strongly recommended to use only pointers to integers or strings as keys.| [in] | htable | A pointer to the hashtable_t. |
| [in] | key | A pointer to the key of the entry to remove. |
data field of the removed entry. NULL if no matching entry is found in the hashtable.