erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

843 lines
24 KiB

5 years ago
  1. /*
  2. * Hash Table Data Type
  3. * Copyright (C) 1997 Kaz Kylheku <kaz@ashi.footprints.net>
  4. *
  5. * Free Software License:
  6. *
  7. * All rights are reserved by the author, with the following exceptions:
  8. * Permission is granted to freely reproduce and distribute this software,
  9. * possibly in exchange for a fee, provided that this copyright notice appears
  10. * intact. Permission is also granted to adapt this software to produce
  11. * derivative works, as long as the modified versions carry this copyright
  12. * notice and additional notices stating that the work has been modified.
  13. * This source code may be translated into executable form and incorporated
  14. * into proprietary software; there is no requirement for such software to
  15. * contain a copyright notice related to this source.
  16. *
  17. * $Id: hash.c,v 1.36.2.11 2000/11/13 01:36:45 kaz Exp $
  18. * $Name: kazlib_1_20 $
  19. */
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #include <assert.h>
  23. #include <string.h>
  24. #define HASH_IMPLEMENTATION
  25. #include "hash.h"
  26. #ifdef KAZLIB_RCSID
  27. static const char rcsid[] = "$Id: hash.c,v 1.36.2.11 2000/11/13 01:36:45 kaz Exp $";
  28. #endif
  29. #define INIT_BITS 6
  30. #define INIT_SIZE (1UL << (INIT_BITS)) /* must be power of two */
  31. #define INIT_MASK ((INIT_SIZE) - 1)
  32. #define next hash_next
  33. #define key hash_key
  34. #define data hash_data
  35. #define hkey hash_hkey
  36. #define table hash_table
  37. #define nchains hash_nchains
  38. #define nodecount hash_nodecount
  39. #define maxcount hash_maxcount
  40. #define highmark hash_highmark
  41. #define lowmark hash_lowmark
  42. #define compare hash_compare
  43. #define function hash_function
  44. #define allocnode hash_allocnode
  45. #define freenode hash_freenode
  46. #define context hash_context
  47. #define mask hash_mask
  48. #define dynamic hash_dynamic
  49. #define table hash_table
  50. #define chain hash_chain
  51. static hnode_t *kl_hnode_alloc(void *context);
  52. static void kl_hnode_free(hnode_t *node, void *context);
  53. static hash_val_t hash_fun_default(const void *key);
  54. static int hash_comp_default(const void *key1, const void *key2);
  55. int hash_val_t_bit;
  56. /*
  57. * Compute the number of bits in the hash_val_t type. We know that hash_val_t
  58. * is an unsigned integral type. Thus the highest value it can hold is a
  59. * Mersenne number (power of two, less one). We initialize a hash_val_t
  60. * object with this value and then shift bits out one by one while counting.
  61. * Notes:
  62. * 1. HASH_VAL_T_MAX is a Mersenne number---one that is one less than a power
  63. * of two. This means that its binary representation consists of all one
  64. * bits, and hence ``val'' is initialized to all one bits.
  65. * 2. While bits remain in val, we increment the bit count and shift it to the
  66. * right, replacing the topmost bit by zero.
  67. */
  68. static void compute_bits(void)
  69. {
  70. hash_val_t val = HASH_VAL_T_MAX; /* 1 */
  71. int bits = 0;
  72. while (val) { /* 2 */
  73. bits++;
  74. val >>= 1;
  75. }
  76. hash_val_t_bit = bits;
  77. }
  78. /*
  79. * Verify whether the given argument is a power of two.
  80. */
  81. static int is_power_of_two(hash_val_t arg)
  82. {
  83. if (arg == 0)
  84. return 0;
  85. while ((arg & 1) == 0)
  86. arg >>= 1;
  87. return (arg == 1);
  88. }
  89. /*
  90. * Compute a shift amount from a given table size
  91. */
  92. static hash_val_t compute_mask(hashcount_t size)
  93. {
  94. assert (is_power_of_two(size));
  95. assert (size >= 2);
  96. return size - 1;
  97. }
  98. /*
  99. * Initialize the table of pointers to null.
  100. */
  101. static void clear_table(hash_t *hash)
  102. {
  103. hash_val_t i;
  104. for (i = 0; i < hash->nchains; i++)
  105. hash->table[i] = NULL;
  106. }
  107. /*
  108. * Double the size of a dynamic table. This works as follows. Each chain splits
  109. * into two adjacent chains. The shift amount increases by one, exposing an
  110. * additional bit of each hashed key. For each node in the original chain, the
  111. * value of this newly exposed bit will decide which of the two new chains will
  112. * receive the node: if the bit is 1, the chain with the higher index will have
  113. * the node, otherwise the lower chain will receive the node. In this manner,
  114. * the hash table will continue to function exactly as before without having to
  115. * rehash any of the keys.
  116. * Notes:
  117. * 1. Overflow check.
  118. * 2. The new number of chains is twice the old number of chains.
  119. * 3. The new mask is one bit wider than the previous, revealing a
  120. * new bit in all hashed keys.
  121. * 4. Allocate a new table of chain pointers that is twice as large as the
  122. * previous one.
  123. * 5. If the reallocation was successful, we perform the rest of the growth
  124. * algorithm, otherwise we do nothing.
  125. * 6. The exposed_bit variable holds a mask with which each hashed key can be
  126. * AND-ed to test the value of its newly exposed bit.
  127. * 7. Now loop over each chain in the table and sort its nodes into two
  128. * chains based on the value of each node's newly exposed hash bit.
  129. * 8. The low chain replaces the current chain. The high chain goes
  130. * into the corresponding sister chain in the upper half of the table.
  131. * 9. We have finished dealing with the chains and nodes. We now update
  132. * the various bookeeping fields of the hash structure.
  133. */
  134. static void grow_table(hash_t *hash)
  135. {
  136. hnode_t **newtable;
  137. assert (2 * hash->nchains > hash->nchains); /* 1 */
  138. newtable = realloc(hash->table,
  139. sizeof *newtable * hash->nchains * 2); /* 4 */
  140. if (newtable) { /* 5 */
  141. hash_val_t mask = (hash->mask << 1) | 1; /* 3 */
  142. hash_val_t exposed_bit = mask ^ hash->mask; /* 6 */
  143. hash_val_t chain;
  144. assert (mask != hash->mask);
  145. for (chain = 0; chain < hash->nchains; chain++) { /* 7 */
  146. hnode_t *low_chain = 0, *high_chain = 0, *hptr, *next;
  147. for (hptr = newtable[chain]; hptr != 0; hptr = next) {
  148. next = hptr->next;
  149. if (hptr->hkey & exposed_bit) {
  150. hptr->next = high_chain;
  151. high_chain = hptr;
  152. } else {
  153. hptr->next = low_chain;
  154. low_chain = hptr;
  155. }
  156. }
  157. newtable[chain] = low_chain; /* 8 */
  158. newtable[chain + hash->nchains] = high_chain;
  159. }
  160. hash->table = newtable; /* 9 */
  161. hash->mask = mask;
  162. hash->nchains *= 2;
  163. hash->lowmark *= 2;
  164. hash->highmark *= 2;
  165. }
  166. assert (kl_hash_verify(hash));
  167. }
  168. /*
  169. * Cut a table size in half. This is done by folding together adjacent chains
  170. * and populating the lower half of the table with these chains. The chains are
  171. * simply spliced together. Once this is done, the whole table is reallocated
  172. * to a smaller object.
  173. * Notes:
  174. * 1. It is illegal to have a hash table with one slot. This would mean that
  175. * hash->shift is equal to hash_val_t_bit, an illegal shift value.
  176. * Also, other things could go wrong, such as hash->lowmark becoming zero.
  177. * 2. Looping over each pair of sister chains, the low_chain is set to
  178. * point to the head node of the chain in the lower half of the table,
  179. * and high_chain points to the head node of the sister in the upper half.
  180. * 3. The intent here is to compute a pointer to the last node of the
  181. * lower chain into the low_tail variable. If this chain is empty,
  182. * low_tail ends up with a null value.
  183. * 4. If the lower chain is not empty, we simply tack the upper chain onto it.
  184. * If the upper chain is a null pointer, nothing happens.
  185. * 5. Otherwise if the lower chain is empty but the upper one is not,
  186. * If the low chain is empty, but the high chain is not, then the
  187. * high chain is simply transferred to the lower half of the table.
  188. * 6. Otherwise if both chains are empty, there is nothing to do.
  189. * 7. All the chain pointers are in the lower half of the table now, so
  190. * we reallocate it to a smaller object. This, of course, invalidates
  191. * all pointer-to-pointers which reference into the table from the
  192. * first node of each chain.
  193. * 8. Though it's unlikely, the reallocation may fail. In this case we
  194. * pretend that the table _was_ reallocated to a smaller object.
  195. * 9. Finally, update the various table parameters to reflect the new size.
  196. */
  197. static void shrink_table(hash_t *hash)
  198. {
  199. hash_val_t chain, nchains;
  200. hnode_t **newtable, *low_tail, *low_chain, *high_chain;
  201. assert (hash->nchains >= 2); /* 1 */
  202. nchains = hash->nchains / 2;
  203. for (chain = 0; chain < nchains; chain++) {
  204. low_chain = hash->table[chain]; /* 2 */
  205. high_chain = hash->table[chain + nchains];
  206. for (low_tail = low_chain; low_tail && low_tail->next; low_tail = low_tail->next)
  207. ; /* 3 */
  208. if (low_chain != 0) /* 4 */
  209. low_tail->next = high_chain;
  210. else if (high_chain != 0) /* 5 */
  211. hash->table[chain] = high_chain;
  212. else
  213. assert (hash->table[chain] == NULL); /* 6 */
  214. }
  215. newtable = realloc(hash->table,
  216. sizeof *newtable * nchains); /* 7 */
  217. if (newtable) /* 8 */
  218. hash->table = newtable;
  219. hash->mask >>= 1; /* 9 */
  220. hash->nchains = nchains;
  221. hash->lowmark /= 2;
  222. hash->highmark /= 2;
  223. assert (kl_hash_verify(hash));
  224. }
  225. /*
  226. * Create a dynamic hash table. Both the hash table structure and the table
  227. * itself are dynamically allocated. Furthermore, the table is extendible in
  228. * that it will automatically grow as its load factor increases beyond a
  229. * certain threshold.
  230. * Notes:
  231. * 1. If the number of bits in the hash_val_t type has not been computed yet,
  232. * we do so here, because this is likely to be the first function that the
  233. * user calls.
  234. * 2. Allocate a hash table control structure.
  235. * 3. If a hash table control structure is successfully allocated, we
  236. * proceed to initialize it. Otherwise we return a null pointer.
  237. * 4. We try to allocate the table of hash chains.
  238. * 5. If we were able to allocate the hash chain table, we can finish
  239. * initializing the hash structure and the table. Otherwise, we must
  240. * backtrack by freeing the hash structure.
  241. * 6. INIT_SIZE should be a power of two. The high and low marks are always set
  242. * to be twice the table size and half the table size respectively. When the
  243. * number of nodes in the table grows beyond the high size (beyond load
  244. * factor 2), it will double in size to cut the load factor down to about
  245. * about 1. If the table shrinks down to or beneath load factor 0.5,
  246. * it will shrink, bringing the load up to about 1. However, the table
  247. * will never shrink beneath INIT_SIZE even if it's emptied.
  248. * 7. This indicates that the table is dynamically allocated and dynamically
  249. * resized on the fly. A table that has this value set to zero is
  250. * assumed to be statically allocated and will not be resized.
  251. * 8. The table of chains must be properly reset to all null pointers.
  252. */
  253. hash_t *kl_hash_create(hashcount_t maxcount, hash_comp_t compfun,
  254. hash_fun_t hashfun)
  255. {
  256. hash_t *hash;
  257. if (hash_val_t_bit == 0) /* 1 */
  258. compute_bits();
  259. hash = malloc(sizeof *hash); /* 2 */
  260. if (hash) { /* 3 */
  261. hash->table = malloc(sizeof *hash->table * INIT_SIZE); /* 4 */
  262. if (hash->table) { /* 5 */
  263. hash->nchains = INIT_SIZE; /* 6 */
  264. hash->highmark = INIT_SIZE * 2;
  265. hash->lowmark = INIT_SIZE / 2;
  266. hash->nodecount = 0;
  267. hash->maxcount = maxcount;
  268. hash->compare = compfun ? compfun : hash_comp_default;
  269. hash->function = hashfun ? hashfun : hash_fun_default;
  270. hash->allocnode = kl_hnode_alloc;
  271. hash->freenode = kl_hnode_free;
  272. hash->context = NULL;
  273. hash->mask = INIT_MASK;
  274. hash->dynamic = 1; /* 7 */
  275. clear_table(hash); /* 8 */
  276. assert (kl_hash_verify(hash));
  277. return hash;
  278. }
  279. free(hash);
  280. }
  281. return NULL;
  282. }
  283. /*
  284. * Select a different set of node allocator routines.
  285. */
  286. void kl_hash_set_allocator(hash_t *hash, hnode_alloc_t al,
  287. hnode_free_t fr, void *context)
  288. {
  289. assert (kl_hash_count(hash) == 0);
  290. assert ((al == 0 && fr == 0) || (al != 0 && fr != 0));
  291. hash->allocnode = al ? al : kl_hnode_alloc;
  292. hash->freenode = fr ? fr : kl_hnode_free;
  293. hash->context = context;
  294. }
  295. /*
  296. * Free every node in the hash using the hash->freenode() function pointer, and
  297. * cause the hash to become empty.
  298. */
  299. void kl_hash_free_nodes(hash_t *hash)
  300. {
  301. hscan_t hs;
  302. hnode_t *node;
  303. kl_hash_scan_begin(&hs, hash);
  304. while ((node = kl_hash_scan_next(&hs))) {
  305. kl_hash_scan_delete(hash, node);
  306. hash->freenode(node, hash->context);
  307. }
  308. hash->nodecount = 0;
  309. clear_table(hash);
  310. }
  311. /*
  312. * Obsolescent function for removing all nodes from a table,
  313. * freeing them and then freeing the table all in one step.
  314. */
  315. void kl_hash_free(hash_t *hash)
  316. {
  317. #ifdef KAZLIB_OBSOLESCENT_DEBUG
  318. assert ("call to obsolescent function hash_free()" && 0);
  319. #endif
  320. kl_hash_free_nodes(hash);
  321. kl_hash_destroy(hash);
  322. }
  323. /*
  324. * Free a dynamic hash table structure.
  325. */
  326. void kl_hash_destroy(hash_t *hash)
  327. {
  328. assert (hash_val_t_bit != 0);
  329. assert (kl_hash_isempty(hash));
  330. free(hash->table);
  331. free(hash);
  332. }
  333. /*
  334. * Initialize a user supplied hash structure. The user also supplies a table of
  335. * chains which is assigned to the hash structure. The table is static---it
  336. * will not grow or shrink.
  337. * 1. See note 1. in hash_create().
  338. * 2. The user supplied array of pointers hopefully contains nchains nodes.
  339. * 3. See note 7. in hash_create().
  340. * 4. We must dynamically compute the mask from the given power of two table
  341. * size.
  342. * 5. The user supplied table can't be assumed to contain null pointers,
  343. * so we reset it here.
  344. */
  345. hash_t *kl_hash_init(hash_t *hash, hashcount_t maxcount,
  346. hash_comp_t compfun, hash_fun_t hashfun, hnode_t **table,
  347. hashcount_t nchains)
  348. {
  349. if (hash_val_t_bit == 0) /* 1 */
  350. compute_bits();
  351. assert (is_power_of_two(nchains));
  352. hash->table = table; /* 2 */
  353. hash->nchains = nchains;
  354. hash->nodecount = 0;
  355. hash->maxcount = maxcount;
  356. hash->compare = compfun ? compfun : hash_comp_default;
  357. hash->function = hashfun ? hashfun : hash_fun_default;
  358. hash->dynamic = 0; /* 3 */
  359. hash->mask = compute_mask(nchains); /* 4 */
  360. clear_table(hash); /* 5 */
  361. assert (kl_hash_verify(hash));
  362. return hash;
  363. }
  364. /*
  365. * Reset the hash scanner so that the next element retrieved by
  366. * hash_scan_next() shall be the first element on the first non-empty chain.
  367. * Notes:
  368. * 1. Locate the first non empty chain.
  369. * 2. If an empty chain is found, remember which one it is and set the next
  370. * pointer to refer to its first element.
  371. * 3. Otherwise if a chain is not found, set the next pointer to NULL
  372. * so that hash_scan_next() shall indicate failure.
  373. */
  374. void kl_hash_scan_begin(hscan_t *scan, hash_t *hash)
  375. {
  376. hash_val_t nchains = hash->nchains;
  377. hash_val_t chain;
  378. scan->table = hash;
  379. /* 1 */
  380. for (chain = 0; chain < nchains && hash->table[chain] == 0; chain++)
  381. ;
  382. if (chain < nchains) { /* 2 */
  383. scan->chain = chain;
  384. scan->next = hash->table[chain];
  385. } else { /* 3 */
  386. scan->next = NULL;
  387. }
  388. }
  389. /*
  390. * Retrieve the next node from the hash table, and update the pointer
  391. * for the next invocation of hash_scan_next().
  392. * Notes:
  393. * 1. Remember the next pointer in a temporary value so that it can be
  394. * returned.
  395. * 2. This assertion essentially checks whether the module has been properly
  396. * initialized. The first point of interaction with the module should be
  397. * either hash_create() or hash_init(), both of which set hash_val_t_bit to
  398. * a non zero value.
  399. * 3. If the next pointer we are returning is not NULL, then the user is
  400. * allowed to call hash_scan_next() again. We prepare the new next pointer
  401. * for that call right now. That way the user is allowed to delete the node
  402. * we are about to return, since we will no longer be needing it to locate
  403. * the next node.
  404. * 4. If there is a next node in the chain (next->next), then that becomes the
  405. * new next node, otherwise ...
  406. * 5. We have exhausted the current chain, and must locate the next subsequent
  407. * non-empty chain in the table.
  408. * 6. If a non-empty chain is found, the first element of that chain becomes
  409. * the new next node. Otherwise there is no new next node and we set the
  410. * pointer to NULL so that the next time hash_scan_next() is called, a null
  411. * pointer shall be immediately returned.
  412. */
  413. hnode_t *kl_hash_scan_next(hscan_t *scan)
  414. {
  415. hnode_t *next = scan->next; /* 1 */
  416. hash_t *hash = scan->table;
  417. hash_val_t chain = scan->chain + 1;
  418. hash_val_t nchains = hash->nchains;
  419. assert (hash_val_t_bit != 0); /* 2 */
  420. if (next) { /* 3 */
  421. if (next->next) { /* 4 */
  422. scan->next = next->next;
  423. } else {
  424. while (chain < nchains && hash->table[chain] == 0) /* 5 */
  425. chain++;
  426. if (chain < nchains) { /* 6 */
  427. scan->chain = chain;
  428. scan->next = hash->table[chain];
  429. } else {
  430. scan->next = NULL;
  431. }
  432. }
  433. }
  434. return next;
  435. }
  436. /*
  437. * Insert a node into the hash table.
  438. * Notes:
  439. * 1. It's illegal to insert more than the maximum number of nodes. The client
  440. * should verify that the hash table is not full before attempting an
  441. * insertion.
  442. * 2. The same key may not be inserted into a table twice.
  443. * 3. If the table is dynamic and the load factor is already at >= 2,
  444. * grow the table.
  445. * 4. We take the bottom N bits of the hash value to derive the chain index,
  446. * where N is the base 2 logarithm of the size of the hash table.
  447. */
  448. void kl_hash_insert(hash_t *hash, hnode_t *node, const void *key)
  449. {
  450. hash_val_t hkey, chain;
  451. assert (hash_val_t_bit != 0);
  452. assert (node->next == NULL);
  453. assert (hash->nodecount < hash->maxcount); /* 1 */
  454. assert (kl_hash_lookup(hash, key) == NULL); /* 2 */
  455. if (hash->dynamic && hash->nodecount >= hash->highmark) /* 3 */
  456. grow_table(hash);
  457. hkey = hash->function(key);
  458. chain = hkey & hash->mask; /* 4 */
  459. node->key = key;
  460. node->hkey = hkey;
  461. node->next = hash->table[chain];
  462. hash->table[chain] = node;
  463. hash->nodecount++;
  464. assert (kl_hash_verify(hash));
  465. }
  466. /*
  467. * Find a node in the hash table and return a pointer to it.
  468. * Notes:
  469. * 1. We hash the key and keep the entire hash value. As an optimization, when
  470. * we descend down the chain, we can compare hash values first and only if
  471. * hash values match do we perform a full key comparison.
  472. * 2. To locate the chain from among 2^N chains, we look at the lower N bits of
  473. * the hash value by anding them with the current mask.
  474. * 3. Looping through the chain, we compare the stored hash value inside each
  475. * node against our computed hash. If they match, then we do a full
  476. * comparison between the unhashed keys. If these match, we have located the
  477. * entry.
  478. */
  479. hnode_t *kl_hash_lookup(hash_t *hash, const void *key)
  480. {
  481. hash_val_t hkey, chain;
  482. hnode_t *nptr;
  483. hkey = hash->function(key); /* 1 */
  484. chain = hkey & hash->mask; /* 2 */
  485. for (nptr = hash->table[chain]; nptr; nptr = nptr->next) { /* 3 */
  486. if (nptr->hkey == hkey && hash->compare(nptr->key, key) == 0)
  487. return nptr;
  488. }
  489. return NULL;
  490. }
  491. /*
  492. * Delete the given node from the hash table. Since the chains
  493. * are singly linked, we must locate the start of the node's chain
  494. * and traverse.
  495. * Notes:
  496. * 1. The node must belong to this hash table, and its key must not have
  497. * been tampered with.
  498. * 2. If this deletion will take the node count below the low mark, we
  499. * shrink the table now.
  500. * 3. Determine which chain the node belongs to, and fetch the pointer
  501. * to the first node in this chain.
  502. * 4. If the node being deleted is the first node in the chain, then
  503. * simply update the chain head pointer.
  504. * 5. Otherwise advance to the node's predecessor, and splice out
  505. * by updating the predecessor's next pointer.
  506. * 6. Indicate that the node is no longer in a hash table.
  507. */
  508. hnode_t *kl_hash_delete(hash_t *hash, hnode_t *node)
  509. {
  510. hash_val_t chain;
  511. hnode_t *hptr;
  512. assert (kl_hash_lookup(hash, node->key) == node); /* 1 */
  513. assert (hash_val_t_bit != 0);
  514. if (hash->dynamic && hash->nodecount <= hash->lowmark
  515. && hash->nodecount > INIT_SIZE)
  516. shrink_table(hash); /* 2 */
  517. chain = node->hkey & hash->mask; /* 3 */
  518. hptr = hash->table[chain];
  519. if (hptr == node) { /* 4 */
  520. hash->table[chain] = node->next;
  521. } else {
  522. while (hptr->next != node) { /* 5 */
  523. assert (hptr != 0);
  524. hptr = hptr->next;
  525. }
  526. assert (hptr->next == node);
  527. hptr->next = node->next;
  528. }
  529. hash->nodecount--;
  530. assert (kl_hash_verify(hash));
  531. node->next = NULL; /* 6 */
  532. return node;
  533. }
  534. int kl_hash_alloc_insert(hash_t *hash, const void *key, void *data)
  535. {
  536. hnode_t *node = hash->allocnode(hash->context);
  537. if (node) {
  538. kl_hnode_init(node, data);
  539. kl_hash_insert(hash, node, key);
  540. return 1;
  541. }
  542. return 0;
  543. }
  544. void kl_hash_delete_free(hash_t *hash, hnode_t *node)
  545. {
  546. kl_hash_delete(hash, node);
  547. hash->freenode(node, hash->context);
  548. }
  549. /*
  550. * Exactly like hash_delete, except does not trigger table shrinkage. This is to be
  551. * used from within a hash table scan operation. See notes for hash_delete.
  552. */
  553. hnode_t *kl_hash_scan_delete(hash_t *hash, hnode_t *node)
  554. {
  555. hash_val_t chain;
  556. hnode_t *hptr;
  557. assert (kl_hash_lookup(hash, node->key) == node);
  558. assert (hash_val_t_bit != 0);
  559. chain = node->hkey & hash->mask;
  560. hptr = hash->table[chain];
  561. if (hptr == node) {
  562. hash->table[chain] = node->next;
  563. } else {
  564. while (hptr->next != node)
  565. hptr = hptr->next;
  566. hptr->next = node->next;
  567. }
  568. hash->nodecount--;
  569. assert (kl_hash_verify(hash));
  570. node->next = NULL;
  571. return node;
  572. }
  573. /*
  574. * Like hash_delete_free but based on hash_scan_delete.
  575. */
  576. void kl_hash_scan_delfree(hash_t *hash, hnode_t *node)
  577. {
  578. kl_hash_scan_delete(hash, node);
  579. hash->freenode(node, hash->context);
  580. }
  581. /*
  582. * Verify whether the given object is a valid hash table. This means
  583. * Notes:
  584. * 1. If the hash table is dynamic, verify whether the high and
  585. * low expansion/shrinkage thresholds are powers of two.
  586. * 2. Count all nodes in the table, and test each hash value
  587. * to see whether it is correct for the node's chain.
  588. */
  589. int kl_hash_verify(hash_t *hash)
  590. {
  591. hashcount_t count = 0;
  592. hash_val_t chain;
  593. hnode_t *hptr;
  594. if (hash->dynamic) { /* 1 */
  595. if (hash->lowmark >= hash->highmark)
  596. return 0;
  597. if (!is_power_of_two(hash->highmark))
  598. return 0;
  599. if (!is_power_of_two(hash->lowmark))
  600. return 0;
  601. }
  602. for (chain = 0; chain < hash->nchains; chain++) { /* 2 */
  603. for (hptr = hash->table[chain]; hptr != 0; hptr = hptr->next) {
  604. if ((hptr->hkey & hash->mask) != chain)
  605. return 0;
  606. count++;
  607. }
  608. }
  609. if (count != hash->nodecount)
  610. return 0;
  611. return 1;
  612. }
  613. /*
  614. * Test whether the hash table is full and return 1 if this is true,
  615. * 0 if it is false.
  616. */
  617. #undef kl_hash_isfull
  618. int kl_hash_isfull(hash_t *hash)
  619. {
  620. return hash->nodecount == hash->maxcount;
  621. }
  622. /*
  623. * Test whether the hash table is empty and return 1 if this is true,
  624. * 0 if it is false.
  625. */
  626. #undef kl_hash_isempty
  627. int kl_hash_isempty(hash_t *hash)
  628. {
  629. return hash->nodecount == 0;
  630. }
  631. static hnode_t *kl_hnode_alloc(void *context)
  632. {
  633. return malloc(sizeof *kl_hnode_alloc(NULL));
  634. }
  635. static void kl_hnode_free(hnode_t *node, void *context)
  636. {
  637. free(node);
  638. }
  639. /*
  640. * Create a hash table node dynamically and assign it the given data.
  641. */
  642. hnode_t *kl_hnode_create(void *data)
  643. {
  644. hnode_t *node = malloc(sizeof *node);
  645. if (node) {
  646. node->data = data;
  647. node->next = NULL;
  648. }
  649. return node;
  650. }
  651. /*
  652. * Initialize a client-supplied node
  653. */
  654. hnode_t *kl_hnode_init(hnode_t *hnode, void *data)
  655. {
  656. hnode->data = data;
  657. hnode->next = NULL;
  658. return hnode;
  659. }
  660. /*
  661. * Destroy a dynamically allocated node.
  662. */
  663. void kl_hnode_destroy(hnode_t *hnode)
  664. {
  665. free(hnode);
  666. }
  667. #undef kl_hnode_put
  668. void kl_hnode_put(hnode_t *node, void *data)
  669. {
  670. node->data = data;
  671. }
  672. #undef kl_hnode_get
  673. void *kl_hnode_get(hnode_t *node)
  674. {
  675. return node->data;
  676. }
  677. #undef kl_hnode_getkey
  678. const void *kl_hnode_getkey(hnode_t *node)
  679. {
  680. return node->key;
  681. }
  682. #undef kl_hash_count
  683. hashcount_t kl_hash_count(hash_t *hash)
  684. {
  685. return hash->nodecount;
  686. }
  687. #undef kl_hash_size
  688. hashcount_t kl_hash_size(hash_t *hash)
  689. {
  690. return hash->nchains;
  691. }
  692. static hash_val_t hash_fun_default(const void *key)
  693. {
  694. static unsigned long randbox[] = {
  695. 0x49848f1bU, 0xe6255dbaU, 0x36da5bdcU, 0x47bf94e9U,
  696. 0x8cbcce22U, 0x559fc06aU, 0xd268f536U, 0xe10af79aU,
  697. 0xc1af4d69U, 0x1d2917b5U, 0xec4c304dU, 0x9ee5016cU,
  698. 0x69232f74U, 0xfead7bb3U, 0xe9089ab6U, 0xf012f6aeU,
  699. };
  700. const unsigned char *str = key;
  701. hash_val_t acc = 0;
  702. while (*str) {
  703. acc ^= randbox[(*str + acc) & 0xf];
  704. acc = (acc << 1) | (acc >> 31);
  705. acc &= 0xffffffffU;
  706. acc ^= randbox[((*str++ >> 4) + acc) & 0xf];
  707. acc = (acc << 2) | (acc >> 30);
  708. acc &= 0xffffffffU;
  709. }
  710. return acc;
  711. }
  712. static int hash_comp_default(const void *key1, const void *key2)
  713. {
  714. return strcmp(key1, key2);
  715. }