hashtab.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* An expandable hash tables datatype.
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. Contributed by Vladimir Makarov <vmakarov@cygnus.com>.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* The hash table code copied from include/hashtab.[hc] and adjusted,
  16. so that the hash table entries are in the flexible array at the end
  17. of the control structure, no callbacks are used and the elements in the
  18. table are of the hash_entry_type type.
  19. Before including this file, define hash_entry_type type and
  20. htab_alloc and htab_free functions. After including it, define
  21. htab_hash and htab_eq inline functions. */
  22. /* This package implements basic hash table functionality. It is possible
  23. to search for an entry, create an entry and destroy an entry.
  24. Elements in the table are generic pointers.
  25. The size of the table is not fixed; if the occupancy of the table
  26. grows too high the hash table will be expanded.
  27. The abstract data implementation is based on generalized Algorithm D
  28. from Knuth's book "The art of computer programming". Hash table is
  29. expanded by creation of new hash table and transferring elements from
  30. the old table to the new table. */
  31. /* The type for a hash code. */
  32. typedef unsigned int hashval_t;
  33. static inline hashval_t htab_hash (hash_entry_type);
  34. static inline bool htab_eq (hash_entry_type, hash_entry_type);
  35. /* This macro defines reserved value for empty table entry. */
  36. #define HTAB_EMPTY_ENTRY ((hash_entry_type) 0)
  37. /* This macro defines reserved value for table entry which contained
  38. a deleted element. */
  39. #define HTAB_DELETED_ENTRY ((hash_entry_type) 1)
  40. /* Hash tables are of the following type. The structure
  41. (implementation) of this type is not needed for using the hash
  42. tables. All work with hash table should be executed only through
  43. functions mentioned below. The size of this structure is subject to
  44. change. */
  45. struct htab {
  46. /* Current size (in entries) of the hash table. */
  47. size_t size;
  48. /* Current number of elements including also deleted elements. */
  49. size_t n_elements;
  50. /* Current number of deleted elements in the table. */
  51. size_t n_deleted;
  52. /* Current size (in entries) of the hash table, as an index into the
  53. table of primes. */
  54. unsigned int size_prime_index;
  55. /* Table itself. */
  56. hash_entry_type entries[];
  57. };
  58. typedef struct htab *htab_t;
  59. /* An enum saying whether we insert into the hash table or not. */
  60. enum insert_option {NO_INSERT, INSERT};
  61. /* Table of primes and multiplicative inverses.
  62. Note that these are not minimally reduced inverses. Unlike when generating
  63. code to divide by a constant, we want to be able to use the same algorithm
  64. all the time. All of these inverses (are implied to) have bit 32 set.
  65. For the record, the function that computed the table is in
  66. libiberty/hashtab.c. */
  67. struct prime_ent
  68. {
  69. hashval_t prime;
  70. hashval_t inv;
  71. hashval_t inv_m2; /* inverse of prime-2 */
  72. hashval_t shift;
  73. };
  74. static struct prime_ent const prime_tab[] = {
  75. { 7, 0x24924925, 0x9999999b, 2 },
  76. { 13, 0x3b13b13c, 0x745d1747, 3 },
  77. { 31, 0x08421085, 0x1a7b9612, 4 },
  78. { 61, 0x0c9714fc, 0x15b1e5f8, 5 },
  79. { 127, 0x02040811, 0x0624dd30, 6 },
  80. { 251, 0x05197f7e, 0x073260a5, 7 },
  81. { 509, 0x01824366, 0x02864fc8, 8 },
  82. { 1021, 0x00c0906d, 0x014191f7, 9 },
  83. { 2039, 0x0121456f, 0x0161e69e, 10 },
  84. { 4093, 0x00300902, 0x00501908, 11 },
  85. { 8191, 0x00080041, 0x00180241, 12 },
  86. { 16381, 0x000c0091, 0x00140191, 13 },
  87. { 32749, 0x002605a5, 0x002a06e6, 14 },
  88. { 65521, 0x000f00e2, 0x00110122, 15 },
  89. { 131071, 0x00008001, 0x00018003, 16 },
  90. { 262139, 0x00014002, 0x0001c004, 17 },
  91. { 524287, 0x00002001, 0x00006001, 18 },
  92. { 1048573, 0x00003001, 0x00005001, 19 },
  93. { 2097143, 0x00004801, 0x00005801, 20 },
  94. { 4194301, 0x00000c01, 0x00001401, 21 },
  95. { 8388593, 0x00001e01, 0x00002201, 22 },
  96. { 16777213, 0x00000301, 0x00000501, 23 },
  97. { 33554393, 0x00001381, 0x00001481, 24 },
  98. { 67108859, 0x00000141, 0x000001c1, 25 },
  99. { 134217689, 0x000004e1, 0x00000521, 26 },
  100. { 268435399, 0x00000391, 0x000003b1, 27 },
  101. { 536870909, 0x00000019, 0x00000029, 28 },
  102. { 1073741789, 0x0000008d, 0x00000095, 29 },
  103. { 2147483647, 0x00000003, 0x00000007, 30 },
  104. /* Avoid "decimal constant so large it is unsigned" for 4294967291. */
  105. { 0xfffffffb, 0x00000006, 0x00000008, 31 }
  106. };
  107. /* The following function returns an index into the above table of the
  108. nearest prime number which is greater than N, and near a power of two. */
  109. static unsigned int
  110. higher_prime_index (unsigned long n)
  111. {
  112. unsigned int low = 0;
  113. unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]);
  114. while (low != high)
  115. {
  116. unsigned int mid = low + (high - low) / 2;
  117. if (n > prime_tab[mid].prime)
  118. low = mid + 1;
  119. else
  120. high = mid;
  121. }
  122. /* If we've run out of primes, abort. */
  123. if (n > prime_tab[low].prime)
  124. abort ();
  125. return low;
  126. }
  127. /* Return the current size of given hash table. */
  128. static inline size_t
  129. htab_size (htab_t htab)
  130. {
  131. return htab->size;
  132. }
  133. /* Return the current number of elements in given hash table. */
  134. static inline size_t
  135. htab_elements (htab_t htab)
  136. {
  137. return htab->n_elements - htab->n_deleted;
  138. }
  139. /* Return X % Y. */
  140. static inline hashval_t
  141. htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift)
  142. {
  143. /* The multiplicative inverses computed above are for 32-bit types, and
  144. requires that we be able to compute a highpart multiply. */
  145. if (sizeof (hashval_t) * __CHAR_BIT__ <= 32)
  146. {
  147. hashval_t t1, t2, t3, t4, q, r;
  148. t1 = ((unsigned long long)x * inv) >> 32;
  149. t2 = x - t1;
  150. t3 = t2 >> 1;
  151. t4 = t1 + t3;
  152. q = t4 >> shift;
  153. r = x - (q * y);
  154. return r;
  155. }
  156. /* Otherwise just use the native division routines. */
  157. return x % y;
  158. }
  159. /* Compute the primary hash for HASH given HTAB's current size. */
  160. static inline hashval_t
  161. htab_mod (hashval_t hash, htab_t htab)
  162. {
  163. const struct prime_ent *p = &prime_tab[htab->size_prime_index];
  164. return htab_mod_1 (hash, p->prime, p->inv, p->shift);
  165. }
  166. /* Compute the secondary hash for HASH given HTAB's current size. */
  167. static inline hashval_t
  168. htab_mod_m2 (hashval_t hash, htab_t htab)
  169. {
  170. const struct prime_ent *p = &prime_tab[htab->size_prime_index];
  171. return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift);
  172. }
  173. /* Create hash table of size SIZE. */
  174. static htab_t
  175. htab_create (size_t size)
  176. {
  177. htab_t result;
  178. unsigned int size_prime_index;
  179. size_prime_index = higher_prime_index (size);
  180. size = prime_tab[size_prime_index].prime;
  181. result = (htab_t) htab_alloc (sizeof (struct htab)
  182. + size * sizeof (hash_entry_type));
  183. result->size = size;
  184. result->n_elements = 0;
  185. result->n_deleted = 0;
  186. result->size_prime_index = size_prime_index;
  187. memset (result->entries, 0, size * sizeof (hash_entry_type));
  188. return result;
  189. }
  190. /* Similar to htab_find_slot, but without several unwanted side effects:
  191. - Does not call htab_eq when it finds an existing entry.
  192. - Does not change the count of elements in the hash table.
  193. This function also assumes there are no deleted entries in the table.
  194. HASH is the hash value for the element to be inserted. */
  195. static hash_entry_type *
  196. find_empty_slot_for_expand (htab_t htab, hashval_t hash)
  197. {
  198. hashval_t index = htab_mod (hash, htab);
  199. size_t size = htab_size (htab);
  200. hash_entry_type *slot = htab->entries + index;
  201. hashval_t hash2;
  202. if (*slot == HTAB_EMPTY_ENTRY)
  203. return slot;
  204. else if (*slot == HTAB_DELETED_ENTRY)
  205. abort ();
  206. hash2 = htab_mod_m2 (hash, htab);
  207. for (;;)
  208. {
  209. index += hash2;
  210. if (index >= size)
  211. index -= size;
  212. slot = htab->entries + index;
  213. if (*slot == HTAB_EMPTY_ENTRY)
  214. return slot;
  215. else if (*slot == HTAB_DELETED_ENTRY)
  216. abort ();
  217. }
  218. }
  219. /* The following function changes size of memory allocated for the
  220. entries and repeatedly inserts the table elements. The occupancy
  221. of the table after the call will be about 50%. Naturally the hash
  222. table must already exist. Remember also that the place of the
  223. table entries is changed. */
  224. static htab_t
  225. htab_expand (htab_t htab)
  226. {
  227. htab_t nhtab;
  228. hash_entry_type *olimit;
  229. hash_entry_type *p;
  230. size_t osize, elts;
  231. osize = htab->size;
  232. olimit = htab->entries + osize;
  233. elts = htab_elements (htab);
  234. /* Resize only when table after removal of unused elements is either
  235. too full or too empty. */
  236. if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
  237. nhtab = htab_create (elts * 2);
  238. else
  239. nhtab = htab_create (osize - 1);
  240. nhtab->n_elements = htab->n_elements - htab->n_deleted;
  241. p = htab->entries;
  242. do
  243. {
  244. hash_entry_type x = *p;
  245. if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
  246. *find_empty_slot_for_expand (nhtab, htab_hash (x)) = x;
  247. p++;
  248. }
  249. while (p < olimit);
  250. htab_free (htab);
  251. return nhtab;
  252. }
  253. /* This function searches for a hash table entry equal to the given
  254. element. It cannot be used to insert or delete an element. */
  255. static hash_entry_type
  256. htab_find (htab_t htab, const hash_entry_type element)
  257. {
  258. hashval_t index, hash2, hash = htab_hash (element);
  259. size_t size;
  260. hash_entry_type entry;
  261. size = htab_size (htab);
  262. index = htab_mod (hash, htab);
  263. entry = htab->entries[index];
  264. if (entry == HTAB_EMPTY_ENTRY
  265. || (entry != HTAB_DELETED_ENTRY && htab_eq (entry, element)))
  266. return entry;
  267. hash2 = htab_mod_m2 (hash, htab);
  268. for (;;)
  269. {
  270. index += hash2;
  271. if (index >= size)
  272. index -= size;
  273. entry = htab->entries[index];
  274. if (entry == HTAB_EMPTY_ENTRY
  275. || (entry != HTAB_DELETED_ENTRY && htab_eq (entry, element)))
  276. return entry;
  277. }
  278. }
  279. /* This function searches for a hash table slot containing an entry
  280. equal to the given element. To delete an entry, call this with
  281. insert=NO_INSERT, then call htab_clear_slot on the slot returned
  282. (possibly after doing some checks). To insert an entry, call this
  283. with insert=INSERT, then write the value you want into the returned
  284. slot. */
  285. static hash_entry_type *
  286. htab_find_slot (htab_t *htabp, const hash_entry_type element,
  287. enum insert_option insert)
  288. {
  289. hash_entry_type *first_deleted_slot;
  290. hashval_t index, hash2, hash = htab_hash (element);
  291. size_t size;
  292. hash_entry_type entry;
  293. htab_t htab = *htabp;
  294. size = htab_size (htab);
  295. if (insert == INSERT && size * 3 <= htab->n_elements * 4)
  296. {
  297. htab = *htabp = htab_expand (htab);
  298. size = htab_size (htab);
  299. }
  300. index = htab_mod (hash, htab);
  301. first_deleted_slot = NULL;
  302. entry = htab->entries[index];
  303. if (entry == HTAB_EMPTY_ENTRY)
  304. goto empty_entry;
  305. else if (entry == HTAB_DELETED_ENTRY)
  306. first_deleted_slot = &htab->entries[index];
  307. else if (htab_eq (entry, element))
  308. return &htab->entries[index];
  309. hash2 = htab_mod_m2 (hash, htab);
  310. for (;;)
  311. {
  312. index += hash2;
  313. if (index >= size)
  314. index -= size;
  315. entry = htab->entries[index];
  316. if (entry == HTAB_EMPTY_ENTRY)
  317. goto empty_entry;
  318. else if (entry == HTAB_DELETED_ENTRY)
  319. {
  320. if (!first_deleted_slot)
  321. first_deleted_slot = &htab->entries[index];
  322. }
  323. else if (htab_eq (entry, element))
  324. return &htab->entries[index];
  325. }
  326. empty_entry:
  327. if (insert == NO_INSERT)
  328. return NULL;
  329. if (first_deleted_slot)
  330. {
  331. htab->n_deleted--;
  332. *first_deleted_slot = HTAB_EMPTY_ENTRY;
  333. return first_deleted_slot;
  334. }
  335. htab->n_elements++;
  336. return &htab->entries[index];
  337. }
  338. /* This function clears a specified slot in a hash table. It is
  339. useful when you've already done the lookup and don't want to do it
  340. again. */
  341. static inline void
  342. htab_clear_slot (htab_t htab, hash_entry_type *slot)
  343. {
  344. if (slot < htab->entries || slot >= htab->entries + htab_size (htab)
  345. || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY)
  346. abort ();
  347. *slot = HTAB_DELETED_ENTRY;
  348. htab->n_deleted++;
  349. }
  350. /* Returns a hash code for pointer P. Simplified version of evahash */
  351. static inline hashval_t
  352. hash_pointer (const void *p)
  353. {
  354. uintptr_t v = (uintptr_t) p;
  355. if (sizeof (v) > sizeof (hashval_t))
  356. v ^= v >> (sizeof (uintptr_t) / 2 * __CHAR_BIT__);
  357. return v;
  358. }