librnr  1.14.5
RoadNarrows Robotics Common Library 1
hash.h File Reference

General purpose hash data and function declarations. More...

#include <limits.h>
#include "rnr/rnrconfig.h"

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_thash_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_thash_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_thash_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_thash_node_unlink (hash_t *hash, hnode_t *node)
 Unlink the given node from the hash table. More...
 
hnode_thash_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_thash_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_thnode_create (void *data)
 Create a hash table node dynamically and assign it the given data. More...
 
hnode_thnode_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...
 

Detailed Description

General purpose hash data and function declarations.

This file has been modified from the original source (see below).

See also
example_hash under "Related Pages" for an example usage of hashing.
Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/hash.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.


Original Source Comment Block
Original Author
Kaz Kylheku (kaz@a.nosp@m.shi..nosp@m.footp.nosp@m.rint.nosp@m.s.net)
Original Copyright
(C) 1997
Original Header
* 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.

Macro Definition Documentation

#define hash_count (   H)    ((H)->count)

Returns number of entires in hash table.

Parameters
HHash 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.

Parameters
HHash table.

Definition at line 385 of file hash.h.

Referenced by hash_table_delete().

#define hash_isfull (   H)    ((H)->count == (H)->maxcount)

Returns true if table is full, false otherwise.

Parameters
HHash table.

Definition at line 379 of file hash.h.

#define HASH_MIN_SIZE   ((hashcount_t)4)

Minimum and initial size of hash table.

Warning
Must be a power of two.

Definition at line 104 of file hash.h.

Referenced by init_hash_sizes().

#define hash_size (   H)    ((H)->nchains)

Returns size of hash table.

Parameters
HHash 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.

Parameters
NHash 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.

Parameters
NHash node.

Definition at line 409 of file hash.h.

Referenced by ConfigIterNext(), ConfigSectionPrint(), force_shrink(), main(), and test_hash().

#define hnode_put (   N,
 
)    ((N)->data = (V))

Pet hash node user data.

Parameters
NHash node.
VUser data.

Definition at line 416 of file hash.h.

Referenced by ConfigSectionAddPair().

Typedef Documentation

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.

Default:
Built-in wrapper to strcmp().
Parameters
key1Key 1
key2Key 2
See also
hash_table_create()

Definition at line 177 of file hash.h.

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.

Default:
Built-in hashing function that hashes over string keys.
Parameters
keyKey to hash.
See also
hash_table_create()

Definition at line 199 of file hash.h.

typedef struct hash_t hash_t

Hash table control structure.

The hash table keeps track of information about a hash table, as well as the actual hash table itself.

Notes:
1. Pointer to the hash table proper. The table is an array of pointers to hash nodes (of type hnode_t). If the table is empty, every element of this table is a null pointer. A non-null entry points to the first element of a chain of nodes.
2. Minimum and initial size of the hash table (must be a power of two).
3. Maximum hash table size (must be >= minsize). The maximum size is the greatest number of nodes that can populate this table. If the table contains this many nodes, no more can be inserted, and the hash_isfull() function returns true.
4. The low mark is a minimum population threshold, measured as a number of nodes. If the table population drops below this value, a table shrinkage will occur. Only dynamic tables are subject to this reduction. No table will shrink beneath a certain absolute minimum number of nodes.
5. The high mark is a population threshold, measured as a number of nodes, which, if exceeded, will trigger a table expansion. Only dynamic hash tables are subject to this expansion.
6. This member keeps track of the size of the hash table—that is, the number of chain pointers.
7. The current hash table mask. If the size of the hash table is 2^N, this value has its low N bits set to 1, and the others clear. It is used to select bits from the result of the hashing function to compute an index into the table.
8. The count member maintains the number of elements that are presently in the hash table.
9. This is the a pointer to the hash table's comparison function. The function is set once at initialization or creation time.
10. Pointer to the table's hashing function, set once at creation or initialization time.
12. User data deallocator. Default is NULL. (i.e. nothing is done to data when hash node is deleted).
13. A flag which indicates whether the table is to be dynamically resized. It is set to 1 in dynamically allocated tables, 0 in tables that are statically allocated.
typedef void(* hnode_data_free_t) (void *key, void *data)

User node data deallocator function.

Default:
NULL (no user key and data are deallocated).
Parameters
keyPointer to node key data.
keyPointer to node user data.
See also
hash_table_create()

Definition at line 212 of file hash.h.

typedef struct hnode_t hnode_t

Hash chain node structure.

