Ruby  2.1.10p492(2016-04-01revision54464)
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 /* Shortcut */
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 primes */
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;
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 {
281 }
282 
283 st_table*
285 {
286  return st_init_table(&type_strhash);
287 }
288 
289 st_table*
291 {
293 }
294 
295 st_table*
297 {
299 }
300 
301 st_table*
303 {
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 int
802 st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
803 {
804  st_table_entry **prev;
805  register st_table_entry *ptr;
806 
807  if (table->num_entries == 0) {
808  if (value != 0) *value = 0;
809  return 0;
810  }
811 
812  if (table->entries_packed) {
813  if (value != 0) *value = PVAL(table, 0);
814  *key = PKEY(table, 0);
815  remove_packed_entry(table, 0);
816  return 1;
817  }
818 
819  prev = &table->bins[table->head->hash % table->num_bins];
820  while ((ptr = *prev) != table->head) prev = &ptr->next;
821  *prev = ptr->next;
822  if (value != 0) *value = ptr->record;
823  *key = ptr->key;
824  remove_entry(table, ptr);
825  st_free_entry(ptr);
826  return 1;
827 }
828 
829 void
831 {
832  st_table_entry *ptr, **last, *tmp;
833  st_index_t i;
834 
835  if (table->entries_packed) {
836  st_index_t i = 0, j = 0;
837  while (PKEY(table, i) != never) {
838  if (i++ == table->real_entries) return;
839  }
840  for (j = i; ++i < table->real_entries;) {
841  if (PKEY(table, i) == never) continue;
842  PACKED_ENT(table, j) = PACKED_ENT(table, i);
843  j++;
844  }
845  table->real_entries = j;
846  /* table->num_entries really should be equal j at this moment, but let set it anyway */
847  table->num_entries = j;
848  return;
849  }
850 
851  for (i = 0; i < table->num_bins; i++) {
852  ptr = *(last = &table->bins[i]);
853  while (ptr != 0) {
854  if (ptr->key == never) {
855  tmp = ptr;
856  *last = ptr = ptr->next;
857  st_free_entry(tmp);
858  }
859  else {
860  ptr = *(last = &ptr->next);
861  }
862  }
863  }
864 }
865 
866 int
868 {
869  st_index_t hash_val, bin_pos;
870  register st_table_entry *ptr, **last, *tmp;
871  st_data_t value = 0;
872  int retval, existing = 0;
873 
874  hash_val = do_hash(key, table);
875 
876  if (table->entries_packed) {
877  st_index_t i = find_packed_index(table, hash_val, key);
878  if (i < table->real_entries) {
879  key = PKEY(table, i);
880  value = PVAL(table, i);
881  existing = 1;
882  }
883  {
884  retval = (*func)(&key, &value, arg, existing);
885  if (!table->entries_packed) {
886  FIND_ENTRY(table, ptr, hash_val, bin_pos);
887  goto unpacked;
888  }
889  switch (retval) {
890  case ST_CONTINUE:
891  if (!existing) {
892  add_packed_direct(table, key, value, hash_val);
893  break;
894  }
895  PVAL_SET(table, i, value);
896  break;
897  case ST_DELETE:
898  if (!existing) break;
899  remove_packed_entry(table, i);
900  }
901  }
902  return existing;
903  }
904 
905  FIND_ENTRY(table, ptr, hash_val, bin_pos);
906 
907  if (ptr != 0) {
908  key = ptr->key;
909  value = ptr->record;
910  existing = 1;
911  }
912  {
913  retval = (*func)(&key, &value, arg, existing);
914  unpacked:
915  switch (retval) {
916  case ST_CONTINUE:
917  if (!existing) {
918  add_direct(table, key, value, hash_val, hash_val % table->num_bins);
919  break;
920  }
921  ptr->record = value;
922  break;
923  case ST_DELETE:
924  if (!existing) break;
925  last = &table->bins[bin_pos];
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  break;
933  }
934  }
935  break;
936  }
937  return existing;
938  }
939 }
940 
941 int
943 {
944  st_table_entry *ptr, **last, *tmp;
945  enum st_retval retval;
946  st_index_t i;
947 
948  if (table->entries_packed) {
949  for (i = 0; i < table->real_entries; i++) {
950  st_data_t key, val;
952  key = PKEY(table, i);
953  val = PVAL(table, i);
954  hash = PHASH(table, i);
955  if (key == never) continue;
956  retval = (*func)(key, val, arg, 0);
957  if (!table->entries_packed) {
958  FIND_ENTRY(table, ptr, hash, i);
959  if (retval == ST_CHECK) {
960  if (!ptr) goto deleted;
961  goto unpacked_continue;
962  }
963  goto unpacked;
964  }
965  switch (retval) {
966  case ST_CHECK: /* check if hash is modified during iteration */
967  if (PHASH(table, i) == 0 && PKEY(table, i) == never) {
968  break;
969  }
970  i = find_packed_index_from(table, hash, key, i);
971  if (i >= table->real_entries) {
972  i = find_packed_index(table, hash, key);
973  if (i >= table->real_entries) goto deleted;
974  }
975  /* fall through */
976  case ST_CONTINUE:
977  break;
978  case ST_STOP:
979  return 0;
980  case ST_DELETE:
981  remove_safe_packed_entry(table, i, never);
982  break;
983  }
984  }
985  return 0;
986  }
987  else {
988  ptr = table->head;
989  }
990 
991  if (ptr != 0) {
992  do {
993  if (ptr->key == never)
994  goto unpacked_continue;
995  i = ptr->hash % table->num_bins;
996  retval = (*func)(ptr->key, ptr->record, arg, 0);
997  unpacked:
998  switch (retval) {
999  case ST_CHECK: /* check if hash is modified during iteration */
1000  for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
1001  if (!tmp) {
1002  deleted:
1003  /* call func with error notice */
1004  retval = (*func)(0, 0, arg, 1);
1005  return 1;
1006  }
1007  }
1008  /* fall through */
1009  case ST_CONTINUE:
1010  unpacked_continue:
1011  ptr = ptr->fore;
1012  break;
1013  case ST_STOP:
1014  return 0;
1015  case ST_DELETE:
1016  last = &table->bins[ptr->hash % table->num_bins];
1017  for (; (tmp = *last) != 0; last = &tmp->next) {
1018  if (ptr == tmp) {
1019  tmp = ptr->fore;
1020  remove_entry(table, ptr);
1021  ptr->key = ptr->record = never;
1022  ptr->hash = 0;
1023  ptr = tmp;
1024  break;
1025  }
1026  }
1027  }
1028  } while (ptr && table->head);
1029  }
1030  return 0;
1031 }
1032 
1033 int
1035 {
1036  st_table_entry *ptr, **last, *tmp;
1037  enum st_retval retval;
1038  st_index_t i;
1039 
1040  if (table->entries_packed) {
1041  for (i = 0; i < table->real_entries; i++) {
1042  st_data_t key, val, hash;
1043  key = PKEY(table, i);
1044  val = PVAL(table, i);
1045  hash = PHASH(table, i);
1046  retval = (*func)(key, val, arg, 0);
1047  if (!table->entries_packed) {
1048  FIND_ENTRY(table, ptr, hash, i);
1049  if (!ptr) return 0;
1050  goto unpacked;
1051  }
1052  switch (retval) {
1053  case ST_CONTINUE:
1054  break;
1055  case ST_CHECK:
1056  case ST_STOP:
1057  return 0;
1058  case ST_DELETE:
1059  remove_packed_entry(table, i);
1060  i--;
1061  break;
1062  }
1063  }
1064  return 0;
1065  }
1066  else {
1067  ptr = table->head;
1068  }
1069 
1070  if (ptr != 0) {
1071  do {
1072  i = ptr->hash % table->num_bins;
1073  retval = (*func)(ptr->key, ptr->record, arg, 0);
1074  unpacked:
1075  switch (retval) {
1076  case ST_CONTINUE:
1077  ptr = ptr->fore;
1078  break;
1079  case ST_CHECK:
1080  case ST_STOP:
1081  return 0;
1082  case ST_DELETE:
1083  last = &table->bins[ptr->hash % table->num_bins];
1084  for (; (tmp = *last) != 0; last = &tmp->next) {
1085  if (ptr == tmp) {
1086  tmp = ptr->fore;
1087  *last = ptr->next;
1088  remove_entry(table, ptr);
1089  st_free_entry(ptr);
1090  ptr = tmp;
1091  break;
1092  }
1093  }
1094  }
1095  } while (ptr && table->head);
1096  }
1097  return 0;
1098 }
1099 
1100 static st_index_t
1101 get_keys(st_table *table, st_data_t *keys, st_index_t size, int check, st_data_t never)
1102 {
1103  st_data_t key;
1104  st_data_t *keys_start = keys;
1105 
1106  if (table->entries_packed) {
1107  st_index_t i;
1108 
1109  if (size > table->real_entries) size = table->real_entries;
1110  for (i = 0; i < size; i++) {
1111  key = PKEY(table, i);
1112  if (check && key == never) continue;
1113  *keys++ = key;
1114  }
1115  }
1116  else {
1117  st_table_entry *ptr = table->head;
1118  st_data_t *keys_end = keys + size;
1119  for (; ptr && keys < keys_end; ptr = ptr->fore) {
1120  key = ptr->key;
1121  if (check && key == never) continue;
1122  *keys++ = key;
1123  }
1124  }
1125 
1126  return keys - keys_start;
1127 }
1128 
1129 st_index_t
1131 {
1132  return get_keys(table, keys, size, 0, 0);
1133 }
1134 
1135 st_index_t
1137 {
1138  return get_keys(table, keys, size, 1, never);
1139 }
1140 
1141 static st_index_t
1142 get_values(st_table *table, st_data_t *values, st_index_t size, int check, st_data_t never)
1143 {
1144  st_data_t key;
1145  st_data_t *values_start = values;
1146 
1147  if (table->entries_packed) {
1148  st_index_t i;
1149 
1150  if (size > table->real_entries) size = table->real_entries;
1151  for (i = 0; i < size; i++) {
1152  key = PKEY(table, i);
1153  if (check && key == never) continue;
1154  *values++ = PVAL(table, i);
1155  }
1156  }
1157  else {
1158  st_table_entry *ptr = table->head;
1159  st_data_t *values_end = values + size;
1160  for (; ptr && values < values_end; ptr = ptr->fore) {
1161  key = ptr->key;
1162  if (check && key == never) continue;
1163  *values++ = ptr->record;
1164  }
1165  }
1166 
1167  return values - values_start;
1168 }
1169 
1170 st_index_t
1172 {
1173  return get_values(table, values, size, 0, 0);
1174 }
1175 
1176 st_index_t
1178 {
1179  return get_values(table, values, size, 1, never);
1180 }
1181 
1182 #if 0 /* unused right now */
1183 int
1184 st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
1185 {
1186  st_table_entry *ptr, **last, *tmp;
1187  enum st_retval retval;
1188  int i;
1189 
1190  if (table->entries_packed) {
1191  for (i = table->num_entries-1; 0 <= i; i--) {
1192  int j;
1193  st_data_t key, val;
1194  key = PKEY(table, i);
1195  val = PVAL(table, i);
1196  retval = (*func)(key, val, arg, 0);
1197  switch (retval) {
1198  case ST_CHECK: /* check if hash is modified during iteration */
1199  for (j = 0; j < table->num_entries; j++) {
1200  if (PKEY(table, j) == key)
1201  break;
1202  }
1203  if (j == table->num_entries) {
1204  /* call func with error notice */
1205  retval = (*func)(0, 0, arg, 1);
1206  return 1;
1207  }
1208  /* fall through */
1209  case ST_CONTINUE:
1210  break;
1211  case ST_STOP:
1212  return 0;
1213  case ST_DELETE:
1214  remove_packed_entry(table, i);
1215  break;
1216  }
1217  }
1218  return 0;
1219  }
1220 
1221  if ((ptr = table->head) != 0) {
1222  ptr = ptr->back;
1223  do {
1224  retval = (*func)(ptr->key, ptr->record, arg, 0);
1225  switch (retval) {
1226  case ST_CHECK: /* check if hash is modified during iteration */
1227  i = ptr->hash % table->num_bins;
1228  for (tmp = table->bins[i]; tmp != ptr; tmp = tmp->next) {
1229  if (!tmp) {
1230  /* call func with error notice */
1231  retval = (*func)(0, 0, arg, 1);
1232  return 1;
1233  }
1234  }
1235  /* fall through */
1236  case ST_CONTINUE:
1237  ptr = ptr->back;
1238  break;
1239  case ST_STOP:
1240  return 0;
1241  case ST_DELETE:
1242  last = &table->bins[ptr->hash % table->num_bins];
1243  for (; (tmp = *last) != 0; last = &tmp->next) {
1244  if (ptr == tmp) {
1245  tmp = ptr->back;
1246  *last = ptr->next;
1247  remove_entry(table, ptr);
1248  st_free_entry(ptr);
1249  ptr = tmp;
1250  break;
1251  }
1252  }
1253  ptr = ptr->next;
1254  free(tmp);
1255  table->num_entries--;
1256  }
1257  } while (ptr && table->head);
1258  }
1259  return 0;
1260 }
1261 #endif
1262 
1263 /*
1264  * hash_32 - 32 bit Fowler/Noll/Vo FNV-1a hash code
1265  *
1266  * @(#) $Hash32: Revision: 1.1 $
1267  * @(#) $Hash32: Id: hash_32a.c,v 1.1 2003/10/03 20:38:53 chongo Exp $
1268  * @(#) $Hash32: Source: /usr/local/src/cmd/fnv/RCS/hash_32a.c,v $
1269  *
1270  ***
1271  *
1272  * Fowler/Noll/Vo hash
1273  *
1274  * The basis of this hash algorithm was taken from an idea sent
1275  * as reviewer comments to the IEEE POSIX P1003.2 committee by:
1276  *
1277  * Phong Vo (http://www.research.att.com/info/kpv/)
1278  * Glenn Fowler (http://www.research.att.com/~gsf/)
1279  *
1280  * In a subsequent ballot round:
1281  *
1282  * Landon Curt Noll (http://www.isthe.com/chongo/)
1283  *
1284  * improved on their algorithm. Some people tried this hash
1285  * and found that it worked rather well. In an EMail message
1286  * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
1287  *
1288  * FNV hashes are designed to be fast while maintaining a low
1289  * collision rate. The FNV speed allows one to quickly hash lots
1290  * of data while maintaining a reasonable collision rate. See:
1291  *
1292  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
1293  *
1294  * for more details as well as other forms of the FNV hash.
1295  ***
1296  *
1297  * To use the recommended 32 bit FNV-1a hash, pass FNV1_32A_INIT as the
1298  * Fnv32_t hashval argument to fnv_32a_buf() or fnv_32a_str().
1299  *
1300  ***
1301  *
1302  * Please do not copyright this code. This code is in the public domain.
1303  *
1304  * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1305  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
1306  * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1307  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
1308  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1309  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1310  * PERFORMANCE OF THIS SOFTWARE.
1311  *
1312  * By:
1313  * chongo <Landon Curt Noll> /\oo/\
1314  * http://www.isthe.com/chongo/
1315  *
1316  * Share and Enjoy! :-)
1317  */
1318 
1319 /*
1320  * 32 bit FNV-1 and FNV-1a non-zero initial basis
1321  *
1322  * The FNV-1 initial basis is the FNV-0 hash of the following 32 octets:
1323  *
1324  * chongo <Landon Curt Noll> /\../\
1325  *
1326  * NOTE: The \'s above are not back-slashing escape characters.
1327  * They are literal ASCII backslash 0x5c characters.
1328  *
1329  * NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
1330  */
1331 #define FNV1_32A_INIT 0x811c9dc5
1332 
1333 /*
1334  * 32 bit magic FNV-1a prime
1335  */
1336 #define FNV_32_PRIME 0x01000193
1337 
1338 #ifdef ST_USE_FNV1
1339 static st_index_t
1340 strhash(st_data_t arg)
1341 {
1342  register const char *string = (const char *)arg;
1343  register st_index_t hval = FNV1_32A_INIT;
1344 
1345  /*
1346  * FNV-1a hash each octet in the buffer
1347  */
1348  while (*string) {
1349  /* xor the bottom with the current octet */
1350  hval ^= (unsigned int)*string++;
1351 
1352  /* multiply by the 32 bit FNV magic prime mod 2^32 */
1353  hval *= FNV_32_PRIME;
1354  }
1355  return hval;
1356 }
1357 #else
1358 
1359 #ifndef UNALIGNED_WORD_ACCESS
1360 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1361  defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1362  defined(__mc68020__)
1363 # define UNALIGNED_WORD_ACCESS 1
1364 # endif
1365 #endif
1366 #ifndef UNALIGNED_WORD_ACCESS
1367 # define UNALIGNED_WORD_ACCESS 0
1368 #endif
1369 
1370 /* MurmurHash described in http://murmurhash.googlepages.com/ */
1371 #ifndef MURMUR
1372 #define MURMUR 2
1373 #endif
1374 
1375 #define MurmurMagic_1 (st_index_t)0xc6a4a793
1376 #define MurmurMagic_2 (st_index_t)0x5bd1e995
1377 #if MURMUR == 1
1378 #define MurmurMagic MurmurMagic_1
1379 #elif MURMUR == 2
1380 #if SIZEOF_ST_INDEX_T > 4
1381 #define MurmurMagic ((MurmurMagic_1 << 32) | MurmurMagic_2)
1382 #else
1383 #define MurmurMagic MurmurMagic_2
1384 #endif
1385 #endif
1386 
1387 static inline st_index_t
1389 {
1390  const st_index_t m = MurmurMagic;
1391 #if MURMUR == 1
1392  h += k;
1393  h *= m;
1394  h ^= h >> r;
1395 #elif MURMUR == 2
1396  k *= m;
1397  k ^= k >> r;
1398  k *= m;
1399 
1400  h *= m;
1401  h ^= k;
1402 #endif
1403  return h;
1404 }
1405 
1406 static inline st_index_t
1408 {
1409 #if MURMUR == 1
1410  h = murmur(h, 0, 10);
1411  h = murmur(h, 0, 17);
1412 #elif MURMUR == 2
1413  h ^= h >> 13;
1414  h *= MurmurMagic;
1415  h ^= h >> 15;
1416 #endif
1417  return h;
1418 }
1419 
1420 #define murmur_step(h, k) murmur((h), (k), 16)
1421 
1422 #if MURMUR == 1
1423 #define murmur1(h) murmur_step((h), 16)
1424 #else
1425 #define murmur1(h) murmur_step((h), 24)
1426 #endif
1427 
1428 st_index_t
1429 st_hash(const void *ptr, size_t len, st_index_t h)
1430 {
1431  const char *data = ptr;
1432  st_index_t t = 0;
1433 
1434  h += 0xdeadbeef;
1435 
1436 #define data_at(n) (st_index_t)((unsigned char)data[(n)])
1437 #define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1438 #if SIZEOF_ST_INDEX_T > 4
1439 #define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1440 #if SIZEOF_ST_INDEX_T > 8
1441 #define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1442  UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1443 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1444 #endif
1445 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1446 #else
1447 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1448 #endif
1449  if (len >= sizeof(st_index_t)) {
1450 #if !UNALIGNED_WORD_ACCESS
1451  int align = (int)((st_data_t)data % sizeof(st_index_t));
1452  if (align) {
1453  st_index_t d = 0;
1454  int sl, sr, pack;
1455 
1456  switch (align) {
1457 #ifdef WORDS_BIGENDIAN
1458 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1459  t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1460 #else
1461 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1462  t |= data_at(n) << CHAR_BIT*(n)
1463 #endif
1465 #undef UNALIGNED_ADD
1466  }
1467 
1468 #ifdef WORDS_BIGENDIAN
1469  t >>= (CHAR_BIT * align) - CHAR_BIT;
1470 #else
1471  t <<= (CHAR_BIT * align);
1472 #endif
1473 
1474  data += sizeof(st_index_t)-align;
1475  len -= sizeof(st_index_t)-align;
1476 
1477  sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1478  sr = CHAR_BIT * align;
1479 
1480  while (len >= sizeof(st_index_t)) {
1481  d = *(st_index_t *)data;
1482 #ifdef WORDS_BIGENDIAN
1483  t = (t << sr) | (d >> sl);
1484 #else
1485  t = (t >> sr) | (d << sl);
1486 #endif
1487  h = murmur_step(h, t);
1488  t = d;
1489  data += sizeof(st_index_t);
1490  len -= sizeof(st_index_t);
1491  }
1492 
1493  pack = len < (size_t)align ? (int)len : align;
1494  d = 0;
1495  switch (pack) {
1496 #ifdef WORDS_BIGENDIAN
1497 # define UNALIGNED_ADD(n) case (n) + 1: \
1498  d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1499 #else
1500 # define UNALIGNED_ADD(n) case (n) + 1: \
1501  d |= data_at(n) << CHAR_BIT*(n)
1502 #endif
1504 #undef UNALIGNED_ADD
1505  }
1506 #ifdef WORDS_BIGENDIAN
1507  t = (t << sr) | (d >> sl);
1508 #else
1509  t = (t >> sr) | (d << sl);
1510 #endif
1511 
1512 #if MURMUR == 2
1513  if (len < (size_t)align) goto skip_tail;
1514 #endif
1515  h = murmur_step(h, t);
1516  data += pack;
1517  len -= pack;
1518  }
1519  else
1520 #endif
1521  {
1522  do {
1523  h = murmur_step(h, *(st_index_t *)data);
1524  data += sizeof(st_index_t);
1525  len -= sizeof(st_index_t);
1526  } while (len >= sizeof(st_index_t));
1527  }
1528  }
1529 
1530  t = 0;
1531  switch (len) {
1532 #ifdef WORDS_BIGENDIAN
1533 # define UNALIGNED_ADD(n) case (n) + 1: \
1534  t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1535 #else
1536 # define UNALIGNED_ADD(n) case (n) + 1: \
1537  t |= data_at(n) << CHAR_BIT*(n)
1538 #endif
1540 #undef UNALIGNED_ADD
1541 #if MURMUR == 1
1542  h = murmur_step(h, t);
1543 #elif MURMUR == 2
1544 # if !UNALIGNED_WORD_ACCESS
1545  skip_tail:
1546 # endif
1547  h ^= t;
1548  h *= MurmurMagic;
1549 #endif
1550  }
1551 
1552  return murmur_finish(h);
1553 }
1554 
1555 st_index_t
1557 {
1558  return murmur_step(h + i, 16);
1559 }
1560 
1561 st_index_t
1563 {
1564  st_index_t v = 0;
1565  h += i;
1566 #ifdef WORDS_BIGENDIAN
1567 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
1568  v = murmur1(v + (h >> 12*8));
1569 #endif
1570 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1571  v = murmur1(v + (h >> 8*8));
1572 #endif
1573 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
1574  v = murmur1(v + (h >> 4*8));
1575 #endif
1576 #endif
1577  v = murmur1(v + h);
1578 #ifndef WORDS_BIGENDIAN
1579 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
1580  v = murmur1(v + (h >> 4*8));
1581 #endif
1582 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1583  v = murmur1(v + (h >> 8*8));
1584 #endif
1585 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
1586  v = murmur1(v + (h >> 12*8));
1587 #endif
1588 #endif
1589  return v;
1590 }
1591 
1592 st_index_t
1594 {
1595  h = murmur_step(h, 10);
1596  h = murmur_step(h, 17);
1597  return h;
1598 }
1599 
1600 #undef st_hash_start
1601 st_index_t
1603 {
1604  return h;
1605 }
1606 
1607 static st_index_t
1609 {
1610  register const char *string = (const char *)arg;
1611  return st_hash(string, strlen(string), FNV1_32A_INIT);
1612 }
1613 #endif
1614 
1615 int
1616 st_locale_insensitive_strcasecmp(const char *s1, const char *s2)
1617 {
1618  unsigned int c1, c2;
1619 
1620  while (1) {
1621  c1 = (unsigned char)*s1++;
1622  c2 = (unsigned char)*s2++;
1623  if (c1 == '\0' || c2 == '\0') {
1624  if (c1 != '\0') return 1;
1625  if (c2 != '\0') return -1;
1626  return 0;
1627  }
1628  if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
1629  if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
1630  if (c1 != c2) {
1631  if (c1 > c2)
1632  return 1;
1633  else
1634  return -1;
1635  }
1636  }
1637 }
1638 
1639 int
1640 st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n)
1641 {
1642  unsigned int c1, c2;
1643 
1644  while (n--) {
1645  c1 = (unsigned char)*s1++;
1646  c2 = (unsigned char)*s2++;
1647  if (c1 == '\0' || c2 == '\0') {
1648  if (c1 != '\0') return 1;
1649  if (c2 != '\0') return -1;
1650  return 0;
1651  }
1652  if ((unsigned int)(c1 - 'A') <= ('Z' - 'A')) c1 += 'a' - 'A';
1653  if ((unsigned int)(c2 - 'A') <= ('Z' - 'A')) c2 += 'a' - 'A';
1654  if (c1 != c2) {
1655  if (c1 > c2)
1656  return 1;
1657  else
1658  return -1;
1659  }
1660  }
1661  return 0;
1662 }
1663 
1664 static st_index_t
1666 {
1667  register const char *string = (const char *)arg;
1668  register st_index_t hval = FNV1_32A_INIT;
1669 
1670  /*
1671  * FNV-1a hash each octet in the buffer
1672  */
1673  while (*string) {
1674  unsigned int c = (unsigned char)*string++;
1675  if ((unsigned int)(c - 'A') <= ('Z' - 'A')) c += 'a' - 'A';
1676  hval ^= c;
1677 
1678  /* multiply by the 32 bit FNV magic prime mod 2^32 */
1679  hval *= FNV_32_PRIME;
1680  }
1681  return hval;
1682 }
1683 
1684 int
1686 {
1687  return x != y;
1688 }
1689 
1690 st_index_t
1692 {
1693  return (st_index_t)n;
1694 }
#define PKEY(table, i)
Definition: st.c:114
Definition: st.h:100
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:1383
void st_cleanup_safe(st_table *table, st_data_t never)
Definition: st.c:830
#define PVAL_SET(table, i, v)
Definition: st.c:118
size_t strlen(const char *)
st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never)
Definition: st.c:1136
Definition: st.h:69
Definition: st.h:100
st_index_t st_hash_uint32(st_index_t h, uint32_t i)
Definition: st.c:1556
int st_locale_insensitive_strcasecmp(const char *s1, const char *s2)
Definition: st.c:1616
static st_index_t get_keys(st_table *table, st_data_t *keys, st_index_t size, int check, st_data_t never)
Definition: st.c:1101
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:1336
#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:867
st_index_t st_numhash(st_data_t n)
Definition: st.c:1691
#define PACKED_UNIT
Definition: st.c:39
st_index_t num_bins
Definition: st.h:71
void st_clear(st_table *table)
Definition: st.c:308
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:113
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:1602
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
#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:48
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
#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:1425
#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:72
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:20
#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:1359
st_table * st_init_strtable(void)
Definition: st.c:284
#define SIZEOF_ST_INDEX_T
Definition: st.h:53
#define st_alloc_table()
Definition: st.c:93
int t(void)
Definition: conftest.c:13
st_table * st_init_strcasetable(void)
Definition: st.c:296
st_index_t st_hash_end(st_index_t h)
Definition: st.c:1593
#define val
VALUE rb_eRuntimeError
Definition: error.c:547
#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:1407
#define murmur_step(h, k)
Definition: st.c:1420
#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:1388
#define snprintf
Definition: subst.h:6
struct st_table::@120::@122 packed
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:1608
#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:1360
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:1034
st_table_entry * back
Definition: st.c:25
struct st_table_entry * head
Definition: st.h:89
union st_table::@120 as
static void rehash(st_table *)
st_table_entry * fore
Definition: st.c:25
st_index_t st_values(st_table *table, st_data_t *values, st_index_t size)
Definition: st.c:1171
#define realloc
Definition: st.c:79
int st_foreach_check(st_table *table, int(*func)(ANYARGS), st_data_t arg, st_data_t never)
Definition: st.c:942
st_index_t st_hash(const void *ptr, size_t len, st_index_t h)
Definition: st.c:1429
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:1361
Definition: st.c:28
st_index_t st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never)
Definition: st.c:1177
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:100
int type
Definition: tcltklib.c:112
#define PTR_NOT_EQUAL(table, ptr, hash_val, key)
Definition: st.c:352
struct st_table_entry ** bins
Definition: st.h:88
static VALUE result
Definition: nkf.c:40
#define FNV1_32A_INIT
Definition: st.c:1331
#define ST_DEFAULT_SECOND_TABLE_SIZE
Definition: st.c:37
st_data_t key
Definition: st.c:22
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
int st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
Definition: st.c:802
#define CHAR_BIT
Definition: ruby.h:198
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 getenv(name)
Definition: win32.c:66
#define free(x)
Definition: st.c:80
int size
Definition: encoding.c:49
#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:98
#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:1250
static st_index_t get_values(st_table *table, st_data_t *values, st_index_t size, int check, st_data_t never)
Definition: st.c:1142
int st_reverse_foreach(st_table *, int(*)(ANYARGS), st_data_t)
int st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: st.c:1640
st_index_t real_entries
Definition: st.h:93
const struct st_hash_type * type
Definition: st.h:70
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
Definition: st.h:100
int st_numcmp(st_data_t x, st_data_t y)
Definition: st.c:1685
#define ST_DEFAULT_INIT_TABLE_SIZE
Definition: st.c:36
#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:1665
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:1562
struct st_table_entry * tail
Definition: st.h:89
st_index_t num_entries
Definition: st.h:85
int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: st.h:117
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_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size)
Definition: st.c:1130
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