PageRenderTime 165ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/st.c

https://github.com/vuxuandung/ruby
C | 1578 lines | 1300 code | 167 blank | 111 comment | 281 complexity | 6edf784fe1f1d80cc4c012327d1a0d3b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD
  1. /* This is a public domain general purpose hash table package written by Peter Moore @ UCB. */
  2. /* static char sccsid[] = "@(#) st.c 5.1 89/12/14 Crucible"; */
  3. #ifdef NOT_RUBY
  4. #include "regint.h"
  5. #include "st.h"
  6. #else
  7. #include "ruby/ruby.h"
  8. #endif
  9. #include <stdio.h>
  10. #ifdef HAVE_STDLIB_H
  11. #include <stdlib.h>
  12. #endif
  13. #include <string.h>
  14. typedef struct st_table_entry st_table_entry;
  15. struct st_table_entry {
  16. st_index_t hash;
  17. st_data_t key;
  18. st_data_t record;
  19. st_table_entry *next;
  20. st_table_entry *fore, *back;
  21. };
  22. typedef struct st_packed_entry {
  23. st_index_t hash;
  24. st_data_t key, val;
  25. } st_packed_entry;
  26. #define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[(expr) ? 1 : -1];
  27. #define ST_DEFAULT_MAX_DENSITY 5
  28. #define ST_DEFAULT_INIT_TABLE_SIZE 11
  29. #define ST_DEFAULT_SECOND_TABLE_SIZE 19
  30. #define ST_DEFAULT_PACKED_TABLE_SIZE 18
  31. #define PACKED_UNIT (int)(sizeof(st_packed_entry) / sizeof(st_table_entry*))
  32. #define MAX_PACKED_HASH (int)(ST_DEFAULT_PACKED_TABLE_SIZE * sizeof(st_table_entry*) / sizeof(st_packed_entry))
  33. STATIC_ASSERT(st_packed_entry, sizeof(st_packed_entry) == sizeof(st_table_entry*[PACKED_UNIT]))
  34. STATIC_ASSERT(st_packed_bins, sizeof(st_packed_entry[MAX_PACKED_HASH]) <= sizeof(st_table_entry*[ST_DEFAULT_PACKED_TABLE_SIZE]))
  35. /*
  36. * DEFAULT_MAX_DENSITY is the default for the largest we allow the
  37. * average number of items per bin before increasing the number of
  38. * bins
  39. *
  40. * DEFAULT_INIT_TABLE_SIZE is the default for the number of bins
  41. * allocated initially
  42. *
  43. */
  44. #define type_numhash st_hashtype_num
  45. const struct st_hash_type st_hashtype_num = {
  46. st_numcmp,
  47. st_numhash,
  48. };
  49. /* extern int strcmp(const char *, const char *); */
  50. static st_index_t strhash(st_data_t);
  51. static const struct st_hash_type type_strhash = {
  52. strcmp,
  53. strhash,
  54. };
  55. static st_index_t strcasehash(st_data_t);
  56. static const struct st_hash_type type_strcasehash = {
  57. st_strcasecmp,
  58. strcasehash,
  59. };
  60. static void rehash(st_table *);
  61. #ifdef RUBY
  62. #define malloc xmalloc
  63. #define calloc xcalloc
  64. #define realloc xrealloc
  65. #define free(x) xfree(x)
  66. #endif
  67. #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
  68. #define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0)
  69. #define do_hash(key,table) (st_index_t)(*(table)->type->hash)((key))
  70. #define do_hash_bin(key,table) (do_hash((key), (table))%(table)->num_bins)
  71. /* preparation for possible allocation improvements */
  72. #define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
  73. #define st_free_entry(entry) free(entry)
  74. #define st_alloc_table() (st_table *)malloc(sizeof(st_table))
  75. #define st_dealloc_table(table) free(table)
  76. #define st_alloc_bins(size) (st_table_entry **)calloc(size, sizeof(st_table_entry *))
  77. #define st_free_bins(bins, size) free(bins)
  78. static inline st_table_entry**
  79. st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
  80. {
  81. bins = (st_table_entry **)realloc(bins, newsize * sizeof(st_table_entry *));
  82. MEMZERO(bins, st_table_entry*, newsize);
  83. return bins;
  84. }
  85. /* Shortage */
  86. #define bins as.big.bins
  87. #define head as.big.head
  88. #define tail as.big.tail
  89. #define real_entries as.packed.real_entries
  90. /* preparation for possible packing improvements */
  91. #define PACKED_BINS(table) ((table)->as.packed.entries)
  92. #define PACKED_ENT(table, i) PACKED_BINS(table)[i]
  93. #define PKEY(table, i) PACKED_ENT((table), (i)).key
  94. #define PVAL(table, i) PACKED_ENT((table), (i)).val
  95. #define PHASH(table, i) PACKED_ENT((table), (i)).hash
  96. #define PKEY_SET(table, i, v) (PKEY((table), (i)) = (v))
  97. #define PVAL_SET(table, i, v) (PVAL((table), (i)) = (v))
  98. #define PHASH_SET(table, i, v) (PHASH((table), (i)) = (v))
  99. /* this function depends much on packed layout, so that it placed here */
  100. static inline void
  101. remove_packed_entry(st_table *table, st_index_t i)
  102. {
  103. table->real_entries--;
  104. table->num_entries--;
  105. if (i < table->real_entries) {
  106. MEMMOVE(&PACKED_ENT(table, i), &PACKED_ENT(table, i+1),
  107. st_packed_entry, table->real_entries - i);
  108. }
  109. }
  110. static inline void
  111. remove_safe_packed_entry(st_table *table, st_index_t i, st_data_t never)
  112. {
  113. table->num_entries--;
  114. PKEY_SET(table, i, never);
  115. PVAL_SET(table, i, never);
  116. PHASH_SET(table, i, 0);
  117. }
  118. /*
  119. * MINSIZE is the minimum size of a dictionary.
  120. */
  121. #define MINSIZE 8
  122. /*
  123. Table of prime numbers 2^n+a, 2<=n<=30.
  124. */
  125. static const unsigned int primes[] = {
  126. ST_DEFAULT_INIT_TABLE_SIZE,
  127. ST_DEFAULT_SECOND_TABLE_SIZE,
  128. 32 + 5,
  129. 64 + 3,
  130. 128 + 3,
  131. 256 + 27,
  132. 512 + 9,
  133. 1024 + 9,
  134. 2048 + 5,
  135. 4096 + 3,
  136. 8192 + 27,
  137. 16384 + 43,
  138. 32768 + 3,
  139. 65536 + 45,
  140. 131072 + 29,
  141. 262144 + 3,
  142. 524288 + 21,
  143. 1048576 + 7,
  144. 2097152 + 17,
  145. 4194304 + 15,
  146. 8388608 + 9,
  147. 16777216 + 43,
  148. 33554432 + 35,
  149. 67108864 + 15,
  150. 134217728 + 29,
  151. 268435456 + 3,
  152. 536870912 + 11,
  153. 1073741824 + 85,
  154. 0
  155. };
  156. static st_index_t
  157. new_size(st_index_t size)
  158. {
  159. int i;
  160. #if 0
  161. for (i=3; i<31; i++) {
  162. if ((1<<i) > size) return 1<<i;
  163. }
  164. return -1;
  165. #else
  166. st_index_t newsize;
  167. for (i = 0, newsize = MINSIZE; i < numberof(primes); i++, newsize <<= 1) {
  168. if (newsize > size) return primes[i];
  169. }
  170. /* Ran out of polynomials */
  171. #ifndef NOT_RUBY
  172. rb_raise(rb_eRuntimeError, "st_table too big");
  173. #endif
  174. return -1; /* should raise exception */
  175. #endif
  176. }
  177. #ifdef HASH_LOG
  178. #ifdef HAVE_UNISTD_H
  179. #include <unistd.h>
  180. #endif
  181. static struct {
  182. int all, total, num, str, strcase;
  183. } collision;
  184. static int init_st = 0;
  185. static void
  186. stat_col(void)
  187. {
  188. char fname[10+sizeof(long)*3];
  189. FILE *f = fopen((snprintf(fname, sizeof(fname), "/tmp/col%ld", (long)getpid()), fname), "w");
  190. fprintf(f, "collision: %d / %d (%6.2f)\n", collision.all, collision.total,
  191. ((double)collision.all / (collision.total)) * 100);
  192. fprintf(f, "num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
  193. fclose(f);
  194. }
  195. #endif
  196. st_table*
  197. st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
  198. {
  199. st_table *tbl;
  200. #ifdef HASH_LOG
  201. # if HASH_LOG+0 < 0
  202. {
  203. const char *e = getenv("ST_HASH_LOG");
  204. if (!e || !*e) init_st = 1;
  205. }
  206. # endif
  207. if (init_st == 0) {
  208. init_st = 1;
  209. atexit(stat_col);
  210. }
  211. #endif
  212. tbl = st_alloc_table();
  213. tbl->type = type;
  214. tbl->num_entries = 0;
  215. tbl->entries_packed = size <= MAX_PACKED_HASH;
  216. if (tbl->entries_packed) {
  217. size = ST_DEFAULT_PACKED_TABLE_SIZE;
  218. }
  219. else {
  220. size = new_size(size); /* round up to prime number */
  221. }
  222. tbl->num_bins = size;
  223. tbl->bins = st_alloc_bins(size);
  224. tbl->head = 0;
  225. tbl->tail = 0;
  226. return tbl;
  227. }
  228. st_table*
  229. st_init_table(const struct st_hash_type *type)
  230. {
  231. return st_init_table_with_size(type, 0);
  232. }
  233. st_table*
  234. st_init_numtable(void)
  235. {
  236. return st_init_table(&type_numhash);
  237. }
  238. st_table*
  239. st_init_numtable_with_size(st_index_t size)
  240. {
  241. return st_init_table_with_size(&type_numhash, size);
  242. }
  243. st_table*
  244. st_init_strtable(void)
  245. {
  246. return st_init_table(&type_strhash);
  247. }
  248. st_table*
  249. st_init_strtable_with_size(st_index_t size)
  250. {
  251. return st_init_table_with_size(&type_strhash, size);
  252. }
  253. st_table*
  254. st_init_strcasetable(void)
  255. {
  256. return st_init_table(&type_strcasehash);
  257. }
  258. st_table*
  259. st_init_strcasetable_with_size(st_index_t size)
  260. {
  261. return st_init_table_with_size(&type_strcasehash, size);
  262. }
  263. void
  264. st_clear(st_table *table)
  265. {
  266. register st_table_entry *ptr, *next;
  267. st_index_t i;
  268. if (table->entries_packed) {
  269. table->num_entries = 0;
  270. table->real_entries = 0;
  271. return;
  272. }
  273. for (i = 0; i < table->num_bins; i++) {
  274. ptr = table->bins[i];
  275. table->bins[i] = 0;
  276. while (ptr != 0) {
  277. next = ptr->next;
  278. st_free_entry(ptr);
  279. ptr = next;
  280. }
  281. }
  282. table->num_entries = 0;
  283. table->head = 0;
  284. table->tail = 0;
  285. }
  286. void
  287. st_free_table(st_table *table)
  288. {
  289. st_clear(table);
  290. st_free_bins(table->bins, table->num_bins);
  291. st_dealloc_table(table);
  292. }
  293. size_t
  294. st_memsize(const st_table *table)
  295. {
  296. if (table->entries_packed) {
  297. return table->num_bins * sizeof (void *) + sizeof(st_table);
  298. }
  299. else {
  300. return table->num_entries * sizeof(struct st_table_entry) + table->num_bins * sizeof (void *) + sizeof(st_table);
  301. }
  302. }
  303. #define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
  304. ((ptr) != 0 && ((ptr)->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
  305. #ifdef HASH_LOG
  306. static void
  307. count_collision(const struct st_hash_type *type)
  308. {
  309. collision.all++;
  310. if (type == &type_numhash) {
  311. collision.num++;
  312. }
  313. else if (type == &type_strhash) {
  314. collision.strcase++;
  315. }
  316. else if (type == &type_strcasehash) {
  317. collision.str++;
  318. }
  319. }
  320. #define COLLISION (collision_check ? count_collision(table->type) : (void)0)
  321. #define FOUND_ENTRY (collision_check ? collision.total++ : (void)0)
  322. #else
  323. #define COLLISION
  324. #define FOUND_ENTRY
  325. #endif
  326. #define FIND_ENTRY(table, ptr, hash_val, bin_pos) \
  327. ((ptr) = find_entry((table), key, (hash_val), ((bin_pos) = (hash_val)%(table)->num_bins)))
  328. static st_table_entry *
  329. find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t bin_pos)
  330. {
  331. register st_table_entry *ptr = table->bins[bin_pos];
  332. FOUND_ENTRY;
  333. if (PTR_NOT_EQUAL(table, ptr, hash_val, key)) {
  334. COLLISION;
  335. while (PTR_NOT_EQUAL(table, ptr->next, hash_val, key)) {
  336. ptr = ptr->next;
  337. }
  338. ptr = ptr->next;
  339. }
  340. return ptr;
  341. }
  342. static inline st_index_t
  343. find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
  344. {
  345. st_index_t i = 0;
  346. while (i < table->real_entries &&
  347. (PHASH(table, i) != hash_val || !EQUAL(table, key, PKEY(table, i)))) {
  348. i++;
  349. }
  350. return i;
  351. }
  352. #define collision_check 0
  353. int
  354. st_lookup(st_table *table, register st_data_t key, st_data_t *value)
  355. {
  356. st_index_t hash_val;
  357. register st_table_entry *ptr;
  358. hash_val = do_hash(key, table);
  359. if (table->entries_packed) {
  360. st_index_t i = find_packed_index(table, hash_val, key);
  361. if (i < table->real_entries) {
  362. if (value != 0) *value = PVAL(table, i);
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. ptr = find_entry(table, key, hash_val, hash_val % table->num_bins);
  368. if (ptr == 0) {
  369. return 0;
  370. }
  371. else {
  372. if (value != 0) *value = ptr->record;
  373. return 1;
  374. }
  375. }
  376. int
  377. st_get_key(st_table *table, register st_data_t key, st_data_t *result)
  378. {
  379. st_index_t hash_val;
  380. register st_table_entry *ptr;
  381. hash_val = do_hash(key, table);
  382. if (table->entries_packed) {
  383. st_index_t i = find_packed_index(table, hash_val, key);
  384. if (i < table->real_entries) {
  385. if (result != 0) *result = PKEY(table, i);
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. ptr = find_entry(table, key, hash_val, hash_val % table->num_bins);
  391. if (ptr == 0) {
  392. return 0;
  393. }
  394. else {
  395. if (result != 0) *result = ptr->key;
  396. return 1;
  397. }
  398. }
  399. #undef collision_check
  400. #define collision_check 1
  401. static inline st_table_entry *
  402. new_entry(st_table * table, st_data_t key, st_data_t value,
  403. st_index_t hash_val, register st_index_t bin_pos)
  404. {
  405. register st_table_entry *entry = st_alloc_entry();
  406. entry->next = table->bins[bin_pos];
  407. table->bins[bin_pos] = entry;
  408. entry->hash = hash_val;
  409. entry->key = key;
  410. entry->record = value;
  411. return entry;
  412. }
  413. static inline void
  414. add_direct(st_table *table, st_data_t key, st_data_t value,
  415. st_index_t hash_val, register st_index_t bin_pos)
  416. {
  417. register st_table_entry *entry;
  418. if (table->num_entries > ST_DEFAULT_MAX_DENSITY * table->num_bins) {
  419. rehash(table);
  420. bin_pos = hash_val % table->num_bins;
  421. }
  422. entry = new_entry(table, key, value, hash_val, bin_pos);
  423. if (table->head != 0) {
  424. entry->fore = 0;
  425. (entry->back = table->tail)->fore = entry;
  426. table->tail = entry;
  427. }
  428. else {
  429. table->head = table->tail = entry;
  430. entry->fore = entry->back = 0;
  431. }
  432. table->num_entries++;
  433. }
  434. static void
  435. unpack_entries(register st_table *table)
  436. {
  437. st_index_t i;
  438. st_packed_entry packed_bins[MAX_PACKED_HASH];
  439. register st_table_entry *entry, *preventry = 0, **chain;
  440. st_table tmp_table = *table;
  441. MEMCPY(packed_bins, PACKED_BINS(table), st_packed_entry, MAX_PACKED_HASH);
  442. table->as.packed.entries = packed_bins;
  443. tmp_table.entries_packed = 0;
  444. #if ST_DEFAULT_INIT_TABLE_SIZE == ST_DEFAULT_PACKED_TABLE_SIZE
  445. MEMZERO(tmp_table.bins, st_table_entry*, tmp_table.num_bins);
  446. #else
  447. tmp_table.bins = st_realloc_bins(tmp_table.bins, ST_DEFAULT_INIT_TABLE_SIZE, tmp_table.num_bins);
  448. tmp_table.num_bins = ST_DEFAULT_INIT_TABLE_SIZE;
  449. #endif
  450. i = 0;
  451. chain = &tmp_table.head;
  452. do {
  453. st_data_t key = packed_bins[i].key;
  454. st_data_t val = packed_bins[i].val;
  455. st_index_t hash = packed_bins[i].hash;
  456. entry = new_entry(&tmp_table, key, val, hash,
  457. hash % ST_DEFAULT_INIT_TABLE_SIZE);
  458. *chain = entry;
  459. entry->back = preventry;
  460. preventry = entry;
  461. chain = &entry->fore;
  462. } while (++i < MAX_PACKED_HASH);
  463. *chain = NULL;
  464. tmp_table.tail = entry;
  465. *table = tmp_table;
  466. }
  467. static void
  468. add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t hash_val)
  469. {
  470. if (table->real_entries < MAX_PACKED_HASH) {
  471. st_index_t i = table->real_entries++;
  472. PKEY_SET(table, i, key);
  473. PVAL_SET(table, i, value);
  474. PHASH_SET(table, i, hash_val);
  475. table->num_entries++;
  476. }
  477. else {
  478. unpack_entries(table);
  479. add_direct(table, key, value, hash_val, hash_val % table->num_bins);
  480. }
  481. }
  482. int
  483. st_insert(register st_table *table, register st_data_t key, st_data_t value)
  484. {
  485. st_index_t hash_val;
  486. register st_index_t bin_pos;
  487. register st_table_entry *ptr;
  488. hash_val = do_hash(key, table);
  489. if (table->entries_packed) {
  490. st_index_t i = find_packed_index(table, hash_val, key);
  491. if (i < table->real_entries) {
  492. PVAL_SET(table, i, value);
  493. return 1;
  494. }
  495. add_packed_direct(table, key, value, hash_val);
  496. return 0;
  497. }
  498. FIND_ENTRY(table, ptr, hash_val, bin_pos);
  499. if (ptr == 0) {
  500. add_direct(table, key, value, hash_val, bin_pos);
  501. return 0;
  502. }
  503. else {
  504. ptr->record = value;
  505. return 1;
  506. }
  507. }
  508. int
  509. st_insert2(register st_table *table, register st_data_t key, st_data_t value,
  510. st_data_t (*func)(st_data_t))
  511. {
  512. st_index_t hash_val;
  513. register st_index_t bin_pos;
  514. register st_table_entry *ptr;
  515. hash_val = do_hash(key, table);
  516. if (table->entries_packed) {
  517. st_index_t i = find_packed_index(table, hash_val, key);
  518. if (i < table->real_entries) {
  519. PVAL_SET(table, i, value);
  520. return 1;
  521. }
  522. key = (*func)(key);
  523. add_packed_direct(table, key, value, hash_val);
  524. return 0;
  525. }
  526. FIND_ENTRY(table, ptr, hash_val, bin_pos);
  527. if (ptr == 0) {
  528. key = (*func)(key);
  529. add_direct(table, key, value, hash_val, bin_pos);
  530. return 0;
  531. }
  532. else {
  533. ptr->record = value;
  534. return 1;
  535. }
  536. }
  537. void
  538. st_add_direct(st_table *table, st_data_t key, st_data_t value)
  539. {
  540. st_index_t hash_val;
  541. hash_val = do_hash(key, table);
  542. if (table->entries_packed) {
  543. add_packed_direct(table, key, value, hash_val);
  544. return;
  545. }
  546. add_direct(table, key, value, hash_val, hash_val % table->num_bins);
  547. }
  548. static void
  549. rehash(register st_table *table)
  550. {
  551. register st_table_entry *ptr, **new_bins;
  552. st_index_t new_num_bins, hash_val;
  553. new_num_bins = new_size(table->num_bins+1);
  554. new_bins = st_realloc_bins(table->bins, new_num_bins, table->num_bins);
  555. table->num_bins = new_num_bins;
  556. table->bins = new_bins;
  557. if ((ptr = table->head) != 0) {
  558. do {
  559. hash_val = ptr->hash % new_num_bins;
  560. ptr->next = new_bins[hash_val];
  561. new_bins[hash_val] = ptr;
  562. } while ((ptr = ptr->fore) != 0);
  563. }
  564. }
  565. st_table*
  566. st_copy(st_table *old_table)
  567. {
  568. st_table *new_table;
  569. st_table_entry *ptr, *entry, *prev, **tailp;
  570. st_index_t num_bins = old_table->num_bins;
  571. st_index_t hash_val;
  572. new_table = st_alloc_table();
  573. if (new_table == 0) {
  574. return 0;
  575. }
  576. *new_table = *old_table;
  577. new_table->bins = st_alloc_bins(num_bins);
  578. if (new_table->bins == 0) {
  579. st_dealloc_table(new_table);
  580. return 0;
  581. }
  582. if (old_table->entries_packed) {
  583. MEMCPY(new_table->bins, old_table->bins, st_table_entry*, old_table->num_bins);
  584. return new_table;
  585. }
  586. if ((ptr = old_table->head) != 0) {
  587. prev = 0;
  588. tailp = &new_table->head;
  589. do {
  590. entry = st_alloc_entry();
  591. if (entry == 0) {
  592. st_free_table(new_table);
  593. return 0;
  594. }
  595. *entry = *ptr;
  596. hash_val = entry->hash % num_bins;
  597. entry->next = new_table->bins[hash_val];
  598. new_table->bins[hash_val] = entry;
  599. entry->back = prev;
  600. *tailp = prev = entry;
  601. tailp = &entry->fore;
  602. } while ((ptr = ptr->fore) != 0);
  603. new_table->tail = prev;
  604. }
  605. return new_table;
  606. }
  607. static inline void
  608. remove_entry(st_table *table, st_table_entry *ptr)
  609. {
  610. if (ptr->fore == 0 && ptr->back == 0) {
  611. table->head = 0;
  612. table->tail = 0;
  613. }
  614. else {
  615. st_table_entry *fore = ptr->fore, *back = ptr->back;
  616. if (fore) fore->back = back;
  617. if (back) back->fore = fore;
  618. if (ptr == table->head) table->head = fore;
  619. if (ptr == table->tail) table->tail = back;
  620. }
  621. table->num_entries--;
  622. }
  623. int
  624. st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
  625. {
  626. st_index_t hash_val;
  627. st_table_entry **prev;
  628. register st_table_entry *ptr;
  629. hash_val = do_hash(*key, table);
  630. if (table->entries_packed) {
  631. st_index_t i = find_packed_index(table, hash_val, *key);
  632. if (i < table->real_entries) {
  633. if (value != 0) *value = PVAL(table, i);
  634. *key = PKEY(table, i);
  635. remove_packed_entry(table, i);
  636. return 1;
  637. }
  638. if (value != 0) *value = 0;
  639. return 0;
  640. }
  641. prev = &table->bins[hash_val % table->num_bins];
  642. for (;(ptr = *prev) != 0; prev = &ptr->next) {
  643. if (EQUAL(table, *key, ptr->key)) {
  644. *prev = ptr->next;
  645. remove_entry(table, ptr);
  646. if (value != 0) *value = ptr->record;
  647. *key = ptr->key;
  648. st_free_entry(ptr);
  649. return 1;
  650. }
  651. }
  652. if (value != 0) *value = 0;
  653. return 0;
  654. }
  655. int
  656. st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never)
  657. {
  658. st_index_t hash_val;
  659. register st_table_entry *ptr;
  660. hash_val = do_hash(*key, table);
  661. if (table->entries_packed) {
  662. st_index_t i = find_packed_index(table, hash_val, *key);
  663. if (i < table->real_entries) {
  664. if (value != 0) *value = PVAL(table, i);
  665. *key = PKEY(table, i);
  666. remove_safe_packed_entry(table, i, never);
  667. return 1;
  668. }
  669. if (value != 0) *value = 0;
  670. return 0;
  671. }
  672. ptr = table->bins[hash_val % table->num_bins];
  673. for (; ptr != 0; ptr = ptr->next) {
  674. if ((ptr->key != never) && EQUAL(table, ptr->key, *key)) {
  675. remove_entry(table, ptr);
  676. *key = ptr->key;
  677. if (value != 0) *value = ptr->record;
  678. ptr->key = ptr->record = never;
  679. return 1;
  680. }
  681. }
  682. if (value != 0) *value = 0;
  683. return 0;
  684. }
  685. void
  686. st_cleanup_safe(st_table *table, st_data_t never)
  687. {
  688. st_table_entry *ptr, **last, *tmp;
  689. st_index_t i;
  690. if (table->entries_packed) {
  691. st_index_t i = 0, j = 0;
  692. while (PKEY(table, i) != never) {
  693. if (i++ == table->real_entries) return;
  694. }
  695. for (j = i; ++i < table->real_entries;) {
  696. if (PKEY(table, i) == never) continue;
  697. PACKED_ENT(table, j) = PACKED_ENT(table, i);
  698. j++;
  699. }
  700. table->real_entries = j;
  701. /* table->num_entries really should be equal j at this moment, but let set it anyway */
  702. table->num_entries = j;
  703. return;
  704. }
  705. for (i = 0; i < table->num_bins; i++) {
  706. ptr = *(last = &table->bins[i]);
  707. while (ptr != 0) {
  708. if (ptr->key == never) {
  709. tmp = ptr;
  710. *last = ptr = ptr->next;
  711. st_free_entry(tmp);
  712. }
  713. else {
  714. ptr = *(last = &ptr->next);
  715. }
  716. }
  717. }
  718. }
  719. int
  720. st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
  721. {
  722. st_index_t hash_val, bin_pos;
  723. register st_table_entry *ptr, **last, *tmp;
  724. st_data_t value = 0;
  725. int retval, existing = 0;
  726. hash_val = do_hash(key, table);
  727. if (table->entries_packed) {
  728. st_index_t i = find_packed_index(table, hash_val, key);
  729. if (i < table->real_entries) {
  730. key = PKEY(table, i);
  731. value = PVAL(table, i);
  732. existing = 1;
  733. }
  734. {
  735. retval = (*func)(&key, &value, arg, existing);
  736. if (!table->entries_packed) {
  737. FIND_ENTRY(table, ptr, hash_val, bin_pos);
  738. goto unpacked;
  739. }
  740. switch (retval) {
  741. case ST_CONTINUE:
  742. if (!existing) {
  743. add_packed_direct(table, key, value, hash_val);
  744. break;
  745. }
  746. PVAL_SET(table, i, value);
  747. break;
  748. case ST_DELETE:
  749. if (!existing) break;
  750. remove_packed_entry(table, i);
  751. }
  752. }
  753. return existing;
  754. }
  755. FIND_ENTRY(table, ptr, hash_val, bin_pos);
  756. if (ptr != 0) {
  757. key = ptr->key;
  758. value = ptr->record;
  759. existing = 1;
  760. }
  761. {
  762. retval = (*func)(&key, &value, arg, existing);
  763. unpacked:
  764. switch (retval) {
  765. case ST_CONTINUE:
  766. if (!existing) {
  767. add_direct(table, key, value, hash_val, hash_val % table->num_bins);
  768. break;
  769. }
  770. ptr->record = value;
  771. break;
  772. case ST_DELETE:
  773. if (!existing) break;
  774. last = &table->bins[bin_pos];
  775. for (; (tmp = *last) != 0; last = &tmp->next) {
  776. if (ptr == tmp) {
  777. tmp = ptr->fore;
  778. *last = ptr->next;
  779. remove_entry(table, ptr);
  780. st_free_entry(ptr);
  781. break;
  782. }
  783. }
  784. break;
  785. }
  786. return existing;
  787. }
  788. }
  789. int
  790. st_foreach_check(st_table *table, int (*func)(ANYARGS), st_data_t arg, st_data_t never)
  791. {
  792. st_table_entry *ptr, **last, *tmp;
  793. enum st_retval retval;
  794. st_index_t i;
  795. if (table->entries_packed) {
  796. for (i = 0; i < table->real_entries; i++) {
  797. st_data_t key, val;
  798. st_index_t hash;
  799. key = PKEY(table, i);
  800. val = PVAL(table, i);
  801. hash = PHASH(table, i);
  802. if (key == never) continue;
  803. retval = (*func)(key, val, arg);
  804. if (!table->entries_packed) {
  805. FIND_ENTRY(table, ptr, hash, i);
  806. if (retval == ST_CHECK) {
  807. if (!ptr) goto deleted;
  808. goto unpacked_continue;
  809. }
  810. goto unpacked;
  811. }
  812. switch (retval) {
  813. case ST_CHECK: /* check if hash is modified during iteration */
  814. if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
  815. break;
  816. }
  817. i = find_packed_index(table, hash, key);
  818. if (i == table->real_entries) {
  819. goto deleted;
  820. }
  821. /* fall through */
  822. case ST_CONTINUE:
  823. break;
  824. case ST_STOP:
  825. return 0;
  826. case ST_DELETE:
  827. remove_safe_packed_entry(table, i, never);
  828. break;
  829. }
  830. }
  831. return 0;
  832. }
  833. else {
  834. ptr = table->head;
  835. }
  836. if (ptr != 0) {
  837. do {
  838. if (ptr->key == never)
  839. goto unpacked_continue;
  840. i = ptr->hash % table->num_bins;
  841. retval = (*func)(ptr->key, ptr->record, arg);
  842. unpacked:
  843. switch (retval) {
  844. case ST_CHECK: /* check if hash is modified during iteration */
  845. for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
  846. if (!tmp) {
  847. deleted:
  848. /* call func with error notice */
  849. retval = (*func)(0, 0, arg, 1);
  850. return 1;
  851. }
  852. }
  853. /* fall through */
  854. case ST_CONTINUE:
  855. unpacked_continue:
  856. ptr = ptr->fore;
  857. break;
  858. case ST_STOP:
  859. return 0;
  860. case ST_DELETE:
  861. last = &table->bins[ptr->hash % table->num_bins];
  862. for (; (tmp = *last) != 0; last = &tmp->next) {
  863. if (ptr == tmp) {
  864. tmp = ptr->fore;
  865. remove_entry(table, ptr);
  866. ptr->key = ptr->record = never;
  867. ptr->hash = 0;
  868. ptr = tmp;
  869. break;
  870. }
  871. }
  872. }
  873. } while (ptr && table->head);
  874. }
  875. return 0;
  876. }
  877. int
  878. st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
  879. {
  880. st_table_entry *ptr, **last, *tmp;
  881. enum st_retval retval;
  882. st_index_t i;
  883. if (table->entries_packed) {
  884. for (i = 0; i < table->real_entries; i++) {
  885. st_data_t key, val, hash;
  886. key = PKEY(table, i);
  887. val = PVAL(table, i);
  888. hash = PHASH(table, i);
  889. retval = (*func)(key, val, arg);
  890. if (!table->entries_packed) {
  891. FIND_ENTRY(table, ptr, hash, i);
  892. if (!ptr) return 0;
  893. goto unpacked;
  894. }
  895. switch (retval) {
  896. case ST_CONTINUE:
  897. break;
  898. case ST_CHECK:
  899. case ST_STOP:
  900. return 0;
  901. case ST_DELETE:
  902. remove_packed_entry(table, i);
  903. i--;
  904. break;
  905. }
  906. }
  907. return 0;
  908. }
  909. else {
  910. ptr = table->head;
  911. }
  912. if (ptr != 0) {
  913. do {
  914. i = ptr->hash % table->num_bins;
  915. retval = (*func)(ptr->key, ptr->record, arg);
  916. unpacked:
  917. switch (retval) {
  918. case ST_CONTINUE:
  919. ptr = ptr->fore;
  920. break;
  921. case ST_CHECK:
  922. case ST_STOP:
  923. return 0;
  924. case ST_DELETE:
  925. last = &table->bins[ptr->hash % table->num_bins];
  926. for (; (tmp = *last) != 0; last = &tmp->next) {
  927. if (ptr == tmp) {
  928. tmp = ptr->fore;
  929. *last = ptr->next;
  930. remove_entry(table, ptr);
  931. st_free_entry(ptr);
  932. ptr = tmp;
  933. break;
  934. }
  935. }
  936. }
  937. } while (ptr && table->head);
  938. }
  939. return 0;
  940. }
  941. #if 0 /* unused right now */
  942. int
  943. st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
  944. {
  945. st_table_entry *ptr, **last, *tmp;
  946. enum st_retval retval;
  947. int i;
  948. if (table->entries_packed) {
  949. for (i = table->num_entries-1; 0 <= i; i--) {
  950. int j;
  951. st_data_t key, val;
  952. key = PKEY(table, i);
  953. val = PVAL(table, i);
  954. retval = (*func)(key, val, arg);
  955. switch (retval) {
  956. case ST_CHECK: /* check if hash is modified during iteration */
  957. for (j = 0; j < table->num_entries; j++) {
  958. if (PKEY(table, j) == key)
  959. break;
  960. }
  961. if (j == table->num_entries) {
  962. /* call func with error notice */
  963. retval = (*func)(0, 0, arg, 1);
  964. return 1;
  965. }
  966. /* fall through */
  967. case ST_CONTINUE:
  968. break;
  969. case ST_STOP:
  970. return 0;
  971. case ST_DELETE:
  972. remove_packed_entry(table, i);
  973. break;
  974. }
  975. }
  976. return 0;
  977. }
  978. if ((ptr = table->head) != 0) {
  979. ptr = ptr->back;
  980. do {
  981. retval = (*func)(ptr->key, ptr->record, arg, 0);
  982. switch (retval) {
  983. case ST_CHECK: /* check if hash is modified during iteration */
  984. i = ptr->hash % table->num_bins;
  985. for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
  986. if (!tmp) {
  987. /* call func with error notice */
  988. retval = (*func)(0, 0, arg, 1);
  989. return 1;
  990. }
  991. }
  992. /* fall through */
  993. case ST_CONTINUE:
  994. ptr = ptr->back;
  995. break;
  996. case ST_STOP:
  997. return 0;
  998. case ST_DELETE:
  999. last = &table->bins[ptr->hash % table->num_bins];
  1000. for (; (tmp = *last) != 0; last = &tmp->next) {
  1001. if (ptr == tmp) {
  1002. tmp = ptr->back;
  1003. *last = ptr->next;
  1004. remove_entry(table, ptr);
  1005. st_free_entry(ptr);
  1006. ptr = tmp;
  1007. break;
  1008. }
  1009. }
  1010. ptr = ptr->next;
  1011. free(tmp);
  1012. table->num_entries--;
  1013. }
  1014. } while (ptr && table->head);
  1015. }
  1016. return 0;
  1017. }
  1018. #endif
  1019. /*
  1020. * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
  1021. *
  1022. * @(#) $Hash32: Revision: 1.1 $
  1023. * @(#) $Hash32: Id: hash_32a.c,v 1.1 2003/10/03 20:38:53 chongo Exp $
  1024. * @(#) $Hash32: Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
  1025. *
  1026. ***
  1027. *
  1028. * Fowler/Noll/Vo hash
  1029. *
  1030. * The basis of this hash algorithm was taken from an idea sent
  1031. * as reviewer comments to the IEEE POSIX P1003.2 committee by:
  1032. *
  1033. * Phong Vo (http://www.research.att.com/info/kpv/)
  1034. * Glenn Fowler (http://www.research.att.com/~gsf/)
  1035. *
  1036. * In a subsequent ballot round:
  1037. *
  1038. * Landon Curt Noll (http://www.isthe.com/chongo/)
  1039. *
  1040. * improved on their algorithm. Some people tried this hash
  1041. * and found that it worked rather well. In an EMail message
  1042. * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
  1043. *
  1044. * FNV hashes are designed to be fast while maintaining a low
  1045. * collision rate. The FNV speed allows one to quickly hash lots
  1046. * of data while maintaining a reasonable collision rate. See:
  1047. *
  1048. * http://www.isthe.com/chongo/tech/comp/fnv/index.html
  1049. *
  1050. * for more details as well as other forms of the FNV hash.
  1051. ***
  1052. *
  1053. * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
  1054. * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
  1055. *
  1056. ***
  1057. *
  1058. * Please do not copyright this code. This code is in the public domain.
  1059. *
  1060. * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  1061. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
  1062. * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  1063. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  1064. * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  1065. * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  1066. * PERFORMANCE OF THIS SOFTWARE.
  1067. *
  1068. * By:
  1069. * chongo <Landon Curt Noll> /\oo/\
  1070. * http://www.isthe.com/chongo/
  1071. *
  1072. * Share and Enjoy! :-)
  1073. */
  1074. /*
  1075. * 32 bit FNV-1 and FNV-1a non-zero initial basis
  1076. *
  1077. * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
  1078. *
  1079. * chongo <Landon Curt Noll> /\../\
  1080. *
  1081. * NOTE: The \'s above are not back-slashing escape characters.
  1082. * They are literal ASCII backslash 0x5c characters.
  1083. *
  1084. * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
  1085. */
  1086. #define FNV1_32A_INIT 0x811c9dc5
  1087. /*
  1088. * 32 bit magic FNV-1a prime
  1089. */
  1090. #define FNV_32_PRIME 0x01000193
  1091. #ifdef ST_USE_FNV1
  1092. static st_index_t
  1093. strhash(st_data_t arg)
  1094. {
  1095. register const char *string = (const char *)arg;
  1096. register st_index_t hval = FNV1_32A_INIT;
  1097. /*
  1098. * FNV-1a hash each octet in the buffer
  1099. */
  1100. while (*string) {
  1101. /* xor the bottom with the current octet */
  1102. hval ^= (unsigned int)*string++;
  1103. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  1104. hval *= FNV_32_PRIME;
  1105. }
  1106. return hval;
  1107. }
  1108. #else
  1109. #ifndef UNALIGNED_WORD_ACCESS
  1110. # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  1111. defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD86) || \
  1112. defined(__mc68020__)
  1113. # define UNALIGNED_WORD_ACCESS 1
  1114. # endif
  1115. #endif
  1116. #ifndef UNALIGNED_WORD_ACCESS
  1117. # define UNALIGNED_WORD_ACCESS 0
  1118. #endif
  1119. /* MurmurHash described in http://murmurhash.googlepages.com/ */
  1120. #ifndef MURMUR
  1121. #define MURMUR 2
  1122. #endif
  1123. #define MurmurMagic_1 (st_index_t)0xc6a4a793
  1124. #define MurmurMagic_2 (st_index_t)0x5bd1e995
  1125. #if MURMUR == 1
  1126. #define MurmurMagic MurmurMagic_1
  1127. #elif MURMUR == 2
  1128. #if SIZEOF_ST_INDEX_T > 4
  1129. #define MurmurMagic ((MurmurMagic_1 << 32) | MurmurMagic_2)
  1130. #else
  1131. #define MurmurMagic MurmurMagic_2
  1132. #endif
  1133. #endif
  1134. static inline st_index_t
  1135. murmur(st_index_t h, st_index_t k, int r)
  1136. {
  1137. const st_index_t m = MurmurMagic;
  1138. #if MURMUR == 1
  1139. h += k;
  1140. h *= m;
  1141. h ^= h >> r;
  1142. #elif MURMUR == 2
  1143. k *= m;
  1144. k ^= k >> r;
  1145. k *= m;
  1146. h *= m;
  1147. h ^= k;
  1148. #endif
  1149. return h;
  1150. }
  1151. static inline st_index_t
  1152. murmur_finish(st_index_t h)
  1153. {
  1154. #if MURMUR == 1
  1155. h = murmur(h, 0, 10);
  1156. h = murmur(h, 0, 17);
  1157. #elif MURMUR == 2
  1158. h ^= h >> 13;
  1159. h *= MurmurMagic;
  1160. h ^= h >> 15;
  1161. #endif
  1162. return h;
  1163. }
  1164. #define murmur_step(h, k) murmur((h), (k), 16)
  1165. #if MURMUR == 1
  1166. #define murmur1(h) murmur_step((h), 16)
  1167. #else
  1168. #define murmur1(h) murmur_step((h), 24)
  1169. #endif
  1170. st_index_t
  1171. st_hash(const void *ptr, size_t len, st_index_t h)
  1172. {
  1173. const char *data = ptr;
  1174. st_index_t t = 0;
  1175. h += 0xdeadbeef;
  1176. #define data_at(n) (st_index_t)((unsigned char)data[(n)])
  1177. #define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
  1178. #if SIZEOF_ST_INDEX_T > 4
  1179. #define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
  1180. #if SIZEOF_ST_INDEX_T > 8
  1181. #define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
  1182. UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
  1183. #define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
  1184. #endif
  1185. #define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
  1186. #else
  1187. #define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
  1188. #endif
  1189. if (len >= sizeof(st_index_t)) {
  1190. #if !UNALIGNED_WORD_ACCESS
  1191. int align = (int)((st_data_t)data % sizeof(st_index_t));
  1192. if (align) {
  1193. st_index_t d = 0;
  1194. int sl, sr, pack;
  1195. switch (align) {
  1196. #ifdef WORDS_BIGENDIAN
  1197. # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
  1198. t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
  1199. #else
  1200. # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
  1201. t |= data_at(n) << CHAR_BIT*(n)
  1202. #endif
  1203. UNALIGNED_ADD_ALL;
  1204. #undef UNALIGNED_ADD
  1205. }
  1206. #ifdef WORDS_BIGENDIAN
  1207. t >>= (CHAR_BIT * align) - CHAR_BIT;
  1208. #else
  1209. t <<= (CHAR_BIT * align);
  1210. #endif
  1211. data += sizeof(st_index_t)-align;
  1212. len -= sizeof(st_index_t)-align;
  1213. sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
  1214. sr = CHAR_BIT * align;
  1215. while (len >= sizeof(st_index_t)) {
  1216. d = *(st_index_t *)data;
  1217. #ifdef WORDS_BIGENDIAN
  1218. t = (t << sr) | (d >> sl);
  1219. #else
  1220. t = (t >> sr) | (d << sl);
  1221. #endif
  1222. h = murmur_step(h, t);
  1223. t = d;
  1224. data += sizeof(st_index_t);
  1225. len -= sizeof(st_index_t);
  1226. }
  1227. pack = len < (size_t)align ? (int)len : align;
  1228. d = 0;
  1229. switch (pack) {
  1230. #ifdef WORDS_BIGENDIAN
  1231. # define UNALIGNED_ADD(n) case (n) + 1: \
  1232. d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
  1233. #else
  1234. # define UNALIGNED_ADD(n) case (n) + 1: \
  1235. d |= data_at(n) << CHAR_BIT*(n)
  1236. #endif
  1237. UNALIGNED_ADD_ALL;
  1238. #undef UNALIGNED_ADD
  1239. }
  1240. #ifdef WORDS_BIGENDIAN
  1241. t = (t << sr) | (d >> sl);
  1242. #else
  1243. t = (t >> sr) | (d << sl);
  1244. #endif
  1245. #if MURMUR == 2
  1246. if (len < (size_t)align) goto skip_tail;
  1247. #endif
  1248. h = murmur_step(h, t);
  1249. data += pack;
  1250. len -= pack;
  1251. }
  1252. else
  1253. #endif
  1254. {
  1255. do {
  1256. h = murmur_step(h, *(st_index_t *)data);
  1257. data += sizeof(st_index_t);
  1258. len -= sizeof(st_index_t);
  1259. } while (len >= sizeof(st_index_t));
  1260. }
  1261. }
  1262. t = 0;
  1263. switch (len) {
  1264. #ifdef WORDS_BIGENDIAN
  1265. # define UNALIGNED_ADD(n) case (n) + 1: \
  1266. t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
  1267. #else
  1268. # define UNALIGNED_ADD(n) case (n) + 1: \
  1269. t |= data_at(n) << CHAR_BIT*(n)
  1270. #endif
  1271. UNALIGNED_ADD_ALL;
  1272. #undef UNALIGNED_ADD
  1273. #if MURMUR == 1
  1274. h = murmur_step(h, t);
  1275. #elif MURMUR == 2
  1276. # if !UNALIGNED_WORD_ACCESS
  1277. skip_tail:
  1278. # endif
  1279. h ^= t;
  1280. h *= MurmurMagic;
  1281. #endif
  1282. }
  1283. return murmur_finish(h);
  1284. }
  1285. st_index_t
  1286. st_hash_uint32(st_index_t h, uint32_t i)
  1287. {
  1288. return murmur_step(h + i, 16);
  1289. }
  1290. st_index_t
  1291. st_hash_uint(st_index_t h, st_index_t i)
  1292. {
  1293. st_index_t v = 0;
  1294. h += i;
  1295. #ifdef WORDS_BIGENDIAN
  1296. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
  1297. v = murmur1(v + (h >> 12*8));
  1298. #endif
  1299. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
  1300. v = murmur1(v + (h >> 8*8));
  1301. #endif
  1302. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
  1303. v = murmur1(v + (h >> 4*8));
  1304. #endif
  1305. #endif
  1306. v = murmur1(v + h);
  1307. #ifndef WORDS_BIGENDIAN
  1308. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
  1309. v = murmur1(v + (h >> 4*8));
  1310. #endif
  1311. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
  1312. v = murmur1(v + (h >> 8*8));
  1313. #endif
  1314. #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
  1315. v = murmur1(v + (h >> 12*8));
  1316. #endif
  1317. #endif
  1318. return v;
  1319. }
  1320. st_index_t
  1321. st_hash_end(st_index_t h)
  1322. {
  1323. h = murmur_step(h, 10);
  1324. h = murmur_step(h, 17);
  1325. return h;
  1326. }
  1327. #undef st_hash_start
  1328. st_index_t
  1329. st_hash_start(st_index_t h)
  1330. {
  1331. return h;
  1332. }
  1333. static st_index_t
  1334. strhash(st_data_t arg)
  1335. {
  1336. register const char *string = (const char *)arg;
  1337. return st_hash(string, strlen(string), FNV1_32A_INIT);
  1338. }
  1339. #endif
  1340. int
  1341. st_strcasecmp(const char *s1, const char *s2)
  1342. {
  1343. unsigned int c1, c2;
  1344. while (1) {
  1345. c1 = (unsigned char)*s1++;
  1346. c2 = (unsigned char)*s2++;
  1347. if (c1 == '\0' || c2 == '\0') {
  1348. if (c1 != '\0') return 1;
  1349. if (c2 != '\0') return -1;
  1350. return 0;
  1351. }
  1352. if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
  1353. if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
  1354. if (c1 != c2) {
  1355. if (c1 > c2)
  1356. return 1;
  1357. else
  1358. return -1;
  1359. }
  1360. }
  1361. }
  1362. int
  1363. st_strncasecmp(const char *s1, const char *s2, size_t n)
  1364. {
  1365. unsigned int c1, c2;
  1366. while (n--) {
  1367. c1 = (unsigned char)*s1++;
  1368. c2 = (unsigned char)*s2++;
  1369. if (c1 == '\0' || c2 == '\0') {
  1370. if (c1 != '\0') return 1;
  1371. if (c2 != '\0') return -1;
  1372. return 0;
  1373. }
  1374. if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
  1375. if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
  1376. if (c1 != c2) {
  1377. if (c1 > c2)
  1378. return 1;
  1379. else
  1380. return -1;
  1381. }
  1382. }
  1383. return 0;
  1384. }
  1385. static st_index_t
  1386. strcasehash(st_data_t arg)
  1387. {
  1388. register const char *string = (const char *)arg;
  1389. register st_index_t hval = FNV1_32A_INIT;
  1390. /*
  1391. * FNV-1a hash each octet in the buffer
  1392. */
  1393. while (*string) {
  1394. unsigned int c = (unsigned char)*string++;
  1395. if ((unsigned int)(c - 'A') <= ('Z' - 'A')) c += 'a' - 'A';
  1396. hval ^= c;
  1397. /* multiply by the 32 bit FNV magic prime mod 2^32 */
  1398. hval *= FNV_32_PRIME;
  1399. }
  1400. return hval;
  1401. }
  1402. int
  1403. st_numcmp(st_data_t x, st_data_t y)
  1404. {
  1405. return x != y;
  1406. }
  1407. st_index_t
  1408. st_numhash(st_data_t n)
  1409. {
  1410. return (st_index_t)n;
  1411. }