Notes:
1. This preprocessing directive is for debugging purposes. The effect is that if the preprocessor symbol KAZLIB_OPAQUE_DEBUG is defined prior to the inclusion of this header, then the structure shall be declared as having the single member int OPAQUE. This way, any attempts by the client code to violate the principles of information hiding (by accessing the structure directly) can be diagnosed at translation time. However, note the resulting compiled unit is not suitable for linking.
2. This is a pointer to the next node in the chain. In the last node of a chain, this pointer is null.
3. This is a back-pointer to the primary pointer to this node. The primary pointer is the previous node's next pointer to this node. If there is no previous node, the primary pointer is the pointer that resides in the hash table. This back-pointer lets us handle deletions easily without searching the chain.
4. The key is a pointer to some user supplied data that contains a unique identifier for each hash node in a given table. The interpretation of the data is up to the user. When creating or initializing a hash table, the user must supply a pointer to a function for comparing two keys, and a pointer to a function for hashing a key into a numeric value.
5. The value is a user-supplied pointer to void which may refer to any data object. It is not interpreted in any way by the hashing module.
6. The hashed key is stored in each node so that we don't have to rehash each key when the table must grow or shrink.
typedef struct hscan_t hscan_t

Hash scanner structure.

The scanner is used for traversals of the data structure.

Notes:
1. Pointer to the hash table that is being traversed.
2. Reference to the current chain in the table being traversed (the chain that contains the next node that shall be retrieved).
3. Pointer to the node that will be retrieved by the subsequent call to hash_scan_next().

Function Documentation

bool_t hash_delete ( hash_t hash,
void *  key 
)

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.

Parameters
hashHash table.
keyHash key.
Returns
Returns true if successful, else returns false.
Exceptions
Not InitialzedHashing not initialized.
Verification FailedOnly 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().

1311 {
1312  hnode_t *node;
1313 
1314  if( (node = hash_lookup(hash, key)) != NULL )
1315  {
1316  hash_node_unlink(hash, node);
1317  hnode_destroy(node, hash->freenodedata);
1318  return true;
1319  }
1320  return false;
1321 }
#define NULL
null pointer
Definition: rnrconfig.h:199
hnode_t * hash_lookup(hash_t *hash, const void *key)
Find a node in the hash table and return a pointer to it.
Definition: hash.c:1246
hnode_t * hash_node_unlink(hash_t *hash, hnode_t *node)
Unlink the given node from the hash table.
Definition: hash.c:1128
hnode_data_free_t freenodedata
Note 11.
Definition: hash.h:280
Hash chain node structure.
Definition: hash.h:149
void hnode_destroy(hnode_t *node, hnode_data_free_t freedatafun)
Hash node and user data deallocator.
Definition: hash.c:1376
bool_t hash_insert ( hash_t hash,
void *  key,
void *  data 
)

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.

Parameters
hashHash table.
keyHash key.
dataUser data.
Returns
Returns true if successful, else returns false.
Exceptions
Not InitialzedHashing not initialized.
Too BigReached maximum table size.
Duplicate KeyNode with key already exists.
Verification FailedOnly 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().

1284 {
1285  hnode_t *node = hnode_create(data);
1286 
1287  if (node)
1288  {
1289  hash_node_insert(hash, node, key);
1290  return true;
1291  }
1292 
1293  return false;
1294 }
void hash_node_insert(hash_t *hash, hnode_t *node, void *key)
Insert a node into the hash table.
Definition: hash.c:1063
hnode_t * hnode_create(void *data)
Create a hash table node dynamically and assign it the given data.
Definition: hash.c:1343
Hash chain node structure.
Definition: hash.h:149
hnode_t* hash_lookup ( hash_t hash,
const void *  key 
)

Find a node in the hash table and return a pointer to it.

Notes & Steps:
1. We hash the key and keep the entire hash value. As an optimization, when we descend down the chain, we can compare hash values first and only if hash values match do we perform a full key comparison.
2. To locate the chain from among 2^N chains, we look at the lower N bits of the hash value by anding them with the current mask.
3. Looping through the chain, we compare the stored hash value inside each node against our computed hash. If they match, then we do a full comparison between the unhashed keys. If these match, we have located the entry.
Parameters
hashHash table.
keyHash key.
Returns
Returns node with key on success, NULL if node not found.

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().

1247 {
1248  hash_val_t hkey, chain;
1249  hnode_t *nptr;
1250 
1251  hkey = hash->hash(key); // Step 1
1252  chain = hkey & hash->mask; // Step 2
1253 
1254  // Step 3
1255  for(nptr = hash->table[chain]; nptr; nptr = nptr->next)
1256  {
1257  if(nptr->hkey == hkey && hash->compare(nptr->key, key) == 0)
1258  {
1259  return nptr;
1260  }
1261  }
1262 
1263  return NULL;
1264 }
hash_val_t mask
Note 7.
Definition: hash.h:276
#define NULL
null pointer
Definition: rnrconfig.h:199
hash_comp_t compare
Note 9.
Definition: hash.h:278
struct hnode_t * next
<Note 1
Definition: hash.h:152
hnode_t ** table
Note 1.
Definition: hash.h:270
unsigned long hash_val_t
hashed value return type
Definition: hash.h:96
void * key
Note 4.
Definition: hash.h:154
hash_fun_t hash
Note 10.
Definition: hash.h:279
Hash chain node structure.
Definition: hash.h:149
hash_val_t hkey
Note 6.
Definition: hash.h:156
void hash_node_delete ( hash_t hash,
hnode_t node 
)

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.

