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