107#elif defined RUBY_EXPORT
109#include "internal/bits.h"
110#include "internal/gc.h"
111#include "internal/hash.h"
112#include "internal/sanitizers.h"
113#include "internal/set_table.h"
114#include "internal/st.h"
115#include "ruby_assert.h"
125#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
126#define EXPECT(expr, val) __builtin_expect(expr, val)
127#define ATTRIBUTE_UNUSED __attribute__((unused))
129#define PREFETCH(addr, write_p)
130#define EXPECT(expr, val) (expr)
131#define ATTRIBUTE_UNUSED
134#define MAX_ENTRIES_START ((unsigned int)-1)
137typedef st_index_t st_hash_t;
145#define type_numhash st_hashtype_num
151static int st_strcmp(st_data_t, st_data_t);
152static st_index_t strhash(st_data_t);
158static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
159static st_index_t strcasehash(st_data_t);
161 st_locale_insensitive_strcasecmp_i,
168#define ST_INIT_VAL 0xafafafafafafafaf
169#define ST_INIT_VAL_BYTE 0xafa
176#define malloc ruby_xmalloc
177#define calloc ruby_xcalloc
178#define realloc ruby_xrealloc
179#define sized_realloc ruby_xrealloc_sized
180#define free ruby_xfree
181#define sized_free ruby_xfree_sized
182#define free_fixed_ptr(v) ruby_xfree_sized((v), sizeof(*(v)))
184#define sized_realloc(ptr, new_size, old_size) realloc(ptr, new_size)
185#define sized_free(v, s) free(v)
186#define free_fixed_ptr(v) free(v)
195 st_hash_t entry_hash, st_data_t entry_key,
196 st_hash_t hash_val, st_data_t key)
198 return (entry_hash == hash_val) &&
199 ((entry_key == key) || (*
type->compare)(key, entry_key) == 0);
206 st_hash_t hash_val, st_data_t key,
207 int *res,
int *rebuilt_p)
209 unsigned int old_rebuilds_num = tab->rebuilds_num;
210 *res = entry_equal(tab->type, entry->hash, entry->key, hash_val, key);
211 *rebuilt_p = old_rebuilds_num != tab->rebuilds_num;
214#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
215 ptr_equal_check((tab), (ptr), (hash_val), (key), &(res), &(rebuilt_p))
220 unsigned char entry_power;
224 unsigned char bin_power;
226 unsigned char size_ind;
229 st_index_t bins_words;
233#if SIZEOF_ST_INDEX_T == 8
252 {16, 17, 2, 0x10000},
253 {17, 18, 2, 0x20000},
254 {18, 19, 2, 0x40000},
255 {19, 20, 2, 0x80000},
256 {20, 21, 2, 0x100000},
257 {21, 22, 2, 0x200000},
258 {22, 23, 2, 0x400000},
259 {23, 24, 2, 0x800000},
260 {24, 25, 2, 0x1000000},
261 {25, 26, 2, 0x2000000},
262 {26, 27, 2, 0x4000000},
263 {27, 28, 2, 0x8000000},
264 {28, 29, 2, 0x10000000},
265 {29, 30, 2, 0x20000000},
266 {30, 31, 2, 0x40000000},
267 {31, 32, 2, 0x80000000},
268 {32, 33, 3, 0x200000000},
269 {33, 34, 3, 0x400000000},
270 {34, 35, 3, 0x800000000},
271 {35, 36, 3, 0x1000000000},
272 {36, 37, 3, 0x2000000000},
273 {37, 38, 3, 0x4000000000},
274 {38, 39, 3, 0x8000000000},
275 {39, 40, 3, 0x10000000000},
276 {40, 41, 3, 0x20000000000},
277 {41, 42, 3, 0x40000000000},
278 {42, 43, 3, 0x80000000000},
279 {43, 44, 3, 0x100000000000},
280 {44, 45, 3, 0x200000000000},
281 {45, 46, 3, 0x400000000000},
282 {46, 47, 3, 0x800000000000},
283 {47, 48, 3, 0x1000000000000},
284 {48, 49, 3, 0x2000000000000},
285 {49, 50, 3, 0x4000000000000},
286 {50, 51, 3, 0x8000000000000},
287 {51, 52, 3, 0x10000000000000},
288 {52, 53, 3, 0x20000000000000},
289 {53, 54, 3, 0x40000000000000},
290 {54, 55, 3, 0x80000000000000},
291 {55, 56, 3, 0x100000000000000},
292 {56, 57, 3, 0x200000000000000},
293 {57, 58, 3, 0x400000000000000},
294 {58, 59, 3, 0x800000000000000},
295 {59, 60, 3, 0x1000000000000000},
296 {60, 61, 3, 0x2000000000000000},
297 {61, 62, 3, 0x4000000000000000},
298 {62, 63, 3, 0x8000000000000000},
321 {16, 17, 2, 0x20000},
322 {17, 18, 2, 0x40000},
323 {18, 19, 2, 0x80000},
324 {19, 20, 2, 0x100000},
325 {20, 21, 2, 0x200000},
326 {21, 22, 2, 0x400000},
327 {22, 23, 2, 0x800000},
328 {23, 24, 2, 0x1000000},
329 {24, 25, 2, 0x2000000},
330 {25, 26, 2, 0x4000000},
331 {26, 27, 2, 0x8000000},
332 {27, 28, 2, 0x10000000},
333 {28, 29, 2, 0x20000000},
334 {29, 30, 2, 0x40000000},
335 {30, 31, 2, 0x80000000},
341#define RESERVED_HASH_VAL (~(st_hash_t) 0)
342#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
344static inline st_hash_t
345normalize_hash_value(st_hash_t hash)
349 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
353static inline st_hash_t
354do_hash(st_data_t key,
st_table *tab)
356 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
357 return normalize_hash_value(hash);
361#define MINIMAL_POWER2 2
363#if MINIMAL_POWER2 < 2
364#error "MINIMAL_POWER2 should be >= 2"
369#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
373get_power2(st_index_t size)
375 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
377 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
388static inline st_index_t
389get_bin(st_index_t *bins,
int s, st_index_t n)
391 return (s == 0 ? ((
unsigned char *) bins)[n]
392 : s == 1 ? ((unsigned short *) bins)[n]
393 : s == 2 ? ((unsigned int *) bins)[n]
394 : ((st_index_t *) bins)[n]);
400set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
402 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
403 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
404 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
405 else ((st_index_t *) bins)[n] = v;
418#define MARK_BIN_EMPTY(tab, i) (set_bin(st_bins_ptr(tab), get_size_ind(tab), i, EMPTY_BIN))
422#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
423#define UNDEFINED_BIN_IND (~(st_index_t) 0)
427#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
428#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
433#define MARK_BIN_DELETED(tab, i) \
435 set_bin(st_bins_ptr(tab), get_size_ind(tab), i, DELETED_BIN); \
440#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
441#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
442#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
446#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin(st_bins_ptr(tab), get_size_ind(tab), i)))
447#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin(st_bins_ptr(tab), get_size_ind(tab), i)))
448#define IND_EMPTY_OR_DELETED_BIN_P(tab, i) (EMPTY_OR_DELETED_BIN_P(get_bin(st_bins_ptr(tab), get_size_ind(tab), i)))
452#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
453#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
456static inline st_index_t
457get_allocated_entries(
const st_table *tab)
459 return ((st_index_t) 1)<<tab->entry_power;
463static inline unsigned int
466 return tab->size_ind;
470static inline st_index_t
473 return ((st_index_t) 1)<<tab->bin_power;
477static inline st_index_t
480 return get_bins_num(tab) - 1;
486 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
490st_allocated_entries_size(
const st_table *tab)
495static inline st_index_t *
498 if (st_has_bins(tab)) {
499 return (st_index_t *)(((
char *)tab->entries) + st_allocated_entries_size(tab));
507static inline st_index_t
508hash_bin(st_hash_t hash_value,
st_table *tab)
510 return hash_value & bins_mask(tab);
514static inline st_index_t
517 if (st_has_bins(tab)) {
518 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
527 memset(st_bins_ptr(tab), 0, bins_size(tab));
534 tab->num_entries = 0;
535 tab->entries_start = tab->entries_bound = 0;
536 if (st_bins_ptr(tab) != NULL)
537 initialize_bins(tab);
545 int all, total, num, str, strcase;
550static int init_st = 0;
557 char fname[10+
sizeof(long)*3];
559 if (!collision.total)
return;
560 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
563 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
564 ((
double)collision.all / (collision.total)) * 100);
565 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
571st_init_existing_table_with_size(
st_table *tab,
const struct st_hash_type *type, st_index_t size)
578 const char *e = getenv(
"ST_HASH_LOG");
579 if (!e || !*e) init_st = 1;
588 n = get_power2(size);
595 tab->entry_power = n;
596 tab->bin_power = features[n].bin_power;
597 tab->size_ind = features[n].size_ind;
603 tab->num_entries = 0;
604 tab->entries_start = tab->entries_bound = 0;
606 size_t memsize = get_allocated_entries(tab) *
sizeof(
st_table_entry);
607 if (tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS) {
608 memsize += bins_size(tab);
612 if (tab->entries == NULL) {
618 tab->rebuilds_num = 0;
623st_init_existing_numtable_with_size(
st_table *tab, st_index_t size)
625 return st_init_existing_table_with_size(tab, &type_numhash, size);
632st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
641 st_init_existing_table_with_size(tab, type, size);
643 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
653st_table_size(
const struct st_table *tbl)
655 return tbl->num_entries;
663 return st_init_table_with_size(type, 0);
669st_init_numtable(
void)
671 return st_init_table(&type_numhash);
676st_init_numtable_with_size(st_index_t size)
678 return st_init_table_with_size(&type_numhash, size);
684st_init_strtable(
void)
686 return st_init_table(&type_strhash);
691st_init_strtable_with_size(st_index_t size)
693 return st_init_table_with_size(&type_strhash, size);
697st_init_existing_strtable_with_size(
st_table *tab, st_index_t size)
699 return st_init_existing_table_with_size(tab, &type_strhash, size);
706st_init_strcasetable(
void)
708 return st_init_table(&type_strcasehash);
714st_init_strcasetable_with_size(st_index_t size)
716 return st_init_table_with_size(&type_strcasehash, size);
728st_entries_memsize(
const st_table *tab)
736 sized_free(tab->entries, st_entries_memsize(tab) + bins_size(tab));
740st_free_embedded_table(
st_table *tab)
742 st_free_entries(tab);
749 st_free_embedded_table(tab);
754st_allocated_memsize(
const st_table *tab)
757 return bins_size(tab) + st_entries_memsize(tab);
765 return sizeof(
st_table) + st_allocated_memsize(tab);
769find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
772find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key);
775find_table_bin_ind_direct(
st_table *table, st_hash_t hash_value, st_data_t key);
778find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
779 st_data_t key, st_index_t *bin_ind);
786 if (type == &type_numhash) {
789 else if (type == &type_strhash) {
792 else if (type == &type_strcasehash) {
797#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
798#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
799#define collision_check 0
808#define REBUILD_THRESHOLD 4
810#if REBUILD_THRESHOLD < 2
811#error "REBUILD_THRESHOLD should be >= 2"
814static void rebuild_table_with(
st_table *
const new_tab,
st_table *
const tab);
815static void rebuild_move_table(
st_table *
const new_tab,
st_table *
const tab);
816static void rebuild_cleanup(
st_table *
const tab);
825 if ((2 * tab->num_entries <= get_allocated_entries(tab)
826 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
827 || tab->num_entries < (1 << MINIMAL_POWER2)) {
829 tab->num_entries = 0;
830 if (st_has_bins(tab))
831 initialize_bins(tab);
832 rebuild_table_with(tab, tab);
839 new_tab = st_init_table_with_size(tab->type,
840 2 * tab->num_entries - 1);
841 rebuild_table_with(new_tab, tab);
842 rebuild_move_table(new_tab, tab);
844 rebuild_cleanup(tab);
851 unsigned int size_ind;
857 new_entries = new_tab->entries;
860 bins = st_bins_ptr(new_tab);
861 size_ind = get_size_ind(new_tab);
862 st_index_t bound = tab->entries_bound;
865 for (i = tab->entries_start; i < bound; i++) {
866 curr_entry_ptr = &entries[i];
867 PREFETCH(entries + i + 1, 0);
868 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
870 if (&new_entries[ni] != curr_entry_ptr)
871 new_entries[ni] = *curr_entry_ptr;
872 if (EXPECT(bins != NULL, 1)) {
873 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
874 curr_entry_ptr->key);
875 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
877 new_tab->num_entries++;
881 assert(new_tab->num_entries == tab->num_entries);
887 st_free_entries(tab);
888 tab->entry_power = new_tab->entry_power;
889 tab->bin_power = new_tab->bin_power;
890 tab->size_ind = new_tab->size_ind;
891 tab->entries = new_tab->entries;
892 free_fixed_ptr(new_tab);
898 tab->entries_start = 0;
899 tab->entries_bound = tab->num_entries;
915static inline st_index_t
916secondary_hash(st_index_t ind,
st_table *tab, st_index_t *perturb)
919 ind = (ind << 2) + ind + *perturb + 1;
920 return hash_bin(ind, tab);
927static inline st_index_t
928find_entry(
st_table *tab, st_hash_t hash_value, st_data_t key)
934 bound = tab->entries_bound;
935 entries = tab->entries;
936 for (i = tab->entries_start; i < bound; i++) {
937 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
938 if (EXPECT(rebuilt_p, 0))
939 return REBUILT_TABLE_ENTRY_IND;
943 return UNDEFINED_ENTRY_IND;
955find_table_entry_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
959#ifdef QUADRATIC_PROBE
967 ind = hash_bin(hash_value, tab);
968#ifdef QUADRATIC_PROBE
971 perturb = hash_value;
975 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
976 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
977 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
978 if (EXPECT(rebuilt_p, 0))
979 return REBUILT_TABLE_ENTRY_IND;
983 else if (EMPTY_BIN_P(bin))
984 return UNDEFINED_ENTRY_IND;
985#ifdef QUADRATIC_PROBE
986 ind = hash_bin(ind + d, tab);
989 ind = secondary_hash(ind, tab, &perturb);
1001find_table_bin_ind(
st_table *tab, st_hash_t hash_value, st_data_t key)
1003 int eq_p, rebuilt_p;
1005#ifdef QUADRATIC_PROBE
1013 ind = hash_bin(hash_value, tab);
1014#ifdef QUADRATIC_PROBE
1017 perturb = hash_value;
1021 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1022 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
1023 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
1024 if (EXPECT(rebuilt_p, 0))
1025 return REBUILT_TABLE_BIN_IND;
1029 else if (EMPTY_BIN_P(bin))
1030 return UNDEFINED_BIN_IND;
1031#ifdef QUADRATIC_PROBE
1032 ind = hash_bin(ind + d, tab);
1035 ind = secondary_hash(ind, tab, &perturb);
1046find_table_bin_ind_direct(
st_table *tab, st_hash_t hash_value, st_data_t key)
1049#ifdef QUADRATIC_PROBE
1056 ind = hash_bin(hash_value, tab);
1057#ifdef QUADRATIC_PROBE
1060 perturb = hash_value;
1064 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1065 if (EMPTY_OR_DELETED_BIN_P(bin))
1067#ifdef QUADRATIC_PROBE
1068 ind = hash_bin(ind + d, tab);
1071 ind = secondary_hash(ind, tab, &perturb);
1087find_table_bin_ptr_and_reserve(
st_table *tab, st_hash_t *hash_value,
1088 st_data_t key, st_index_t *bin_ind)
1090 int eq_p, rebuilt_p;
1092 st_hash_t curr_hash_value = *hash_value;
1093#ifdef QUADRATIC_PROBE
1098 st_index_t entry_index;
1099 st_index_t first_deleted_bin_ind;
1102 ind = hash_bin(curr_hash_value, tab);
1103#ifdef QUADRATIC_PROBE
1106 perturb = curr_hash_value;
1109 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1110 entries = tab->entries;
1112 entry_index = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1113 if (EMPTY_BIN_P(entry_index)) {
1115 entry_index = UNDEFINED_ENTRY_IND;
1116 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1118 ind = first_deleted_bin_ind;
1119 MARK_BIN_EMPTY(tab, ind);
1123 else if (! DELETED_BIN_P(entry_index)) {
1124 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1125 if (EXPECT(rebuilt_p, 0))
1126 return REBUILT_TABLE_ENTRY_IND;
1130 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1131 first_deleted_bin_ind = ind;
1132#ifdef QUADRATIC_PROBE
1133 ind = hash_bin(ind + d, tab);
1136 ind = secondary_hash(ind, tab, &perturb);
1147st_lookup(
st_table *tab, st_data_t key, st_data_t *value)
1150 st_hash_t hash = do_hash(key, tab);
1153 if (!st_has_bins(tab)) {
1154 bin = find_entry(tab, hash, key);
1155 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1157 if (bin == UNDEFINED_ENTRY_IND)
1161 bin = find_table_entry_ind(tab, hash, key);
1162 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1164 if (bin == UNDEFINED_ENTRY_IND)
1169 *value = tab->entries[bin].record;
1176st_get_key(
st_table *tab, st_data_t key, st_data_t *result)
1179 st_hash_t hash = do_hash(key, tab);
1182 if (!st_has_bins(tab)) {
1183 bin = find_entry(tab, hash, key);
1184 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1186 if (bin == UNDEFINED_ENTRY_IND)
1190 bin = find_table_entry_ind(tab, hash, key);
1191 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1193 if (bin == UNDEFINED_ENTRY_IND)
1198 *result = tab->entries[bin].key;
1204rebuild_table_if_necessary(
st_table *tab)
1206 st_index_t bound = tab->entries_bound;
1208 if (bound == get_allocated_entries(tab) || tab->entries_start == MAX_ENTRIES_START) {
1217st_insert(
st_table *tab, st_data_t key, st_data_t value)
1222 st_hash_t hash_value;
1226 hash_value = do_hash(key, tab);
1228 rebuild_table_if_necessary(tab);
1229 if (!st_has_bins(tab)) {
1230 bin = find_entry(tab, hash_value, key);
1231 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1233 new_p = bin == UNDEFINED_ENTRY_IND;
1236 bin_ind = UNDEFINED_BIN_IND;
1239 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1241 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1243 new_p = bin == UNDEFINED_ENTRY_IND;
1247 ind = tab->entries_bound++;
1248 entry = &tab->entries[ind];
1249 entry->hash = hash_value;
1251 entry->record = value;
1252 if (bin_ind != UNDEFINED_BIN_IND)
1253 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1256 tab->entries[bin].record = value;
1265st_insert_no_rebuild(
st_table *tab, st_data_t key, st_data_t value)
1267 if (tab->entries_bound == get_allocated_entries(tab)) {
1271 return st_insert(tab, key, value);
1278st_add_direct_with_hash(
st_table *tab,
1279 st_data_t key, st_data_t value, st_hash_t hash)
1285 assert(hash != RESERVED_HASH_VAL);
1287 rebuild_table_if_necessary(tab);
1288 ind = tab->entries_bound++;
1289 entry = &tab->entries[ind];
1292 entry->record = value;
1294 if (st_has_bins(tab)) {
1295 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1296 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1301rb_st_add_direct_with_hash(
st_table *tab,
1302 st_data_t key, st_data_t value, st_hash_t hash)
1304 st_add_direct_with_hash(tab, key, value, normalize_hash_value(hash));
1310st_add_direct(
st_table *tab, st_data_t key, st_data_t value)
1312 st_hash_t hash_value;
1314 hash_value = do_hash(key, tab);
1315 st_add_direct_with_hash(tab, key, value, hash_value);
1322st_insert2(
st_table *tab, st_data_t key, st_data_t value,
1323 st_data_t (*func)(st_data_t))
1328 st_hash_t hash_value;
1332 hash_value = do_hash(key, tab);
1334 rebuild_table_if_necessary(tab);
1335 if (!st_has_bins(tab)) {
1336 bin = find_entry(tab, hash_value, key);
1337 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1339 new_p = bin == UNDEFINED_ENTRY_IND;
1342 bin_ind = UNDEFINED_BIN_IND;
1345 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1347 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1349 new_p = bin == UNDEFINED_ENTRY_IND;
1354 ind = tab->entries_bound++;
1355 entry = &tab->entries[ind];
1356 entry->hash = hash_value;
1358 entry->record = value;
1359 if (bin_ind != UNDEFINED_BIN_IND)
1360 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1363 tab->entries[bin].record = value;
1370 *new_tab = *old_tab;
1371 size_t memsize = get_allocated_entries(old_tab) *
sizeof(
st_table_entry);
1372 memsize += bins_size(old_tab);
1375 if (new_tab->entries == NULL) {
1379 MEMCPY(new_tab->entries, old_tab->entries,
char, memsize);
1390 return st_replace_no_check(new_tab, old_tab);
1401 if (new_tab == NULL)
1405 if (st_replace_no_check(new_tab, old_tab) == NULL) {
1406 st_free_table(new_tab);
1416update_range_for_deleted(
st_table *tab, st_index_t n)
1420 if (tab->entries_start == n) {
1421 st_index_t start = n + 1;
1422 st_index_t bound = tab->entries_bound;
1424 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1425 tab->entries_start = start > MAX_ENTRIES_START ? MAX_ENTRIES_START : (
unsigned int)start;
1434st_general_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1441 hash = do_hash(*key, tab);
1443 if (!st_has_bins(tab)) {
1444 bin = find_entry(tab, hash, *key);
1445 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1447 if (bin == UNDEFINED_ENTRY_IND) {
1448 if (value != 0) *value = 0;
1453 bin_ind = find_table_bin_ind(tab, hash, *key);
1454 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1456 if (bin_ind == UNDEFINED_BIN_IND) {
1457 if (value != 0) *value = 0;
1460 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1461 MARK_BIN_DELETED(tab, bin_ind);
1463 entry = &tab->entries[bin];
1465 if (value != 0) *value = entry->record;
1466 MARK_ENTRY_DELETED(entry);
1468 update_range_for_deleted(tab, bin);
1473st_delete(
st_table *tab, st_data_t *key, st_data_t *value)
1475 return st_general_delete(tab, key, value);
1484st_delete_safe(
st_table *tab, st_data_t *key, st_data_t *value,
1485 st_data_t never ATTRIBUTE_UNUSED)
1487 return st_general_delete(tab, key, value);
1495st_shift(
st_table *tab, st_data_t *key, st_data_t *value)
1497 st_index_t i, bound;
1502 entries = tab->entries;
1503 bound = tab->entries_bound;
1504 for (i = tab->entries_start; i < bound; i++) {
1505 curr_entry_ptr = &entries[i];
1506 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1507 st_hash_t entry_hash = curr_entry_ptr->hash;
1508 st_data_t entry_key = curr_entry_ptr->key;
1510 if (value != 0) *value = curr_entry_ptr->record;
1513 if (!st_has_bins(tab)) {
1514 bin = find_entry(tab, entry_hash, entry_key);
1515 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1516 entries = tab->entries;
1519 curr_entry_ptr = &entries[bin];
1522 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1523 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1524 entries = tab->entries;
1527 curr_entry_ptr = &entries[get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind)
1529 MARK_BIN_DELETED(tab, bin_ind);
1531 MARK_ENTRY_DELETED(curr_entry_ptr);
1533 update_range_for_deleted(tab, i);
1537 if (value != 0) *value = 0;
1543st_cleanup_safe(
st_table *tab ATTRIBUTE_UNUSED,
1544 st_data_t never ATTRIBUTE_UNUSED)
1558st_update(
st_table *tab, st_data_t key,
1559 st_update_callback_func *func, st_data_t arg)
1565 st_data_t value = 0, old_key;
1566 int retval, existing;
1567 st_hash_t hash = do_hash(key, tab);
1570 entries = tab->entries;
1571 if (!st_has_bins(tab)) {
1572 bin = find_entry(tab, hash, key);
1573 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1575 existing = bin != UNDEFINED_ENTRY_IND;
1576 entry = &entries[bin];
1577 bin_ind = UNDEFINED_BIN_IND;
1580 bin_ind = find_table_bin_ind(tab, hash, key);
1581 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1583 existing = bin_ind != UNDEFINED_BIN_IND;
1585 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1586 entry = &entries[bin];
1591 value = entry->record;
1595 unsigned int rebuilds_num = tab->rebuilds_num;
1597 retval = (*func)(&key, &value, arg, existing);
1601 assert(rebuilds_num == tab->rebuilds_num);
1607 st_add_direct_with_hash(tab, key, value, hash);
1610 if (old_key != key) {
1613 entry->record = value;
1617 if (bin_ind != UNDEFINED_BIN_IND)
1618 MARK_BIN_DELETED(tab, bin_ind);
1619 MARK_ENTRY_DELETED(entry);
1621 update_range_for_deleted(tab, bin);
1637st_general_foreach(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1643 enum st_retval retval;
1644 st_index_t i, rebuilds_num;
1647 int error_p, packed_p = !st_has_bins(tab);
1649 entries = tab->entries;
1652 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1653 curr_entry_ptr = &entries[i];
1654 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1656 key = curr_entry_ptr->key;
1657 rebuilds_num = tab->rebuilds_num;
1658 hash = curr_entry_ptr->hash;
1659 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1661 if (retval == ST_REPLACE && replace) {
1663 value = curr_entry_ptr->record;
1664 retval = (*replace)(&key, &value, arg, TRUE);
1665 curr_entry_ptr->key = key;
1666 curr_entry_ptr->record = value;
1669 if (rebuilds_num != tab->rebuilds_num) {
1671 entries = tab->entries;
1672 packed_p = !st_has_bins(tab);
1674 i = find_entry(tab, hash, key);
1675 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1677 error_p = i == UNDEFINED_ENTRY_IND;
1680 i = find_table_entry_ind(tab, hash, key);
1681 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1683 error_p = i == UNDEFINED_ENTRY_IND;
1686 if (error_p && check_p) {
1688 retval = (*func)(0, 0, arg, 1);
1691 curr_entry_ptr = &entries[i];
1704 st_data_t key = curr_entry_ptr->key;
1708 bin = find_entry(tab, hash, key);
1709 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1711 if (bin == UNDEFINED_ENTRY_IND)
1715 bin_ind = find_table_bin_ind(tab, hash, key);
1716 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1718 if (bin_ind == UNDEFINED_BIN_IND)
1720 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1721 MARK_BIN_DELETED(tab, bin_ind);
1723 curr_entry_ptr = &entries[bin];
1724 MARK_ENTRY_DELETED(curr_entry_ptr);
1726 update_range_for_deleted(tab, bin);
1736st_foreach_with_hash(
st_table *tab, st_foreach_with_hash_callback_func *func, st_data_t arg)
1739 enum st_retval retval;
1740 st_index_t i, rebuilds_num;
1743 int packed_p = !st_has_bins(tab);
1745 entries = tab->entries;
1748 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1749 curr_entry_ptr = &entries[i];
1750 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1752 key = curr_entry_ptr->key;
1753 rebuilds_num = tab->rebuilds_num;
1754 hash = curr_entry_ptr->hash;
1755 retval = (*func)(key, curr_entry_ptr->record, hash, arg);
1757 if (rebuilds_num != tab->rebuilds_num) {
1759 entries = tab->entries;
1760 packed_p = !st_has_bins(tab);
1762 i = find_entry(tab, hash, key);
1763 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1767 i = find_table_entry_ind(tab, hash, key);
1768 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1772 curr_entry_ptr = &entries[i];
1786st_foreach_with_replace(
st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1788 return st_general_foreach(tab, func, replace, arg, TRUE);
1792 st_foreach_callback_func *func;
1797apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1799 const struct functor *f = (
void *)d;
1800 return f->func(k, v, f->arg);
1804st_foreach(
st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1806 const struct functor f = { func, arg };
1807 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1812st_foreach_check(
st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1813 st_data_t never ATTRIBUTE_UNUSED)
1815 return st_general_foreach(tab, func, 0, arg, TRUE);
1820static inline st_index_t
1821st_general_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1823 st_index_t i, bound;
1824 st_data_t key, *keys_start, *keys_end;
1827 bound = tab->entries_bound;
1829 keys_end = keys + size;
1830 for (i = tab->entries_start; i < bound; i++) {
1831 if (keys == keys_end)
1833 curr_entry_ptr = &entries[i];
1834 key = curr_entry_ptr->key;
1835 if (! DELETED_ENTRY_P(curr_entry_ptr))
1839 return keys - keys_start;
1843st_keys(
st_table *tab, st_data_t *keys, st_index_t size)
1845 return st_general_keys(tab, keys, size);
1850st_keys_check(
st_table *tab, st_data_t *keys, st_index_t size,
1851 st_data_t never ATTRIBUTE_UNUSED)
1853 return st_general_keys(tab, keys, size);
1858static inline st_index_t
1859st_general_values(
st_table *tab, st_data_t *values, st_index_t size)
1861 st_index_t i, bound;
1862 st_data_t *values_start, *values_end;
1865 values_start = values;
1866 values_end = values + size;
1867 bound = tab->entries_bound;
1868 for (i = tab->entries_start; i < bound; i++) {
1869 if (values == values_end)
1871 curr_entry_ptr = &entries[i];
1872 if (! DELETED_ENTRY_P(curr_entry_ptr))
1873 *values++ = curr_entry_ptr->record;
1876 return values - values_start;
1880st_values(
st_table *tab, st_data_t *values, st_index_t size)
1882 return st_general_values(tab, values, size);
1887st_values_check(
st_table *tab, st_data_t *values, st_index_t size,
1888 st_data_t never ATTRIBUTE_UNUSED)
1890 return st_general_values(tab, values, size);
1893#define FNV1_32A_INIT 0x811c9dc5
1898#define FNV_32_PRIME 0x01000193
1901#ifndef UNALIGNED_WORD_ACCESS
1902# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1903 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1904 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1905 defined(__mc68020__)
1906# define UNALIGNED_WORD_ACCESS 1
1909#ifndef UNALIGNED_WORD_ACCESS
1910# define UNALIGNED_WORD_ACCESS 0
1916#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1917#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1919#if ST_INDEX_BITS <= 32
1920#define C1 (st_index_t)0xcc9e2d51
1921#define C2 (st_index_t)0x1b873593
1923#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1924#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1926NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1927NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1928NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1930static inline st_index_t
1931murmur_step(st_index_t h, st_index_t k)
1933#if ST_INDEX_BITS <= 32
1949static inline st_index_t
1950murmur_finish(st_index_t h)
1952#if ST_INDEX_BITS <= 32
1956 const st_index_t c1 = 0x85ebca6b;
1957 const st_index_t c2 = 0xc2b2ae35;
1963 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1964 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1966#if ST_INDEX_BITS > 64
1983st_hash(
const void *ptr,
size_t len, st_index_t h)
1985 const char *data = ptr;
1989#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1990#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1991#if SIZEOF_ST_INDEX_T > 4
1992#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1993#if SIZEOF_ST_INDEX_T > 8
1994#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
1995 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
1996#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
1998#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
2000#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
2003 if (
len >=
sizeof(st_index_t)) {
2004#if !UNALIGNED_WORD_ACCESS
2005 int align = (int)((st_data_t)data %
sizeof(st_index_t));
2011#ifdef WORDS_BIGENDIAN
2012# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
2013 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
2015# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
2016 t |= data_at(n) << CHAR_BIT*(n)
2022#ifdef WORDS_BIGENDIAN
2023 t >>= (CHAR_BIT * align) - CHAR_BIT;
2025 t <<= (CHAR_BIT * align);
2028 data +=
sizeof(st_index_t)-align;
2029 len -=
sizeof(st_index_t)-align;
2031 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
2032 sr = CHAR_BIT * align;
2034 while (
len >=
sizeof(st_index_t)) {
2035 d = *(st_index_t *)data;
2036#ifdef WORDS_BIGENDIAN
2037 t = (t << sr) | (d >> sl);
2039 t = (t >> sr) | (d << sl);
2041 h = murmur_step(h, t);
2043 data +=
sizeof(st_index_t);
2044 len -=
sizeof(st_index_t);
2047 pack =
len < (size_t)align ? (
int)
len : align;
2050#ifdef WORDS_BIGENDIAN
2051# define UNALIGNED_ADD(n) case (n) + 1: \
2052 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2054# define UNALIGNED_ADD(n) case (n) + 1: \
2055 d |= data_at(n) << CHAR_BIT*(n)
2060#ifdef WORDS_BIGENDIAN
2061 t = (t << sr) | (d >> sl);
2063 t = (t >> sr) | (d << sl);
2066 if (
len < (
size_t)align)
goto skip_tail;
2068 h = murmur_step(h, t);
2074#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
2075#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
2077#define aligned_data data
2081 h = murmur_step(h, *(st_index_t *)aligned_data);
2082 data +=
sizeof(st_index_t);
2083 len -=
sizeof(st_index_t);
2084 }
while (
len >=
sizeof(st_index_t));
2090#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
2092#if SIZEOF_ST_INDEX_T > 4
2093 case 7: t |= data_at(6) << 48;
2094 case 6: t |= data_at(5) << 40;
2095 case 5: t |= data_at(4) << 32;
2097 t |= (st_index_t)*(uint32_t*)aligned_data;
2101 case 3: t |= data_at(2) << 16;
2102 case 2: t |= data_at(1) << 8;
2103 case 1: t |= data_at(0);
2105#ifdef WORDS_BIGENDIAN
2106# define UNALIGNED_ADD(n) case (n) + 1: \
2107 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2109# define UNALIGNED_ADD(n) case (n) + 1: \
2110 t |= data_at(n) << CHAR_BIT*(n)
2118 h ^= t; h -= ROTL(t, 7);
2124 return murmur_finish(h);
2128st_hash_uint32(st_index_t h, uint32_t i)
2130 return murmur_step(h, i);
2133NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
2135st_hash_uint(st_index_t h, st_index_t i)
2140#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
2141 h = murmur_step(h, i >> 8*8);
2143 h = murmur_step(h, i);
2148st_hash_end(st_index_t h)
2150 h = murmur_finish(h);
2156rb_st_hash_start(st_index_t h)
2162strhash(st_data_t arg)
2164 register const char *
string = (
const char *)arg;
2165 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2169st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
2176 if (c1 ==
'\0' || c2 ==
'\0') {
2177 if (c1 !=
'\0')
return 1;
2178 if (c2 !=
'\0')
return -1;
2181 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2182 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2193st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2198 for (i = 0; i < n; i++) {
2201 if (c1 ==
'\0' || c2 ==
'\0') {
2202 if (c1 !=
'\0')
return 1;
2203 if (c2 !=
'\0')
return -1;
2206 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2207 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2219st_strcmp(st_data_t lhs, st_data_t rhs)
2221 const char *s1 = (
char *)lhs;
2222 const char *s2 = (
char *)rhs;
2223 return strcmp(s1, s2);
2227st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2229 const char *s1 = (
char *)lhs;
2230 const char *s2 = (
char *)rhs;
2231 return st_locale_insensitive_strcasecmp(s1, s2);
2234NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2236strcasehash(st_data_t arg)
2238 register const char *
string = (
const char *)arg;
2239 register st_index_t hval = FNV1_32A_INIT;
2245 unsigned int c = (
unsigned char)*
string++;
2246 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2250 hval *= FNV_32_PRIME;
2256st_numcmp(st_data_t x, st_data_t y)
2262st_numhash(st_data_t n)
2264 enum {s1 = 11, s2 = 3};
2265 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2273st_expand_table(
st_table *tab, st_index_t siz)
2278 if (siz <= get_allocated_entries(tab))
2281 tmp = st_init_table_with_size(tab->type, siz);
2282 n = get_allocated_entries(tab);
2284 st_free_entries(tab);
2286 tab->entry_power = tmp->entry_power;
2287 tab->bin_power = tmp->bin_power;
2288 tab->size_ind = tmp->size_ind;
2289 tab->entries = tmp->entries;
2290 tab->rebuilds_num++;
2291 free_fixed_ptr(tmp);
2299 int eq_p, rebuilt_p;
2303 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2304 p = &tab->entries[i];
2305 if (DELETED_ENTRY_P(p))
2307 for (j = i + 1; j < tab->entries_bound; j++) {
2308 q = &tab->entries[j];
2309 if (DELETED_ENTRY_P(q))
2311 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2312 if (EXPECT(rebuilt_p, 0))
2316 MARK_ENTRY_DELETED(q);
2318 update_range_for_deleted(tab, j);
2330 int eq_p, rebuilt_p;
2333 unsigned int const size_ind = get_size_ind(tab);
2334 initialize_bins(tab);
2335 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2338#ifdef QUADRATIC_PROBE
2341 st_index_t perturb = p->hash;
2344 if (DELETED_ENTRY_P(p))
2347 ind = hash_bin(p->hash, tab);
2349 st_index_t bin = get_bin(st_bins_ptr(tab), size_ind, ind);
2350 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2352 set_bin(st_bins_ptr(tab), size_ind, ind, i + ENTRY_BASE);
2357 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2358 if (EXPECT(rebuilt_p, 0))
2362 q->record = p->record;
2363 MARK_ENTRY_DELETED(p);
2365 update_range_for_deleted(tab, bin);
2370#ifdef QUADRATIC_PROBE
2371 ind = hash_bin(ind + d, tab);
2374 ind = secondary_hash(ind, tab, &perturb);
2392 if (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2393 rebuilt_p = st_rehash_linear(tab);
2395 rebuilt_p = st_rehash_indexed(tab);
2396 }
while (rebuilt_p);
2400st_stringify(
VALUE key)
2403 rb_hash_key_str(key) : key;
2409 st_data_t k = st_stringify(key);
2411 e.hash = do_hash(k, tab);
2415 tab->entries[tab->entries_bound++] = e;
2426 for (i = 0; i < argc; ) {
2427 st_data_t k = st_stringify(argv[i++]);
2428 st_data_t v = argv[i++];
2429 st_insert(tab, k, v);
2441 for (i = 0; i < argc; ) {
2442 VALUE key = argv[i++];
2443 VALUE val = argv[i++];
2444 st_insert_single(tab, hash, key, val);
2454rb_hash_bulk_insert_into_st_table(
long argc,
const VALUE *argv,
VALUE hash)
2456 st_index_t n, size = argc / 2;
2457 st_table *tab = RHASH_ST_TABLE(hash);
2459 tab = RHASH_TBL_RAW(hash);
2460 n = tab->entries_bound + size;
2461 st_expand_table(tab, n);
2462 if (UNLIKELY(tab->num_entries))
2463 st_insert_generic(tab, argc, argv, hash);
2465 st_insert_single(tab, hash, argv[0], argv[1]);
2466 else if (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2467 st_insert_linear(tab, argc, argv, hash);
2469 st_insert_generic(tab, argc, argv, hash);
2475 st_index_t num = tab->num_entries;
2476 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2478 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2479 rebuild_table_with(new_tab, tab);
2480 rebuild_move_table(new_tab, tab);
2481 rebuild_cleanup(tab);
2489struct set_table_entry {
2495set_ptr_equal_check(
const set_table *tab,
const set_table_entry *entry,
2496 st_hash_t hash_val, st_data_t key,
2497 int *res,
int *rebuilt_p)
2499 unsigned int old_rebuilds_num = tab->rebuilds_num;
2500 *res = entry_equal(tab->type, entry->hash, entry->key, hash_val, key);
2501 *rebuilt_p = old_rebuilds_num != tab->rebuilds_num;
2504#define SET_DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
2505 set_ptr_equal_check((tab), (ptr), (hash_val), (key), &(res), &(rebuilt_p))
2508static inline st_hash_t
2509set_do_hash(st_data_t key,
set_table *tab)
2511 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
2512 return normalize_hash_value(hash);
2516static inline unsigned int
2519 return tab->size_ind;
2523static inline st_index_t
2526 return ((st_index_t) 1)<<tab->bin_power;
2530static inline st_index_t
2533 return set_get_bins_num(tab) - 1;
2538static inline st_index_t
2539set_hash_bin(st_hash_t hash_value,
set_table *tab)
2541 return hash_value & set_bins_mask(tab);
2545static inline st_index_t
2546set_get_allocated_entries(
const set_table *tab)
2548 return ((st_index_t) 1)<<tab->entry_power;
2552set_allocated_entries_size(
const set_table *tab)
2554 return set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2560 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
2564static inline st_index_t
2567 if (set_has_bins(tab)) {
2568 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
2574static inline st_index_t *
2577 if (set_has_bins(tab)) {
2578 return (st_index_t *)(((
char *)tab->
entries) + set_allocated_entries_size(tab));
2588 memset(set_bins_ptr(tab), 0, set_bins_size(tab));
2595 tab->num_entries = 0;
2596 tab->entries_start = tab->entries_bound = 0;
2597 if (set_bins_ptr(tab) != NULL)
2598 set_initialize_bins(tab);
2604 size_t memsize = set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2605 if (set_has_bins(tab)) {
2606 memsize += set_bins_size(tab);
2619 const char *e = getenv(
"ST_HASH_LOG");
2620 if (!e || !*e) init_st = 1;
2629 n = get_power2(size);
2632 tab->entry_power = n;
2633 tab->bin_power = features[n].bin_power;
2634 tab->size_ind = features[n].size_ind;
2636 tab->
entries = (set_table_entry *)malloc(set_entries_memsize(tab));
2637 set_make_tab_empty(tab);
2638 tab->rebuilds_num = 0;
2648 if (tab == NULL) tab = malloc(
sizeof(
set_table));
2650 set_init_existing_table_with_size(tab, type, size);
2656set_init_numtable(
void)
2658 return set_init_table_with_size(NULL, &type_numhash, 0);
2662set_init_numtable_with_size(st_index_t size)
2664 return set_init_table_with_size(NULL, &type_numhash, size);
2668set_init_embedded_numtable_with_size(
set_table *tab, st_index_t size)
2670 return set_init_existing_table_with_size(tab, &type_numhash, size);
2674set_table_size(
const struct set_table *tbl)
2676 return tbl->num_entries;
2683 set_make_tab_empty(tab);
2684 tab->rebuilds_num++;
2690 sized_free(tab->
entries, set_entries_memsize(tab));
2698 set_free_embedded_table(tab);
2699 free_fixed_ptr(tab);
2707 + (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS ? 0 : set_bins_size(tab))
2708 + set_get_allocated_entries(tab) * sizeof(set_table_entry));
2712set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2715set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2718set_find_table_bin_ind_direct(
set_table *table, st_hash_t hash_value, st_data_t key);
2721set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2722 st_data_t key, st_index_t *bin_ind);
2726static void set_rebuild_cleanup(
set_table *
const tab);
2735 if ((2 * tab->num_entries <= set_get_allocated_entries(tab)
2736 && REBUILD_THRESHOLD * tab->num_entries > set_get_allocated_entries(tab))
2737 || tab->num_entries < (1 << MINIMAL_POWER2)) {
2739 tab->num_entries = 0;
2740 if (set_has_bins(tab))
2741 set_initialize_bins(tab);
2742 set_rebuild_table_with(tab, tab);
2749 new_tab = set_init_table_with_size(NULL, tab->type,
2750 2 * tab->num_entries - 1);
2751 set_rebuild_table_with(new_tab, tab);
2752 set_rebuild_move_table(new_tab, tab);
2754 set_rebuild_cleanup(tab);
2761 unsigned int size_ind;
2762 set_table_entry *new_entries;
2763 set_table_entry *curr_entry_ptr;
2767 new_entries = new_tab->
entries;
2770 bins = set_bins_ptr(new_tab);
2771 size_ind = set_get_size_ind(new_tab);
2772 st_index_t bound = tab->entries_bound;
2773 set_table_entry *entries = tab->
entries;
2775 for (i = tab->entries_start; i < bound; i++) {
2776 curr_entry_ptr = &entries[i];
2777 PREFETCH(entries + i + 1, 0);
2778 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
2780 if (&new_entries[ni] != curr_entry_ptr)
2781 new_entries[ni] = *curr_entry_ptr;
2782 if (EXPECT(bins != NULL, 1)) {
2783 bin_ind = set_find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
2784 curr_entry_ptr->key);
2785 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
2787 new_tab->num_entries++;
2791 assert(new_tab->num_entries == tab->num_entries);
2797 sized_free(tab->
entries, set_entries_memsize(tab));
2800 tab->entry_power = new_tab->entry_power;
2801 tab->bin_power = new_tab->bin_power;
2802 tab->size_ind = new_tab->size_ind;
2804 free_fixed_ptr(new_tab);
2808set_rebuild_cleanup(
set_table *
const tab)
2810 tab->entries_start = 0;
2811 tab->entries_bound = tab->num_entries;
2812 tab->rebuilds_num++;
2827static inline st_index_t
2828set_secondary_hash(st_index_t ind,
set_table *tab, st_index_t *perturb)
2831 ind = (ind << 2) + ind + *perturb + 1;
2832 return set_hash_bin(ind, tab);
2839static inline st_index_t
2840set_find_entry(
set_table *tab, st_hash_t hash_value, st_data_t key)
2842 int eq_p, rebuilt_p;
2843 st_index_t i, bound;
2844 set_table_entry *entries;
2846 bound = tab->entries_bound;
2848 for (i = tab->entries_start; i < bound; i++) {
2849 SET_DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
2850 if (EXPECT(rebuilt_p, 0))
2851 return REBUILT_TABLE_ENTRY_IND;
2855 return UNDEFINED_ENTRY_IND;
2867set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2869 int eq_p, rebuilt_p;
2871#ifdef QUADRATIC_PROBE
2877 set_table_entry *entries = tab->
entries;
2879 ind = set_hash_bin(hash_value, tab);
2880#ifdef QUADRATIC_PROBE
2883 perturb = hash_value;
2886 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2887 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2888 SET_DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2889 if (EXPECT(rebuilt_p, 0))
2890 return REBUILT_TABLE_ENTRY_IND;
2894 else if (EMPTY_BIN_P(bin))
2895 return UNDEFINED_ENTRY_IND;
2896#ifdef QUADRATIC_PROBE
2897 ind = set_hash_bin(ind + d, tab);
2900 ind = set_secondary_hash(ind, tab, &perturb);
2911set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2913 int eq_p, rebuilt_p;
2915#ifdef QUADRATIC_PROBE
2921 set_table_entry *entries = tab->
entries;
2923 ind = set_hash_bin(hash_value, tab);
2924#ifdef QUADRATIC_PROBE
2927 perturb = hash_value;
2930 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2931 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2932 SET_DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2933 if (EXPECT(rebuilt_p, 0))
2934 return REBUILT_TABLE_BIN_IND;
2938 else if (EMPTY_BIN_P(bin))
2939 return UNDEFINED_BIN_IND;
2940#ifdef QUADRATIC_PROBE
2941 ind = set_hash_bin(ind + d, tab);
2944 ind = set_secondary_hash(ind, tab, &perturb);
2954set_find_table_bin_ind_direct(
set_table *tab, st_hash_t hash_value, st_data_t key)
2957#ifdef QUADRATIC_PROBE
2964 ind = set_hash_bin(hash_value, tab);
2965#ifdef QUADRATIC_PROBE
2968 perturb = hash_value;
2971 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2972 if (EMPTY_OR_DELETED_BIN_P(bin))
2974#ifdef QUADRATIC_PROBE
2975 ind = set_hash_bin(ind + d, tab);
2978 ind = set_secondary_hash(ind, tab, &perturb);
2985#define MARK_SET_BIN_EMPTY(tab, i) (set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, EMPTY_BIN))
2997set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2998 st_data_t key, st_index_t *bin_ind)
3000 int eq_p, rebuilt_p;
3002 st_hash_t curr_hash_value = *hash_value;
3003#ifdef QUADRATIC_PROBE
3008 st_index_t entry_index;
3009 st_index_t firset_deleted_bin_ind;
3010 set_table_entry *entries;
3012 ind = set_hash_bin(curr_hash_value, tab);
3013#ifdef QUADRATIC_PROBE
3016 perturb = curr_hash_value;
3018 firset_deleted_bin_ind = UNDEFINED_BIN_IND;
3021 entry_index = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
3022 if (EMPTY_BIN_P(entry_index)) {
3024 entry_index = UNDEFINED_ENTRY_IND;
3025 if (firset_deleted_bin_ind != UNDEFINED_BIN_IND) {
3027 ind = firset_deleted_bin_ind;
3028 MARK_SET_BIN_EMPTY(tab, ind);
3032 else if (! DELETED_BIN_P(entry_index)) {
3033 SET_DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
3034 if (EXPECT(rebuilt_p, 0))
3035 return REBUILT_TABLE_ENTRY_IND;
3039 else if (firset_deleted_bin_ind == UNDEFINED_BIN_IND)
3040 firset_deleted_bin_ind = ind;
3041#ifdef QUADRATIC_PROBE
3042 ind = set_hash_bin(ind + d, tab);
3045 ind = set_secondary_hash(ind, tab, &perturb);
3055set_table_lookup(
set_table *tab, st_data_t key)
3058 st_hash_t hash = set_do_hash(key, tab);
3061 if (!set_has_bins(tab)) {
3062 bin = set_find_entry(tab, hash, key);
3063 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3065 if (bin == UNDEFINED_ENTRY_IND)
3069 bin = set_find_table_entry_ind(tab, hash, key);
3070 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3072 if (bin == UNDEFINED_ENTRY_IND)
3081set_rebuild_table_if_necessary(
set_table *tab)
3083 st_index_t bound = tab->entries_bound;
3085 if (bound == set_get_allocated_entries(tab) || tab->entries_start == MAX_ENTRIES_START) {
3086 set_rebuild_table(tab);
3094set_insert(
set_table *tab, st_data_t key)
3096 set_table_entry *entry;
3099 st_hash_t hash_value;
3103 hash_value = set_do_hash(key, tab);
3105 set_rebuild_table_if_necessary(tab);
3106 if (!set_has_bins(tab)) {
3107 bin = set_find_entry(tab, hash_value, key);
3108 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3110 new_p = bin == UNDEFINED_ENTRY_IND;
3113 bin_ind = UNDEFINED_BIN_IND;
3116 bin = set_find_table_bin_ptr_and_reserve(tab, &hash_value,
3118 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3120 new_p = bin == UNDEFINED_ENTRY_IND;
3124 ind = tab->entries_bound++;
3126 entry->hash = hash_value;
3128 if (bin_ind != UNDEFINED_BIN_IND)
3129 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
3139 *new_tab = *old_tab;
3140 size_t memsize = set_allocated_entries_size(old_tab) + set_bins_size(old_tab);
3141 new_tab->
entries = (set_table_entry *)malloc(memsize);
3152 if (set_replace(new_tab, old_tab) == NULL) {
3153 set_free_table(new_tab);
3163set_update_range_for_deleted(
set_table *tab, st_index_t n)
3167 if (tab->entries_start == n) {
3168 st_index_t start = n + 1;
3169 st_index_t bound = tab->entries_bound;
3170 set_table_entry *entries = tab->
entries;
3171 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
3172 tab->entries_start = start > MAX_ENTRIES_START ? MAX_ENTRIES_START : (
unsigned int)start;
3179#define MARK_SET_BIN_DELETED(tab, i) \
3181 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, DELETED_BIN); \
3187set_table_delete(
set_table *tab, st_data_t *key)
3189 set_table_entry *entry;
3194 hash = set_do_hash(*key, tab);
3196 if (!set_has_bins(tab)) {
3197 bin = set_find_entry(tab, hash, *key);
3198 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3200 if (bin == UNDEFINED_ENTRY_IND) {
3205 bin_ind = set_find_table_bin_ind(tab, hash, *key);
3206 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3208 if (bin_ind == UNDEFINED_BIN_IND) {
3211 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3212 MARK_SET_BIN_DELETED(tab, bin_ind);
3216 MARK_ENTRY_DELETED(entry);
3218 set_update_range_for_deleted(tab, bin);
3231set_general_foreach(
set_table *tab, set_foreach_check_callback_func *func,
3232 set_update_callback_func *replace, st_data_t arg,
3237 set_table_entry *entries, *curr_entry_ptr;
3238 enum st_retval retval;
3239 st_index_t i, rebuilds_num;
3242 int error_p, packed_p = !set_has_bins(tab);
3247 for (i = tab->entries_start; i < tab->entries_bound; i++) {
3248 curr_entry_ptr = &entries[i];
3249 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
3251 key = curr_entry_ptr->key;
3252 rebuilds_num = tab->rebuilds_num;
3253 hash = curr_entry_ptr->hash;
3254 retval = (*func)(key, arg, 0);
3256 if (retval == ST_REPLACE && replace) {
3257 retval = (*replace)(&key, arg, TRUE);
3258 curr_entry_ptr->key = key;
3261 if (rebuilds_num != tab->rebuilds_num) {
3264 packed_p = !set_has_bins(tab);
3266 i = set_find_entry(tab, hash, key);
3267 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3269 error_p = i == UNDEFINED_ENTRY_IND;
3272 i = set_find_table_entry_ind(tab, hash, key);
3273 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3275 error_p = i == UNDEFINED_ENTRY_IND;
3278 if (error_p && check_p) {
3280 retval = (*func)(0, arg, 1);
3283 curr_entry_ptr = &entries[i];
3296 st_data_t key = curr_entry_ptr->key;
3300 bin = set_find_entry(tab, hash, key);
3301 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3303 if (bin == UNDEFINED_ENTRY_IND)
3307 bin_ind = set_find_table_bin_ind(tab, hash, key);
3308 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3310 if (bin_ind == UNDEFINED_BIN_IND)
3312 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3313 MARK_SET_BIN_DELETED(tab, bin_ind);
3315 curr_entry_ptr = &entries[bin];
3316 MARK_ENTRY_DELETED(curr_entry_ptr);
3318 set_update_range_for_deleted(tab, bin);
3327set_foreach_with_replace(
set_table *tab, set_foreach_check_callback_func *func, set_update_callback_func *replace, st_data_t arg)
3329 return set_general_foreach(tab, func, replace, arg, TRUE);
3333 set_foreach_callback_func *func;
3338set_apply_functor(st_data_t k, st_data_t d,
int _)
3340 const struct set_functor *f = (
void *)d;
3341 return f->func(k, f->arg);
3345set_table_foreach(
set_table *tab, set_foreach_callback_func *func, st_data_t arg)
3347 const struct set_functor f = { func, arg };
3348 return set_general_foreach(tab, set_apply_functor, NULL, (st_data_t)&f, FALSE);
3353set_foreach_check(
set_table *tab, set_foreach_check_callback_func *func, st_data_t arg,
3354 st_data_t never ATTRIBUTE_UNUSED)
3356 return set_general_foreach(tab, func, NULL, arg, TRUE);
3362set_keys(
set_table *tab, st_data_t *keys, st_index_t size)
3364 st_index_t i, bound;
3365 st_data_t key, *keys_start, *keys_end;
3366 set_table_entry *curr_entry_ptr, *entries = tab->
entries;
3368 bound = tab->entries_bound;
3370 keys_end = keys + size;
3371 for (i = tab->entries_start; i < bound; i++) {
3372 if (keys == keys_end)
3374 curr_entry_ptr = &entries[i];
3375 key = curr_entry_ptr->key;
3376 if (! DELETED_ENTRY_P(curr_entry_ptr))
3380 return keys - keys_start;
3386 st_index_t num = tab->num_entries;
3387 if (REBUILD_THRESHOLD * num <= set_get_allocated_entries(tab)) {
3389 set_table *new_tab = set_init_table_with_size(NULL, tab->type, 2 * num);
3390 set_rebuild_table_with(new_tab, tab);
3391 set_rebuild_move_table(new_tab, tab);
3392 set_rebuild_cleanup(tab);
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
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.
set_table_entry * entries
Array of size 2^entry_power.
uintptr_t VALUE
Type that represents a Ruby object.