Parameters
hashHash table.
nodeHash node to delete.
Exceptions
Not InitialzedHashing not initialized.
No KeyHash key not found.
Verification FailedOnly 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().

1218 {
1219  hash_node_unlink(hash, node);
1220  hnode_destroy(node, hash->freenodedata);
1221 }
hnode_t * hash_node_unlink(hash_t *hash, hnode_t *node)
Unlink the given node from the hash table.
Definition: hash.c:1128
hnode_data_free_t freenodedata
Note 11.
Definition: hash.h:280
void hnode_destroy(hnode_t *node, hnode_data_free_t freedatafun)
Hash node and user data deallocator.
Definition: hash.c:1376
hnode_t* hash_node_fast_unlink ( hash_t hash,
hnode_t node 
)

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.

See also
hash_node_unlink().
Parameters
hashHash table.
nodeNode to unlink, typically found by scanning.
Returns
Unlinked node.
Exceptions
Not InitialzedHashing not initialized.
No KeyHash key not found.
Verification FailedOnly 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().

1179 {
1180  LOGDIAG4CALL(_TPTR(hash), _TPTR(node));
1181 
1182  assert(hash_val_t_bit != 0);
1183  assert(hash_lookup(hash, node->key) == node); /* 1 */
1184 
1185  // Step 2 as in hash_node_inlink()
1186  if (node->next)
1187  {
1188  node->next->pself = node->pself;
1189  }
1190 
1191  // Step 3 as in hash_node_inlink()
1192  *node->pself = node->next;
1193 
1194  hash->count--;
1195 
1196  if( hash_self_verify )
1197  {
1198  assert(hash_table_verify(hash));
1199  }
1200 
1201  return node;
1202 }
struct hnode_t ** pself
Note 3.
Definition: hash.h:153
hnode_t * hash_lookup(hash_t *hash, const void *key)
Find a node in the hash table and return a pointer to it.
Definition: hash.c:1246
struct hnode_t * next
<Note 1
Definition: hash.h:152
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
hashcount_t count
Note 8.
Definition: hash.h:277
void * key
Note 4.
Definition: hash.h:154
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
bool_t hash_table_verify(hash_t *hash)
Verify hash table consistency.
Definition: hash.c:888
void hash_node_insert ( hash_t hash,
hnode_t node,
void *  key 
)

Insert a node into the hash table.

Steps & Notes:
1. It's illegal to insert more than the maximum number of nodes. The client should verify that the hash table is not full before attempting an insertion.
2. The same key may not be inserted into a table twice.
3. If the table is dynamic and the load factor is already at >= 2, grow the table.
4. We take the top N bits of the hash value to derive the chain index, where N is the base 2 logarithm of the size of the hash table.
Parameters
hashHash table.
nodeNode to insert.
keyHash key.
Exceptions
Not InitialzedHashing not initialized.
Too BigReached maximum table size.
Duplicate KeyNode with key already exists.
Verification FailedOnly 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().

1064 {
1065  LOGDIAG4CALL(_TPTR(hash), _TPTR(node), _TPTR(key));
1066 
1067  hash_val_t hkey, chain;
1068 
1069  assert(hash_val_t_bit != 0);
1070  assert(hash->count < hash->maxsize); // Step 1
1071  assert(hash_lookup(hash, key) == NULL); // Step 2
1072 
1073  // Step 3
1074  if(hash->dynamic && (hash->count+1) >= hash->highmark)
1075  {
1076  grow_table(hash);
1077  }
1078 
1079  hkey = hash->hash(key);
1080  chain = hkey & hash->mask; // Step 4
1081 
1082  node->key = key;
1083  node->hkey = hkey;
1084  node->pself = hash->table + chain;
1085  node->next = hash->table[chain];
1086  if (node->next)
1087  {
1088  node->next->pself = &node->next;
1089  }
1090  hash->table[chain] = node;
1091  hash->count++;
1092 
1093  if( hash_self_verify )
1094  {
1095  assert(hash_table_verify(hash));
1096  }
1097 }
bool_t dynamic
Note 12.
Definition: hash.h:281
struct hnode_t ** pself
Note 3.
Definition: hash.h:153
hash_val_t mask
Note 7.
Definition: hash.h:276
hashcount_t maxsize
Note 3.
Definition: hash.h:272
#define NULL
null pointer
Definition: rnrconfig.h:199
hnode_t * hash_lookup(hash_t *hash, const void *key)
Find a node in the hash table and return a pointer to it.
Definition: hash.c:1246
static void grow_table(hash_t *hash)
Double the size of a dynamic table.
Definition: hash.c:388
struct hnode_t * next
<Note 1
Definition: hash.h:152
hashcount_t highmark
Note 4.
Definition: hash.h:273
hnode_t ** table
Note 1.
Definition: hash.h:270
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
hashcount_t count
Note 8.
Definition: hash.h:277
unsigned long hash_val_t
hashed value return type
Definition: hash.h:96
void * key
Note 4.
Definition: hash.h:154
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
hash_fun_t hash
Note 10.
Definition: hash.h:279
bool_t hash_table_verify(hash_t *hash)
Verify hash table consistency.
Definition: hash.c:888
hash_val_t hkey
Note 6.
Definition: hash.h:156
hnode_t* hash_node_unlink ( hash_t hash,
hnode_t node 
)

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.

