106#elif defined RUBY_EXPORT
108#include "internal/bits.h"
109#include "internal/hash.h"
110#include "internal/sanitizers.h"
121#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
122#define EXPECT(expr, val) __builtin_expect(expr, val)
123#define ATTRIBUTE_UNUSED __attribute__((unused))
125#define PREFETCH(addr, write_p)
126#define EXPECT(expr, val) (expr)
127#define ATTRIBUTE_UNUSED
131typedef st_index_t st_hash_t;
139#define type_numhash st_hashtype_num
140static const struct st_hash_type st_hashtype_num = {
145static int st_strcmp(st_data_t, st_data_t);
146static st_index_t strhash(st_data_t);
147static const struct st_hash_type type_strhash = {
152static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
153static st_index_t strcasehash(st_data_t);
154static const struct st_hash_type type_strcasehash = {
155 st_locale_insensitive_strcasecmp_i,
162#define ST_INIT_VAL 0xafafafafafafafaf
163#define ST_INIT_VAL_BYTE 0xafa
170#define malloc ruby_xmalloc
171#define calloc ruby_xcalloc
172#define realloc ruby_xrealloc
173#define free ruby_xfree
176#define EQUAL(tab,x,y) ((x) == (y) || (*(tab)->type->compare)((x),(y)) == 0)
177#define PTR_EQUAL(tab, ptr, hash_val, key_) \
178 ((ptr)->hash == (hash_val) && EQUAL((tab), (key_), (ptr)->key))
182#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
184 unsigned int _old_rebuilds_num = (tab)->rebuilds_num; \
185 res = PTR_EQUAL(tab, ptr, hash_val, key); \
186 rebuilt_p = _old_rebuilds_num != (tab)->rebuilds_num; \
192 unsigned char entry_power;
196 unsigned char bin_power;
198 unsigned char size_ind;
201 st_index_t bins_words;
205#if SIZEOF_ST_INDEX_T == 8
224 {16, 17, 2, 0x10000},
225 {17, 18, 2, 0x20000},
226 {18, 19, 2, 0x40000},
227 {19, 20, 2, 0x80000},
228 {20, 21, 2, 0x100000},
229 {21, 22, 2, 0x200000},
230 {22, 23, 2, 0x400000},
231 {23, 24, 2, 0x800000},
232 {24, 25, 2, 0x1000000},
233 {25, 26, 2, 0x2000000},
234 {26, 27, 2, 0x4000000},
235 {27, 28, 2, 0x8000000},
236 {28, 29, 2, 0x10000000},
237 {29, 30, 2, 0x20000000},
238 {30, 31, 2, 0x40000000},
239 {31, 32, 2, 0x80000000},
240 {32, 33, 3, 0x200000000},
241 {33, 34, 3, 0x400000000},
242 {34, 35, 3, 0x800000000},
243 {35, 36, 3, 0x1000000000},
244 {36, 37, 3, 0x2000000000},
245 {37, 38, 3, 0x4000000000},
246 {38, 39, 3, 0x8000000000},
247 {39, 40, 3, 0x10000000000},
248 {40, 41, 3, 0x20000000000},
249 {41, 42, 3, 0x40000000000},
250 {42, 43, 3, 0x80000000000},
251 {43, 44, 3, 0x100000000000},
252 {44, 45, 3, 0x200000000000},
253 {45, 46, 3, 0x400000000000},
254 {46, 47, 3, 0x800000000000},
255 {47, 48, 3, 0x1000000000000},
256 {48, 49, 3, 0x2000000000000},
257 {49, 50, 3, 0x4000000000000},
258 {50, 51, 3, 0x8000000000000},
259 {51, 52, 3, 0x10000000000000},
260 {52, 53, 3, 0x20000000000000},
261 {53, 54, 3, 0x40000000000000},
262 {54, 55, 3, 0x80000000000000},
263 {55, 56, 3, 0x100000000000000},
264 {56, 57, 3, 0x200000000000000},
265 {57, 58, 3, 0x400000000000000},
266 {58, 59, 3, 0x800000000000000},
267 {59, 60, 3, 0x1000000000000000},
268 {60, 61, 3, 0x2000000000000000},
269 {61, 62, 3, 0x4000000000000000},
270 {62, 63, 3, 0x8000000000000000},
293 {16, 17, 2, 0x20000},
294 {17, 18, 2, 0x40000},
295 {18, 19, 2, 0x80000},
296 {19, 20, 2, 0x100000},
297 {20, 21, 2, 0x200000},
298 {21, 22, 2, 0x400000},
299 {22, 23, 2, 0x800000},
300 {23, 24, 2, 0x1000000},
301 {24, 25, 2, 0x2000000},
302 {25, 26, 2, 0x4000000},
303 {26, 27, 2, 0x8000000},
304 {27, 28, 2, 0x10000000},
305 {28, 29, 2, 0x20000000},
306 {29, 30, 2, 0x40000000},
307 {30, 31, 2, 0x80000000},
313#define RESERVED_HASH_VAL (~(st_hash_t) 0)
314#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
317static inline st_hash_t
318do_hash(st_data_t key, st_table *tab)
320 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
324 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
328#define MINIMAL_POWER2 2
330#if MINIMAL_POWER2 < 2
331#error "MINIMAL_POWER2 should be >= 2"
336#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
340get_power2(st_index_t size)
342 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
344 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
355static inline st_index_t
356get_bin(st_index_t *bins,
int s, st_index_t n)
358 return (s == 0 ? ((
unsigned char *) bins)[n]
359 : s == 1 ? ((
unsigned short *) bins)[n]
360 : s == 2 ? ((
unsigned int *) bins)[n]
361 : ((st_index_t *) bins)[n]);
367set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
369 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
370 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
371 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
372 else ((st_index_t *) bins)[n] = v;
385#define MARK_BIN_EMPTY(tab, i) (set_bin((tab)->bins, get_size_ind(tab), i, EMPTY_BIN))
389#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
390#define UNDEFINED_BIN_IND (~(st_index_t) 0)
394#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
395#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
400#define MARK_BIN_DELETED(tab, i) \
402 set_bin((tab)->bins, get_size_ind(tab), i, DELETED_BIN); \
407#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
408#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
409#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
413#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
414#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
415#define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
419#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
420#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
423static inline unsigned int
424get_size_ind(
const st_table *tab)
426 return tab->size_ind;
430static inline st_index_t
431get_bins_num(
const st_table *tab)
433 return ((st_index_t) 1)<<tab->bin_power;
437static inline st_index_t
438bins_mask(
const st_table *tab)
440 return get_bins_num(tab) - 1;
445static inline st_index_t
446hash_bin(st_hash_t hash_value, st_table *tab)
448 return hash_value & bins_mask(tab);
452static inline st_index_t
453get_allocated_entries(
const st_table *tab)
455 return ((st_index_t) 1)<<tab->entry_power;
459static inline st_index_t
460bins_size(
const st_table *tab)
462 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
467initialize_bins(st_table *tab)
469 memset(tab->bins, 0, bins_size(tab));
474make_tab_empty(st_table *tab)
476 tab->num_entries = 0;
477 tab->entries_start = tab->entries_bound = 0;
478 if (tab->bins != NULL)
479 initialize_bins(tab);
487 int all, total, num, str, strcase;
492static int init_st = 0;
499 char fname[10+
sizeof(long)*3];
501 if (!collision.total)
return;
502 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
505 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
506 ((
double)collision.all / (collision.total)) * 100);
507 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
513st_init_existing_table_with_size(st_table *tab,
const struct st_hash_type *type, st_index_t size)
520 const char *e = getenv(
"ST_HASH_LOG");
521 if (!e || !*e) init_st = 1;
530 n = get_power2(size);
537 tab->entry_power = n;
538 tab->bin_power = features[n].bin_power;
539 tab->size_ind = features[n].size_ind;
540 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
543 tab->bins = (st_index_t *) malloc(bins_size(tab));
545 if (tab->bins == NULL) {
551 tab->entries = (st_table_entry *) malloc(get_allocated_entries(tab)
552 *
sizeof(st_table_entry));
554 if (tab->entries == NULL) {
560 tab->rebuilds_num = 0;
568st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
570 st_table *tab = malloc(
sizeof(st_table));
577 st_init_existing_table_with_size(tab, type, size);
579 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
589st_table_size(
const struct st_table *tbl)
591 return tbl->num_entries;
597st_init_table(
const struct st_hash_type *type)
599 return st_init_table_with_size(type, 0);
605st_init_numtable(
void)
607 return st_init_table(&type_numhash);
612st_init_numtable_with_size(st_index_t size)
614 return st_init_table_with_size(&type_numhash, size);
620st_init_strtable(
void)
622 return st_init_table(&type_strhash);
627st_init_strtable_with_size(st_index_t size)
629 return st_init_table_with_size(&type_strhash, size);
635st_init_strcasetable(
void)
637 return st_init_table(&type_strcasehash);
643st_init_strcasetable_with_size(st_index_t size)
645 return st_init_table_with_size(&type_strcasehash, size);
650st_clear(st_table *tab)
658st_free_table(st_table *tab)
667st_memsize(
const st_table *tab)
669 return(
sizeof(st_table)
670 + (tab->bins == NULL ? 0 : bins_size(tab))
671 + get_allocated_entries(tab) *
sizeof(st_table_entry));
675find_table_entry_ind(st_table *tab, st_hash_t hash_value, st_data_t key);
678find_table_bin_ind(st_table *tab, st_hash_t hash_value, st_data_t key);
681find_table_bin_ind_direct(st_table *table, st_hash_t hash_value, st_data_t key);
684find_table_bin_ptr_and_reserve(st_table *tab, st_hash_t *hash_value,
685 st_data_t key, st_index_t *bin_ind);
689count_collision(
const struct st_hash_type *type)
692 if (type == &type_numhash) {
695 else if (type == &type_strhash) {
698 else if (type == &type_strcasehash) {
703#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
704#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
705#define collision_check 0
714#define REBUILD_THRESHOLD 4
716#if REBUILD_THRESHOLD < 2
717#error "REBUILD_THRESHOLD should be >= 2"
720static void rebuild_table_with(st_table *new_tab, st_table *tab);
727rebuild_table(st_table *tab)
729 if ((2 * tab->num_entries <= get_allocated_entries(tab)
730 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
731 || tab->num_entries < (1 << MINIMAL_POWER2)) {
733 tab->num_entries = 0;
734 if (tab->bins != NULL)
735 initialize_bins(tab);
736 rebuild_table_with(tab, tab);
743 new_tab = st_init_table_with_size(tab->type,
744 2 * tab->num_entries - 1);
745 rebuild_table_with(new_tab, tab);
750rebuild_table_with(st_table *new_tab, st_table *tab)
753 unsigned int size_ind;
754 st_table_entry *new_entries;
755 st_table_entry *curr_entry_ptr;
759 new_entries = new_tab->entries;
762 bins = new_tab->bins;
763 size_ind = get_size_ind(new_tab);
764 st_index_t bound = tab->entries_bound;
765 st_table_entry *entries = tab->entries;
767 for (i = tab->entries_start; i < bound; i++) {
768 curr_entry_ptr = &entries[i];
769 PREFETCH(entries + i + 1, 0);
770 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
772 if (&new_entries[ni] != curr_entry_ptr)
773 new_entries[ni] = *curr_entry_ptr;
774 if (EXPECT(bins != NULL, 1)) {
775 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
776 curr_entry_ptr->key);
777 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
779 new_tab->num_entries++;
782 if (new_tab != tab) {
783 tab->entry_power = new_tab->entry_power;
784 tab->bin_power = new_tab->bin_power;
785 tab->size_ind = new_tab->size_ind;
787 tab->bins = new_tab->bins;
789 tab->entries = new_tab->entries;
792 tab->entries_start = 0;
793 tab->entries_bound = tab->num_entries;
809static inline st_index_t
810secondary_hash(st_index_t ind, st_table *tab, st_index_t *perturb)
813 ind = (ind << 2) + ind + *perturb + 1;
814 return hash_bin(ind, tab);
821static inline st_index_t
822find_entry(st_table *tab, st_hash_t hash_value, st_data_t key)
826 st_table_entry *entries;
828 bound = tab->entries_bound;
829 entries = tab->entries;
830 for (i = tab->entries_start; i < bound; i++) {
831 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
832 if (EXPECT(rebuilt_p, 0))
833 return REBUILT_TABLE_ENTRY_IND;
837 return UNDEFINED_ENTRY_IND;
849find_table_entry_ind(st_table *tab, st_hash_t hash_value, st_data_t key)
853#ifdef QUADRATIC_PROBE
859 st_table_entry *entries = tab->entries;
861 ind = hash_bin(hash_value, tab);
862#ifdef QUADRATIC_PROBE
865 perturb = hash_value;
869 bin = get_bin(tab->bins, get_size_ind(tab), ind);
870 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
871 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
872 if (EXPECT(rebuilt_p, 0))
873 return REBUILT_TABLE_ENTRY_IND;
877 else if (EMPTY_BIN_P(bin))
878 return UNDEFINED_ENTRY_IND;
879#ifdef QUADRATIC_PROBE
880 ind = hash_bin(ind + d, tab);
883 ind = secondary_hash(ind, tab, &perturb);
895find_table_bin_ind(st_table *tab, st_hash_t hash_value, st_data_t key)
899#ifdef QUADRATIC_PROBE
905 st_table_entry *entries = tab->entries;
907 ind = hash_bin(hash_value, tab);
908#ifdef QUADRATIC_PROBE
911 perturb = hash_value;
915 bin = get_bin(tab->bins, get_size_ind(tab), ind);
916 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
917 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
918 if (EXPECT(rebuilt_p, 0))
919 return REBUILT_TABLE_BIN_IND;
923 else if (EMPTY_BIN_P(bin))
924 return UNDEFINED_BIN_IND;
925#ifdef QUADRATIC_PROBE
926 ind = hash_bin(ind + d, tab);
929 ind = secondary_hash(ind, tab, &perturb);
940find_table_bin_ind_direct(st_table *tab, st_hash_t hash_value, st_data_t key)
943#ifdef QUADRATIC_PROBE
950 ind = hash_bin(hash_value, tab);
951#ifdef QUADRATIC_PROBE
954 perturb = hash_value;
958 bin = get_bin(tab->bins, get_size_ind(tab), ind);
959 if (EMPTY_OR_DELETED_BIN_P(bin))
961#ifdef QUADRATIC_PROBE
962 ind = hash_bin(ind + d, tab);
965 ind = secondary_hash(ind, tab, &perturb);
981find_table_bin_ptr_and_reserve(st_table *tab, st_hash_t *hash_value,
982 st_data_t key, st_index_t *bin_ind)
986 st_hash_t curr_hash_value = *hash_value;
987#ifdef QUADRATIC_PROBE
992 st_index_t entry_index;
993 st_index_t first_deleted_bin_ind;
994 st_table_entry *entries;
996 ind = hash_bin(curr_hash_value, tab);
997#ifdef QUADRATIC_PROBE
1000 perturb = curr_hash_value;
1003 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1004 entries = tab->entries;
1006 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
1007 if (EMPTY_BIN_P(entry_index)) {
1009 entry_index = UNDEFINED_ENTRY_IND;
1010 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1012 ind = first_deleted_bin_ind;
1013 MARK_BIN_EMPTY(tab, ind);
1017 else if (! DELETED_BIN_P(entry_index)) {
1018 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1019 if (EXPECT(rebuilt_p, 0))
1020 return REBUILT_TABLE_ENTRY_IND;
1024 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1025 first_deleted_bin_ind = ind;
1026#ifdef QUADRATIC_PROBE
1027 ind = hash_bin(ind + d, tab);
1030 ind = secondary_hash(ind, tab, &perturb);
1041st_lookup(st_table *tab, st_data_t key, st_data_t *value)
1044 st_hash_t hash = do_hash(key, tab);
1047 if (tab->bins == NULL) {
1048 bin = find_entry(tab, hash, key);
1049 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1051 if (bin == UNDEFINED_ENTRY_IND)
1055 bin = find_table_entry_ind(tab, hash, key);
1056 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1058 if (bin == UNDEFINED_ENTRY_IND)
1063 *value = tab->entries[bin].record;
1070st_get_key(st_table *tab, st_data_t key, st_data_t *result)
1073 st_hash_t hash = do_hash(key, tab);
1076 if (tab->bins == NULL) {
1077 bin = find_entry(tab, hash, key);
1078 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1080 if (bin == UNDEFINED_ENTRY_IND)
1084 bin = find_table_entry_ind(tab, hash, key);
1085 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1087 if (bin == UNDEFINED_ENTRY_IND)
1092 *result = tab->entries[bin].key;
1098rebuild_table_if_necessary (st_table *tab)
1100 st_index_t bound = tab->entries_bound;
1102 if (bound == get_allocated_entries(tab))
1110st_insert(st_table *tab, st_data_t key, st_data_t value)
1112 st_table_entry *entry;
1115 st_hash_t hash_value;
1119 hash_value = do_hash(key, tab);
1121 rebuild_table_if_necessary(tab);
1122 if (tab->bins == NULL) {
1123 bin = find_entry(tab, hash_value, key);
1124 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1126 new_p = bin == UNDEFINED_ENTRY_IND;
1129 bin_ind = UNDEFINED_BIN_IND;
1132 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1134 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1136 new_p = bin == UNDEFINED_ENTRY_IND;
1140 ind = tab->entries_bound++;
1141 entry = &tab->entries[ind];
1142 entry->hash = hash_value;
1144 entry->record = value;
1145 if (bin_ind != UNDEFINED_BIN_IND)
1146 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1149 tab->entries[bin].record = value;
1156st_add_direct_with_hash(st_table *tab,
1157 st_data_t key, st_data_t value, st_hash_t hash)
1159 st_table_entry *entry;
1163 rebuild_table_if_necessary(tab);
1164 ind = tab->entries_bound++;
1165 entry = &tab->entries[ind];
1168 entry->record = value;
1170 if (tab->bins != NULL) {
1171 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1172 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1177rb_st_add_direct_with_hash(st_table *tab,
1178 st_data_t key, st_data_t value, st_hash_t hash)
1180 st_add_direct_with_hash(tab, key, value, hash);
1186st_add_direct(st_table *tab, st_data_t key, st_data_t value)
1188 st_hash_t hash_value;
1190 hash_value = do_hash(key, tab);
1191 st_add_direct_with_hash(tab, key, value, hash_value);
1198st_insert2(st_table *tab, st_data_t key, st_data_t value,
1199 st_data_t (*func)(st_data_t))
1201 st_table_entry *entry;
1204 st_hash_t hash_value;
1208 hash_value = do_hash(key, tab);
1210 rebuild_table_if_necessary (tab);
1211 if (tab->bins == NULL) {
1212 bin = find_entry(tab, hash_value, key);
1213 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1215 new_p = bin == UNDEFINED_ENTRY_IND;
1218 bin_ind = UNDEFINED_BIN_IND;
1221 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1223 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1225 new_p = bin == UNDEFINED_ENTRY_IND;
1230 ind = tab->entries_bound++;
1231 entry = &tab->entries[ind];
1232 entry->hash = hash_value;
1234 entry->record = value;
1235 if (bin_ind != UNDEFINED_BIN_IND)
1236 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1239 tab->entries[bin].record = value;
1245st_replace(st_table *new_tab, st_table *old_tab)
1247 *new_tab = *old_tab;
1248 if (old_tab->bins == NULL)
1249 new_tab->bins = NULL;
1251 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1253 if (new_tab->bins == NULL) {
1258 new_tab->entries = (st_table_entry *) malloc(get_allocated_entries(old_tab)
1259 *
sizeof(st_table_entry));
1261 if (new_tab->entries == NULL) {
1265 MEMCPY(new_tab->entries, old_tab->entries, st_table_entry,
1266 get_allocated_entries(old_tab));
1267 if (old_tab->bins != NULL)
1268 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1275st_copy(st_table *old_tab)
1279 new_tab = (st_table *) malloc(
sizeof(st_table));
1281 if (new_tab == NULL)
1285 if (st_replace(new_tab, old_tab) == NULL) {
1286 st_free_table(new_tab);
1296update_range_for_deleted(st_table *tab, st_index_t n)
1300 if (tab->entries_start == n) {
1301 st_index_t start = n + 1;
1302 st_index_t bound = tab->entries_bound;
1303 st_table_entry *entries = tab->entries;
1304 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1305 tab->entries_start = start;
1314st_general_delete(st_table *tab, st_data_t *key, st_data_t *value)
1316 st_table_entry *entry;
1321 hash = do_hash(*key, tab);
1323 if (tab->bins == NULL) {
1324 bin = find_entry(tab, hash, *key);
1325 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1327 if (bin == UNDEFINED_ENTRY_IND) {
1328 if (value != 0) *value = 0;
1333 bin_ind = find_table_bin_ind(tab, hash, *key);
1334 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1336 if (bin_ind == UNDEFINED_BIN_IND) {
1337 if (value != 0) *value = 0;
1340 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1341 MARK_BIN_DELETED(tab, bin_ind);
1343 entry = &tab->entries[bin];
1345 if (value != 0) *value = entry->record;
1346 MARK_ENTRY_DELETED(entry);
1348 update_range_for_deleted(tab, bin);
1353st_delete(st_table *tab, st_data_t *key, st_data_t *value)
1355 return st_general_delete(tab, key, value);
1364st_delete_safe(st_table *tab, st_data_t *key, st_data_t *value,
1365 st_data_t never ATTRIBUTE_UNUSED)
1367 return st_general_delete(tab, key, value);
1375st_shift(st_table *tab, st_data_t *key, st_data_t *value)
1377 st_index_t i, bound;
1379 st_table_entry *entries, *curr_entry_ptr;
1382 entries = tab->entries;
1383 bound = tab->entries_bound;
1384 for (i = tab->entries_start; i < bound; i++) {
1385 curr_entry_ptr = &entries[i];
1386 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1387 st_hash_t entry_hash = curr_entry_ptr->hash;
1388 st_data_t entry_key = curr_entry_ptr->key;
1390 if (value != 0) *value = curr_entry_ptr->record;
1393 if (tab->bins == NULL) {
1394 bin = find_entry(tab, entry_hash, entry_key);
1395 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1396 entries = tab->entries;
1399 curr_entry_ptr = &entries[bin];
1402 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1403 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1404 entries = tab->entries;
1407 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1409 MARK_BIN_DELETED(tab, bin_ind);
1411 MARK_ENTRY_DELETED(curr_entry_ptr);
1413 update_range_for_deleted(tab, i);
1417 if (value != 0) *value = 0;
1423st_cleanup_safe(st_table *tab ATTRIBUTE_UNUSED,
1424 st_data_t never ATTRIBUTE_UNUSED)
1438st_update(st_table *tab, st_data_t key,
1439 st_update_callback_func *func, st_data_t arg)
1441 st_table_entry *entry = NULL;
1443 st_table_entry *entries;
1445 st_data_t value = 0, old_key;
1446 int retval, existing;
1447 st_hash_t hash = do_hash(key, tab);
1450 entries = tab->entries;
1451 if (tab->bins == NULL) {
1452 bin = find_entry(tab, hash, key);
1453 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1455 existing = bin != UNDEFINED_ENTRY_IND;
1456 entry = &entries[bin];
1457 bin_ind = UNDEFINED_BIN_IND;
1460 bin_ind = find_table_bin_ind(tab, hash, key);
1461 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1463 existing = bin_ind != UNDEFINED_BIN_IND;
1465 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1466 entry = &entries[bin];
1471 value = entry->record;
1474 retval = (*func)(&key, &value, arg, existing);
1478 st_add_direct_with_hash(tab, key, value, hash);
1481 if (old_key != key) {
1484 entry->record = value;
1488 if (bin_ind != UNDEFINED_BIN_IND)
1489 MARK_BIN_DELETED(tab, bin_ind);
1490 MARK_ENTRY_DELETED(entry);
1492 update_range_for_deleted(tab, bin);
1508st_general_foreach(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1513 st_table_entry *entries, *curr_entry_ptr;
1514 enum st_retval retval;
1515 st_index_t i, rebuilds_num;
1518 int error_p, packed_p = tab->bins == NULL;
1520 entries = tab->entries;
1523 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1524 curr_entry_ptr = &entries[i];
1525 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1527 key = curr_entry_ptr->key;
1528 rebuilds_num = tab->rebuilds_num;
1529 hash = curr_entry_ptr->hash;
1530 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1532 if (retval == ST_REPLACE && replace) {
1534 value = curr_entry_ptr->record;
1535 retval = (*replace)(&key, &value, arg, TRUE);
1536 curr_entry_ptr->key = key;
1537 curr_entry_ptr->record = value;
1540 if (rebuilds_num != tab->rebuilds_num) {
1542 entries = tab->entries;
1543 packed_p = tab->bins == NULL;
1545 i = find_entry(tab, hash, key);
1546 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1548 error_p = i == UNDEFINED_ENTRY_IND;
1551 i = find_table_entry_ind(tab, hash, key);
1552 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1554 error_p = i == UNDEFINED_ENTRY_IND;
1557 if (error_p && check_p) {
1559 retval = (*func)(0, 0, arg, 1);
1562 curr_entry_ptr = &entries[i];
1575 st_data_t key = curr_entry_ptr->key;
1579 bin = find_entry(tab, hash, key);
1580 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1582 if (bin == UNDEFINED_ENTRY_IND)
1586 bin_ind = find_table_bin_ind(tab, hash, key);
1587 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1589 if (bin_ind == UNDEFINED_BIN_IND)
1591 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1592 MARK_BIN_DELETED(tab, bin_ind);
1594 curr_entry_ptr = &entries[bin];
1595 MARK_ENTRY_DELETED(curr_entry_ptr);
1597 update_range_for_deleted(tab, bin);
1606st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1608 return st_general_foreach(tab, func, replace, arg, TRUE);
1612 st_foreach_callback_func *func;
1617apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1619 const struct functor *f = (
void *)d;
1620 return f->func(k, v, f->arg);
1624st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1626 const struct functor f = { func, arg };
1627 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1632st_foreach_check(st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1633 st_data_t never ATTRIBUTE_UNUSED)
1635 return st_general_foreach(tab, func, 0, arg, TRUE);
1640static inline st_index_t
1641st_general_keys(st_table *tab, st_data_t *keys, st_index_t size)
1643 st_index_t i, bound;
1644 st_data_t key, *keys_start, *keys_end;
1645 st_table_entry *curr_entry_ptr, *entries = tab->entries;
1647 bound = tab->entries_bound;
1649 keys_end = keys + size;
1650 for (i = tab->entries_start; i < bound; i++) {
1651 if (keys == keys_end)
1653 curr_entry_ptr = &entries[i];
1654 key = curr_entry_ptr->key;
1655 if (! DELETED_ENTRY_P(curr_entry_ptr))
1659 return keys - keys_start;
1663st_keys(st_table *tab, st_data_t *keys, st_index_t size)
1665 return st_general_keys(tab, keys, size);
1670st_keys_check(st_table *tab, st_data_t *keys, st_index_t size,
1671 st_data_t never ATTRIBUTE_UNUSED)
1673 return st_general_keys(tab, keys, size);
1678static inline st_index_t
1679st_general_values(st_table *tab, st_data_t *values, st_index_t size)
1681 st_index_t i, bound;
1682 st_data_t *values_start, *values_end;
1683 st_table_entry *curr_entry_ptr, *entries = tab->entries;
1685 values_start = values;
1686 values_end = values + size;
1687 bound = tab->entries_bound;
1688 for (i = tab->entries_start; i < bound; i++) {
1689 if (values == values_end)
1691 curr_entry_ptr = &entries[i];
1692 if (! DELETED_ENTRY_P(curr_entry_ptr))
1693 *values++ = curr_entry_ptr->record;
1696 return values - values_start;
1700st_values(st_table *tab, st_data_t *values, st_index_t size)
1702 return st_general_values(tab, values, size);
1707st_values_check(st_table *tab, st_data_t *values, st_index_t size,
1708 st_data_t never ATTRIBUTE_UNUSED)
1710 return st_general_values(tab, values, size);
1713#define FNV1_32A_INIT 0x811c9dc5
1718#define FNV_32_PRIME 0x01000193
1721#ifndef UNALIGNED_WORD_ACCESS
1722# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1723 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1724 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1725 defined(__mc68020__)
1726# define UNALIGNED_WORD_ACCESS 1
1729#ifndef UNALIGNED_WORD_ACCESS
1730# define UNALIGNED_WORD_ACCESS 0
1736#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1737#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1739#if ST_INDEX_BITS <= 32
1740#define C1 (st_index_t)0xcc9e2d51
1741#define C2 (st_index_t)0x1b873593
1743#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1744#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1746NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1747NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1748NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1750static inline st_index_t
1751murmur_step(st_index_t h, st_index_t k)
1753#if ST_INDEX_BITS <= 32
1769static inline st_index_t
1770murmur_finish(st_index_t h)
1772#if ST_INDEX_BITS <= 32
1776 const st_index_t c1 = 0x85ebca6b;
1777 const st_index_t c2 = 0xc2b2ae35;
1783 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1784 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1786#if ST_INDEX_BITS > 64
1803st_hash(
const void *ptr,
size_t len, st_index_t h)
1805 const char *data = ptr;
1809#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1810#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1811#if SIZEOF_ST_INDEX_T > 4
1812#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1813#if SIZEOF_ST_INDEX_T > 8
1814#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1815 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1816#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1818#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1820#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1823 if (
len >=
sizeof(st_index_t)) {
1824#if !UNALIGNED_WORD_ACCESS
1825 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1831#ifdef WORDS_BIGENDIAN
1832# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1833 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1835# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1836 t |= data_at(n) << CHAR_BIT*(n)
1842#ifdef WORDS_BIGENDIAN
1843 t >>= (CHAR_BIT * align) - CHAR_BIT;
1845 t <<= (CHAR_BIT * align);
1848 data +=
sizeof(st_index_t)-align;
1849 len -=
sizeof(st_index_t)-align;
1851 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1852 sr = CHAR_BIT * align;
1854 while (
len >=
sizeof(st_index_t)) {
1855 d = *(st_index_t *)data;
1856#ifdef WORDS_BIGENDIAN
1857 t = (t << sr) | (d >> sl);
1859 t = (t >> sr) | (d << sl);
1861 h = murmur_step(h, t);
1863 data +=
sizeof(st_index_t);
1864 len -=
sizeof(st_index_t);
1867 pack =
len < (size_t)align ? (
int)
len : align;
1870#ifdef WORDS_BIGENDIAN
1871# define UNALIGNED_ADD(n) case (n) + 1: \
1872 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1874# define UNALIGNED_ADD(n) case (n) + 1: \
1875 d |= data_at(n) << CHAR_BIT*(n)
1880#ifdef WORDS_BIGENDIAN
1881 t = (t << sr) | (d >> sl);
1883 t = (t >> sr) | (d << sl);
1886 if (
len < (
size_t)align)
goto skip_tail;
1888 h = murmur_step(h, t);
1894#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1895#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1897#define aligned_data data
1901 h = murmur_step(h, *(st_index_t *)aligned_data);
1902 data +=
sizeof(st_index_t);
1903 len -=
sizeof(st_index_t);
1904 }
while (
len >=
sizeof(st_index_t));
1910#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1912#if SIZEOF_ST_INDEX_T > 4
1913 case 7: t |= data_at(6) << 48;
1914 case 6: t |= data_at(5) << 40;
1915 case 5: t |= data_at(4) << 32;
1917 t |= (st_index_t)*(uint32_t*)aligned_data;
1921 case 3: t |= data_at(2) << 16;
1922 case 2: t |= data_at(1) << 8;
1923 case 1: t |= data_at(0);
1925#ifdef WORDS_BIGENDIAN
1926# define UNALIGNED_ADD(n) case (n) + 1: \
1927 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1929# define UNALIGNED_ADD(n) case (n) + 1: \
1930 t |= data_at(n) << CHAR_BIT*(n)
1938 h ^= t; h -= ROTL(t, 7);
1944 return murmur_finish(h);
1948st_hash_uint32(st_index_t h, uint32_t i)
1950 return murmur_step(h, i);
1953NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
1955st_hash_uint(st_index_t h, st_index_t i)
1960#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1961 h = murmur_step(h, i >> 8*8);
1963 h = murmur_step(h, i);
1968st_hash_end(st_index_t h)
1970 h = murmur_finish(h);
1976rb_st_hash_start(st_index_t h)
1982strhash(st_data_t arg)
1984 register const char *
string = (
const char *)arg;
1985 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
1989st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
1996 if (c1 ==
'\0' || c2 ==
'\0') {
1997 if (c1 !=
'\0')
return 1;
1998 if (c2 !=
'\0')
return -1;
2001 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2002 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2013st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2018 for (i = 0; i < n; i++) {
2021 if (c1 ==
'\0' || c2 ==
'\0') {
2022 if (c1 !=
'\0')
return 1;
2023 if (c2 !=
'\0')
return -1;
2026 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2027 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2039st_strcmp(st_data_t lhs, st_data_t rhs)
2041 const char *s1 = (
char *)lhs;
2042 const char *s2 = (
char *)rhs;
2043 return strcmp(s1, s2);
2047st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2049 const char *s1 = (
char *)lhs;
2050 const char *s2 = (
char *)rhs;
2051 return st_locale_insensitive_strcasecmp(s1, s2);
2054NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2056strcasehash(st_data_t arg)
2058 register const char *
string = (
const char *)arg;
2059 register st_index_t hval = FNV1_32A_INIT;
2065 unsigned int c = (
unsigned char)*
string++;
2066 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2070 hval *= FNV_32_PRIME;
2076st_numcmp(st_data_t x, st_data_t y)
2082st_numhash(st_data_t n)
2084 enum {s1 = 11, s2 = 3};
2085 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2093st_expand_table(st_table *tab, st_index_t siz)
2098 if (siz <= get_allocated_entries(tab))
2101 tmp = st_init_table_with_size(tab->type, siz);
2102 n = get_allocated_entries(tab);
2103 MEMCPY(tmp->entries, tab->entries, st_table_entry, n);
2107 tab->entry_power = tmp->entry_power;
2108 tab->bin_power = tmp->bin_power;
2109 tab->size_ind = tmp->size_ind;
2110 tab->entries = tmp->entries;
2112 tab->rebuilds_num++;
2119st_rehash_linear(st_table *tab)
2121 int eq_p, rebuilt_p;
2123 st_table_entry *p, *q;
2128 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2129 p = &tab->entries[i];
2130 if (DELETED_ENTRY_P(p))
2132 for (j = i + 1; j < tab->entries_bound; j++) {
2133 q = &tab->entries[j];
2134 if (DELETED_ENTRY_P(q))
2136 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2137 if (EXPECT(rebuilt_p, 0))
2141 MARK_ENTRY_DELETED(q);
2143 update_range_for_deleted(tab, j);
2153st_rehash_indexed(st_table *tab)
2155 int eq_p, rebuilt_p;
2157 st_index_t
const n = bins_size(tab);
2158 unsigned int const size_ind = get_size_ind(tab);
2159 st_index_t *bins = realloc(tab->bins, n);
2161 initialize_bins(tab);
2162 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2163 st_table_entry *p = &tab->entries[i];
2165#ifdef QUADRATIC_PROBE
2168 st_index_t perturb = p->hash;
2171 if (DELETED_ENTRY_P(p))
2174 ind = hash_bin(p->hash, tab);
2176 st_index_t bin = get_bin(bins, size_ind, ind);
2177 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2179 set_bin(bins, size_ind, ind, i + ENTRY_BASE);
2183 st_table_entry *q = &tab->entries[bin - ENTRY_BASE];
2184 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2185 if (EXPECT(rebuilt_p, 0))
2189 q->record = p->record;
2190 MARK_ENTRY_DELETED(p);
2192 update_range_for_deleted(tab, bin);
2197#ifdef QUADRATIC_PROBE
2198 ind = hash_bin(ind + d, tab);
2201 ind = secondary_hash(ind, tab, &perturb);
2214st_rehash(st_table *tab)
2219 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2220 rebuilt_p = st_rehash_linear(tab);
2222 rebuilt_p = st_rehash_indexed(tab);
2223 }
while (rebuilt_p);
2227st_stringify(
VALUE key)
2230 rb_hash_key_str(key) : key;
2236 st_data_t k = st_stringify(key);
2238 e.hash = do_hash(k, tab);
2242 tab->entries[tab->entries_bound++] = e;
2249st_insert_linear(st_table *tab,
long argc,
const VALUE *argv,
VALUE hash)
2253 for (i = 0; i < argc; ) {
2254 st_data_t k = st_stringify(argv[i++]);
2255 st_data_t v = argv[i++];
2256 st_insert(tab, k, v);
2263st_insert_generic(st_table *tab,
long argc,
const VALUE *argv,
VALUE hash)
2268 for (i = 0; i < argc; ) {
2269 VALUE key = argv[i++];
2270 VALUE val = argv[i++];
2271 st_insert_single(tab, hash, key, val);
2281rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2283 st_index_t n, size = argc / 2;
2284 st_table *tab = RHASH_ST_TABLE(hash);
2286 tab = RHASH_TBL_RAW(hash);
2287 n = tab->entries_bound + size;
2288 st_expand_table(tab, n);
2289 if (UNLIKELY(tab->num_entries))
2290 st_insert_generic(tab, argc, argv, hash);
2292 st_insert_single(tab, hash, argv[0], argv[1]);
2293 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2294 st_insert_linear(tab, argc, argv, hash);
2296 st_insert_generic(tab, argc, argv, hash);
2301rb_st_nth_key(st_table *tab, st_index_t index)
2303 if (LIKELY(tab->entries_start == 0 &&
2304 tab->num_entries == tab->entries_bound &&
2305 index < tab->num_entries)) {
2306 return tab->entries[index].key;
2309 rb_bug(
"unreachable");
2314rb_st_compact_table(st_table *tab)
2316 st_index_t num = tab->num_entries;
2317 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2319 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2320 rebuild_table_with(new_tab, tab);
#define Qundef
Old name of RUBY_Qundef.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_cString
String class.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
VALUE type(ANYARGS)
ANYARGS-ed function type.
#define _(args)
This was a transition path from K&R to ANSI.
uintptr_t VALUE
Type that represents a Ruby object.