33 #define STATIC_ASSERT(name, expr) typedef int static_assert_##name##_check[(expr) ? 1 : -1];
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))
55 #define type_numhash st_hashtype_num
77 #define malloc xmalloc
78 #define calloc xcalloc
79 #define realloc xrealloc
80 #define free(x) xfree(x)
83 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
85 #define EQUAL(table,x,y) ((x)==(y) || (*(table)->type->compare)((x),(y)) == 0)
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)
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)
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
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))
189 for (i=3; i<31; i++) {
190 if ((1<<i) > size)
return 1<<
i;
196 for (i = 0, newsize =
MINSIZE; i <
numberof(primes); i++, newsize <<= 1) {
197 if (newsize > size)
return primes[
i];
212 int all, total, num, str, strcase;
214 static int init_st = 0;
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);
236 const char *e =
getenv(
"ST_HASH_LOG");
237 if (!e || !*e) init_st = 1;
319 for (i = 0; i < table->
num_bins; i++) {
320 ptr = table->
bins[
i];
352 #define PTR_NOT_EQUAL(table, ptr, hash_val, key) \
353 ((ptr) != 0 && ((ptr)->hash != (hash_val) || !EQUAL((table), (key), (ptr)->key)))
363 else if (type == &type_strhash) {
366 else if (type == &type_strcasehash) {
370 #define COLLISION (collision_check ? count_collision(table->type) : (void)0)
371 #define FOUND_ENTRY (collision_check ? collision.total++ : (void)0)
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)))
400 (
PHASH(table, i) != hash_val || !
EQUAL(table, key,
PKEY(table, i)))) {
406 #define collision_check 0
414 hash_val =
do_hash(key, table);
419 if (value != 0) *value =
PVAL(table, i);
431 if (value != 0) *value = ptr->
record;
442 hash_val =
do_hash(key, table);
447 if (result != 0) *result =
PKEY(table, i);
459 if (result != 0) *result = ptr->
key;
464 #undef collision_check
465 #define collision_check 1
474 table->
bins[bin_pos] = entry;
475 entry->
hash = hash_val;
489 bin_pos = hash_val % table->
num_bins;
492 entry =
new_entry(table, key, value, hash_val, bin_pos);
494 if (table->
head != 0) {
515 table->
as.
packed.entries = packed_bins;
517 #if ST_DEFAULT_INIT_TABLE_SIZE == ST_DEFAULT_PACKED_TABLE_SIZE
524 chain = &tmp_table.
head;
529 entry =
new_entry(&tmp_table, key, val, hash,
532 entry->
back = preventry;
534 chain = &entry->
fore;
535 }
while (++i < MAX_PACKED_HASH);
537 tmp_table.
tail = entry;
565 hash_val =
do_hash(key, table);
580 add_direct(table, key, value, hash_val, bin_pos);
597 hash_val =
do_hash(key, table);
614 add_direct(table, key, value, hash_val, bin_pos);
628 hash_val =
do_hash(key, table);
646 table->
bins = new_bins;
648 if ((ptr = table->
head) != 0) {
650 hash_val = ptr->
hash % new_num_bins;
651 ptr->
next = new_bins[hash_val];
652 new_bins[hash_val] = ptr;
653 }
while ((ptr = ptr->
fore) != 0);
666 if (new_table == 0) {
670 *new_table = *old_table;
673 if (new_table->
bins == 0) {
683 if ((ptr = old_table->
head) != 0) {
685 tailp = &new_table->
head;
693 hash_val = entry->
hash % num_bins;
694 entry->
next = new_table->
bins[hash_val];
695 new_table->
bins[hash_val] = entry;
697 *tailp = prev = entry;
698 tailp = &entry->
fore;
699 }
while ((ptr = ptr->
fore) != 0);
700 new_table->
tail = prev;
709 if (ptr->
fore == 0 && ptr->
back == 0) {
730 hash_val =
do_hash(*key, table);
735 if (value != 0) *value =
PVAL(table, i);
736 *key =
PKEY(table, i);
740 if (value != 0) *value = 0;
745 for (;(ptr = *prev) != 0; prev = &ptr->
next) {
749 if (value != 0) *value = ptr->
record;
756 if (value != 0) *value = 0;
766 hash_val =
do_hash(*key, table);
771 if (value != 0) *value =
PVAL(table, i);
772 *key =
PKEY(table, i);
776 if (value != 0) *value = 0;
782 for (; ptr != 0; ptr = ptr->
next) {
783 if ((ptr->
key != never) &&
EQUAL(table, ptr->
key, *key)) {
786 if (value != 0) *value = ptr->
record;
792 if (value != 0) *value = 0;
804 while (
PKEY(table, i) != never) {
808 if (
PKEY(table, i) == never)
continue;
818 for (i = 0; i < table->
num_bins; i++) {
819 ptr = *(last = &table->
bins[
i]);
821 if (ptr->
key == never) {
823 *last = ptr = ptr->
next;
827 ptr = *(last = &ptr->
next);
839 int retval, existing = 0;
841 hash_val =
do_hash(key, table);
846 key =
PKEY(table, i);
847 value =
PVAL(table, i);
851 retval = (*func)(&
key, &value, arg, existing);
865 if (!existing)
break;
880 retval = (*func)(&
key, &value, arg, existing);
891 if (!existing)
break;
892 last = &table->
bins[bin_pos];
893 for (; (tmp = *
last) != 0; last = &tmp->
next) {
919 key =
PKEY(table, i);
920 val =
PVAL(table, i);
921 hash =
PHASH(table, i);
922 if (key == never)
continue;
923 retval = (*func)(
key,
val, arg);
927 if (!ptr)
goto deleted;
928 goto unpacked_continue;
934 if (
PHASH(table, i) == 0 &&
PKEY(table, i) == never) {
959 if (ptr->
key == never)
960 goto unpacked_continue;
962 retval = (*func)(ptr->
key, ptr->
record, arg);
966 for (tmp = table->
bins[i]; tmp != ptr; tmp = tmp->
next) {
970 retval = (*func)(0, 0, arg, 1);
983 for (; (tmp = *
last) != 0; last = &tmp->
next) {
994 }
while (ptr && table->
head);
1009 key =
PKEY(table, i);
1010 val =
PVAL(table, i);
1011 hash =
PHASH(table, i);
1012 retval = (*func)(
key,
val, arg);
1039 retval = (*func)(ptr->
key, ptr->
record, arg);
1050 for (; (tmp = *
last) != 0; last = &tmp->
next) {
1061 }
while (ptr && table->
head);
1078 key =
PKEY(table, i);
1079 val =
PVAL(table, i);
1080 retval = (*func)(
key,
val, arg);
1089 retval = (*func)(0, 0, arg, 1);
1105 if ((ptr = table->
head) != 0) {
1108 retval = (*func)(ptr->
key, ptr->
record, arg, 0);
1112 for (tmp = table->
bins[i]; tmp != ptr; tmp = tmp->
next) {
1115 retval = (*func)(0, 0, arg, 1);
1127 for (; (tmp = *
last) != 0; last = &tmp->
next) {
1141 }
while (ptr && table->
head);
1215 #define FNV1_32A_INIT 0x811c9dc5
1220 #define FNV_32_PRIME 0x01000193
1226 register const char *
string = (
const char *)arg;
1234 hval ^= (
unsigned int)*
string++;
1243 #ifndef UNALIGNED_WORD_ACCESS
1244 # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1245 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD86) || \
1246 defined(__mc68020__)
1247 # define UNALIGNED_WORD_ACCESS 1
1250 #ifndef UNALIGNED_WORD_ACCESS
1251 # define UNALIGNED_WORD_ACCESS 0
1259 #define MurmurMagic_1 (st_index_t)0xc6a4a793
1260 #define MurmurMagic_2 (st_index_t)0x5bd1e995
1262 #define MurmurMagic MurmurMagic_1
1264 #if SIZEOF_ST_INDEX_T > 4
1265 #define MurmurMagic ((MurmurMagic_1 << 32) | MurmurMagic_2)
1267 #define MurmurMagic MurmurMagic_2
1304 #define murmur_step(h, k) murmur((h), (k), 16)
1307 #define murmur1(h) murmur_step((h), 16)
1309 #define murmur1(h) murmur_step((h), 24)
1315 const char *data = ptr;
1320 #define data_at(n) (st_index_t)((unsigned char)data[(n)])
1321 #define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1322 #if SIZEOF_ST_INDEX_T > 4
1323 #define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1324 #if SIZEOF_ST_INDEX_T > 8
1325 #define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1326 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1327 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1329 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1331 #define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1334 #if !UNALIGNED_WORD_ACCESS
1341 #ifdef WORDS_BIGENDIAN
1342 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1343 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1345 # define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1346 t |= data_at(n) << CHAR_BIT*(n)
1349 #undef UNALIGNED_ADD
1352 #ifdef WORDS_BIGENDIAN
1366 #ifdef WORDS_BIGENDIAN
1367 t = (t << sr) | (d >> sl);
1369 t = (t >> sr) | (d << sl);
1377 pack = len < (size_t)align ? (
int)len : align;
1380 #ifdef WORDS_BIGENDIAN
1381 # define UNALIGNED_ADD(n) case (n) + 1: \
1382 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1384 # define UNALIGNED_ADD(n) case (n) + 1: \
1385 d |= data_at(n) << CHAR_BIT*(n)
1388 #undef UNALIGNED_ADD
1390 #ifdef WORDS_BIGENDIAN
1391 t = (t << sr) | (d >> sl);
1393 t = (t >> sr) | (d << sl);
1397 if (len < (
size_t)align)
goto skip_tail;
1416 #ifdef WORDS_BIGENDIAN
1417 # define UNALIGNED_ADD(n) case (n) + 1: \
1418 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1420 # define UNALIGNED_ADD(n) case (n) + 1: \
1421 t |= data_at(n) << CHAR_BIT*(n)
1424 #undef UNALIGNED_ADD
1428 # if !UNALIGNED_WORD_ACCESS
1450 #ifdef WORDS_BIGENDIAN
1451 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
1454 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1457 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
1462 #ifndef WORDS_BIGENDIAN
1463 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 4*8
1466 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1469 #if SIZEOF_ST_INDEX_T*CHAR_BIT > 12*8
1484 #undef st_hash_start
1494 register const char *
string = (
const char *)arg;
1502 unsigned int c1, c2;
1505 c1 = (
unsigned char)*s1++;
1506 c2 = (
unsigned char)*s2++;
1507 if (c1 ==
'\0' || c2 ==
'\0') {
1508 if (c1 !=
'\0')
return 1;
1509 if (c2 !=
'\0')
return -1;
1512 if ((
unsigned int)(c1 -
'A') <= (
'Z' -
'A')) c1 +=
'a' -
'A';
1513 if ((
unsigned int)(c2 -
'A') <= (
'Z' -
'A')) c2 +=
'a' -
'A';
1526 unsigned int c1, c2;
1529 c1 = (
unsigned char)*s1++;
1530 c2 = (
unsigned char)*s2++;
1531 if (c1 ==
'\0' || c2 ==
'\0') {
1532 if (c1 !=
'\0')
return 1;
1533 if (c2 !=
'\0')
return -1;
1536 if ((
unsigned int)(c1 -
'A') <= (
'Z' -
'A')) c1 +=
'a' -
'A';
1537 if ((
unsigned int)(c2 -
'A') <= (
'Z' -
'A')) c2 +=
'a' -
'A';
1551 register const char *
string = (
const char *)arg;
1558 unsigned int c = (
unsigned char)*
string++;
1559 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
static st_index_t new_size(st_index_t size)
void st_cleanup_safe(st_table *table, st_data_t never)
#define PVAL_SET(table, i, v)
size_t strlen(const char *)
st_index_t st_hash_uint32(st_index_t h, uint32_t i)
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)
static st_index_t find_packed_index(st_table *table, st_index_t hash_val, st_data_t key)
int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg)
st_index_t st_numhash(st_data_t n)
void st_clear(st_table *table)
SSL_METHOD *(* func)(void)
st_table * st_init_strcasetable_with_size(st_index_t size)
static const struct st_hash_type type_strhash
st_index_t st_hash_start(st_index_t h)
void rb_raise(VALUE exc, const char *fmt,...)
#define st_free_entry(entry)
st_table * st_init_table_with_size(const struct st_hash_type *type, st_index_t size)
st_table * st_init_numtable(void)
struct st_table::@84::@86 packed
#define st_dealloc_table(table)
static void add_packed_direct(st_table *table, st_data_t key, st_data_t value, st_index_t hash_val)
int st_strncasecmp(const char *s1, const char *s2, size_t n)
#define ST_DEFAULT_MAX_DENSITY
static void unpack_entries(register st_table *table)
unsigned int entries_packed
#define FIND_ENTRY(table, ptr, hash_val, bin_pos)
const struct st_hash_type st_hashtype_num
st_table * st_init_strtable_with_size(st_index_t size)
#define MEMZERO(p, type, n)
st_table * st_init_strtable(void)
#define SIZEOF_ST_INDEX_T
st_table * st_init_strcasetable(void)
st_index_t st_hash_end(st_index_t h)
#define st_alloc_bins(size)
#define UNALIGNED_ADD_ALL
static st_index_t murmur_finish(st_index_t h)
#define murmur_step(h, k)
static st_index_t murmur(st_index_t h, st_index_t k, int r)
int st_get_key(st_table *table, register st_data_t key, st_data_t *result)
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)
static const unsigned int primes[]
static st_index_t strhash(st_data_t)
#define PHASH_SET(table, i, v)
int st_delete(register st_table *table, register st_data_t *key, st_data_t *value)
static st_table_entry * find_entry(st_table *table, st_data_t key, st_index_t hash_val, st_index_t bin_pos)
#define STATIC_ASSERT(name, expr)
static st_table_entry ** st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
#define MEMCPY(p1, p2, type, n)
int st_foreach(st_table *table, int(*func)(ANYARGS), st_data_t arg)
struct st_table_entry * head
static void rehash(st_table *)
int st_foreach_check(st_table *table, int(*func)(ANYARGS), st_data_t arg, st_data_t never)
st_index_t st_hash(const void *ptr, size_t len, st_index_t h)
static const struct st_hash_type type_strcasehash
#define MEMMOVE(p1, p2, type, n)
static void remove_safe_packed_entry(st_table *table, st_index_t i, st_data_t never)
#define PTR_NOT_EQUAL(table, ptr, hash_val, key)
struct st_table_entry ** bins
#define ST_DEFAULT_SECOND_TABLE_SIZE
int st_lookup(st_table *table, register st_data_t key, st_data_t *value)
#define st_free_bins(bins, size)
int st_delete_safe(register st_table *table, register st_data_t *key, st_data_t *value, st_data_t never)
static void remove_packed_entry(st_table *table, st_index_t i)
st_table * st_copy(st_table *old_table)
int st_insert2(register st_table *table, register st_data_t key, st_data_t value, st_data_t(*func)(st_data_t))
#define do_hash(key, table)
void st_add_direct(st_table *table, st_data_t key, st_data_t value)
#define EQUAL(table, x, y)
#define ST_DEFAULT_PACKED_TABLE_SIZE
int st_reverse_foreach(st_table *, int(*)(ANYARGS), st_data_t)
const struct st_hash_type * type
static unsigned int hash(const char *str, unsigned int len)
int st_numcmp(st_data_t x, st_data_t y)
#define ST_DEFAULT_INIT_TABLE_SIZE
int st_strcasecmp(const char *s1, const char *s2)
#define PACKED_BINS(table)
struct st_packed_entry st_packed_entry
void st_free_table(st_table *table)
static st_index_t strcasehash(st_data_t)
size_t st_memsize(const st_table *table)
st_index_t st_hash_uint(st_index_t h, st_index_t i)
struct st_table_entry * tail
int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
st_table * st_init_table(const struct st_hash_type *type)
static void remove_entry(st_table *table, st_table_entry *ptr)
st_table * st_init_numtable_with_size(st_index_t size)
#define PACKED_ENT(table, i)
int st_insert(register st_table *table, register st_data_t key, st_data_t value)
#define PKEY_SET(table, i, v)