libamxc
1.10.3
C Generic Data Containers
|
Modules | |
Hash Table Iterator | |
Data Structures | |
struct | _amxc_htable |
The hash table structure. More... | |
Macros | |
#define | amxc_htable_for_each(it, htable) |
Loops over items in the hash table. More... | |
#define | amxc_htable_iterate(it, htable) |
Loops over items in the hash table. More... | |
#define | AMXC_HTABLE_RANGE UINT32_MAX |
Out of range indicator. More... | |
Typedefs | |
typedef unsigned int(* | amxc_htable_hash_func_t) (const char *key, const unsigned int len) |
Definition of the hash function. More... | |
typedef void(* | amxc_htable_it_delete_t) (const char *key, amxc_htable_it_t *it) |
Definition of the hash table item delete function. More... | |
typedef struct _amxc_htable | amxc_htable_t |
The hash table structure. More... | |
Functions | |
int | amxc_htable_new (amxc_htable_t **htable, const size_t reserve) |
Allocates a hash table. More... | |
void | amxc_htable_delete (amxc_htable_t **htable, amxc_htable_it_delete_t func) |
Frees the previously allocated hash table. More... | |
int | amxc_htable_init (amxc_htable_t *const htable, const size_t reserve) |
Initializes a hash table. More... | |
void | amxc_htable_clean (amxc_htable_t *const htable, amxc_htable_it_delete_t func) |
Removes all items from the hash table. More... | |
void | amxc_htable_set_hash_func (amxc_htable_t *const htable, amxc_htable_hash_func_t func) |
Sets the hash function for the hash table. More... | |
unsigned int | amxc_htable_key2index (const amxc_htable_t *const htable, const char *const key) |
Converts a key into an index. More... | |
AMXC_INLINE bool | amxc_htable_is_empty (const amxc_htable_t *const htable) |
Checks that the hash table is empty. More... | |
AMXC_INLINE size_t | amxc_htable_size (const amxc_htable_t *const htable) |
Calculates the size of the hash table. More... | |
AMXC_INLINE size_t | amxc_htable_capacity (const amxc_htable_t *const htable) |
Calculates the capacity of the hash table. More... | |
int | amxc_htable_insert (amxc_htable_t *const htable, const char *const key, amxc_htable_it_t *const it) |
Inserts an item in the hash table. More... | |
amxc_htable_it_t * | amxc_htable_get (const amxc_htable_t *const htable, const char *const key) |
Gets a hash table iterator from the hash table. More... | |
amxc_htable_it_t * | amxc_htable_take (amxc_htable_t *const htable, const char *const key) |
Removes a hash table iterator from the hash table. More... | |
amxc_htable_it_t * | amxc_htable_get_first (const amxc_htable_t *const htable) |
Gets the first item stored in the table. More... | |
amxc_htable_it_t * | amxc_htable_get_last (const amxc_htable_t *const htable) |
Gets the last item stored in the table. More... | |
amxc_array_t * | amxc_htable_get_sorted_keys (const amxc_htable_t *const htable) |
Creates an array containing all keys of the hash table. More... | |
AMXC_INLINE bool | amxc_htable_contains (const amxc_htable_t *const htable, const char *const key) |
Verifies that a key is in the hash table. More... | |
int | amxc_htable_move (amxc_htable_t *const dest, amxc_htable_t *const src) |
Moves all items from one hash table to another hash table. More... | |
AMXC_INLINE amxc_htable_it_t * | amxc_htable_take_first (const amxc_htable_t *const htable) |
Removes the first item stored in the table. More... | |
Loops over items in the hash table.
Iterates over the hash table and updates the iterator each time.
Definition at line 102 of file amxc_htable.h.
Loops over items in the hash table.
Iterates over the hash table and updates the iterator each time.
Definition at line 119 of file amxc_htable.h.
#define AMXC_HTABLE_RANGE UINT32_MAX |
Out of range indicator.
Definition at line 129 of file amxc_htable.h.
typedef unsigned int(* amxc_htable_hash_func_t) (const char *key, const unsigned int len) |
Definition of the hash function.
The hash function calculates a hash from the key. A custom hash function can be implemented and set on the hash table using amxc_htable_set_hash_func.
A set of hash functions is already available in see amxc_hash.h.
The calculated hash is used to calculate the index of the position of the key in the array.
Definition at line 158 of file amxc_htable.h.
typedef void(* amxc_htable_it_delete_t) (const char *key, amxc_htable_it_t *it) |
Definition of the hash table item delete function.
A pointer to a delete function is used in the following functions amxc_htable_delete, amxc_htable_clean, amxc_htable_it_clean.
Definition at line 168 of file amxc_htable.h.
typedef struct _amxc_htable amxc_htable_t |
The hash table structure.
AMXC_INLINE size_t amxc_htable_capacity | ( | const amxc_htable_t *const | htable | ) |
Calculates the capacity of the hash table.
The capacity is the number of items that is reserved
htable | a pointer to the hash table structure |
Definition at line 351 of file amxc_htable.h.
void amxc_htable_clean | ( | amxc_htable_t *const | htable, |
amxc_htable_it_delete_t | func | ||
) |
Removes all items from the hash table.
Removes all items from the hash table, if a delete function is provided, it is called for each item after it was removed from the table. The buffer used to store the items is freed as well.
htable | a pointer to the htable structure |
func | pointer to a function that is called to free each item in the hash table |
Definition at line 200 of file amxc_htable.c.
AMXC_INLINE bool amxc_htable_contains | ( | const amxc_htable_t *const | htable, |
const char *const | key | ||
) |
Verifies that a key is in the hash table.
htable | a pointer to the hash table structure |
key | the key |
Definition at line 512 of file amxc_htable.h.
void amxc_htable_delete | ( | amxc_htable_t ** | htable, |
amxc_htable_it_delete_t | func | ||
) |
Frees the previously allocated hash table.
Removes all items from the hash table, if a delete function is provided, it is called for each item after it was removed from the hash table.
Frees the allocated memory and sets the pointer to NULL.
htable | a pointer to the location where the pointer to the hash table is be stored |
func | pointer to a function that is called to free each item in the hash table |
Definition at line 172 of file amxc_htable.c.
amxc_htable_it_t* amxc_htable_get | ( | const amxc_htable_t *const | htable, |
const char *const | key | ||
) |
Gets a hash table iterator from the hash table.
The key provided is converted into an index using amxc_htable_key2index. If multiple items are available at the calculated position with the same key, the first one is returned. The next item can be retrieved with amxc_htable_it_get_next or with amxc_htable_it_get_next_key if it must have the same key.
htable | a pointer to the hash table structure |
key | the item key |
Definition at line 278 of file amxc_htable.c.
amxc_htable_it_t* amxc_htable_get_first | ( | const amxc_htable_t *const | htable | ) |
Gets the first item stored in the table.
This function iterates the table from the beginning and returns the first item encounter.
htable | a pointer to the hash table structure |
Definition at line 313 of file amxc_htable.c.
amxc_htable_it_t* amxc_htable_get_last | ( | const amxc_htable_t *const | htable | ) |
Gets the last item stored in the table.
This function iterates the table from the end and returns the last item encounter.
htable | a pointer to the hash table structure |
Definition at line 326 of file amxc_htable.c.
amxc_array_t* amxc_htable_get_sorted_keys | ( | const amxc_htable_t *const | htable | ) |
Creates an array containing all keys of the hash table.
The array will be sorted in ascending order. If the hash table contains duplicate keys, the sorted array will contain duplicate keys as well.
The array will not contain empty items and the size is matching the number of items in the hash table.
The returned array should be deleted when not used anymore using amxc_array_delete, no delete function is needed.
htable | a pointer to the hash table structure |
Definition at line 339 of file amxc_htable.c.
int amxc_htable_init | ( | amxc_htable_t *const | htable, |
const size_t | reserve | ||
) |
Initializes a hash table.
Initializes the hash table structure. All pointers are reset to NULL. This function is typically called for hash tables that are on the stack. Allocating and initializing a hash table on the heap can be done using amxc_htable_new
htable | a pointer to the hash table structure. |
reserve | Number of items that needs to be reserved |
Definition at line 185 of file amxc_htable.c.
int amxc_htable_insert | ( | amxc_htable_t *const | htable, |
const char *const | key, | ||
amxc_htable_it_t *const | it | ||
) |
Inserts an item in the hash table.
The key provided is converted into an index using amxc_htable_key2index. When an position is already taken the items on that position are chained. The same key can be used for different items. When the number of items in the hash table is above 75% of the capacity, the table grows. The number of items that are reserved will be doubled with a maximum of 1024 items each time.
htable | a pointer to the hash table structure |
key | the item key |
it | the item htable iterator |
Definition at line 237 of file amxc_htable.c.
AMXC_INLINE bool amxc_htable_is_empty | ( | const amxc_htable_t *const | htable | ) |
Checks that the hash table is empty.
htable | a pointer to the hash table structure |
Definition at line 311 of file amxc_htable.h.
unsigned int amxc_htable_key2index | ( | const amxc_htable_t *const | htable, |
const char *const | key | ||
) |
Converts a key into an index.
This function converts a key into an index using the hash function and the reserved size of the table
htable | a pointer to the htable structure |
key | the key for which an index must be calculated |
Definition at line 225 of file amxc_htable.c.
int amxc_htable_move | ( | amxc_htable_t *const | dest, |
amxc_htable_t *const | src | ||
) |
Moves all items from one hash table to another hash table.
After the move the source hash table will be empty.
If the destination hash table already contained items, the items of the source hash table are added to the destination.
dest | a pointer to the destination hash table structure |
src | a pointer to the source hash table structure |
Definition at line 363 of file amxc_htable.c.
int amxc_htable_new | ( | amxc_htable_t ** | htable, |
const size_t | reserve | ||
) |
Allocates a hash table.
Allocates and initializes memory to store a hash table. This function allocates memory from the heap, if a hash table is on the stack, it can be initialized using function amxc_htable_init
htable | a pointer to the location where the pointer to the new hash table can be stored |
reserve | Number of items that needs to be reserved |
Definition at line 148 of file amxc_htable.c.
void amxc_htable_set_hash_func | ( | amxc_htable_t *const | htable, |
amxc_htable_hash_func_t | func | ||
) |
Sets the hash function for the hash table.
A custom implementation for hash calculation function can be set on the hash table. By default the amxc_BKDR_hash function is set to calculate the hashes. Other hash functions are available, see Hash functions
htable | a pointer to the htable structure |
func | pointer to a function that is called for each hash that needs be calculated |
Definition at line 211 of file amxc_htable.c.
AMXC_INLINE size_t amxc_htable_size | ( | const amxc_htable_t *const | htable | ) |
Calculates the size of the hash table.
The size of the hash table is expressed in items.
htable | a pointer to the hash table structure |
Definition at line 334 of file amxc_htable.h.
amxc_htable_it_t* amxc_htable_take | ( | amxc_htable_t *const | htable, |
const char *const | key | ||
) |
Removes a hash table iterator from the hash table.
The key provided is converted into an index using amxc_htable_key2index. If multiple items are available at the calculated position with the same key, (collision) the first one is removed and returned.
htable | a pointer to the hash table structure |
key | the item key |
Definition at line 304 of file amxc_htable.c.
AMXC_INLINE amxc_htable_it_t* amxc_htable_take_first | ( | const amxc_htable_t *const | htable | ) |
Removes the first item stored in the table.
This function iterates the table from the beginning and removes and returns the first one encounter.
htable | a pointer to the hash table structure |
Definition at line 696 of file amxc_htable.h.