107#elif defined RUBY_EXPORT
109#include "internal/bits.h"
110#include "internal/hash.h"
111#include "internal/sanitizers.h"
112#include "internal/set_table.h"
113#include "internal/st.h"
114#include "ruby_assert.h"
124#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
125#define EXPECT(expr, val) __builtin_expect(expr, val)
126#define ATTRIBUTE_UNUSED __attribute__((unused))
128#define PREFETCH(addr, write_p)
129#define EXPECT(expr, val) (expr)
130#define ATTRIBUTE_UNUSED
134typedef st_index_t st_hash_t;
142#define type_numhash st_hashtype_num
148static int st_strcmp(st_data_t, st_data_t);
149static st_index_t strhash(st_data_t);
155static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
156static st_index_t strcasehash(st_data_t);
158 st_locale_insensitive_strcasecmp_i,
165#define ST_INIT_VAL 0xafafafafafafafaf
166#define ST_INIT_VAL_BYTE 0xafa
173#define malloc ruby_xmalloc
174#define calloc ruby_xcalloc
175#define realloc ruby_xrealloc
176#define free ruby_xfree
179#define EQUAL(tab,x,y) ((x) == (y) || (*(tab)->type->compare)((x),(y)) == 0)
180#define PTR_EQUAL(tab, ptr, hash_val, key_) \
181 ((ptr)->hash == (hash_val) && EQUAL((tab), (key_), (ptr)->key))
185#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
187 unsigned int _old_rebuilds_num = (tab)->rebuilds_num; \
188 res = PTR_EQUAL(tab, ptr, hash_val, key); \
189 rebuilt_p = _old_rebuilds_num != (tab)->rebuilds_num; \
195 unsigned char entry_power;
199 unsigned char bin_power;
201 unsigned char size_ind;
204 st_index_t bins_words;
208#if SIZEOF_ST_INDEX_T == 8
227 {16, 17, 2, 0x10000},
228 {17, 18, 2, 0x20000},
229 {18, 19, 2, 0x40000},
230 {19, 20, 2, 0x80000},
231 {20, 21, 2, 0x100000},
232 {21, 22, 2, 0x200000},
233 {22, 23, 2, 0x400000},
234 {23, 24, 2, 0x800000},
235 {24, 25, 2, 0x1000000},
236 {25, 26, 2, 0x2000000},
237 {26, 27, 2, 0x4000000},
238 {27, 28, 2, 0x8000000},
239 {28, 29, 2, 0x10000000},
240 {29, 30, 2, 0x20000000},
241 {30, 31, 2, 0x40000000},
242 {31, 32, 2, 0x80000000},
243 {32, 33, 3, 0x200000000},
244 {33, 34, 3, 0x400000000},
245 {34, 35, 3, 0x800000000},
246 {35, 36, 3, 0x1000000000},
247 {36, 37, 3, 0x2000000000},
248 {37, 38, 3, 0x4000000000},
249 {38, 39, 3, 0x8000000000},
250 {39, 40, 3, 0x10000000000},
251 {40, 41, 3, 0x20000000000},
252 {41, 42, 3, 0x40000000000},
253 {42, 43, 3, 0x80000000000},
254 {43, 44, 3, 0x100000000000},
255 {44, 45, 3, 0x200000000000},
256 {45, 46, 3, 0x400000000000},
257 {46, 47, 3, 0x800000000000},
258 {47, 48, 3, 0x1000000000000},
259 {48, 49, 3, 0x2000000000000},
260 {49, 50, 3, 0x4000000000000},
261 {50, 51, 3, 0x8000000000000},
262 {51, 52, 3, 0x10000000000000},
263 {52, 53, 3, 0x20000000000000},
264 {53, 54, 3, 0x40000000000000},
265 {54, 55, 3, 0x80000000000000},
266 {55, 56, 3, 0x100000000000000},
267 {56, 57, 3, 0x200000000000000},
268 {57, 58, 3, 0x400000000000000},
269 {58, 59, 3, 0x800000000000000},
270 {59, 60, 3, 0x1000000000000000},
271 {60, 61, 3, 0x2000000000000000},
272 {61, 62, 3, 0x4000000000000000},
273 {62, 63, 3, 0x8000000000000000},
296 {16, 17, 2, 0x20000},
297 {17, 18, 2, 0x40000},
298 {18, 19, 2, 0x80000},
299 {19, 20, 2, 0x100000},
300 {20, 21, 2, 0x200000},
301 {21, 22, 2, 0x400000},
302 {22, 23, 2, 0x800000},
303 {23, 24, 2, 0x1000000},
304 {24, 25, 2, 0x2000000},
305 {25, 26, 2, 0x4000000},
306 {26, 27, 2, 0x8000000},
307 {27, 28, 2, 0x10000000},
308 {28, 29, 2, 0x20000000},
309 {29, 30, 2, 0x40000000},
310 {30, 31, 2, 0x80000000},
316#define RESERVED_HASH_VAL (~(st_hash_t) 0)
317#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
319static inline st_hash_t
320normalize_hash_value(st_hash_t hash)
324 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
328static inline st_hash_t
329do_hash(st_data_t key,
st_table *tab)
331 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
332 return normalize_hash_value(hash);
336#define MINIMAL_POWER2 2
338#if MINIMAL_POWER2 < 2
339#error "MINIMAL_POWER2 should be >= 2"
344#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
348get_power2(st_index_t size)
350 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
352 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
363static inline st_index_t
364get_bin(st_index_t *bins,
int s, st_index_t n)
366 return (s == 0 ? ((
unsigned char *) bins)[n]
367 : s == 1 ? ((unsigned short *) bins)[n]
368 : s == 2 ? ((unsigned int *) bins)[n]
369 : ((st_index_t *) bins)[n]);
375set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
377 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
378 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
379 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
380 else ((st_index_t *) bins)[n] = v;
393#define MARK_BIN_EMPTY(tab, i) (set_bin((tab)->bins, get_size_ind(tab), i, EMPTY_BIN))
397#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
398#define UNDEFINED_BIN_IND (~(st_index_t) 0)
402#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
403#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
408#define MARK_BIN_DELETED(tab, i) \
410 set_bin((tab)->bins, get_size_ind(tab), i, DELETED_BIN); \
415#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
416#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
417#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
421#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
422#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
423#define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin((tab)->bins, get_size_ind(tab), i)))
427#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
428#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
431static inline unsigned int
434 return tab->size_ind;
438static inline st_index_t
441 return ((st_index_t) 1)<<tab->bin_power;
445static inline st_index_t
448 return get_bins_num(tab) - 1;
453static inline st_index_t
454hash_bin(st_hash_t hash_value,
st_table *tab)
456 return hash_value & bins_mask(tab);
460static inline st_index_t
461get_allocated_entries(
const st_table *tab)
463 return ((st_index_t) 1)<<tab->entry_power;
467static inline st_index_t
470 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
477 memset(tab->bins, 0, bins_size(tab));
484 tab->num_entries = 0;
485 tab->entries_start = tab->entries_bound = 0;
486 if (tab->bins != NULL)
487 initialize_bins(tab);
495 int all, total, num, str, strcase;
500static int init_st = 0;
507 char fname[10+
sizeof(long)*3];
509 if (!collision.total)
return;
510 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
513 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
514 ((
double)collision.all / (collision.total)) * 100);
515 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
521st_init_existing_table_with_size(
st_table *tab,
const struct st_hash_type *type, st_index_t size)
528 const char *e = getenv(
"ST_HASH_LOG");
529 if (!e || !*e) init_st = 1;
538 n = get_power2(size);
545 tab->entry_power = n;
546 tab->bin_power = features[n].bin_power;
547 tab->size_ind = features[n].size_ind;
548 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
551 tab->bins = (st_index_t *) malloc(bins_size(tab));
553 if (tab->bins == NULL) {
559 tab->entries = (
st_table_entry *) malloc(get_allocated_entries(tab)
562 if (tab->entries == NULL) {
568 tab->rebuilds_num = 0;
576st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
585 st_init_existing_table_with_size(tab, type, size);
587 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
597st_table_size(
const struct st_table *tbl)
599 return tbl->num_entries;
607 return st_init_table_with_size(type, 0);
613st_init_numtable(
void)
615 return st_init_table(&type_numhash);
620st_init_numtable_with_size(st_index_t size)
622 return st_init_table_with_size(&type_numhash, size);
628st_init_strtable(
void)
630 return st_init_table(&type_strhash);
635st_init_strtable_with_size(st_index_t size)
637 return st_init_table_with_size(&type_strhash, size);
643st_init_strcasetable(
void)
645 return st_init_table(&type_strcasehash);
651st_init_strcasetable_with_size(st_index_t size)
653 return st_init_table_with_size(&type_strcasehash, size);
678 + (tab->bins == NULL ? 0 : bins_size(tab))
683find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
686find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
689find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
692find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
693 st_data_t key, st_index_t *bin_ind);
700 if (type == &type_numhash) {
703 else if (type == &type_strhash) {
706 else if (type == &type_strcasehash) {
711#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
712#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
713#define collision_check 0
722#define REBUILD_THRESHOLD 4
724#if REBUILD_THRESHOLD < 2
725#error "REBUILD_THRESHOLD should be >= 2"
728static void rebuild_table_with(
st_table *
const new_tab,
st_table *
const tab);
729static void rebuild_move_table(
st_table *
const new_tab,
st_table *
const tab);
730static void rebuild_cleanup(
st_table *
const tab);
739 if ((2 * tab->num_entries <= get_allocated_entries(tab)
740 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
741 || tab->num_entries < (1 << MINIMAL_POWER2)) {
743 tab->num_entries = 0;
744 if (tab->bins != NULL)
745 initialize_bins(tab);
746 rebuild_table_with(tab, tab);
753 new_tab = st_init_table_with_size(tab->type,
754 2 * tab->num_entries - 1);
755 rebuild_table_with(new_tab, tab);
756 rebuild_move_table(new_tab, tab);
758 rebuild_cleanup(tab);
765 unsigned int size_ind;
771 new_entries = new_tab->entries;
774 bins = new_tab->bins;
775 size_ind = get_size_ind(new_tab);
776 st_index_t bound = tab->entries_bound;
779 for (i = tab->entries_start; i < bound; i++) {
780 curr_entry_ptr = &entries[i];
781 PREFETCH(entries + i + 1, 0);
782 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
784 if (&new_entries[ni] != curr_entry_ptr)
785 new_entries[ni] = *curr_entry_ptr;
786 if (EXPECT(bins != NULL, 1)) {
787 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
788 curr_entry_ptr->key);
789 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
791 new_tab->num_entries++;
795 assert(new_tab->num_entries == tab->num_entries);
801 tab->entry_power = new_tab->entry_power;
802 tab->bin_power = new_tab->bin_power;
803 tab->size_ind = new_tab->size_ind;
805 tab->bins = new_tab->bins;
807 tab->entries = new_tab->entries;
814 tab->entries_start = 0;
815 tab->entries_bound = tab->num_entries;
831static inline st_index_t
832secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perturb)
835 ind = (ind << 2) + ind + *perturb + 1;
836 return hash_bin(ind, tab);
843static inline st_index_t
844find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
850 bound = tab->entries_bound;
851 entries = tab->entries;
852 for (i = tab->entries_start; i < bound; i++) {
853 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
854 if (EXPECT(rebuilt_p, 0))
855 return REBUILT_TABLE_ENTRY_IND;
859 return UNDEFINED_ENTRY_IND;
871find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
875#ifdef QUADRATIC_PROBE
883 ind = hash_bin(hash_value, tab);
884#ifdef QUADRATIC_PROBE
887 perturb = hash_value;
891 bin = get_bin(tab->bins, get_size_ind(tab), ind);
892 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
893 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
894 if (EXPECT(rebuilt_p, 0))
895 return REBUILT_TABLE_ENTRY_IND;
899 else if (EMPTY_BIN_P(bin))
900 return UNDEFINED_ENTRY_IND;
901#ifdef QUADRATIC_PROBE
902 ind = hash_bin(ind + d, tab);
905 ind = secondary_hash(ind, tab, &perturb);
917find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
921#ifdef QUADRATIC_PROBE
929 ind = hash_bin(hash_value, tab);
930#ifdef QUADRATIC_PROBE
933 perturb = hash_value;
937 bin = get_bin(tab->bins, get_size_ind(tab), ind);
938 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
939 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
940 if (EXPECT(rebuilt_p, 0))
941 return REBUILT_TABLE_BIN_IND;
945 else if (EMPTY_BIN_P(bin))
946 return UNDEFINED_BIN_IND;
947#ifdef QUADRATIC_PROBE
948 ind = hash_bin(ind + d, tab);
951 ind = secondary_hash(ind, tab, &perturb);
962find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
965#ifdef QUADRATIC_PROBE
972 ind = hash_bin(hash_value, tab);
973#ifdef QUADRATIC_PROBE
976 perturb = hash_value;
980 bin = get_bin(tab->bins, get_size_ind(tab), ind);
981 if (EMPTY_OR_DELETED_BIN_P(bin))
983#ifdef QUADRATIC_PROBE
984 ind = hash_bin(ind + d, tab);
987 ind = secondary_hash(ind, tab, &perturb);
1003find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
1004 st_data_t key, st_index_t *bin_ind)
1006 int eq_p, rebuilt_p;
1008 st_hash_t curr_hash_value = *hash_value;
1009#ifdef QUADRATIC_PROBE
1014 st_index_t entry_index;
1015 st_index_t first_deleted_bin_ind;
1018 ind = hash_bin(curr_hash_value, tab);
1019#ifdef QUADRATIC_PROBE
1022 perturb = curr_hash_value;
1025 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1026 entries = tab->entries;
1028 entry_index = get_bin(tab->bins, get_size_ind(tab), ind);
1029 if (EMPTY_BIN_P(entry_index)) {
1031 entry_index = UNDEFINED_ENTRY_IND;
1032 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1034 ind = first_deleted_bin_ind;
1035 MARK_BIN_EMPTY(tab, ind);
1039 else if (! DELETED_BIN_P(entry_index)) {
1040 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1041 if (EXPECT(rebuilt_p, 0))
1042 return REBUILT_TABLE_ENTRY_IND;
1046 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1047 first_deleted_bin_ind = ind;
1048#ifdef QUADRATIC_PROBE
1049 ind = hash_bin(ind + d, tab);
1052 ind = secondary_hash(ind, tab, &perturb);
1063st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1066 st_hash_t hash = do_hash(key, tab);
1069 if (tab->bins == NULL) {
1070 bin = find_entry(tab, hash, key);
1071 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1073 if (bin == UNDEFINED_ENTRY_IND)
1077 bin = find_table_entry_ind(tab, hash, key);
1078 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1080 if (bin == UNDEFINED_ENTRY_IND)
1085 *value = tab->entries[bin].record;
1092st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1095 st_hash_t hash = do_hash(key, tab);
1098 if (tab->bins == NULL) {
1099 bin = find_entry(tab, hash, key);
1100 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1102 if (bin == UNDEFINED_ENTRY_IND)
1106 bin = find_table_entry_ind(tab, hash, key);
1107 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1109 if (bin == UNDEFINED_ENTRY_IND)
1114 *result = tab->entries[bin].key;
1120rebuild_table_if_necessary (
st_table *tab)
1122 st_index_t bound = tab->entries_bound;
1124 if (bound == get_allocated_entries(tab))
1132st_insert(
st_table *tab, st_data_t key, st_data_t value)
1137 st_hash_t hash_value;
1141 hash_value = do_hash(key, tab);
1143 rebuild_table_if_necessary(tab);
1144 if (tab->bins == NULL) {
1145 bin = find_entry(tab, hash_value, key);
1146 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1148 new_p = bin == UNDEFINED_ENTRY_IND;
1151 bin_ind = UNDEFINED_BIN_IND;
1154 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1156 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1158 new_p = bin == UNDEFINED_ENTRY_IND;
1162 ind = tab->entries_bound++;
1163 entry = &tab->entries[ind];
1164 entry->hash = hash_value;
1166 entry->record = value;
1167 if (bin_ind != UNDEFINED_BIN_IND)
1168 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1171 tab->entries[bin].record = value;
1178st_add_direct_with_hash(
st_table *tab,
1179 st_data_t key, st_data_t value, st_hash_t hash)
1185 assert(hash != RESERVED_HASH_VAL);
1187 rebuild_table_if_necessary(tab);
1188 ind = tab->entries_bound++;
1189 entry = &tab->entries[ind];
1192 entry->record = value;
1194 if (tab->bins != NULL) {
1195 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1196 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1201rb_st_add_direct_with_hash(
st_table *tab,
1202 st_data_t key, st_data_t value, st_hash_t hash)
1204 st_add_direct_with_hash(tab, key, value, normalize_hash_value(hash));
1210st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1212 st_hash_t hash_value;
1214 hash_value = do_hash(key, tab);
1215 st_add_direct_with_hash(tab, key, value, hash_value);
1222st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1223 st_data_t (*func)(st_data_t))
1228 st_hash_t hash_value;
1232 hash_value = do_hash(key, tab);
1234 rebuild_table_if_necessary (tab);
1235 if (tab->bins == NULL) {
1236 bin = find_entry(tab, hash_value, key);
1237 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1239 new_p = bin == UNDEFINED_ENTRY_IND;
1242 bin_ind = UNDEFINED_BIN_IND;
1245 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1247 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1249 new_p = bin == UNDEFINED_ENTRY_IND;
1254 ind = tab->entries_bound++;
1255 entry = &tab->entries[ind];
1256 entry->hash = hash_value;
1258 entry->record = value;
1259 if (bin_ind != UNDEFINED_BIN_IND)
1260 set_bin(tab->bins, get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1263 tab->entries[bin].record = value;
1271 *new_tab = *old_tab;
1272 if (old_tab->bins == NULL)
1273 new_tab->bins = NULL;
1275 new_tab->bins = (st_index_t *) malloc(bins_size(old_tab));
1277 if (new_tab->bins == NULL) {
1282 new_tab->entries = (
st_table_entry *) malloc(get_allocated_entries(old_tab)
1285 if (new_tab->entries == NULL) {
1290 get_allocated_entries(old_tab));
1291 if (old_tab->bins != NULL)
1292 MEMCPY(new_tab->bins, old_tab->bins,
char, bins_size(old_tab));
1305 if (new_tab == NULL)
1309 if (st_replace(new_tab, old_tab) == NULL) {
1310 st_free_table(new_tab);
1320update_range_for_deleted(
st_table *tab, st_index_t n)
1324 if (tab->entries_start == n) {
1325 st_index_t start = n + 1;
1326 st_index_t bound = tab->entries_bound;
1328 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1329 tab->entries_start = start;
1338st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1345 hash = do_hash(*key, tab);
1347 if (tab->bins == NULL) {
1348 bin = find_entry(tab, hash, *key);
1349 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1351 if (bin == UNDEFINED_ENTRY_IND) {
1352 if (value != 0) *value = 0;
1357 bin_ind = find_table_bin_ind(tab, hash, *key);
1358 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1360 if (bin_ind == UNDEFINED_BIN_IND) {
1361 if (value != 0) *value = 0;
1364 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1365 MARK_BIN_DELETED(tab, bin_ind);
1367 entry = &tab->entries[bin];
1369 if (value != 0) *value = entry->record;
1370 MARK_ENTRY_DELETED(entry);
1372 update_range_for_deleted(tab, bin);
1377st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1379 return st_general_delete(tab, key, value);
1388st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1389 st_data_t never ATTRIBUTE_UNUSED)
1391 return st_general_delete(tab, key, value);
1399st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1401 st_index_t i, bound;
1406 entries = tab->entries;
1407 bound = tab->entries_bound;
1408 for (i = tab->entries_start; i < bound; i++) {
1409 curr_entry_ptr = &entries[i];
1410 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1411 st_hash_t entry_hash = curr_entry_ptr->hash;
1412 st_data_t entry_key = curr_entry_ptr->key;
1414 if (value != 0) *value = curr_entry_ptr->record;
1417 if (tab->bins == NULL) {
1418 bin = find_entry(tab, entry_hash, entry_key);
1419 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1420 entries = tab->entries;
1423 curr_entry_ptr = &entries[bin];
1426 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1427 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1428 entries = tab->entries;
1431 curr_entry_ptr = &entries[get_bin(tab->bins, get_size_ind(tab), bin_ind)
1433 MARK_BIN_DELETED(tab, bin_ind);
1435 MARK_ENTRY_DELETED(curr_entry_ptr);
1437 update_range_for_deleted(tab, i);
1441 if (value != 0) *value = 0;
1447st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1448 st_data_t never ATTRIBUTE_UNUSED)
1462st_update(
st_table *tab, st_data_t key,
1463 st_update_callback_func *func, st_data_t arg)
1469 st_data_t value = 0, old_key;
1470 int retval, existing;
1471 st_hash_t hash = do_hash(key, tab);
1474 entries = tab->entries;
1475 if (tab->bins == NULL) {
1476 bin = find_entry(tab, hash, key);
1477 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1479 existing = bin != UNDEFINED_ENTRY_IND;
1480 entry = &entries[bin];
1481 bin_ind = UNDEFINED_BIN_IND;
1484 bin_ind = find_table_bin_ind(tab, hash, key);
1485 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1487 existing = bin_ind != UNDEFINED_BIN_IND;
1489 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1490 entry = &entries[bin];
1495 value = entry->record;
1498 retval = (*func)(&key, &value, arg, existing);
1502 st_add_direct_with_hash(tab, key, value, hash);
1505 if (old_key != key) {
1508 entry->record = value;
1512 if (bin_ind != UNDEFINED_BIN_IND)
1513 MARK_BIN_DELETED(tab, bin_ind);
1514 MARK_ENTRY_DELETED(entry);
1516 update_range_for_deleted(tab, bin);
1532st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1538 enum st_retval retval;
1539 st_index_t i, rebuilds_num;
1542 int error_p, packed_p = tab->bins == NULL;
1544 entries = tab->entries;
1547 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1548 curr_entry_ptr = &entries[i];
1549 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1551 key = curr_entry_ptr->key;
1552 rebuilds_num = tab->rebuilds_num;
1553 hash = curr_entry_ptr->hash;
1554 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1556 if (retval == ST_REPLACE && replace) {
1558 value = curr_entry_ptr->record;
1559 retval = (*replace)(&key, &value, arg, TRUE);
1560 curr_entry_ptr->key = key;
1561 curr_entry_ptr->record = value;
1564 if (rebuilds_num != tab->rebuilds_num) {
1566 entries = tab->entries;
1567 packed_p = tab->bins == NULL;
1569 i = find_entry(tab, hash, key);
1570 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1572 error_p = i == UNDEFINED_ENTRY_IND;
1575 i = find_table_entry_ind(tab, hash, key);
1576 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1578 error_p = i == UNDEFINED_ENTRY_IND;
1581 if (error_p && check_p) {
1583 retval = (*func)(0, 0, arg, 1);
1586 curr_entry_ptr = &entries[i];
1599 st_data_t key = curr_entry_ptr->key;
1603 bin = find_entry(tab, hash, key);
1604 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1606 if (bin == UNDEFINED_ENTRY_IND)
1610 bin_ind = find_table_bin_ind(tab, hash, key);
1611 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1613 if (bin_ind == UNDEFINED_BIN_IND)
1615 bin = get_bin(tab->bins, get_size_ind(tab), bin_ind) - ENTRY_BASE;
1616 MARK_BIN_DELETED(tab, bin_ind);
1618 curr_entry_ptr = &entries[bin];
1619 MARK_ENTRY_DELETED(curr_entry_ptr);
1621 update_range_for_deleted(tab, bin);
1630st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1632 return st_general_foreach(tab, func, replace, arg, TRUE);
1636 st_foreach_callback_func *func;
1641apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1643 const struct functor *f = (
void *)d;
1644 return f->func(k, v, f->arg);
1648st_foreach(
st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1650 const struct functor f = { func, arg };
1651 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1656st_foreach_check(
st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1657 st_data_t never ATTRIBUTE_UNUSED)
1659 return st_general_foreach(tab, func, 0, arg, TRUE);
1664static inline st_index_t
1665st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1667 st_index_t i, bound;
1668 st_data_t key, *keys_start, *keys_end;
1671 bound = tab->entries_bound;
1673 keys_end = keys + size;
1674 for (i = tab->entries_start; i < bound; i++) {
1675 if (keys == keys_end)
1677 curr_entry_ptr = &entries[i];
1678 key = curr_entry_ptr->key;
1679 if (! DELETED_ENTRY_P(curr_entry_ptr))
1683 return keys - keys_start;
1687st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1689 return st_general_keys(tab, keys, size);
1694st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1695 st_data_t never ATTRIBUTE_UNUSED)
1697 return st_general_keys(tab, keys, size);
1702static inline st_index_t
1703st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1705 st_index_t i, bound;
1706 st_data_t *values_start, *values_end;
1709 values_start = values;
1710 values_end = values + size;
1711 bound = tab->entries_bound;
1712 for (i = tab->entries_start; i < bound; i++) {
1713 if (values == values_end)
1715 curr_entry_ptr = &entries[i];
1716 if (! DELETED_ENTRY_P(curr_entry_ptr))
1717 *values++ = curr_entry_ptr->record;
1720 return values - values_start;
1724st_values(
st_table *tab, st_data_t *values, st_index_t size)
1726 return st_general_values(tab, values, size);
1731st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1732 st_data_t never ATTRIBUTE_UNUSED)
1734 return st_general_values(tab, values, size);
1737#define FNV1_32A_INIT 0x811c9dc5
1742#define FNV_32_PRIME 0x01000193
1745#ifndef UNALIGNED_WORD_ACCESS
1746# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1747 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1748 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1749 defined(__mc68020__)
1750# define UNALIGNED_WORD_ACCESS 1
1753#ifndef UNALIGNED_WORD_ACCESS
1754# define UNALIGNED_WORD_ACCESS 0
1760#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1761#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1763#if ST_INDEX_BITS <= 32
1764#define C1 (st_index_t)0xcc9e2d51
1765#define C2 (st_index_t)0x1b873593
1767#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1768#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1770NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1771NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1772NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1774static inline st_index_t
1775murmur_step(st_index_t h, st_index_t k)
1777#if ST_INDEX_BITS <= 32
1793static inline st_index_t
1794murmur_finish(st_index_t h)
1796#if ST_INDEX_BITS <= 32
1800 const st_index_t c1 = 0x85ebca6b;
1801 const st_index_t c2 = 0xc2b2ae35;
1807 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1808 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1810#if ST_INDEX_BITS > 64
1827st_hash(
const void *ptr,
size_t len, st_index_t h)
1829 const char *data = ptr;
1833#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1834#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1835#if SIZEOF_ST_INDEX_T > 4
1836#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1837#if SIZEOF_ST_INDEX_T > 8
1838#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1839 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1840#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1842#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
1844#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
1847 if (
len >=
sizeof(st_index_t)) {
1848#if !UNALIGNED_WORD_ACCESS
1849 int align = (int)((st_data_t)data %
sizeof(st_index_t));
1855#ifdef WORDS_BIGENDIAN
1856# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1857 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
1859# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
1860 t |= data_at(n) << CHAR_BIT*(n)
1866#ifdef WORDS_BIGENDIAN
1867 t >>= (CHAR_BIT * align) - CHAR_BIT;
1869 t <<= (CHAR_BIT * align);
1872 data +=
sizeof(st_index_t)-align;
1873 len -=
sizeof(st_index_t)-align;
1875 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
1876 sr = CHAR_BIT * align;
1878 while (
len >=
sizeof(st_index_t)) {
1879 d = *(st_index_t *)data;
1880#ifdef WORDS_BIGENDIAN
1881 t = (t << sr) | (d >> sl);
1883 t = (t >> sr) | (d << sl);
1885 h = murmur_step(h, t);
1887 data +=
sizeof(st_index_t);
1888 len -=
sizeof(st_index_t);
1891 pack =
len < (size_t)align ? (
int)
len : align;
1894#ifdef WORDS_BIGENDIAN
1895# define UNALIGNED_ADD(n) case (n) + 1: \
1896 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1898# define UNALIGNED_ADD(n) case (n) + 1: \
1899 d |= data_at(n) << CHAR_BIT*(n)
1904#ifdef WORDS_BIGENDIAN
1905 t = (t << sr) | (d >> sl);
1907 t = (t >> sr) | (d << sl);
1910 if (
len < (
size_t)align)
goto skip_tail;
1912 h = murmur_step(h, t);
1918#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
1919#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
1921#define aligned_data data
1925 h = murmur_step(h, *(st_index_t *)aligned_data);
1926 data +=
sizeof(st_index_t);
1927 len -=
sizeof(st_index_t);
1928 }
while (
len >=
sizeof(st_index_t));
1934#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
1936#if SIZEOF_ST_INDEX_T > 4
1937 case 7: t |= data_at(6) << 48;
1938 case 6: t |= data_at(5) << 40;
1939 case 5: t |= data_at(4) << 32;
1941 t |= (st_index_t)*(uint32_t*)aligned_data;
1945 case 3: t |= data_at(2) << 16;
1946 case 2: t |= data_at(1) << 8;
1947 case 1: t |= data_at(0);
1949#ifdef WORDS_BIGENDIAN
1950# define UNALIGNED_ADD(n) case (n) + 1: \
1951 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
1953# define UNALIGNED_ADD(n) case (n) + 1: \
1954 t |= data_at(n) << CHAR_BIT*(n)
1962 h ^= t; h -= ROTL(t, 7);
1968 return murmur_finish(h);
1972st_hash_uint32(st_index_t h, uint32_t i)
1974 return murmur_step(h, i);
1977NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
1979st_hash_uint(st_index_t h, st_index_t i)
1984#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
1985 h = murmur_step(h, i >> 8*8);
1987 h = murmur_step(h, i);
1992st_hash_end(st_index_t h)
1994 h = murmur_finish(h);
2000rb_st_hash_start(st_index_t h)
2006strhash(st_data_t arg)
2008 register const char *
string = (
const char *)arg;
2009 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2013st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
2020 if (c1 ==
'\0' || c2 ==
'\0') {
2021 if (c1 !=
'\0')
return 1;
2022 if (c2 !=
'\0')
return -1;
2025 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2026 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2037st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2042 for (i = 0; i < n; i++) {
2045 if (c1 ==
'\0' || c2 ==
'\0') {
2046 if (c1 !=
'\0')
return 1;
2047 if (c2 !=
'\0')
return -1;
2050 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2051 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2063st_strcmp(st_data_t lhs, st_data_t rhs)
2065 const char *s1 = (
char *)lhs;
2066 const char *s2 = (
char *)rhs;
2067 return strcmp(s1, s2);
2071st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2073 const char *s1 = (
char *)lhs;
2074 const char *s2 = (
char *)rhs;
2075 return st_locale_insensitive_strcasecmp(s1, s2);
2078NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2080strcasehash(st_data_t arg)
2082 register const char *
string = (
const char *)arg;
2083 register st_index_t hval = FNV1_32A_INIT;
2089 unsigned int c = (
unsigned char)*
string++;
2090 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2094 hval *= FNV_32_PRIME;
2100st_numcmp(st_data_t x, st_data_t y)
2106st_numhash(st_data_t n)
2108 enum {s1 = 11, s2 = 3};
2109 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2117st_expand_table(
st_table *tab, st_index_t siz)
2122 if (siz <= get_allocated_entries(tab))
2125 tmp = st_init_table_with_size(tab->type, siz);
2126 n = get_allocated_entries(tab);
2131 tab->entry_power = tmp->entry_power;
2132 tab->bin_power = tmp->bin_power;
2133 tab->size_ind = tmp->size_ind;
2134 tab->entries = tmp->entries;
2136 tab->rebuilds_num++;
2145 int eq_p, rebuilt_p;
2152 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2153 p = &tab->entries[i];
2154 if (DELETED_ENTRY_P(p))
2156 for (j = i + 1; j < tab->entries_bound; j++) {
2157 q = &tab->entries[j];
2158 if (DELETED_ENTRY_P(q))
2160 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2161 if (EXPECT(rebuilt_p, 0))
2165 MARK_ENTRY_DELETED(q);
2167 update_range_for_deleted(tab, j);
2179 int eq_p, rebuilt_p;
2181 st_index_t
const n = bins_size(tab);
2182 unsigned int const size_ind = get_size_ind(tab);
2183 st_index_t *bins = realloc(tab->bins, n);
2185 initialize_bins(tab);
2186 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2189#ifdef QUADRATIC_PROBE
2192 st_index_t perturb = p->hash;
2195 if (DELETED_ENTRY_P(p))
2198 ind = hash_bin(p->hash, tab);
2200 st_index_t bin = get_bin(bins, size_ind, ind);
2201 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2203 set_bin(bins, size_ind, ind, i + ENTRY_BASE);
2208 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2209 if (EXPECT(rebuilt_p, 0))
2213 q->record = p->record;
2214 MARK_ENTRY_DELETED(p);
2216 update_range_for_deleted(tab, bin);
2221#ifdef QUADRATIC_PROBE
2222 ind = hash_bin(ind + d, tab);
2225 ind = secondary_hash(ind, tab, &perturb);
2243 if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2244 rebuilt_p = st_rehash_linear(tab);
2246 rebuilt_p = st_rehash_indexed(tab);
2247 }
while (rebuilt_p);
2251st_stringify(
VALUE key)
2254 rb_hash_key_str(key) : key;
2260 st_data_t k = st_stringify(key);
2262 e.hash = do_hash(k, tab);
2266 tab->entries[tab->entries_bound++] = e;
2277 for (i = 0; i < argc; ) {
2278 st_data_t k = st_stringify(argv[i++]);
2279 st_data_t v = argv[i++];
2280 st_insert(tab, k, v);
2292 for (i = 0; i < argc; ) {
2293 VALUE key = argv[i++];
2294 VALUE val = argv[i++];
2295 st_insert_single(tab, hash, key, val);
2305rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2307 st_index_t n, size = argc / 2;
2308 st_table *tab = RHASH_ST_TABLE(hash);
2310 tab = RHASH_TBL_RAW(hash);
2311 n = tab->entries_bound + size;
2312 st_expand_table(tab, n);
2313 if (UNLIKELY(tab->num_entries))
2314 st_insert_generic(tab, argc, argv, hash);
2316 st_insert_single(tab, hash, argv[0], argv[1]);
2317 else if (tab->bin_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2318 st_insert_linear(tab, argc, argv, hash);
2320 st_insert_generic(tab, argc, argv, hash);
2326 st_index_t num = tab->num_entries;
2327 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2329 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2330 rebuild_table_with(new_tab, tab);
2331 rebuild_move_table(new_tab, tab);
2332 rebuild_cleanup(tab);
2340struct set_table_entry {
2346static inline st_hash_t
2347set_do_hash(st_data_t key,
set_table *tab)
2349 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
2350 return normalize_hash_value(hash);
2354static inline unsigned int
2357 return tab->size_ind;
2361static inline st_index_t
2364 return ((st_index_t) 1)<<tab->bin_power;
2368static inline st_index_t
2371 return set_get_bins_num(tab) - 1;
2376static inline st_index_t
2377set_hash_bin(st_hash_t hash_value,
set_table *tab)
2379 return hash_value & set_bins_mask(tab);
2383static inline st_index_t
2384set_get_allocated_entries(
const set_table *tab)
2386 return ((st_index_t) 1)<<tab->entry_power;
2390static inline st_index_t
2393 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
2400 memset(tab->bins, 0, set_bins_size(tab));
2407 tab->num_entries = 0;
2408 tab->entries_start = tab->entries_bound = 0;
2409 if (tab->bins != NULL)
2410 set_initialize_bins(tab);
2421 const char *e = getenv(
"ST_HASH_LOG");
2422 if (!e || !*e) init_st = 1;
2431 n = get_power2(size);
2434 tab->entry_power = n;
2435 tab->bin_power = features[n].bin_power;
2436 tab->size_ind = features[n].size_ind;
2437 if (n <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2440 tab->bins = (st_index_t *) malloc(set_bins_size(tab));
2442 tab->entries = (set_table_entry *) malloc(set_get_allocated_entries(tab)
2443 *
sizeof(set_table_entry));
2444 set_make_tab_empty(tab);
2445 tab->rebuilds_num = 0;
2455 if (tab == NULL) tab = malloc(
sizeof(
set_table));
2457 set_init_existing_table_with_size(tab, type, size);
2463set_init_numtable(
void)
2465 return set_init_table_with_size(NULL, &type_numhash, 0);
2469set_table_size(
const struct set_table *tbl)
2471 return tbl->num_entries;
2478 set_make_tab_empty(tab);
2479 tab->rebuilds_num++;
2497 + (tab->bins == NULL ? 0 : set_bins_size(tab))
2498 + set_get_allocated_entries(tab) * sizeof(set_table_entry));
2502set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2505set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2508set_find_table_bin_ind_direct(
set_table *table, st_hash_t hash_value, st_data_t key);
2511set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2512 st_data_t key, st_index_t *bin_ind);
2516static void set_rebuild_cleanup(
set_table *
const tab);
2525 if ((2 * tab->num_entries <= set_get_allocated_entries(tab)
2526 && REBUILD_THRESHOLD * tab->num_entries > set_get_allocated_entries(tab))
2527 || tab->num_entries < (1 << MINIMAL_POWER2)) {
2529 tab->num_entries = 0;
2530 if (tab->bins != NULL)
2531 set_initialize_bins(tab);
2532 set_rebuild_table_with(tab, tab);
2539 new_tab = set_init_table_with_size(NULL, tab->type,
2540 2 * tab->num_entries - 1);
2541 set_rebuild_table_with(new_tab, tab);
2542 set_rebuild_move_table(new_tab, tab);
2544 set_rebuild_cleanup(tab);
2551 unsigned int size_ind;
2552 set_table_entry *new_entries;
2553 set_table_entry *curr_entry_ptr;
2557 new_entries = new_tab->entries;
2560 bins = new_tab->bins;
2561 size_ind = set_get_size_ind(new_tab);
2562 st_index_t bound = tab->entries_bound;
2563 set_table_entry *entries = tab->entries;
2565 for (i = tab->entries_start; i < bound; i++) {
2566 curr_entry_ptr = &entries[i];
2567 PREFETCH(entries + i + 1, 0);
2568 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
2570 if (&new_entries[ni] != curr_entry_ptr)
2571 new_entries[ni] = *curr_entry_ptr;
2572 if (EXPECT(bins != NULL, 1)) {
2573 bin_ind = set_find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
2574 curr_entry_ptr->key);
2575 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
2577 new_tab->num_entries++;
2581 assert(new_tab->num_entries == tab->num_entries);
2587 tab->entry_power = new_tab->entry_power;
2588 tab->bin_power = new_tab->bin_power;
2589 tab->size_ind = new_tab->size_ind;
2591 tab->bins = new_tab->bins;
2593 tab->entries = new_tab->entries;
2598set_rebuild_cleanup(
set_table *
const tab)
2600 tab->entries_start = 0;
2601 tab->entries_bound = tab->num_entries;
2602 tab->rebuilds_num++;
2617static inline st_index_t
2618set_secondary_hash(st_index_t ind,
set_table *tab, st_index_t *perturb)
2621 ind = (ind << 2) + ind + *perturb + 1;
2622 return set_hash_bin(ind, tab);
2629static inline st_index_t
2630set_find_entry(
set_table *tab, st_hash_t hash_value, st_data_t key)
2632 int eq_p, rebuilt_p;
2633 st_index_t i, bound;
2634 set_table_entry *entries;
2636 bound = tab->entries_bound;
2637 entries = tab->entries;
2638 for (i = tab->entries_start; i < bound; i++) {
2639 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
2640 if (EXPECT(rebuilt_p, 0))
2641 return REBUILT_TABLE_ENTRY_IND;
2645 return UNDEFINED_ENTRY_IND;
2657set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2659 int eq_p, rebuilt_p;
2661#ifdef QUADRATIC_PROBE
2667 set_table_entry *entries = tab->entries;
2669 ind = set_hash_bin(hash_value, tab);
2670#ifdef QUADRATIC_PROBE
2673 perturb = hash_value;
2676 bin = get_bin(tab->bins, set_get_size_ind(tab), ind);
2677 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2678 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2679 if (EXPECT(rebuilt_p, 0))
2680 return REBUILT_TABLE_ENTRY_IND;
2684 else if (EMPTY_BIN_P(bin))
2685 return UNDEFINED_ENTRY_IND;
2686#ifdef QUADRATIC_PROBE
2687 ind = set_hash_bin(ind + d, tab);
2690 ind = set_secondary_hash(ind, tab, &perturb);
2701set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2703 int eq_p, rebuilt_p;
2705#ifdef QUADRATIC_PROBE
2711 set_table_entry *entries = tab->entries;
2713 ind = set_hash_bin(hash_value, tab);
2714#ifdef QUADRATIC_PROBE
2717 perturb = hash_value;
2720 bin = get_bin(tab->bins, set_get_size_ind(tab), ind);
2721 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2722 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2723 if (EXPECT(rebuilt_p, 0))
2724 return REBUILT_TABLE_BIN_IND;
2728 else if (EMPTY_BIN_P(bin))
2729 return UNDEFINED_BIN_IND;
2730#ifdef QUADRATIC_PROBE
2731 ind = set_hash_bin(ind + d, tab);
2734 ind = set_secondary_hash(ind, tab, &perturb);
2744set_find_table_bin_ind_direct(
set_table *tab, st_hash_t hash_value, st_data_t key)
2747#ifdef QUADRATIC_PROBE
2754 ind = set_hash_bin(hash_value, tab);
2755#ifdef QUADRATIC_PROBE
2758 perturb = hash_value;
2761 bin = get_bin(tab->bins, set_get_size_ind(tab), ind);
2762 if (EMPTY_OR_DELETED_BIN_P(bin))
2764#ifdef QUADRATIC_PROBE
2765 ind = set_hash_bin(ind + d, tab);
2768 ind = set_secondary_hash(ind, tab, &perturb);
2775#define MARK_SET_BIN_EMPTY(tab, i) (set_bin((tab)->bins, set_get_size_ind(tab), i, EMPTY_BIN))
2787set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2788 st_data_t key, st_index_t *bin_ind)
2790 int eq_p, rebuilt_p;
2792 st_hash_t curr_hash_value = *hash_value;
2793#ifdef QUADRATIC_PROBE
2798 st_index_t entry_index;
2799 st_index_t firset_deleted_bin_ind;
2800 set_table_entry *entries;
2802 ind = set_hash_bin(curr_hash_value, tab);
2803#ifdef QUADRATIC_PROBE
2806 perturb = curr_hash_value;
2808 firset_deleted_bin_ind = UNDEFINED_BIN_IND;
2809 entries = tab->entries;
2811 entry_index = get_bin(tab->bins, set_get_size_ind(tab), ind);
2812 if (EMPTY_BIN_P(entry_index)) {
2814 entry_index = UNDEFINED_ENTRY_IND;
2815 if (firset_deleted_bin_ind != UNDEFINED_BIN_IND) {
2817 ind = firset_deleted_bin_ind;
2818 MARK_SET_BIN_EMPTY(tab, ind);
2822 else if (! DELETED_BIN_P(entry_index)) {
2823 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
2824 if (EXPECT(rebuilt_p, 0))
2825 return REBUILT_TABLE_ENTRY_IND;
2829 else if (firset_deleted_bin_ind == UNDEFINED_BIN_IND)
2830 firset_deleted_bin_ind = ind;
2831#ifdef QUADRATIC_PROBE
2832 ind = set_hash_bin(ind + d, tab);
2835 ind = set_secondary_hash(ind, tab, &perturb);
2845set_lookup(
set_table *tab, st_data_t key)
2848 st_hash_t hash = set_do_hash(key, tab);
2851 if (tab->bins == NULL) {
2852 bin = set_find_entry(tab, hash, key);
2853 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2855 if (bin == UNDEFINED_ENTRY_IND)
2859 bin = set_find_table_entry_ind(tab, hash, key);
2860 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2862 if (bin == UNDEFINED_ENTRY_IND)
2871set_rebuild_table_if_necessary (
set_table *tab)
2873 st_index_t bound = tab->entries_bound;
2875 if (bound == set_get_allocated_entries(tab))
2876 set_rebuild_table(tab);
2883set_insert(
set_table *tab, st_data_t key)
2885 set_table_entry *entry;
2888 st_hash_t hash_value;
2892 hash_value = set_do_hash(key, tab);
2894 set_rebuild_table_if_necessary(tab);
2895 if (tab->bins == NULL) {
2896 bin = set_find_entry(tab, hash_value, key);
2897 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2899 new_p = bin == UNDEFINED_ENTRY_IND;
2902 bin_ind = UNDEFINED_BIN_IND;
2905 bin = set_find_table_bin_ptr_and_reserve(tab, &hash_value,
2907 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2909 new_p = bin == UNDEFINED_ENTRY_IND;
2913 ind = tab->entries_bound++;
2914 entry = &tab->entries[ind];
2915 entry->hash = hash_value;
2917 if (bin_ind != UNDEFINED_BIN_IND)
2918 set_bin(tab->bins, set_get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
2928 *new_tab = *old_tab;
2929 if (old_tab->bins == NULL)
2930 new_tab->bins = NULL;
2932 new_tab->bins = (st_index_t *) malloc(set_bins_size(old_tab));
2934 new_tab->entries = (set_table_entry *) malloc(set_get_allocated_entries(old_tab)
2935 *
sizeof(set_table_entry));
2936 MEMCPY(new_tab->entries, old_tab->entries, set_table_entry,
2937 set_get_allocated_entries(old_tab));
2938 if (old_tab->bins != NULL)
2939 MEMCPY(new_tab->bins, old_tab->bins,
char, set_bins_size(old_tab));
2950 if (set_replace(new_tab, old_tab) == NULL) {
2951 set_free_table(new_tab);
2961set_update_range_for_deleted(
set_table *tab, st_index_t n)
2965 if (tab->entries_start == n) {
2966 st_index_t start = n + 1;
2967 st_index_t bound = tab->entries_bound;
2968 set_table_entry *entries = tab->entries;
2969 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
2970 tab->entries_start = start;
2977#define MARK_SET_BIN_DELETED(tab, i) \
2979 set_bin((tab)->bins, set_get_size_ind(tab), i, DELETED_BIN); \
2985set_delete(
set_table *tab, st_data_t *key)
2987 set_table_entry *entry;
2992 hash = set_do_hash(*key, tab);
2994 if (tab->bins == NULL) {
2995 bin = set_find_entry(tab, hash, *key);
2996 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
2998 if (bin == UNDEFINED_ENTRY_IND) {
3003 bin_ind = set_find_table_bin_ind(tab, hash, *key);
3004 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3006 if (bin_ind == UNDEFINED_BIN_IND) {
3009 bin = get_bin(tab->bins, set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3010 MARK_SET_BIN_DELETED(tab, bin_ind);
3012 entry = &tab->entries[bin];
3014 MARK_ENTRY_DELETED(entry);
3016 set_update_range_for_deleted(tab, bin);
3029set_general_foreach(
set_table *tab, set_foreach_check_callback_func *func,
3030 set_update_callback_func *replace, st_data_t arg,
3035 set_table_entry *entries, *curr_entry_ptr;
3036 enum st_retval retval;
3037 st_index_t i, rebuilds_num;
3040 int error_p, packed_p = tab->bins == NULL;
3042 entries = tab->entries;
3045 for (i = tab->entries_start; i < tab->entries_bound; i++) {
3046 curr_entry_ptr = &entries[i];
3047 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
3049 key = curr_entry_ptr->key;
3050 rebuilds_num = tab->rebuilds_num;
3051 hash = curr_entry_ptr->hash;
3052 retval = (*func)(key, arg, 0);
3054 if (retval == ST_REPLACE && replace) {
3055 retval = (*replace)(&key, arg, TRUE);
3056 curr_entry_ptr->key = key;
3059 if (rebuilds_num != tab->rebuilds_num) {
3061 entries = tab->entries;
3062 packed_p = tab->bins == NULL;
3064 i = set_find_entry(tab, hash, key);
3065 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3067 error_p = i == UNDEFINED_ENTRY_IND;
3070 i = set_find_table_entry_ind(tab, hash, key);
3071 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3073 error_p = i == UNDEFINED_ENTRY_IND;
3076 if (error_p && check_p) {
3078 retval = (*func)(0, arg, 1);
3081 curr_entry_ptr = &entries[i];
3094 st_data_t key = curr_entry_ptr->key;
3098 bin = set_find_entry(tab, hash, key);
3099 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3101 if (bin == UNDEFINED_ENTRY_IND)
3105 bin_ind = set_find_table_bin_ind(tab, hash, key);
3106 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3108 if (bin_ind == UNDEFINED_BIN_IND)
3110 bin = get_bin(tab->bins, set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3111 MARK_SET_BIN_DELETED(tab, bin_ind);
3113 curr_entry_ptr = &entries[bin];
3114 MARK_ENTRY_DELETED(curr_entry_ptr);
3116 set_update_range_for_deleted(tab, bin);
3125set_foreach_with_replace(
set_table *tab, set_foreach_check_callback_func *func, set_update_callback_func *replace, st_data_t arg)
3127 return set_general_foreach(tab, func, replace, arg, TRUE);
3131 set_foreach_callback_func *func;
3136set_apply_functor(st_data_t k, st_data_t d,
int _)
3138 const struct set_functor *f = (
void *)d;
3139 return f->func(k, f->arg);
3143set_foreach(
set_table *tab, set_foreach_callback_func *func, st_data_t arg)
3145 const struct set_functor f = { func, arg };
3146 return set_general_foreach(tab, set_apply_functor, NULL, (st_data_t)&f, FALSE);
3151set_foreach_check(
set_table *tab, set_foreach_check_callback_func *func, st_data_t arg,
3152 st_data_t never ATTRIBUTE_UNUSED)
3154 return set_general_foreach(tab, func, NULL, arg, TRUE);
3160set_keys(
set_table *tab, st_data_t *keys, st_index_t size)
3162 st_index_t i, bound;
3163 st_data_t key, *keys_start, *keys_end;
3164 set_table_entry *curr_entry_ptr, *entries = tab->entries;
3166 bound = tab->entries_bound;
3168 keys_end = keys + size;
3169 for (i = tab->entries_start; i < bound; i++) {
3170 if (keys == keys_end)
3172 curr_entry_ptr = &entries[i];
3173 key = curr_entry_ptr->key;
3174 if (! DELETED_ENTRY_P(curr_entry_ptr))
3178 return keys - keys_start;
3184 st_index_t num = tab->num_entries;
3185 if (REBUILD_THRESHOLD * num <= set_get_allocated_entries(tab)) {
3187 set_table *new_tab = set_init_table_with_size(NULL, tab->type, 2 * num);
3188 set_rebuild_table_with(new_tab, tab);
3189 set_rebuild_move_table(new_tab, tab);
3190 set_rebuild_cleanup(tab);
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
#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.