Notes & Steps:
1. The node must belong to this hash table, and its key must not have been tampered with.
2. If there is a next node, then we must update its back pointer to skip this node.
3. We must update the pointer that is pointed at by the back-pointer to skip over the node that is being deleted and instead point to the successor (or to NULL if the node being deleted is the last one).
Parameters
hashHash table.
nodeHash node to unlink.
Returns
Unlinked node.
Exceptions
Not InitialzedHashing not initialized.
No KeyHash key not found.
Verification FailedOnly 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().

1129 {
1130  LOGDIAG4CALL(_TPTR(hash), _TPTR(node));
1131 
1132  assert(hash_val_t_bit != 0);
1133  assert(hash_lookup(hash, node->key) == node); // Step 1
1134 
1135  if(hash->dynamic && (hash->count-1) <= hash->lowmark
1136  && hash->count > hash->minsize)
1137  {
1138  shrink_table(hash);
1139  }
1140 
1141  // Step 2
1142  if (node->next)
1143  {
1144  node->next->pself = node->pself;
1145  }
1146 
1147  // Step 3
1148  *node->pself = node->next;
1149 
1150  hash->count--;
1151 
1152  if( hash_self_verify )
1153  {
1154  assert(hash_table_verify(hash));
1155  }
1156 
1157  return node;
1158 }
bool_t dynamic
Note 12.
Definition: hash.h:281
struct hnode_t ** pself
Note 3.
Definition: hash.h:153
hashcount_t minsize
Note 2.
Definition: hash.h:271
hnode_t * hash_lookup(hash_t *hash, const void *key)
Find a node in the hash table and return a pointer to it.
Definition: hash.c:1246
struct hnode_t * next
<Note 1
Definition: hash.h:152
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
hashcount_t count
Note 8.
Definition: hash.h:277
hashcount_t lowmark
Note 5.
Definition: hash.h:274
void * key
Note 4.
Definition: hash.h:154
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
static void shrink_table(hash_t *hash)
Cut a dynamic table size in half.
Definition: hash.c:545
bool_t hash_table_verify(hash_t *hash)
Verify hash table consistency.
Definition: hash.c:888
void hash_scan_begin ( hscan_t scan,
hash_t hash 
)

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.

Notes & Steps:
1. Locate the first non empty chain.
2. If an empty chain is found, remember which one it is and set the next pointer to refer to its first element.
3. Otherwise if a chain is not found, set the next pointer to NULL so that hash_scan_next() shall indicate failure.
Parameters
scanHash iterator.
hashHash 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().

938 {
939  hash_val_t nchains = hash->nchains;
940  hash_val_t chain;
941 
942  scan->hash = hash;
943 
944  // Step 1
945  for(chain = 0; chain < nchains && hash->table[chain] == 0; chain++);
946 
947  // Step 2
948  if(chain < nchains)
949  {
950  scan->chain = chain;
951  scan->next = hash->table[chain];
952  }
953 
954  // Step 3
955  else
956  {
957  scan->next = NULL;
958  }
959 }
hashcount_t nchains
Note 6.
Definition: hash.h:275
hash_t * hash
Note 1.
Definition: hash.h:305
#define NULL
null pointer
Definition: rnrconfig.h:199
hnode_t ** table
Note 1.
Definition: hash.h:270
hash_val_t chain
Note 2.
Definition: hash.h:306
unsigned long hash_val_t
hashed value return type
Definition: hash.h:96
hnode_t * next
Note 3.
Definition: hash.h:307
hnode_t* hash_scan_next ( hscan_t scan)

Retrieve the next node from the hash table.

The scanner (iterator) updates the pointer for the next invocation of hash_scan_next().

