plibsys 0.0.5
|
Hash table. More...
Go to the source code of this file.
Typedefs | |
typedef struct PHashTable_ | PHashTable |
Opaque data structure for a hash table. | |
Functions | |
P_LIB_API PHashTable * | p_hash_table_new (void) |
Initializes a new hash table. | |
P_LIB_API void | p_hash_table_insert (PHashTable *table, ppointer key, ppointer value) |
Inserts a new key-value pair into a hash table. | |
P_LIB_API ppointer | p_hash_table_lookup (const PHashTable *table, pconstpointer key) |
Searches for a specifed key in the hash table. | |
P_LIB_API PList * | p_hash_table_keys (const PHashTable *table) |
Gives a list of all the stored keys in the hash table. | |
P_LIB_API PList * | p_hash_table_values (const PHashTable *table) |
Gives a list of all the stored values in the hash table. | |
P_LIB_API void | p_hash_table_free (PHashTable *table) |
Frees a previously initialized PHashTable. | |
P_LIB_API void | p_hash_table_remove (PHashTable *table, pconstpointer key) |
Removes key from a hash table. | |
P_LIB_API PList * | p_hash_table_lookup_by_value (const PHashTable *table, pconstpointer val, PCompareFunc func) |
Searches for a specifed key in the hash table by its value. | |
Hash table.
A hash table is a data structure used to map keys to values. The hash table consists of several internal slots which hold a list of values. A hash function is used to compute an index in the array of the slots from a given key. The hash function itself is fast and it takes a constant time to compute the internal slot index.
When the number of pairs in the hash table is small the lookup and insert (remove) operations are very fast and have average complexity O(1), because every slot holds almost the only one pair. As the number of internal slots is fixed, the increasing number of pairs will lead to degraded performance and the average complexity of the operations can drop to O(N) in the worst case. This is because the more pairs are inserted the more longer the list of values is placed in every slot.
This is a simple hash table implementation which is not intended for heavy usage (several thousands), see PTree if you need the best performance on large data sets. This implementation doesn't support multi-inserts when several values belong to the same key.
Note that PHashTable stores keys and values only as pointers, so you need to free used memory manually, p_hash_table_free() will not do it in any way.
Integers (up to 32 bits) can be stored in pointers using P_POINTER_TO_INT and P_INT_TO_POINTER macros.
Definition in file phashtable.h.
typedef struct PHashTable_ PHashTable |
Opaque data structure for a hash table.
Definition at line 71 of file phashtable.h.
P_LIB_API void p_hash_table_free | ( | PHashTable * | table | ) |
P_LIB_API void p_hash_table_insert | ( | PHashTable * | table, |
ppointer | key, | ||
ppointer | value ) |
Inserts a new key-value pair into a hash table.
table | Initialized hash table. |
key | Key to insert. |
value | Value to insert. |
This function only stores pointers, so you need to manually free pointed data after using the hash table.
P_LIB_API PList * p_hash_table_keys | ( | const PHashTable * | table | ) |
Gives a list of all the stored keys in the hash table.
table | Hash table to collect the keys from. |
P_LIB_API ppointer p_hash_table_lookup | ( | const PHashTable * | table, |
pconstpointer | key ) |
Searches for a specifed key in the hash table.
table | Hash table to lookup in. |
key | Key to lookup for. |
P_LIB_API PList * p_hash_table_lookup_by_value | ( | const PHashTable * | table, |
pconstpointer | val, | ||
PCompareFunc | func ) |
Searches for a specifed key in the hash table by its value.
table | Hash table to lookup in. |
val | Value to lookup keys for. |
func | Function to compare table's values with val, if NULL then values will be compared as pointers. |
The compare function should return 0 if a value from the hash table (the first parameter) is accepted related to the given lookup value (the second parameter), and -1 or 1 otherwise.
P_LIB_API PHashTable * p_hash_table_new | ( | void | ) |
Initializes a new hash table.
P_LIB_API void p_hash_table_remove | ( | PHashTable * | table, |
pconstpointer | key ) |
Removes key from a hash table.
table | Hash table to remove the key from. |
key | Key to remove (if exists). |
P_LIB_API PList * p_hash_table_values | ( | const PHashTable * | table | ) |
Gives a list of all the stored values in the hash table.
table | Hash table to collect the values from. |