![]() |
librnr
1.14.5
RoadNarrows Robotics Common Library 1
|
General purpose hash data and function declarations. More...
Go to the source code of this file.
Classes | |
| struct | hnode_t |
| Hash chain node structure. More... | |
| struct | hash_t |
| Hash table control structure. More... | |
| struct | hscan_t |
| Hash scanner structure. More... | |
Macros | |
| #define | HASH_IMPLEMENTATION |
| Hash implementation (original source had different hash configs, we have one) | |
| #define | HASHCOUNT_T_MAX ULONG_MAX |
| Maximum maximum hash table size. | |
| #define | HASH_VAL_T_MAX ULONG_MAX |
| Maximum hashed value (a Mersenne number 2^N-1) | |
| #define | HASH_MIN_SIZE ((hashcount_t)4) |
| Minimum and initial size of hash table. More... | |
| #define | HASH_MAX_SIZE ((hashcount_t)HASHCOUNT_T_MAX) |
| Maximum size of hash table. | |
| #define | hash_isfull(H) ((H)->count == (H)->maxcount) |
| Returns true if table is full, false otherwise. More... | |
| #define | hash_isempty(H) ((H)->count == 0) |
| Returns true if table is empty, false otherwise. More... | |
| #define | hash_count(H) ((H)->count) |
| Returns number of entires in hash table. More... | |
| #define | hash_size(H) ((H)->nchains) |
| Returns size of hash table. More... | |
| #define | hnode_get(N) ((N)->data) |
| Get hash node user data. More... | |
| #define | hnode_getkey(N) ((N)->key) |
| Get hash node hash key. More... | |
| #define | hnode_put(N, V) ((N)->data = (V)) |
| Pet hash node user data. More... | |
Typedefs | |
| typedef unsigned long | hashcount_t |
| maximum hash table size type | |
| typedef unsigned long | hash_val_t |
| hashed value return type | |
| typedef struct hnode_t | hnode_t |
| Hash chain node structure. More... | |
| typedef int(* | hash_comp_t) (const void *key1, const void *key2) |
| User comparison function override type. More... | |
| typedef hash_val_t(* | hash_fun_t) (const void *key) |
| User hashing function type. More... | |
| typedef void(* | hnode_data_free_t) (void *key, void *data) |
| User node data deallocator function. More... | |
| typedef struct hash_t | hash_t |
| Hash table control structure. More... | |
| typedef struct hscan_t | hscan_t |
| Hash scanner structure. More... | |
Functions | |
| 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... | |
| int | 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... | |
General purpose hash data and function declarations.
This file has been modified from the original source (see below).
—
* Hash Table Data Type * Copyright (C) 1997 Kaz Kylheku <kaz@ashi.footprints.net> * * Free Software License: * * All rights are reserved by the author, with the following exceptions: * Permission is granted to freely reproduce and distribute this software, * possibly in exchange for a fee, provided that this copyright notice appears * intact. Permission is also granted to adapt this software to produce * derivative works, as long as the modified versions carry this copyright * notice and additional notices stating that the work has been modified. * The copyright extends to translations of this work into other languages, * including machine languages. *
Definition in file hash.h.
| #define hash_count | ( | H | ) | ((H)->count) |
Returns number of entires in hash table.
| H | Hash table. |
Definition at line 391 of file hash.h.
Referenced by force_grow(), force_shrink(), and main().
| #define hash_isempty | ( | H | ) | ((H)->count == 0) |
Returns true if table is empty, false otherwise.
| H | Hash table. |
Definition at line 385 of file hash.h.
Referenced by hash_table_delete().
| #define hash_isfull | ( | H | ) | ((H)->count == (H)->maxcount) |
| #define HASH_MIN_SIZE ((hashcount_t)4) |
Minimum and initial size of hash table.
Definition at line 104 of file hash.h.
Referenced by init_hash_sizes().
| #define hash_size | ( | H | ) | ((H)->nchains) |
Returns size of hash table.
| H | Hash table. |
Definition at line 397 of file hash.h.
Referenced by force_grow(), force_shrink(), and main().
| #define hnode_get | ( | N | ) | ((N)->data) |
Get hash node user data.
| N | Hash node. |
Definition at line 403 of file hash.h.
Referenced by ConfigDbPrint(), ConfigGetStr(), ConfigSectionAddPair(), ConfigSectionGet(), ConfigSectionPrint(), force_shrink(), main(), and test_hash().
| #define hnode_getkey | ( | N | ) | ((N)->key) |
Get hash node hash key.
| N | Hash node. |
Definition at line 409 of file hash.h.
Referenced by ConfigIterNext(), ConfigSectionPrint(), force_shrink(), main(), and test_hash().
| #define hnode_put | ( | N, | |
| V | |||
| ) | ((N)->data = (V)) |
Pet hash node user data.
| N | Hash node. |
| V | User data. |
Definition at line 416 of file hash.h.
Referenced by ConfigSectionAddPair().
| typedef int(* hash_comp_t) (const void *key1, const void *key2) |
User comparison function override type.
A comparison function takes two keys and produces a value of -1 if the left key is less than the right key, a value of 0 if the keys are equal, and a value of 1 if the left key is greater than the right key.
| key1 | Key 1 |
| key2 | Key 2 |
| typedef hash_val_t(* hash_fun_t) (const void *key) |
User hashing function type.
The hashing function performs some computation on a key and produces an integral value of type hash_val_t based on that key. For best results, the function should have a good randomness properties in all significant bits over the set of keys that are being inserted into a given hash table. In particular, the most significant bits of hash_val_t are most significant to the hash module. Only as the hash table expands are less significant bits examined. Thus a function that has good distribution in its upper bits but not lower is preferrable to one that has poor distribution in the upper bits but not the lower ones.
| key | Key to hash. |
Hash table control structure.
The hash table keeps track of information about a hash table, as well as the actual hash table itself.
| typedef void(* hnode_data_free_t) (void *key, void *data) |
User node data deallocator function.
| key | Pointer to node key data. |
| key | Pointer to node user data. |
Hash chain node structure.
Hash scanner structure.
The scanner is used for traversals of the data structure.
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().
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.
| int hash_table_verify | ( | hash_t * | hash | ) |
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.