Notes & Steps:
1. Remember the next pointer in a temporary value so that it can be returned.
2. This assertion essentially checks whether the module has been properly initialized. The first point of interaction with the module should be either hash_table_create() or hash_init(), both of which set hash_val_t_bit to a non zero value.
3. If the next pointer we are returning is not NULL, then the user is allowed to call hash_scan_next() again. We prepare the new next pointer for that call right now. That way the user is allowed to delete the node we are about to return, since we will no longer be needing it to locate the next node.
4. If there is a next node in the chain (next->next), then that becomes the new next node, otherwise ...
5. We have exhausted the current chain, and must locate the next subsequent non-empty chain in the table.
6. If a non-empty chain is found, the first element of that chain becomes the new next node. Otherwise there is no new next node and we set the pointer to NULL so that the next time hash_scan_next() is called, a null pointer shall be immediately returned.
Returns
Returns next node on success, NULL if no more nodes.
Exceptions
Not InitialzedHashing 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().

999 {
1000  hnode_t *next = scan->next; // Step 1
1001  hash_t *hash = scan->hash;
1002  hash_val_t chain = scan->chain + 1;
1003  hash_val_t nchains = hash->nchains;
1004 
1005  assert(hash_val_t_bit != 0); // Step 2
1006 
1007  // Step 3
1008  if(next)
1009  {
1010  // Step 4
1011  if(next->next)
1012  {
1013  scan->next = next->next;
1014  }
1015  else
1016  {
1017  // Step 5
1018  while(chain < nchains && hash->table[chain] == 0)
1019  {
1020  chain++;
1021  }
1022  // Step 6
1023  if (chain < nchains)
1024  {
1025  scan->chain = chain;
1026  scan->next = hash->table[chain];
1027  }
1028  else
1029  {
1030  scan->next = NULL;
1031  }
1032  }
1033  }
1034  return next;
1035 }
hashcount_t nchains
Note 6.
Definition: hash.h:275
hash_t * hash
Note 1.
Definition: hash.h:305
#define NULL
null pointer
Definition: rnrconfig.h:199
struct hnode_t * next
<Note 1
Definition: hash.h:152
hnode_t ** table
Note 1.
Definition: hash.h:270
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
hash_val_t chain
Note 2.
Definition: hash.h:306
unsigned long hash_val_t
hashed value return type
Definition: hash.h:96
hnode_t * next
Note 3.
Definition: hash.h:307
Hash table control structure.
Definition: hash.h:267
Hash chain node structure.
Definition: hash.h:149
void hash_set_self_verify ( bool_t  selfverify)

Enable (disable) automatic self-checks during hash table modification operatations.

Parameters
selfverifyTrue to enable, false to disable.
See also
hash_table_verify()

Definition at line 1331 of file hash.c.

References hash_self_verify.

Referenced by main(), and test_hash().

1332 {
1333  hash_self_verify = selfverify;
1334 }
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
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.

Notes & Steps:
1. If the number of bits in the hash_val_t type has not been computed yet, we do so here, because this is likely to be the first function that the user calls.
2. Allocate hash table.
3. Initialize hash table sizes. The minsize should be a power of two. The maxsize should be >= minsize. Both values will be adjusted. The high and low marks are always set to be twice the table size and half the table size respectively. When the number of nodes in the table grows beyond the high size (beyond load factor 2), it will double in size to cut the load factor down to about about 1. If the table shrinks down to or beneath load factor 0.5, it will shrink, bringing the load up to about 1. However, the table will never shrink beneath minsize even if it's emptied.
4. Allocate the table of hash chains.
5. Finish initializing the hash structure and the table. 6. This indicates that the table is dynamically allocated and dynamically resized on the fly. A table that has this value set to zero is assumed to be statically allocated and will not be resized.
7. The table of chains must be properly reset to all null pointers. 8. Verify table's correctness, if self-verification is true.
Parameters
isdynamicThe hash table is [not] automatically grow. If set to false, the hash table is initialized t be fixed at minsize and maxsize is ignored.
minsizeMinimum and initial number of hash entries that will be supported.
maxsizeMaximum number of hash entries that will be supported.
compfunHash key comparator function. NULL will use the default strcmp() wrapper.
hashfunHashing function. NULL will used the default string hashing function.
freedatafunUser key and data deallocator function. NULL will cause no user key or data to be deleted.
Returns
Newly created, empty hash table.
Exceptions
Verification FailedOnly 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().

