hashtab.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /* An expandable hash tables datatype.
  2. Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2009, 2010
  3. Free Software Foundation, Inc.
  4. Contributed by Vladimir Makarov (vmakarov@cygnus.com).
  5. This file is part of the libiberty library.
  6. Libiberty is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with libiberty; see the file COPYING.LIB. If
  16. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. /* This package implements basic hash table functionality. It is possible
  19. to search for an entry, create an entry and destroy an entry.
  20. Elements in the table are generic pointers.
  21. The size of the table is not fixed; if the occupancy of the table
  22. grows too high the hash table will be expanded.
  23. The abstract data implementation is based on generalized Algorithm D
  24. from Knuth's book "The art of computer programming". Hash table is
  25. expanded by creation of new hash table and transferring elements from
  26. the old table to the new table. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include <sys/types.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_MALLOC_H
  38. #include <malloc.h>
  39. #endif
  40. #ifdef HAVE_LIMITS_H
  41. #include <limits.h>
  42. #endif
  43. #ifdef HAVE_INTTYPES_H
  44. #include <inttypes.h>
  45. #endif
  46. #ifdef HAVE_STDINT_H
  47. #include <stdint.h>
  48. #endif
  49. #include <stdio.h>
  50. #include "libiberty.h"
  51. #include "ansidecl.h"
  52. #include "hashtab.h"
  53. #ifndef CHAR_BIT
  54. #define CHAR_BIT 8
  55. #endif
  56. static unsigned int higher_prime_index (unsigned long);
  57. static hashval_t htab_mod_1 (hashval_t, hashval_t, hashval_t, int);
  58. static hashval_t htab_mod (hashval_t, htab_t);
  59. static hashval_t htab_mod_m2 (hashval_t, htab_t);
  60. static hashval_t hash_pointer (const void *);
  61. static int eq_pointer (const void *, const void *);
  62. static int htab_expand (htab_t);
  63. static PTR *find_empty_slot_for_expand (htab_t, hashval_t);
  64. /* At some point, we could make these be NULL, and modify the
  65. hash-table routines to handle NULL specially; that would avoid
  66. function-call overhead for the common case of hashing pointers. */
  67. htab_hash htab_hash_pointer = hash_pointer;
  68. htab_eq htab_eq_pointer = eq_pointer;
  69. /* Table of primes and multiplicative inverses.
  70. Note that these are not minimally reduced inverses. Unlike when generating
  71. code to divide by a constant, we want to be able to use the same algorithm
  72. all the time. All of these inverses (are implied to) have bit 32 set.
  73. For the record, here's the function that computed the table; it's a
  74. vastly simplified version of the function of the same name from gcc. */
  75. #if 0
  76. unsigned int
  77. ceil_log2 (unsigned int x)
  78. {
  79. int i;
  80. for (i = 31; i >= 0 ; --i)
  81. if (x > (1u << i))
  82. return i+1;
  83. abort ();
  84. }
  85. unsigned int
  86. choose_multiplier (unsigned int d, unsigned int *mlp, unsigned char *shiftp)
  87. {
  88. unsigned long long mhigh;
  89. double nx;
  90. int lgup, post_shift;
  91. int pow, pow2;
  92. int n = 32, precision = 32;
  93. lgup = ceil_log2 (d);
  94. pow = n + lgup;
  95. pow2 = n + lgup - precision;
  96. nx = ldexp (1.0, pow) + ldexp (1.0, pow2);
  97. mhigh = nx / d;
  98. *shiftp = lgup - 1;
  99. *mlp = mhigh;
  100. return mhigh >> 32;
  101. }
  102. #endif
  103. struct prime_ent
  104. {
  105. hashval_t prime;
  106. hashval_t inv;
  107. hashval_t inv_m2; /* inverse of prime-2 */
  108. hashval_t shift;
  109. };
  110. static struct prime_ent const prime_tab[] = {
  111. { 7, 0x24924925, 0x9999999b, 2 },
  112. { 13, 0x3b13b13c, 0x745d1747, 3 },
  113. { 31, 0x08421085, 0x1a7b9612, 4 },
  114. { 61, 0x0c9714fc, 0x15b1e5f8, 5 },
  115. { 127, 0x02040811, 0x0624dd30, 6 },
  116. { 251, 0x05197f7e, 0x073260a5, 7 },
  117. { 509, 0x01824366, 0x02864fc8, 8 },
  118. { 1021, 0x00c0906d, 0x014191f7, 9 },
  119. { 2039, 0x0121456f, 0x0161e69e, 10 },
  120. { 4093, 0x00300902, 0x00501908, 11 },
  121. { 8191, 0x00080041, 0x00180241, 12 },
  122. { 16381, 0x000c0091, 0x00140191, 13 },
  123. { 32749, 0x002605a5, 0x002a06e6, 14 },
  124. { 65521, 0x000f00e2, 0x00110122, 15 },
  125. { 131071, 0x00008001, 0x00018003, 16 },
  126. { 262139, 0x00014002, 0x0001c004, 17 },
  127. { 524287, 0x00002001, 0x00006001, 18 },
  128. { 1048573, 0x00003001, 0x00005001, 19 },
  129. { 2097143, 0x00004801, 0x00005801, 20 },
  130. { 4194301, 0x00000c01, 0x00001401, 21 },
  131. { 8388593, 0x00001e01, 0x00002201, 22 },
  132. { 16777213, 0x00000301, 0x00000501, 23 },
  133. { 33554393, 0x00001381, 0x00001481, 24 },
  134. { 67108859, 0x00000141, 0x000001c1, 25 },
  135. { 134217689, 0x000004e1, 0x00000521, 26 },
  136. { 268435399, 0x00000391, 0x000003b1, 27 },
  137. { 536870909, 0x00000019, 0x00000029, 28 },
  138. { 1073741789, 0x0000008d, 0x00000095, 29 },
  139. { 2147483647, 0x00000003, 0x00000007, 30 },
  140. /* Avoid "decimal constant so large it is unsigned" for 4294967291. */
  141. { 0xfffffffb, 0x00000006, 0x00000008, 31 }
  142. };
  143. /* The following function returns an index into the above table of the
  144. nearest prime number which is greater than N, and near a power of two. */
  145. static unsigned int
  146. higher_prime_index (unsigned long n)
  147. {
  148. unsigned int low = 0;
  149. unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]);
  150. while (low != high)
  151. {
  152. unsigned int mid = low + (high - low) / 2;
  153. if (n > prime_tab[mid].prime)
  154. low = mid + 1;
  155. else
  156. high = mid;
  157. }
  158. /* If we've run out of primes, abort. */
  159. if (n > prime_tab[low].prime)
  160. {
  161. fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
  162. abort ();
  163. }
  164. return low;
  165. }
  166. /* Returns non-zero if P1 and P2 are equal. */
  167. static int
  168. eq_pointer (const PTR p1, const PTR p2)
  169. {
  170. return p1 == p2;
  171. }
  172. /* The parens around the function names in the next two definitions
  173. are essential in order to prevent macro expansions of the name.
  174. The bodies, however, are expanded as expected, so they are not
  175. recursive definitions. */
  176. /* Return the current size of given hash table. */
  177. #define htab_size(htab) ((htab)->size)
  178. size_t
  179. (htab_size) (htab_t htab)
  180. {
  181. return htab_size (htab);
  182. }
  183. /* Return the current number of elements in given hash table. */
  184. #define htab_elements(htab) ((htab)->n_elements - (htab)->n_deleted)
  185. size_t
  186. (htab_elements) (htab_t htab)
  187. {
  188. return htab_elements (htab);
  189. }
  190. /* Return X % Y. */
  191. static inline hashval_t
  192. htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift)
  193. {
  194. /* The multiplicative inverses computed above are for 32-bit types, and
  195. requires that we be able to compute a highpart multiply. */
  196. #ifdef UNSIGNED_64BIT_TYPE
  197. __extension__ typedef UNSIGNED_64BIT_TYPE ull;
  198. if (sizeof (hashval_t) * CHAR_BIT <= 32)
  199. {
  200. hashval_t t1, t2, t3, t4, q, r;
  201. t1 = ((ull)x * inv) >> 32;
  202. t2 = x - t1;
  203. t3 = t2 >> 1;
  204. t4 = t1 + t3;
  205. q = t4 >> shift;
  206. r = x - (q * y);
  207. return r;
  208. }
  209. #endif
  210. /* Otherwise just use the native division routines. */
  211. return x % y;
  212. }
  213. /* Compute the primary hash for HASH given HTAB's current size. */
  214. static inline hashval_t
  215. htab_mod (hashval_t hash, htab_t htab)
  216. {
  217. const struct prime_ent *p = &prime_tab[htab->size_prime_index];
  218. return htab_mod_1 (hash, p->prime, p->inv, p->shift);
  219. }
  220. /* Compute the secondary hash for HASH given HTAB's current size. */
  221. static inline hashval_t
  222. htab_mod_m2 (hashval_t hash, htab_t htab)
  223. {
  224. const struct prime_ent *p = &prime_tab[htab->size_prime_index];
  225. return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift);
  226. }
  227. /* This function creates table with length slightly longer than given
  228. source length. Created hash table is initiated as empty (all the
  229. hash table entries are HTAB_EMPTY_ENTRY). The function returns the
  230. created hash table, or NULL if memory allocation fails. */
  231. htab_t
  232. htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
  233. htab_del del_f, htab_alloc alloc_f, htab_free free_f)
  234. {
  235. return htab_create_typed_alloc (size, hash_f, eq_f, del_f, alloc_f, alloc_f,
  236. free_f);
  237. }
  238. /* As above, but uses the variants of ALLOC_F and FREE_F which accept
  239. an extra argument. */
  240. htab_t
  241. htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f,
  242. htab_del del_f, void *alloc_arg,
  243. htab_alloc_with_arg alloc_f,
  244. htab_free_with_arg free_f)
  245. {
  246. htab_t result;
  247. unsigned int size_prime_index;
  248. size_prime_index = higher_prime_index (size);
  249. size = prime_tab[size_prime_index].prime;
  250. result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab));
  251. if (result == NULL)
  252. return NULL;
  253. result->entries = (PTR *) (*alloc_f) (alloc_arg, size, sizeof (PTR));
  254. if (result->entries == NULL)
  255. {
  256. if (free_f != NULL)
  257. (*free_f) (alloc_arg, result);
  258. return NULL;
  259. }
  260. result->size = size;
  261. result->size_prime_index = size_prime_index;
  262. result->hash_f = hash_f;
  263. result->eq_f = eq_f;
  264. result->del_f = del_f;
  265. result->alloc_arg = alloc_arg;
  266. result->alloc_with_arg_f = alloc_f;
  267. result->free_with_arg_f = free_f;
  268. return result;
  269. }
  270. /*
  271. @deftypefn Supplemental htab_t htab_create_typed_alloc (size_t @var{size}, @
  272. htab_hash @var{hash_f}, htab_eq @var{eq_f}, htab_del @var{del_f}, @
  273. htab_alloc @var{alloc_tab_f}, htab_alloc @var{alloc_f}, @
  274. htab_free @var{free_f})
  275. This function creates a hash table that uses two different allocators
  276. @var{alloc_tab_f} and @var{alloc_f} to use for allocating the table itself
  277. and its entries respectively. This is useful when variables of different
  278. types need to be allocated with different allocators.
  279. The created hash table is slightly larger than @var{size} and it is
  280. initially empty (all the hash table entries are @code{HTAB_EMPTY_ENTRY}).
  281. The function returns the created hash table, or @code{NULL} if memory
  282. allocation fails.
  283. @end deftypefn
  284. */
  285. htab_t
  286. htab_create_typed_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
  287. htab_del del_f, htab_alloc alloc_tab_f,
  288. htab_alloc alloc_f, htab_free free_f)
  289. {
  290. htab_t result;
  291. unsigned int size_prime_index;
  292. size_prime_index = higher_prime_index (size);
  293. size = prime_tab[size_prime_index].prime;
  294. result = (htab_t) (*alloc_tab_f) (1, sizeof (struct htab));
  295. if (result == NULL)
  296. return NULL;
  297. result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
  298. if (result->entries == NULL)
  299. {
  300. if (free_f != NULL)
  301. (*free_f) (result);
  302. return NULL;
  303. }
  304. result->size = size;
  305. result->size_prime_index = size_prime_index;
  306. result->hash_f = hash_f;
  307. result->eq_f = eq_f;
  308. result->del_f = del_f;
  309. result->alloc_f = alloc_f;
  310. result->free_f = free_f;
  311. return result;
  312. }
  313. /* Update the function pointers and allocation parameter in the htab_t. */
  314. void
  315. htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f,
  316. htab_del del_f, PTR alloc_arg,
  317. htab_alloc_with_arg alloc_f, htab_free_with_arg free_f)
  318. {
  319. htab->hash_f = hash_f;
  320. htab->eq_f = eq_f;
  321. htab->del_f = del_f;
  322. htab->alloc_arg = alloc_arg;
  323. htab->alloc_with_arg_f = alloc_f;
  324. htab->free_with_arg_f = free_f;
  325. }
  326. /* These functions exist solely for backward compatibility. */
  327. #undef htab_create
  328. htab_t
  329. htab_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
  330. {
  331. return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free);
  332. }
  333. htab_t
  334. htab_try_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
  335. {
  336. return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free);
  337. }
  338. /* This function frees all memory allocated for given hash table.
  339. Naturally the hash table must already exist. */
  340. void
  341. htab_delete (htab_t htab)
  342. {
  343. size_t size = htab_size (htab);
  344. PTR *entries = htab->entries;
  345. int i;
  346. if (htab->del_f)
  347. for (i = size - 1; i >= 0; i--)
  348. if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
  349. (*htab->del_f) (entries[i]);
  350. if (htab->free_f != NULL)
  351. {
  352. (*htab->free_f) (entries);
  353. (*htab->free_f) (htab);
  354. }
  355. else if (htab->free_with_arg_f != NULL)
  356. {
  357. (*htab->free_with_arg_f) (htab->alloc_arg, entries);
  358. (*htab->free_with_arg_f) (htab->alloc_arg, htab);
  359. }
  360. }
  361. /* This function clears all entries in the given hash table. */
  362. void
  363. htab_empty (htab_t htab)
  364. {
  365. size_t size = htab_size (htab);
  366. PTR *entries = htab->entries;
  367. int i;
  368. if (htab->del_f)
  369. for (i = size - 1; i >= 0; i--)
  370. if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
  371. (*htab->del_f) (entries[i]);
  372. /* Instead of clearing megabyte, downsize the table. */
  373. if (size > 1024*1024 / sizeof (PTR))
  374. {
  375. int nindex = higher_prime_index (1024 / sizeof (PTR));
  376. int nsize = prime_tab[nindex].prime;
  377. if (htab->free_f != NULL)
  378. (*htab->free_f) (htab->entries);
  379. else if (htab->free_with_arg_f != NULL)
  380. (*htab->free_with_arg_f) (htab->alloc_arg, htab->entries);
  381. if (htab->alloc_with_arg_f != NULL)
  382. htab->entries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
  383. sizeof (PTR *));
  384. else
  385. htab->entries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
  386. htab->size = nsize;
  387. htab->size_prime_index = nindex;
  388. }
  389. else
  390. memset (entries, 0, size * sizeof (PTR));
  391. htab->n_deleted = 0;
  392. htab->n_elements = 0;
  393. }
  394. /* Similar to htab_find_slot, but without several unwanted side effects:
  395. - Does not call htab->eq_f when it finds an existing entry.
  396. - Does not change the count of elements/searches/collisions in the
  397. hash table.
  398. This function also assumes there are no deleted entries in the table.
  399. HASH is the hash value for the element to be inserted. */
  400. static PTR *
  401. find_empty_slot_for_expand (htab_t htab, hashval_t hash)
  402. {
  403. hashval_t index = htab_mod (hash, htab);
  404. size_t size = htab_size (htab);
  405. PTR *slot = htab->entries + index;
  406. hashval_t hash2;
  407. if (*slot == HTAB_EMPTY_ENTRY)
  408. return slot;
  409. else if (*slot == HTAB_DELETED_ENTRY)
  410. abort ();
  411. hash2 = htab_mod_m2 (hash, htab);
  412. for (;;)
  413. {
  414. index += hash2;
  415. if (index >= size)
  416. index -= size;
  417. slot = htab->entries + index;
  418. if (*slot == HTAB_EMPTY_ENTRY)
  419. return slot;
  420. else if (*slot == HTAB_DELETED_ENTRY)
  421. abort ();
  422. }
  423. }
  424. /* The following function changes size of memory allocated for the
  425. entries and repeatedly inserts the table elements. The occupancy
  426. of the table after the call will be about 50%. Naturally the hash
  427. table must already exist. Remember also that the place of the
  428. table entries is changed. If memory allocation failures are allowed,
  429. this function will return zero, indicating that the table could not be
  430. expanded. If all goes well, it will return a non-zero value. */
  431. static int
  432. htab_expand (htab_t htab)
  433. {
  434. PTR *oentries;
  435. PTR *olimit;
  436. PTR *p;
  437. PTR *nentries;
  438. size_t nsize, osize, elts;
  439. unsigned int oindex, nindex;
  440. oentries = htab->entries;
  441. oindex = htab->size_prime_index;
  442. osize = htab->size;
  443. olimit = oentries + osize;
  444. elts = htab_elements (htab);
  445. /* Resize only when table after removal of unused elements is either
  446. too full or too empty. */
  447. if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
  448. {
  449. nindex = higher_prime_index (elts * 2);
  450. nsize = prime_tab[nindex].prime;
  451. }
  452. else
  453. {
  454. nindex = oindex;
  455. nsize = osize;
  456. }
  457. if (htab->alloc_with_arg_f != NULL)
  458. nentries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
  459. sizeof (PTR *));
  460. else
  461. nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
  462. if (nentries == NULL)
  463. return 0;
  464. htab->entries = nentries;
  465. htab->size = nsize;
  466. htab->size_prime_index = nindex;
  467. htab->n_elements -= htab->n_deleted;
  468. htab->n_deleted = 0;
  469. p = oentries;
  470. do
  471. {
  472. PTR x = *p;
  473. if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
  474. {
  475. PTR *q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
  476. *q = x;
  477. }
  478. p++;
  479. }
  480. while (p < olimit);
  481. if (htab->free_f != NULL)
  482. (*htab->free_f) (oentries);
  483. else if (htab->free_with_arg_f != NULL)
  484. (*htab->free_with_arg_f) (htab->alloc_arg, oentries);
  485. return 1;
  486. }
  487. /* This function searches for a hash table entry equal to the given
  488. element. It cannot be used to insert or delete an element. */
  489. PTR
  490. htab_find_with_hash (htab_t htab, const PTR element, hashval_t hash)
  491. {
  492. hashval_t index, hash2;
  493. size_t size;
  494. PTR entry;
  495. htab->searches++;
  496. size = htab_size (htab);
  497. index = htab_mod (hash, htab);
  498. entry = htab->entries[index];
  499. if (entry == HTAB_EMPTY_ENTRY
  500. || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
  501. return entry;
  502. hash2 = htab_mod_m2 (hash, htab);
  503. for (;;)
  504. {
  505. htab->collisions++;
  506. index += hash2;
  507. if (index >= size)
  508. index -= size;
  509. entry = htab->entries[index];
  510. if (entry == HTAB_EMPTY_ENTRY
  511. || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
  512. return entry;
  513. }
  514. }
  515. /* Like htab_find_slot_with_hash, but compute the hash value from the
  516. element. */
  517. PTR
  518. htab_find (htab_t htab, const PTR element)
  519. {
  520. return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
  521. }
  522. /* This function searches for a hash table slot containing an entry
  523. equal to the given element. To delete an entry, call this with
  524. insert=NO_INSERT, then call htab_clear_slot on the slot returned
  525. (possibly after doing some checks). To insert an entry, call this
  526. with insert=INSERT, then write the value you want into the returned
  527. slot. When inserting an entry, NULL may be returned if memory
  528. allocation fails. */
  529. PTR *
  530. htab_find_slot_with_hash (htab_t htab, const PTR element,
  531. hashval_t hash, enum insert_option insert)
  532. {
  533. PTR *first_deleted_slot;
  534. hashval_t index, hash2;
  535. size_t size;
  536. PTR entry;
  537. size = htab_size (htab);
  538. if (insert == INSERT && size * 3 <= htab->n_elements * 4)
  539. {
  540. if (htab_expand (htab) == 0)
  541. return NULL;
  542. size = htab_size (htab);
  543. }
  544. index = htab_mod (hash, htab);
  545. htab->searches++;
  546. first_deleted_slot = NULL;
  547. entry = htab->entries[index];
  548. if (entry == HTAB_EMPTY_ENTRY)
  549. goto empty_entry;
  550. else if (entry == HTAB_DELETED_ENTRY)
  551. first_deleted_slot = &htab->entries[index];
  552. else if ((*htab->eq_f) (entry, element))
  553. return &htab->entries[index];
  554. hash2 = htab_mod_m2 (hash, htab);
  555. for (;;)
  556. {
  557. htab->collisions++;
  558. index += hash2;
  559. if (index >= size)
  560. index -= size;
  561. entry = htab->entries[index];
  562. if (entry == HTAB_EMPTY_ENTRY)
  563. goto empty_entry;
  564. else if (entry == HTAB_DELETED_ENTRY)
  565. {
  566. if (!first_deleted_slot)
  567. first_deleted_slot = &htab->entries[index];
  568. }
  569. else if ((*htab->eq_f) (entry, element))
  570. return &htab->entries[index];
  571. }
  572. empty_entry:
  573. if (insert == NO_INSERT)
  574. return NULL;
  575. if (first_deleted_slot)
  576. {
  577. htab->n_deleted--;
  578. *first_deleted_slot = HTAB_EMPTY_ENTRY;
  579. return first_deleted_slot;
  580. }
  581. htab->n_elements++;
  582. return &htab->entries[index];
  583. }
  584. /* Like htab_find_slot_with_hash, but compute the hash value from the
  585. element. */
  586. PTR *
  587. htab_find_slot (htab_t htab, const PTR element, enum insert_option insert)
  588. {
  589. return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
  590. insert);
  591. }
  592. /* This function deletes an element with the given value from hash
  593. table (the hash is computed from the element). If there is no matching
  594. element in the hash table, this function does nothing. */
  595. void
  596. htab_remove_elt (htab_t htab, PTR element)
  597. {
  598. htab_remove_elt_with_hash (htab, element, (*htab->hash_f) (element));
  599. }
  600. /* This function deletes an element with the given value from hash
  601. table. If there is no matching element in the hash table, this
  602. function does nothing. */
  603. void
  604. htab_remove_elt_with_hash (htab_t htab, PTR element, hashval_t hash)
  605. {
  606. PTR *slot;
  607. slot = htab_find_slot_with_hash (htab, element, hash, NO_INSERT);
  608. if (*slot == HTAB_EMPTY_ENTRY)
  609. return;
  610. if (htab->del_f)
  611. (*htab->del_f) (*slot);
  612. *slot = HTAB_DELETED_ENTRY;
  613. htab->n_deleted++;
  614. }
  615. /* This function clears a specified slot in a hash table. It is
  616. useful when you've already done the lookup and don't want to do it
  617. again. */
  618. void
  619. htab_clear_slot (htab_t htab, PTR *slot)
  620. {
  621. if (slot < htab->entries || slot >= htab->entries + htab_size (htab)
  622. || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY)
  623. abort ();
  624. if (htab->del_f)
  625. (*htab->del_f) (*slot);
  626. *slot = HTAB_DELETED_ENTRY;
  627. htab->n_deleted++;
  628. }
  629. /* This function scans over the entire hash table calling
  630. CALLBACK for each live entry. If CALLBACK returns false,
  631. the iteration stops. INFO is passed as CALLBACK's second
  632. argument. */
  633. void
  634. htab_traverse_noresize (htab_t htab, htab_trav callback, PTR info)
  635. {
  636. PTR *slot;
  637. PTR *limit;
  638. slot = htab->entries;
  639. limit = slot + htab_size (htab);
  640. do
  641. {
  642. PTR x = *slot;
  643. if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
  644. if (!(*callback) (slot, info))
  645. break;
  646. }
  647. while (++slot < limit);
  648. }
  649. /* Like htab_traverse_noresize, but does resize the table when it is
  650. too empty to improve effectivity of subsequent calls. */
  651. void
  652. htab_traverse (htab_t htab, htab_trav callback, PTR info)
  653. {
  654. size_t size = htab_size (htab);
  655. if (htab_elements (htab) * 8 < size && size > 32)
  656. htab_expand (htab);
  657. htab_traverse_noresize (htab, callback, info);
  658. }
  659. /* Return the fraction of fixed collisions during all work with given
  660. hash table. */
  661. double
  662. htab_collisions (htab_t htab)
  663. {
  664. if (htab->searches == 0)
  665. return 0.0;
  666. return (double) htab->collisions / (double) htab->searches;
  667. }
  668. /* Hash P as a null-terminated string.
  669. Copied from gcc/hashtable.c. Zack had the following to say with respect
  670. to applicability, though note that unlike hashtable.c, this hash table
  671. implementation re-hashes rather than chain buckets.
  672. http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html
  673. From: Zack Weinberg <zackw@panix.com>
  674. Date: Fri, 17 Aug 2001 02:15:56 -0400
  675. I got it by extracting all the identifiers from all the source code
  676. I had lying around in mid-1999, and testing many recurrences of
  677. the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either
  678. prime numbers or the appropriate identity. This was the best one.
  679. I don't remember exactly what constituted "best", except I was
  680. looking at bucket-length distributions mostly.
  681. So it should be very good at hashing identifiers, but might not be
  682. as good at arbitrary strings.
  683. I'll add that it thoroughly trounces the hash functions recommended
  684. for this use at http://burtleburtle.net/bob/hash/index.html, both
  685. on speed and bucket distribution. I haven't tried it against the
  686. function they just started using for Perl's hashes. */
  687. hashval_t
  688. htab_hash_string (const PTR p)
  689. {
  690. const unsigned char *str = (const unsigned char *) p;
  691. hashval_t r = 0;
  692. unsigned char c;
  693. while ((c = *str++) != 0)
  694. r = r * 67 + c - 113;
  695. return r;
  696. }
  697. /* DERIVED FROM:
  698. --------------------------------------------------------------------
  699. lookup2.c, by Bob Jenkins, December 1996, Public Domain.
  700. hash(), hash2(), hash3, and mix() are externally useful functions.
  701. Routines to test the hash are included if SELF_TEST is defined.
  702. You can use this free for any purpose. It has no warranty.
  703. --------------------------------------------------------------------
  704. */
  705. /*
  706. --------------------------------------------------------------------
  707. mix -- mix 3 32-bit values reversibly.
  708. For every delta with one or two bit set, and the deltas of all three
  709. high bits or all three low bits, whether the original value of a,b,c
  710. is almost all zero or is uniformly distributed,
  711. * If mix() is run forward or backward, at least 32 bits in a,b,c
  712. have at least 1/4 probability of changing.
  713. * If mix() is run forward, every bit of c will change between 1/3 and
  714. 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
  715. mix() was built out of 36 single-cycle latency instructions in a
  716. structure that could supported 2x parallelism, like so:
  717. a -= b;
  718. a -= c; x = (c>>13);
  719. b -= c; a ^= x;
  720. b -= a; x = (a<<8);
  721. c -= a; b ^= x;
  722. c -= b; x = (b>>13);
  723. ...
  724. Unfortunately, superscalar Pentiums and Sparcs can't take advantage
  725. of that parallelism. They've also turned some of those single-cycle
  726. latency instructions into multi-cycle latency instructions. Still,
  727. this is the fastest good hash I could find. There were about 2^^68
  728. to choose from. I only looked at a billion or so.
  729. --------------------------------------------------------------------
  730. */
  731. /* same, but slower, works on systems that might have 8 byte hashval_t's */
  732. #define mix(a,b,c) \
  733. { \
  734. a -= b; a -= c; a ^= (c>>13); \
  735. b -= c; b -= a; b ^= (a<< 8); \
  736. c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
  737. a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
  738. b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
  739. c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
  740. a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
  741. b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
  742. c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
  743. }
  744. /*
  745. --------------------------------------------------------------------
  746. hash() -- hash a variable-length key into a 32-bit value
  747. k : the key (the unaligned variable-length array of bytes)
  748. len : the length of the key, counting by bytes
  749. level : can be any 4-byte value
  750. Returns a 32-bit value. Every bit of the key affects every bit of
  751. the return value. Every 1-bit and 2-bit delta achieves avalanche.
  752. About 36+6len instructions.
  753. The best hash table sizes are powers of 2. There is no need to do
  754. mod a prime (mod is sooo slow!). If you need less than 32 bits,
  755. use a bitmask. For example, if you need only 10 bits, do
  756. h = (h & hashmask(10));
  757. In which case, the hash table should have hashsize(10) elements.
  758. If you are hashing n strings (ub1 **)k, do it like this:
  759. for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
  760. By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
  761. code any way you wish, private, educational, or commercial. It's free.
  762. See http://burtleburtle.net/bob/hash/evahash.html
  763. Use for hash table lookup, or anything where one collision in 2^32 is
  764. acceptable. Do NOT use for cryptographic purposes.
  765. --------------------------------------------------------------------
  766. */
  767. hashval_t
  768. iterative_hash (const PTR k_in /* the key */,
  769. register size_t length /* the length of the key */,
  770. register hashval_t initval /* the previous hash, or
  771. an arbitrary value */)
  772. {
  773. register const unsigned char *k = (const unsigned char *)k_in;
  774. register hashval_t a,b,c,len;
  775. /* Set up the internal state */
  776. len = length;
  777. a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
  778. c = initval; /* the previous hash value */
  779. /*---------------------------------------- handle most of the key */
  780. #ifndef WORDS_BIGENDIAN
  781. /* On a little-endian machine, if the data is 4-byte aligned we can hash
  782. by word for better speed. This gives nondeterministic results on
  783. big-endian machines. */
  784. if (sizeof (hashval_t) == 4 && (((size_t)k)&3) == 0)
  785. while (len >= 12) /* aligned */
  786. {
  787. a += *(hashval_t *)(k+0);
  788. b += *(hashval_t *)(k+4);
  789. c += *(hashval_t *)(k+8);
  790. mix(a,b,c);
  791. k += 12; len -= 12;
  792. }
  793. else /* unaligned */
  794. #endif
  795. while (len >= 12)
  796. {
  797. a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
  798. b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
  799. c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
  800. mix(a,b,c);
  801. k += 12; len -= 12;
  802. }
  803. /*------------------------------------- handle the last 11 bytes */
  804. c += length;
  805. switch(len) /* all the case statements fall through */
  806. {
  807. case 11: c+=((hashval_t)k[10]<<24);
  808. case 10: c+=((hashval_t)k[9]<<16);
  809. case 9 : c+=((hashval_t)k[8]<<8);
  810. /* the first byte of c is reserved for the length */
  811. case 8 : b+=((hashval_t)k[7]<<24);
  812. case 7 : b+=((hashval_t)k[6]<<16);
  813. case 6 : b+=((hashval_t)k[5]<<8);
  814. case 5 : b+=k[4];
  815. case 4 : a+=((hashval_t)k[3]<<24);
  816. case 3 : a+=((hashval_t)k[2]<<16);
  817. case 2 : a+=((hashval_t)k[1]<<8);
  818. case 1 : a+=k[0];
  819. /* case 0: nothing left to add */
  820. }
  821. mix(a,b,c);
  822. /*-------------------------------------------- report the result */
  823. return c;
  824. }
  825. /* Returns a hash code for pointer P. Simplified version of evahash */
  826. static hashval_t
  827. hash_pointer (const PTR p)
  828. {
  829. intptr_t v = (intptr_t) p;
  830. unsigned a, b, c;
  831. a = b = 0x9e3779b9;
  832. a += v >> (sizeof (intptr_t) * CHAR_BIT / 2);
  833. b += v & (((intptr_t) 1 << (sizeof (intptr_t) * CHAR_BIT / 2)) - 1);
  834. c = 0x42135234;
  835. mix (a, b, c);
  836. return c;
  837. }