![]() |
librnr
1.14.5
RoadNarrows Robotics Common Library 1
|
General purpose hash data and function declarations. More...
#include <stdlib.h>#include <stddef.h>#include <assert.h>#include <string.h>#include "rnr/rnrconfig.h"#include "rnr/new.h"#include "rnr/log.h"#include "rnr/hash.h"Go to the source code of this file.
Functions | |
| static void | compute_bits () |
| Compute the number of bits in the hash_val_t type. More... | |
| static bool_t | is_power_of_two (hash_val_t arg) |
| Verify whether the given argument is a power of two. More... | |
| static hash_val_t | compute_mask (hashcount_t size) |
| Compute a mask for a given table size. More... | |
| static void | init_hash_sizes (hash_t *hash, hashcount_t minsize, hashcount_t maxsize) |
| Initialize the table size values. More... | |
| static hash_val_t | hash_fun_default (const void *key) |
| Default hashing function over null-terminated string keys. More... | |
| static int | hash_comp_default (const void *key1, const void *key2) |
| Default hash key comparator function of null-terminated key strings. More... | |
| static void | clear_table (hash_t *hash) |
| Initialize the table of pointers to null. More... | |
| static void | grow_table (hash_t *hash) |
| Double the size of a dynamic table. More... | |
| static void | shrink_table (hash_t *hash) |
| Cut a dynamic table size in half. More... | |
| hash_t * | hash_table_create (bool_t isdynamic, hashcount_t minsize, hashcount_t maxsize, hash_comp_t compfun, hash_fun_t hashfun, hnode_data_free_t freedatafun) |
| Create a dynamic hash table. More... | |
| void | hash_table_destroy (hash_t *hash) |
| Delete the hash table, all of its entries, and all of the user data. More... | |
| void | hash_table_delete (hash_t *hash) |
| Frees a dynamic hash table structure. More... | |
| hash_t * | hash_table_init (hash_t *hash, hashcount_t minsize, hashcount_t maxsize, hash_comp_t compfun, hash_fun_t hashfun, hnode_data_free_t freedatafun, hnode_t **table, hashcount_t nchains) |
| Initialize a user supplied hash table. More... | |
| bool_t | hash_table_verify (hash_t *hash) |
| Verify hash table consistency. More... | |
| void | hash_scan_begin (hscan_t *scan, hash_t *hash) |
| Reset the hash scanner (iterator). More... | |
| hnode_t * | hash_scan_next (hscan_t *scan) |
| Retrieve the next node from the hash table. More... | |
| void | hash_node_insert (hash_t *hash, hnode_t *node, void *key) |
| Insert a node into the hash table. More... | |
| hnode_t * | hash_node_unlink (hash_t *hash, hnode_t *node) |
| Unlink the given node from the hash table. More... | |
| hnode_t * | hash_node_fast_unlink (hash_t *hash, hnode_t *node) |
| Fast version to unlink the given node from the hash table. More... | |
| void | hash_node_delete (hash_t *hash, hnode_t *node) |
| Unlink and delete a hash node from the hash table. More... | |
| hnode_t * | hash_lookup (hash_t *hash, const void *key) |
| Find a node in the hash table and return a pointer to it. More... | |
| bool_t | hash_insert (hash_t *hash, void *key, void *data) |
| Insert user data with the given key into the hash table. More... | |
| bool_t | hash_delete (hash_t *hash, void *key) |
| Unlink and delete a hash node with the given key from the hash table. More... | |
| void | hash_set_self_verify (bool_t selfverify) |
| Enable (disable) automatic self-checks during hash table modification operatations. More... | |
| hnode_t * | hnode_create (void *data) |
| Create a hash table node dynamically and assign it the given data. More... | |
| hnode_t * | hnode_init (hnode_t *node, void *data) |
| Initialize a client-supplied hash node. More... | |
| void | hnode_destroy (hnode_t *node, hnode_data_free_t freedatafun) |
| Hash node and user data deallocator. More... | |
| void | hnode_delete (hnode_t *node) |
| Destroy a dynamically allocated node. More... | |
Variables | |
| static int | hash_val_t_bit = 0 |
| Number of bits in hashed value. Must be a power of 2. | |
| static bool_t | hash_self_verify = false |
| Do [not] perform auto-verification after hash table operations. | |
General purpose hash data and function declarations.
This file has been modified from the original source (see below).
Exceptons map to assert() calls that terminate the calling application.
Definition in file hash.c.
|
static |
Initialize the table of pointers to null.
| hash | Hash table. |
Definition at line 307 of file hash.c.
References hash_t::nchains, NULL, and hash_t::table.
Referenced by hash_table_create(), and hash_table_init().
|
static |
Compute the number of bits in the hash_val_t type.
We know that hash_val_t is an unsigned integral type. Thus the highest value it can hold is a Mersenne number (power of two, less one). We initialize a hash_val_t object with this value and then shift bits out one by one while counting.
Definition at line 141 of file hash.c.
References hash_val_t_bit, and HASH_VAL_T_MAX.
Referenced by hash_table_create(), and hash_table_init().
|
static |
Compute a mask for a given table size.
| size | Table size. |
Definition at line 185 of file hash.c.
References is_power_of_two().
Referenced by hash_table_create(), and hash_table_init().
|
static |
Double the size of a dynamic table.
This works as follows:
Each chain splits into two adjacent chains. The shift amount increases by one, exposing an additional bit of each hashed key. For each node in the original chain, the value of this newly exposed bit will decide which of the two new chains will receive the node: if the bit is 1, the chain with the higher index will have the node, otherwise the lower chain will receive the node. In this manner, the hash table will continue to function exactly as before without having to rehash any of the keys.
| hash | Hash table. |
| Table Overflow | Reached system limits. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 388 of file hash.c.
References _TPTR, _VARFMT, hash_self_verify, hash_table_verify(), hash_t::highmark, hnode_t::hkey, LOGDIAG4, LOGDIAG4CALL, hash_t::lowmark, hash_t::mask, hash_t::nchains, new_overs(), hnode_t::next, NULL, hnode_t::pself, and hash_t::table.
Referenced by hash_node_insert().
|
static |
Default hash key comparator function of null-terminated key strings.
| key1 | Key string 1. |
| key2 | Key string 2. |
Definition at line 297 of file hash.c.
Referenced by hash_table_create(), and hash_table_init().
Unlink and delete a hash node with the given key from the hash table.
The user data and key are deallocated as per the any user supplied data free function. The node is then deallocated.
| hash | Hash table. |
| key | Hash key. |
| Not Initialzed | Hashing not initialized. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1310 of file hash.c.
References hash_t::freenodedata, hash_lookup(), hash_node_unlink(), hnode_destroy(), and NULL.
Referenced by ConfigDelete(), and ConfigSectionDelete().
|
static |
Default hashing function over null-terminated string keys.
| key | Null-terminated key string. |
Definition at line 263 of file hash.c.
Referenced by hash_table_create(), and hash_table_init().
Insert user data with the given key into the hash table.
The data and key are attached to new hash node which is automatically allocated and the node is inserted into the hash table.
| hash | Hash table. |
| key | Hash key. |
| data | User data. |
| Not Initialzed | Hashing not initialized. |
| Too Big | Reached maximum table size. |
| Duplicate Key | Node with key already exists. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1283 of file hash.c.
References hash_node_insert(), and hnode_create().
Referenced by ConfigSectionAddPair(), ConfigSectionNew(), force_grow(), main(), and test_hash().
Find a node in the hash table and return a pointer to it.
| hash | Hash table. |
| key | Hash key. |
Definition at line 1246 of file hash.c.
References hash_t::compare, hash_t::hash, hnode_t::hkey, hnode_t::key, hash_t::mask, hnode_t::next, NULL, and hash_t::table.
Referenced by ConfigGetStr(), ConfigSectionAddPair(), ConfigSectionDelete(), ConfigSectionGet(), ConfigSectionNew(), hash_delete(), hash_node_fast_unlink(), hash_node_insert(), hash_node_unlink(), and main().
Unlink and delete a hash node from the hash table.
The user data and key are deallocated as per the any user supplied data free function. The node is then deallocated.
| hash | Hash table. |
| node | Hash node to delete. |
| Not Initialzed | Hashing not initialized. |
| No Key | Hash key not found. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1217 of file hash.c.
References hash_t::freenodedata, hash_node_unlink(), and hnode_destroy().
Referenced by force_shrink(), and main().
Fast version to unlink the given node from the hash table.
Same as hash_node_unlink() except does not trigger table shrinkage. This call is optimized for hash table scan operations.
| hash | Hash table. |
| node | Node to unlink, typically found by scanning. |
| Not Initialzed | Hashing not initialized. |
| No Key | Hash key not found. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1178 of file hash.c.
References _TPTR, hash_t::count, hash_lookup(), hash_self_verify, hash_table_verify(), hash_val_t_bit, hnode_t::key, LOGDIAG4CALL, hnode_t::next, and hnode_t::pself.
Referenced by hash_table_destroy().
Insert a node into the hash table.
| hash | Hash table. |
| node | Node to insert. |
| key | Hash key. |
| Not Initialzed | Hashing not initialized. |
| Too Big | Reached maximum table size. |
| Duplicate Key | Node with key already exists. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1063 of file hash.c.
References _TPTR, hash_t::count, hash_t::dynamic, grow_table(), hash_t::hash, hash_lookup(), hash_self_verify, hash_table_verify(), hash_val_t_bit, hash_t::highmark, hnode_t::hkey, hnode_t::key, LOGDIAG4CALL, hash_t::mask, hash_t::maxsize, hnode_t::next, NULL, hnode_t::pself, and hash_t::table.
Referenced by hash_insert().
Unlink the given node from the hash table.
This is easy, because each node contains a back pointer to the previous node's next pointer.
No data are deleted.
| hash | Hash table. |
| node | Hash node to unlink. |
| Not Initialzed | Hashing not initialized. |
| No Key | Hash key not found. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 1128 of file hash.c.
References _TPTR, hash_t::count, hash_t::dynamic, hash_lookup(), hash_self_verify, hash_table_verify(), hash_val_t_bit, hnode_t::key, LOGDIAG4CALL, hash_t::lowmark, hash_t::minsize, hnode_t::next, hnode_t::pself, and shrink_table().
Referenced by hash_delete(), and hash_node_delete().
Reset the hash scanner (iterator).
After this call the next element retrieved by hash_scan_next() shall be the first element on the first non-empty chain.
| scan | Hash iterator. |
| hash | Hash table. |
Definition at line 937 of file hash.c.
References hscan_t::chain, hscan_t::hash, hash_t::nchains, hscan_t::next, NULL, and hash_t::table.
Referenced by ConfigDbIterNew(), ConfigDbPrint(), ConfigSectionIterNew(), ConfigSectionPrint(), force_shrink(), hash_table_destroy(), main(), and test_hash().
Retrieve the next node from the hash table.
The scanner (iterator) updates the pointer for the next invocation of hash_scan_next().
| Not Initialzed | Hashing not initialized. |
Definition at line 998 of file hash.c.
References hscan_t::chain, hscan_t::hash, hash_val_t_bit, hash_t::nchains, hnode_t::next, hscan_t::next, NULL, and hash_t::table.
Referenced by ConfigDbPrint(), ConfigIterNext(), ConfigSectionPrint(), force_shrink(), hash_table_destroy(), main(), and test_hash().
| void hash_set_self_verify | ( | bool_t | selfverify | ) |
Enable (disable) automatic self-checks during hash table modification operatations.
| selfverify | True to enable, false to disable. |
Definition at line 1331 of file hash.c.
References hash_self_verify.
Referenced by main(), and test_hash().
| hash_t* hash_table_create | ( | bool_t | isdynamic, |
| hashcount_t | minsize, | ||
| hashcount_t | maxsize, | ||
| hash_comp_t | compfun, | ||
| hash_fun_t | hashfun, | ||
| hnode_data_free_t | freedatafun | ||
| ) |
Create a dynamic hash table.
Both the hash table structure and the table itself are dynamically allocated. Furthermore, the table is extendible in that it will automatically grow as its load factor increases beyond a certain threshold.
| isdynamic | The hash table is [not] automatically grow. If set to false, the hash table is initialized t be fixed at minsize and maxsize is ignored. |
| minsize | Minimum and initial number of hash entries that will be supported. |
| maxsize | Maximum number of hash entries that will be supported. |
| compfun | Hash key comparator function. NULL will use the default strcmp() wrapper. |
| hashfun | Hashing function. NULL will used the default string hashing function. |
| freedatafun | User key and data deallocator function. NULL will cause no user key or data to be deleted. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 691 of file hash.c.
References _TBOOL, _TPTR, _TULONG, clear_table(), hash_t::compare, compute_bits(), compute_mask(), hash_t::count, hash_t::dynamic, hash_t::freenodedata, hash_t::hash, hash_comp_default(), hash_fun_default(), hash_self_verify, hash_table_verify(), hash_val_t_bit, hash_t::highmark, init_hash_sizes(), LOGDIAG4, LOGDIAG4CALL, hash_t::lowmark, hash_t::mask, hash_t::maxsize, hash_t::minsize, hash_t::nchains, NEW, and hash_t::table.
Referenced by ConfigDbNew(), ConfigSectionNew(), main(), and test_hash().
| void hash_table_delete | ( | hash_t * | hash | ) |
Frees a dynamic hash table structure.
| hash | Hash table. |
| Not Initialzed | Hashing not initialized. |
| Not Empty | Hash table not empty. |
Definition at line 776 of file hash.c.
References hash_isempty, hash_val_t_bit, and hash_t::table.
Referenced by hash_table_destroy().
| void hash_table_destroy | ( | hash_t * | hash | ) |
Delete the hash table, all of its entries, and all of the user data.
| hash | Hash table. |
Definition at line 750 of file hash.c.
References _TPTR, hash_t::freenodedata, hash_node_fast_unlink(), hash_scan_begin(), hash_scan_next(), hash_table_delete(), hnode_destroy(), and LOGDIAG4CALL.
Referenced by ConfigDbDelete(), ConfigMainHashDeleteCb(), and main().
| hash_t* hash_table_init | ( | hash_t * | hash, |
| hashcount_t | minsize, | ||
| hashcount_t | maxsize, | ||
| hash_comp_t | compfun, | ||
| hash_fun_t | hashfun, | ||
| hnode_data_free_t | freedatafun, | ||
| hnode_t ** | table, | ||
| hashcount_t | nchains | ||
| ) |
Initialize a user supplied hash table.
The user also supplies a table of chains which is assigned to the hash table . The table is static—it will not grow or shrink.
| hash | The destination hash table. |
| minsize | Minimum and initial number of hash entries that will be supported. |
| maxsize | Maximum number of hash entries that will be supported. |
| compfun | Hash key comparator function. NULL will use the default strcmp() wrapper. |
| hashfun | Hashing function. NULL will used the default string hashing function. |
| freedatafun | User key and data deallocator function. NULL will cause no user key or data to be deleted. |
| table | The actual hash chain table to attach. |
| nchains | Number of nodes in hash chain. |
| Bad Parameter | nchain not a power of 2. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 828 of file hash.c.
References _TPTR, _TULONG, clear_table(), hash_t::compare, compute_bits(), compute_mask(), hash_t::count, hash_t::dynamic, hash_t::freenodedata, hash_t::hash, hash_comp_default(), hash_fun_default(), hash_self_verify, hash_table_verify(), hash_val_t_bit, hash_t::highmark, init_hash_sizes(), is_power_of_two(), LOGDIAG4, LOGDIAG4CALL, hash_t::lowmark, hash_t::mask, hash_t::maxsize, hash_t::minsize, hash_t::nchains, and hash_t::table.
Verify hash table consistency.
If hash_self_verify enabled (see hash_set_self_verify()), this call is automatically called during hash table modification operations.
Definition at line 888 of file hash.c.
References CHKEXPR_PTR, CHKEXPR_ULONG, hash_t::count, hash_t::dynamic, hash_t::highmark, is_power_of_two(), hash_t::lowmark, hash_t::mask, hash_t::nchains, hnode_t::next, and hash_t::table.
Referenced by grow_table(), hash_node_fast_unlink(), hash_node_insert(), hash_node_unlink(), hash_table_create(), hash_table_init(), and shrink_table().
| hnode_t* hnode_create | ( | void * | data | ) |
Create a hash table node dynamically and assign it the given data.
| data | User data. |
Definition at line 1343 of file hash.c.
References hnode_t::data, NEW, hnode_t::next, NULL, and hnode_t::pself.
Referenced by hash_insert().
| void hnode_delete | ( | hnode_t * | node | ) |
| void hnode_destroy | ( | hnode_t * | node, |
| hnode_data_free_t | freedatafun | ||
| ) |
Hash node and user data deallocator.
| node | Node to deallocate. |
| freedatafun | User key and data deallocator. May be NULL. |
Definition at line 1376 of file hash.c.
References hnode_t::data, hnode_t::key, and NULL.
Referenced by hash_delete(), hash_node_delete(), and hash_table_destroy().
Initialize a client-supplied hash node.
| node | Hash node. |
| data | User data. |
Definition at line 1362 of file hash.c.
References hnode_t::data, hnode_t::next, NULL, and hnode_t::pself.
|
static |
Initialize the table size values.
The minimum size is adjusted to be a power of two and is guaranteed to be at least HASH_MIN_SIZE.
The maximum size adjusted to be >= minsize.
| hash | Hash table. |
| minsize | Desired minimum and initial size. |
| maxsize | Desired maximum size. |
Definition at line 215 of file hash.c.
References HASH_MIN_SIZE, hash_t::highmark, LOGDIAG4, hash_t::lowmark, hash_t::maxsize, and hash_t::minsize.
Referenced by hash_table_create(), and hash_table_init().
|
static |
Verify whether the given argument is a power of two.
| arg | Argument to test. |
Definition at line 163 of file hash.c.
Referenced by compute_mask(), hash_table_init(), and hash_table_verify().
|
static |
Cut a dynamic table size in half.
This is done by folding together adjacent chains and populating the lower half of the table with these chains. The chains are simply spliced together. Once this is done, the whole table is reallocated to a smaller object.
| hash | Hash table. |
| Table Overflow | Reached system limits. |
| Verification Failed | Only if hash_self_verify enabled. |
Definition at line 545 of file hash.c.
References _TPTR, _VARFMT, hash_self_verify, hash_table_verify(), hash_t::highmark, LOGDIAG4, LOGDIAG4CALL, hash_t::lowmark, hash_t::mask, hash_t::nchains, new_overs(), hnode_t::next, NULL, hnode_t::pself, and hash_t::table.
Referenced by hash_node_unlink().