697 {
698  hash_t *hash;
699 
700  LOGDIAG4CALL(_TBOOL(isdynamic), _TULONG(minsize), _TULONG(maxsize),
701  _TPTR(compfun), _TPTR(hashfun), _TPTR(freedatafun));
702 
703  // Step 1
704  if( hash_val_t_bit == 0 )
705  {
706  compute_bits();
707  }
708 
709  // Step 2
710  hash = NEW(hash_t);
711 
712  // Step 3
713  init_hash_sizes(hash, minsize, maxsize);
714 
715  // Step 4
716  hash->table = (hnode_t **)new(sizeof(hnode_t *) * hash->minsize);
717 
718  // Step 5
719  hash->nchains = minsize;
720  hash->mask = compute_mask(hash->nchains);
721  hash->count = 0;
722  hash->compare = compfun ? compfun : hash_comp_default;
723  hash->hash = hashfun ? hashfun : hash_fun_default;
724  hash->freenodedata = freedatafun;
725 
726  // Step 6
727  hash->dynamic = isdynamic;
728 
729  // Step 7
730  clear_table(hash);
731 
732  // Step 8
733  if( hash_self_verify )
734  {
735  assert(hash_table_verify(hash));
736  }
737 
738  LOGDIAG4("min=%lu, max=%lu, high=%lu, low=%lu, nchains=%lu, mask=0x%lx\n",
739  hash->minsize, hash->maxsize, hash->highmark,
740  hash->lowmark, hash->nchains, hash->mask);
741 
742  return hash;
743 }
static void compute_bits()
Compute the number of bits in the hash_val_t type.
Definition: hash.c:141
bool_t dynamic
Note 12.
Definition: hash.h:281
hash_val_t mask
Note 7.
Definition: hash.h:276
hashcount_t nchains
Note 6.
Definition: hash.h:275
hashcount_t maxsize
Note 3.
Definition: hash.h:272
hashcount_t minsize
Note 2.
Definition: hash.h:271
static void clear_table(hash_t *hash)
Initialize the table of pointers to null.
Definition: hash.c:307
#define _TULONG(var)
unsigned long int (decimal)
Definition: log.h:580
hash_comp_t compare
Note 9.
Definition: hash.h:278
static hash_val_t hash_fun_default(const void *key)
Default hashing function over null-terminated string keys.
Definition: hash.c:263
hashcount_t highmark
Note 4.
Definition: hash.h:273
hnode_t ** table
Note 1.
Definition: hash.h:270
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
hashcount_t count
Note 8.
Definition: hash.h:277
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
#define NEW(T)
Allocate new type.
Definition: new.h:49
hashcount_t lowmark
Note 5.
Definition: hash.h:274
#define _TBOOL(var)
boolean
Definition: log.h:588
static hash_val_t compute_mask(hashcount_t size)
Compute a mask for a given table size.
Definition: hash.c:185
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
hnode_data_free_t freenodedata
Note 11.
Definition: hash.h:280
Hash table control structure.
Definition: hash.h:267
hash_fun_t hash
Note 10.
Definition: hash.h:279
Hash chain node structure.
Definition: hash.h:149
bool_t hash_table_verify(hash_t *hash)
Verify hash table consistency.
Definition: hash.c:888
static void init_hash_sizes(hash_t *hash, hashcount_t minsize, hashcount_t maxsize)
Initialize the table size values.
Definition: hash.c:215
static int hash_comp_default(const void *key1, const void *key2)
Default hash key comparator function of null-terminated key strings.
Definition: hash.c:297
void hash_table_delete ( hash_t hash)

Frees a dynamic hash table structure.

Parameters
hashHash table.
Warning
The hash table must be empty.
Exceptions
Not InitialzedHashing not initialized.
Not EmptyHash 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().

777 {
778  assert(hash_val_t_bit != 0);
779  assert(hash_isempty(hash));
780 
781  delete(hash->table);
782  delete(hash);
783 }
hnode_t ** table
Note 1.
Definition: hash.h:270
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
#define hash_isempty(H)
Returns true if table is empty, false otherwise.
Definition: hash.h:385
void hash_table_destroy ( hash_t hash)

Delete the hash table, all of its entries, and all of the user data.

Parameters
hashHash 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().

751 {
752  hscan_t hs;
753  hnode_t *node;
754 
755  LOGDIAG4CALL(_TPTR(hash));
756 
757  hash_scan_begin(&hs, hash);
758  while((node = hash_scan_next(&hs)))
759  {
760  hash_node_fast_unlink(hash, node);
761  hnode_destroy(node, hash->freenodedata);
762  }
763  hash_table_delete(hash);
764 }
hnode_t * hash_scan_next(hscan_t *scan)
Retrieve the next node from the hash table.
Definition: hash.c:998
hnode_t * hash_node_fast_unlink(hash_t *hash, hnode_t *node)
Fast version to unlink the given node from the hash table.
Definition: hash.c:1178
void hash_table_delete(hash_t *hash)
Frees a dynamic hash table structure.
Definition: hash.c:776
void hash_scan_begin(hscan_t *scan, hash_t *hash)
Reset the hash scanner (iterator).
Definition: hash.c:937
Hash scanner structure.
Definition: hash.h:302
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
hnode_data_free_t freenodedata
Note 11.
Definition: hash.h:280
Hash chain node structure.
Definition: hash.h:149
void hnode_destroy(hnode_t *node, hnode_data_free_t freedatafun)
Hash node and user data deallocator.
Definition: hash.c:1376
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.

Notes & Steps:
1. Initialize hashing.
2. Assign hash chains. The user supplied array of pointers hopefully contains nchains nodes.
3. Fixed size. 4. We must dynamically compute the mask from the given power of two table size.
5. The user supplied table can't be assumed to contain null pointers, so we reset it here.
Warning
All data in destination hash table is overwritten.
See also
hash_table_create()
Parameters
hashThe destination hash table.
minsizeMinimum and initial number of hash entries that will be supported.
maxsizeMaximum number of hash entries that will be supported.
compfunHash key comparator function. NULL will use the default strcmp() wrapper.
hashfunHashing function. NULL will used the default string hashing function.
freedatafunUser key and data deallocator function. NULL will cause no user key or data to be deleted.
tableThe actual hash chain table to attach.
nchainsNumber of nodes in hash chain.
Returns
Hash table.
Exceptions
Bad Parameternchain not a power of 2.
Verification FailedOnly 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.

832 {
833  LOGDIAG4CALL(_TPTR(hash), _TULONG(minsize), _TULONG(maxsize),
834  _TPTR(compfun), _TPTR(hashfun), _TPTR(freedatafun),
835  _TPTR(table), _TULONG(nchains));
836 
837  // Step 1
838  if(hash_val_t_bit == 0)
839  {
840  compute_bits();
841  }
842 
843  assert(is_power_of_two(nchains));
844 
845  init_hash_sizes(hash, minsize, maxsize);
846 
847  // Step 2
848  hash->table = table;
849  hash->nchains = nchains;
850  hash->mask = compute_mask(nchains); // Step 4
851  hash->count = 0;
852  hash->compare = compfun ? compfun : hash_comp_default;
853  hash->hash = hashfun ? hashfun : hash_fun_default;
854  hash->freenodedata = freedatafun;
855  hash->dynamic = false; // Step 3
856 
857  clear_table(hash); // Step 5
858 
859  if( hash_self_verify )
860  {
861  assert (hash_table_verify(hash));
862  }
863 
864  LOGDIAG4("min=%lu, max=%lu, high=%lu, low=%lu, nchains=%lu, mask=0x%lx\n",
865  hash->minsize, hash->maxsize, hash->highmark,
866  hash->lowmark, hash->nchains, hash->mask);
867 
868  return hash;
869 }
static void compute_bits()
Compute the number of bits in the hash_val_t type.
Definition: hash.c:141
bool_t dynamic
Note 12.
Definition: hash.h:281
hash_val_t mask
Note 7.
Definition: hash.h:276
hashcount_t nchains
Note 6.
Definition: hash.h:275
hashcount_t maxsize
Note 3.
Definition: hash.h:272
hashcount_t minsize
Note 2.
Definition: hash.h:271
static void clear_table(hash_t *hash)
Initialize the table of pointers to null.
Definition: hash.c:307
#define _TULONG(var)
unsigned long int (decimal)
Definition: log.h:580
hash_comp_t compare
Note 9.
Definition: hash.h:278
static hash_val_t hash_fun_default(const void *key)
Default hashing function over null-terminated string keys.
Definition: hash.c:263
hashcount_t highmark
Note 4.
Definition: hash.h:273
hnode_t ** table
Note 1.
Definition: hash.h:270
static int hash_val_t_bit
Number of bits in hashed value. Must be a power of 2.
Definition: hash.c:114
static bool_t hash_self_verify
Do [not] perform auto-verification after hash table operations.
Definition: hash.c:119
hashcount_t count
Note 8.
Definition: hash.h:277
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
hashcount_t lowmark
Note 5.
Definition: hash.h:274
static hash_val_t compute_mask(hashcount_t size)
Compute a mask for a given table size.
Definition: hash.c:185
#define _TPTR(var)
pointer
Definition: log.h:587
#define LOGDIAG4CALL(...)
Standard Diagnostic Level 4 function call tracing.
Definition: log.h:442
hnode_data_free_t freenodedata
Note 11.
Definition: hash.h:280
hash_fun_t hash
Note 10.
Definition: hash.h:279
bool_t hash_table_verify(hash_t *hash)
Verify hash table consistency.
Definition: hash.c:888
static bool_t is_power_of_two(hash_val_t arg)
Verify whether the given argument is a power of two.
Definition: hash.c:163
static void init_hash_sizes(hash_t *hash, hashcount_t minsize, hashcount_t maxsize)
Initialize the table size values.
Definition: hash.c:215
static int hash_comp_default(const void *key1, const void *key2)
Default hash key comparator function of null-terminated key strings.
Definition: hash.c:297
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.

Notes & Steps:
1. If the hash table is dynamic, verify whether the high and low expansion/shrinkage thresholds are powers of two. 2. For each chain, verify whether the back pointers are correctly set. Count all nodes in the table, and test each hash value to see whether it is correct for the node's chain.
Returns
Returns true is the table is okay, else returns false.

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().

889 {
890  hashcount_t count = 0;
891  hash_val_t chain;
892  hnode_t **npp;
893 
894  // Step 1
895  if (hash->dynamic)
896  {
897  CHKEXPR_ULONG(hash->lowmark, (hash->lowmark < hash->highmark), false);
898  CHKEXPR_ULONG(hash->lowmark, is_power_of_two(hash->lowmark), false);
899  CHKEXPR_ULONG(hash->highmark, is_power_of_two(hash->highmark), false);
900  }
901 
902  // Step 2
903  for(chain = 0; chain < hash->nchains; chain++)
904  {
905  for(npp = hash->table + chain; *npp; npp = &(*npp)->next)
906  {
907  CHKEXPR_PTR(npp, ((*npp)->pself == npp), false);
908  CHKEXPR_PTR(npp, (((*npp)->hkey & hash->mask) == chain), false);
909  count++;
910  }
911  }
912 
913  CHKEXPR_ULONG(hash->count, (hash->count == count), false);
914 
915  return true;
916 }
bool_t dynamic
Note 12.
Definition: hash.h:281
hash_val_t mask
Note 7.
Definition: hash.h:276
hashcount_t nchains
Note 6.
Definition: hash.h:275
#define CHKEXPR_PTR(val, expr,...)
check pointer
Definition: log.h:712
struct hnode_t * next
<Note 1
Definition: hash.h:152
hashcount_t highmark
Note 4.
Definition: hash.h:273
hnode_t ** table
Note 1.
Definition: hash.h:270
unsigned long hashcount_t
maximum hash table size type
Definition: hash.h:94
hashcount_t count
Note 8.
Definition: hash.h:277
unsigned long hash_val_t
hashed value return type
Definition: hash.h:96
hashcount_t lowmark
Note 5.
Definition: hash.h:274
Hash chain node structure.
Definition: hash.h:149
static bool_t is_power_of_two(hash_val_t arg)
Verify whether the given argument is a power of two.
Definition: hash.c:163
#define CHKEXPR_ULONG(val, expr,...)
check unsigned long integer
Definition: log.h:697
hnode_t* hnode_create ( void *  data)

Create a hash table node dynamically and assign it the given data.

Parameters
dataUser data.
Returns
New hash node.

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().

1344 {
1345  hnode_t *node = NEW(hnode_t);
1346 
1347  node->data = data;
1348  node->next = NULL;
1349  node->pself = NULL;
1350 
1351  return node;
1352 }
struct hnode_t ** pself
Note 3.
Definition: hash.h:153
void * data
Note 5.
Definition: hash.h:155
#define NULL
null pointer
Definition: rnrconfig.h:199
struct hnode_t * next
<Note 1
Definition: hash.h:152
#define NEW(T)
Allocate new type.
Definition: new.h:49
Hash chain node structure.
Definition: hash.h:149
void hnode_delete ( hnode_t node)

Destroy a dynamically allocated node.

User data and key are not touched.

Parameters
nodeHash node.

Definition at line 1392 of file hash.c.

1393 {
1394  delete(node);
1395 }
void hnode_destroy ( hnode_t node,
hnode_data_free_t  freedatafun 
)

Hash node and user data deallocator.

Parameters
nodeNode to deallocate.
freedatafunUser 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().

1377 {
1378  if( freedatafun != NULL )
1379  {
1380  freedatafun(node->key, node->data);
1381  }
1382  delete(node);
1383 }
void * data
Note 5.
Definition: hash.h:155
#define NULL
null pointer
Definition: rnrconfig.h:199
void * key
Note 4.
Definition: hash.h:154
hnode_t* hnode_init ( hnode_t node,
void *  data 
)

Initialize a client-supplied hash node.

Parameters
nodeHash node.
dataUser data.
Returns
Same hash node.

Definition at line 1362 of file hash.c.

References hnode_t::data, hnode_t::next, NULL, and hnode_t::pself.

1363 {
1364  node->data = data;
1365  node->next = NULL;
1366  node->pself = NULL;
1367  return node;
1368 }
struct hnode_t ** pself
Note 3.
Definition: hash.h:153
void * data
Note 5.
Definition: hash.h:155
#define NULL
null pointer
Definition: rnrconfig.h:199
struct hnode_t * next
<Note 1
Definition: hash.h:152