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/static_assert.h"
115#include "internal/st.h"
116#include "ruby_assert.h"
126#define PREFETCH(addr, write_p) __builtin_prefetch(addr, write_p)
127#define EXPECT(expr, val) __builtin_expect(expr, val)
128#define ATTRIBUTE_UNUSED __attribute__((unused))
130#define PREFETCH(addr, write_p)
131#define EXPECT(expr, val) (expr)
132#define ATTRIBUTE_UNUSED
135#define MAX_ENTRIES_START ((unsigned int)-1)
138typedef st_index_t st_hash_t;
146#define type_numhash st_hashtype_num
147static const struct st_hash_type st_hashtype_num = {
152static int st_strcmp(st_data_t, st_data_t);
153static st_index_t strhash(st_data_t);
154static const struct st_hash_type type_strhash = {
159static int st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs);
160static st_index_t strcasehash(st_data_t);
161static const struct st_hash_type type_strcasehash = {
162 st_locale_insensitive_strcasecmp_i,
169#define ST_INIT_VAL 0xafafafafafafafaf
170#define ST_INIT_VAL_BYTE 0xafa
177#define malloc ruby_xmalloc
178#define calloc ruby_xcalloc
179#define realloc ruby_xrealloc
180#define sized_realloc ruby_xrealloc_sized
181#define free ruby_xfree
182#define sized_free ruby_xfree_sized
183#define free_fixed_ptr(v) ruby_xfree_sized((v), sizeof(*(v)))
185#define sized_realloc(ptr, new_size, old_size) realloc(ptr, new_size)
186#define sized_free(v, s) free(v)
187#define free_fixed_ptr(v) free(v)
195entry_equal(
const struct st_hash_type *type,
196 st_hash_t entry_hash, st_data_t entry_key,
197 st_hash_t hash_val, st_data_t key)
199 return (entry_hash == hash_val) &&
200 ((entry_key == key) || (*
type->compare)(key, entry_key) == 0);
206ptr_equal_check(
const st_table *tab,
const st_table_entry *entry,
207 st_hash_t hash_val, st_data_t key,
208 int *res,
int *rebuilt_p)
210 unsigned int old_rebuilds_num = tab->rebuilds_num;
211 *res = entry_equal(tab->type, entry->hash, entry->key, hash_val, key);
212 *rebuilt_p = old_rebuilds_num != tab->rebuilds_num;
215#define DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
216 ptr_equal_check((tab), (ptr), (hash_val), (key), &(res), &(rebuilt_p))
221 unsigned char entry_power;
225 unsigned char bin_power;
227 unsigned char size_ind;
230 st_index_t bins_words;
234#if SIZEOF_ST_INDEX_T == 8
253 {16, 17, 2, 0x10000},
254 {17, 18, 2, 0x20000},
255 {18, 19, 2, 0x40000},
256 {19, 20, 2, 0x80000},
257 {20, 21, 2, 0x100000},
258 {21, 22, 2, 0x200000},
259 {22, 23, 2, 0x400000},
260 {23, 24, 2, 0x800000},
261 {24, 25, 2, 0x1000000},
262 {25, 26, 2, 0x2000000},
263 {26, 27, 2, 0x4000000},
264 {27, 28, 2, 0x8000000},
265 {28, 29, 2, 0x10000000},
266 {29, 30, 2, 0x20000000},
267 {30, 31, 2, 0x40000000},
268 {31, 32, 2, 0x80000000},
269 {32, 33, 3, 0x200000000},
270 {33, 34, 3, 0x400000000},
271 {34, 35, 3, 0x800000000},
272 {35, 36, 3, 0x1000000000},
273 {36, 37, 3, 0x2000000000},
274 {37, 38, 3, 0x4000000000},
275 {38, 39, 3, 0x8000000000},
276 {39, 40, 3, 0x10000000000},
277 {40, 41, 3, 0x20000000000},
278 {41, 42, 3, 0x40000000000},
279 {42, 43, 3, 0x80000000000},
280 {43, 44, 3, 0x100000000000},
281 {44, 45, 3, 0x200000000000},
282 {45, 46, 3, 0x400000000000},
283 {46, 47, 3, 0x800000000000},
284 {47, 48, 3, 0x1000000000000},
285 {48, 49, 3, 0x2000000000000},
286 {49, 50, 3, 0x4000000000000},
287 {50, 51, 3, 0x8000000000000},
288 {51, 52, 3, 0x10000000000000},
289 {52, 53, 3, 0x20000000000000},
290 {53, 54, 3, 0x40000000000000},
291 {54, 55, 3, 0x80000000000000},
292 {55, 56, 3, 0x100000000000000},
293 {56, 57, 3, 0x200000000000000},
294 {57, 58, 3, 0x400000000000000},
295#define MAX_BINS_WORDS 0x800000000000000
296 {58, 59, 3, MAX_BINS_WORDS},
319 {16, 17, 2, 0x20000},
320 {17, 18, 2, 0x40000},
321 {18, 19, 2, 0x80000},
322 {19, 20, 2, 0x100000},
323 {20, 21, 2, 0x200000},
324 {21, 22, 2, 0x400000},
325 {22, 23, 2, 0x800000},
326 {23, 24, 2, 0x1000000},
327 {24, 25, 2, 0x2000000},
328 {25, 26, 2, 0x4000000},
329 {26, 27, 2, 0x8000000},
330#define MAX_BINS_WORDS 0x10000000
331 {27, 28, 2, MAX_BINS_WORDS},
338#define MAX_ADDRESSABLE_ENTRIES (SIZE_MAX / sizeof(st_table_entry))
339#define MAX_ENTRIES (((st_index_t) 1)<<MAX_POWER2)
340STATIC_ASSERT(max_power_entries_overflow, MAX_ENTRIES < MAX_ADDRESSABLE_ENTRIES);
341#define MAX_BINS_SIZE (SIZE_MAX - (MAX_ENTRIES * sizeof(st_table_entry)))
342STATIC_ASSERT(max_power_bins_overflow, (MAX_BINS_SIZE /
sizeof(st_index_t)) > MAX_BINS_WORDS);
346#define RESERVED_HASH_VAL (~(st_hash_t) 0)
347#define RESERVED_HASH_SUBSTITUTION_VAL ((st_hash_t) 0)
349static inline st_hash_t
350normalize_hash_value(st_hash_t hash)
354 return hash == RESERVED_HASH_VAL ? RESERVED_HASH_SUBSTITUTION_VAL : hash;
358static inline st_hash_t
359do_hash(st_data_t key, st_table *tab)
361 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
362 return normalize_hash_value(hash);
366#define MINIMAL_POWER2 2
368#if MINIMAL_POWER2 < 2
369#error "MINIMAL_POWER2 should be >= 2"
374#define MAX_POWER2_FOR_TABLES_WITHOUT_BINS 4
378get_power2(st_index_t size)
380 unsigned int n = ST_INDEX_BITS - nlz_intptr(size);
382 return n < MINIMAL_POWER2 ? MINIMAL_POWER2 : n;
393static inline st_index_t
394get_bin(st_index_t *bins,
int s, st_index_t n)
396 return (s == 0 ? ((
unsigned char *) bins)[n]
397 : s == 1 ? ((
unsigned short *) bins)[n]
398 : s == 2 ? ((
unsigned int *) bins)[n]
399 : ((st_index_t *) bins)[n]);
405set_bin(st_index_t *bins,
int s, st_index_t n, st_index_t v)
407 if (s == 0) ((
unsigned char *) bins)[n] = (
unsigned char) v;
408 else if (s == 1) ((
unsigned short *) bins)[n] = (
unsigned short) v;
409 else if (s == 2) ((
unsigned int *) bins)[n] = (
unsigned int) v;
410 else ((st_index_t *) bins)[n] = v;
423#define MARK_BIN_EMPTY(tab, i) (set_bin(st_bins_ptr(tab), get_size_ind(tab), i, EMPTY_BIN))
427#define UNDEFINED_ENTRY_IND (~(st_index_t) 0)
428#define UNDEFINED_BIN_IND (~(st_index_t) 0)
432#define REBUILT_TABLE_ENTRY_IND (~(st_index_t) 1)
433#define REBUILT_TABLE_BIN_IND (~(st_index_t) 1)
438#define MARK_BIN_DELETED(tab, i) \
440 set_bin(st_bins_ptr(tab), get_size_ind(tab), i, DELETED_BIN); \
445#define EMPTY_BIN_P(b) ((b) == EMPTY_BIN)
446#define DELETED_BIN_P(b) ((b) == DELETED_BIN)
447#define EMPTY_OR_DELETED_BIN_P(b) ((b) <= DELETED_BIN)
451#define IND_EMPTY_BIN_P(tab, i) (EMPTY_BIN_P(get_bin(st_bins_ptr(tab), get_size_ind(tab), i)))
452#define IND_DELETED_BIN_P(tab, i) (DELETED_BIN_P(get_bin(st_bins_ptr(tab), get_size_ind(tab), i)))
453#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)))
457#define MARK_ENTRY_DELETED(e_ptr) ((e_ptr)->hash = RESERVED_HASH_VAL)
458#define DELETED_ENTRY_P(e_ptr) ((e_ptr)->hash == RESERVED_HASH_VAL)
461static inline st_index_t
462get_allocated_entries(
const st_table *tab)
464 return ((st_index_t) 1)<<tab->entry_power;
468static inline unsigned int
469get_size_ind(
const st_table *tab)
471 return tab->size_ind;
475static inline st_index_t
476get_bins_num(
const st_table *tab)
478 return ((st_index_t) 1)<<tab->bin_power;
482static inline st_index_t
483bins_mask(
const st_table *tab)
485 return get_bins_num(tab) - 1;
489st_has_bins(
const st_table *tab)
491 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
495st_allocated_entries_size(
const st_table *tab)
497 return get_allocated_entries(tab) *
sizeof(st_table_entry);
500static inline st_index_t *
501st_bins_ptr(
const st_table *tab)
503 if (st_has_bins(tab)) {
504 return (st_index_t *)(((
char *)tab->entries) + st_allocated_entries_size(tab));
512static inline st_index_t
513hash_bin(st_hash_t hash_value, st_table *tab)
515 return hash_value & bins_mask(tab);
519static inline st_index_t
520bins_size(
const st_table *tab)
522 if (st_has_bins(tab)) {
523 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
530initialize_bins(st_table *tab)
532 memset(st_bins_ptr(tab), 0, bins_size(tab));
537make_tab_empty(st_table *tab)
539 tab->num_entries = 0;
540 tab->entries_start = tab->entries_bound = 0;
541 if (st_bins_ptr(tab) != NULL)
542 initialize_bins(tab);
550 int all, total, num, str, strcase;
555static int init_st = 0;
562 char fname[10+
sizeof(long)*3];
564 if (!collision.total)
return;
565 f = fopen((snprintf(fname,
sizeof(fname),
"/tmp/col%ld", (
long)getpid()), fname),
"w");
568 fprintf(f,
"collision: %d / %d (%6.2f)\n", collision.all, collision.total,
569 ((
double)collision.all / (collision.total)) * 100);
570 fprintf(f,
"num: %d, str: %d, strcase: %d\n", collision.num, collision.str, collision.strcase);
576st_init_existing_table_with_size(st_table *tab,
const struct st_hash_type *type, st_index_t size)
583 const char *e = getenv(
"ST_HASH_LOG");
584 if (!e || !*e) init_st = 1;
593 n = get_power2(size);
600 tab->entry_power = n;
601 tab->bin_power = features[n].bin_power;
602 tab->size_ind = features[n].size_ind;
608 tab->num_entries = 0;
609 tab->entries_start = tab->entries_bound = 0;
611 size_t memsize = get_allocated_entries(tab) *
sizeof(st_table_entry);
612 if (tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS) {
613 memsize += bins_size(tab);
615 tab->entries = (st_table_entry *)malloc(memsize);
617 if (tab->entries == NULL) {
623 tab->rebuilds_num = 0;
628st_init_existing_numtable_with_size(st_table *tab, st_index_t size)
630 return st_init_existing_table_with_size(tab, &type_numhash, size);
637st_init_table_with_size(
const struct st_hash_type *type, st_index_t size)
639 st_table *tab = malloc(
sizeof(st_table));
646 st_init_existing_table_with_size(tab, type, size);
648 if (st_init_existing_table_with_size(tab, type, size) == NULL) {
658st_table_size(
const struct st_table *tbl)
660 return tbl->num_entries;
666st_init_table(
const struct st_hash_type *type)
668 return st_init_table_with_size(type, 0);
674st_init_numtable(
void)
676 return st_init_table(&type_numhash);
681st_init_numtable_with_size(st_index_t size)
683 return st_init_table_with_size(&type_numhash, size);
689st_init_strtable(
void)
691 return st_init_table(&type_strhash);
696st_init_strtable_with_size(st_index_t size)
698 return st_init_table_with_size(&type_strhash, size);
702st_init_existing_strtable_with_size(st_table *tab, st_index_t size)
704 return st_init_existing_table_with_size(tab, &type_strhash, size);
711st_init_strcasetable(
void)
713 return st_init_table(&type_strcasehash);
719st_init_strcasetable_with_size(st_index_t size)
721 return st_init_table_with_size(&type_strcasehash, size);
726st_clear(st_table *tab)
733st_entries_memsize(
const st_table *tab)
735 return get_allocated_entries(tab) *
sizeof(st_table_entry);
739st_free_entries(
const st_table *tab)
741 sized_free(tab->entries, st_entries_memsize(tab) + bins_size(tab));
745st_free_embedded_table(st_table *tab)
747 st_free_entries(tab);
752st_free_table(st_table *tab)
754 st_free_embedded_table(tab);
759st_allocated_memsize(
const st_table *tab)
762 return bins_size(tab) + st_entries_memsize(tab);
767st_memsize(
const st_table *tab)
770 return sizeof(st_table) + st_allocated_memsize(tab);
774find_table_entry_ind(st_table *tab, st_hash_t hash_value, st_data_t key);
777find_table_bin_ind(st_table *tab, st_hash_t hash_value, st_data_t key);
780find_table_bin_ind_direct(st_table *table, st_hash_t hash_value, st_data_t key);
783find_table_bin_ptr_and_reserve(st_table *tab, st_hash_t *hash_value,
784 st_data_t key, st_index_t *bin_ind);
788count_collision(
const struct st_hash_type *type)
791 if (type == &type_numhash) {
794 else if (type == &type_strhash) {
797 else if (type == &type_strcasehash) {
802#define COLLISION (collision_check ? count_collision(tab->type) : (void)0)
803#define FOUND_BIN (collision_check ? collision.total++ : (void)0)
804#define collision_check 0
813#define REBUILD_THRESHOLD 4
815#if REBUILD_THRESHOLD < 2
816#error "REBUILD_THRESHOLD should be >= 2"
819static void rebuild_table_with(st_table *
const new_tab, st_table *
const tab);
820static void rebuild_move_table(st_table *
const new_tab, st_table *
const tab);
821static void rebuild_cleanup(st_table *
const tab);
828rebuild_table(st_table *tab)
830 if ((2 * tab->num_entries <= get_allocated_entries(tab)
831 && REBUILD_THRESHOLD * tab->num_entries > get_allocated_entries(tab))
832 || tab->num_entries < (1 << MINIMAL_POWER2)) {
834 tab->num_entries = 0;
835 if (st_has_bins(tab))
836 initialize_bins(tab);
837 rebuild_table_with(tab, tab);
844 new_tab = st_init_table_with_size(tab->type,
845 2 * tab->num_entries - 1);
846 rebuild_table_with(new_tab, tab);
847 rebuild_move_table(new_tab, tab);
849 rebuild_cleanup(tab);
853rebuild_table_with(st_table *
const new_tab, st_table *
const tab)
856 unsigned int size_ind;
857 st_table_entry *new_entries;
858 st_table_entry *curr_entry_ptr;
862 new_entries = new_tab->entries;
865 bins = st_bins_ptr(new_tab);
866 size_ind = get_size_ind(new_tab);
867 st_index_t bound = tab->entries_bound;
868 st_table_entry *entries = tab->entries;
870 for (i = tab->entries_start; i < bound; i++) {
871 curr_entry_ptr = &entries[i];
872 PREFETCH(entries + i + 1, 0);
873 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
875 if (&new_entries[ni] != curr_entry_ptr)
876 new_entries[ni] = *curr_entry_ptr;
877 if (EXPECT(bins != NULL, 1)) {
878 bin_ind = find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
879 curr_entry_ptr->key);
880 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
882 new_tab->num_entries++;
886 assert(new_tab->num_entries == tab->num_entries);
890rebuild_move_table(st_table *
const new_tab, st_table *
const tab)
892 st_free_entries(tab);
893 tab->entry_power = new_tab->entry_power;
894 tab->bin_power = new_tab->bin_power;
895 tab->size_ind = new_tab->size_ind;
896 tab->entries = new_tab->entries;
897 free_fixed_ptr(new_tab);
901rebuild_cleanup(st_table *
const tab)
903 tab->entries_start = 0;
904 tab->entries_bound = tab->num_entries;
920static inline st_index_t
921secondary_hash(st_index_t ind, st_table *tab, st_index_t *perturb)
924 ind = (ind << 2) + ind + *perturb + 1;
925 return hash_bin(ind, tab);
932static inline st_index_t
933find_entry(st_table *tab, st_hash_t hash_value, st_data_t key)
937 st_table_entry *entries;
939 bound = tab->entries_bound;
940 entries = tab->entries;
941 for (i = tab->entries_start; i < bound; i++) {
942 DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
943 if (EXPECT(rebuilt_p, 0))
944 return REBUILT_TABLE_ENTRY_IND;
948 return UNDEFINED_ENTRY_IND;
960find_table_entry_ind(st_table *tab, st_hash_t hash_value, st_data_t key)
964#ifdef QUADRATIC_PROBE
970 st_table_entry *entries = tab->entries;
972 ind = hash_bin(hash_value, tab);
973#ifdef QUADRATIC_PROBE
976 perturb = hash_value;
980 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
981 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
982 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
983 if (EXPECT(rebuilt_p, 0))
984 return REBUILT_TABLE_ENTRY_IND;
988 else if (EMPTY_BIN_P(bin))
989 return UNDEFINED_ENTRY_IND;
990#ifdef QUADRATIC_PROBE
991 ind = hash_bin(ind + d, tab);
994 ind = secondary_hash(ind, tab, &perturb);
1006find_table_bin_ind(st_table *tab, st_hash_t hash_value, st_data_t key)
1008 int eq_p, rebuilt_p;
1010#ifdef QUADRATIC_PROBE
1016 st_table_entry *entries = tab->entries;
1018 ind = hash_bin(hash_value, tab);
1019#ifdef QUADRATIC_PROBE
1022 perturb = hash_value;
1026 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1027 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
1028 DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
1029 if (EXPECT(rebuilt_p, 0))
1030 return REBUILT_TABLE_BIN_IND;
1034 else if (EMPTY_BIN_P(bin))
1035 return UNDEFINED_BIN_IND;
1036#ifdef QUADRATIC_PROBE
1037 ind = hash_bin(ind + d, tab);
1040 ind = secondary_hash(ind, tab, &perturb);
1051find_table_bin_ind_direct(st_table *tab, st_hash_t hash_value, st_data_t key)
1054#ifdef QUADRATIC_PROBE
1061 ind = hash_bin(hash_value, tab);
1062#ifdef QUADRATIC_PROBE
1065 perturb = hash_value;
1069 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1070 if (EMPTY_OR_DELETED_BIN_P(bin))
1072#ifdef QUADRATIC_PROBE
1073 ind = hash_bin(ind + d, tab);
1076 ind = secondary_hash(ind, tab, &perturb);
1092find_table_bin_ptr_and_reserve(st_table *tab, st_hash_t *hash_value,
1093 st_data_t key, st_index_t *bin_ind)
1095 int eq_p, rebuilt_p;
1097 st_hash_t curr_hash_value = *hash_value;
1098#ifdef QUADRATIC_PROBE
1103 st_index_t entry_index;
1104 st_index_t first_deleted_bin_ind;
1105 st_table_entry *entries;
1107 ind = hash_bin(curr_hash_value, tab);
1108#ifdef QUADRATIC_PROBE
1111 perturb = curr_hash_value;
1114 first_deleted_bin_ind = UNDEFINED_BIN_IND;
1115 entries = tab->entries;
1117 entry_index = get_bin(st_bins_ptr(tab), get_size_ind(tab), ind);
1118 if (EMPTY_BIN_P(entry_index)) {
1120 entry_index = UNDEFINED_ENTRY_IND;
1121 if (first_deleted_bin_ind != UNDEFINED_BIN_IND) {
1123 ind = first_deleted_bin_ind;
1124 MARK_BIN_EMPTY(tab, ind);
1128 else if (! DELETED_BIN_P(entry_index)) {
1129 DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
1130 if (EXPECT(rebuilt_p, 0))
1131 return REBUILT_TABLE_ENTRY_IND;
1135 else if (first_deleted_bin_ind == UNDEFINED_BIN_IND)
1136 first_deleted_bin_ind = ind;
1137#ifdef QUADRATIC_PROBE
1138 ind = hash_bin(ind + d, tab);
1141 ind = secondary_hash(ind, tab, &perturb);
1152st_lookup(st_table *tab, st_data_t key, st_data_t *value)
1155 st_hash_t hash = do_hash(key, tab);
1158 if (!st_has_bins(tab)) {
1159 bin = find_entry(tab, hash, key);
1160 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1162 if (bin == UNDEFINED_ENTRY_IND)
1166 bin = find_table_entry_ind(tab, hash, key);
1167 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1169 if (bin == UNDEFINED_ENTRY_IND)
1174 *value = tab->entries[bin].record;
1181st_get_key(st_table *tab, st_data_t key, st_data_t *result)
1184 st_hash_t hash = do_hash(key, tab);
1187 if (!st_has_bins(tab)) {
1188 bin = find_entry(tab, hash, key);
1189 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1191 if (bin == UNDEFINED_ENTRY_IND)
1195 bin = find_table_entry_ind(tab, hash, key);
1196 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1198 if (bin == UNDEFINED_ENTRY_IND)
1203 *result = tab->entries[bin].key;
1209rebuild_table_if_necessary(st_table *tab)
1211 st_index_t bound = tab->entries_bound;
1213 if (bound == get_allocated_entries(tab) || tab->entries_start == MAX_ENTRIES_START) {
1222st_insert(st_table *tab, st_data_t key, st_data_t value)
1224 st_table_entry *entry;
1227 st_hash_t hash_value;
1231 hash_value = do_hash(key, tab);
1233 rebuild_table_if_necessary(tab);
1234 if (!st_has_bins(tab)) {
1235 bin = find_entry(tab, hash_value, key);
1236 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1238 new_p = bin == UNDEFINED_ENTRY_IND;
1241 bin_ind = UNDEFINED_BIN_IND;
1244 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1246 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1248 new_p = bin == UNDEFINED_ENTRY_IND;
1252 ind = tab->entries_bound++;
1253 entry = &tab->entries[ind];
1254 entry->hash = hash_value;
1256 entry->record = value;
1257 if (bin_ind != UNDEFINED_BIN_IND)
1258 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1261 tab->entries[bin].record = value;
1270st_insert_no_rebuild(st_table *tab, st_data_t key, st_data_t value)
1272 if (tab->entries_bound == get_allocated_entries(tab)) {
1276 return st_insert(tab, key, value);
1283st_add_direct_with_hash(st_table *tab,
1284 st_data_t key, st_data_t value, st_hash_t hash)
1286 st_table_entry *entry;
1290 assert(hash != RESERVED_HASH_VAL);
1292 rebuild_table_if_necessary(tab);
1293 ind = tab->entries_bound++;
1294 entry = &tab->entries[ind];
1297 entry->record = value;
1299 if (st_has_bins(tab)) {
1300 bin_ind = find_table_bin_ind_direct(tab, hash, key);
1301 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1306rb_st_add_direct_with_hash(st_table *tab,
1307 st_data_t key, st_data_t value, st_hash_t hash)
1309 st_add_direct_with_hash(tab, key, value, normalize_hash_value(hash));
1315st_add_direct(st_table *tab, st_data_t key, st_data_t value)
1317 st_hash_t hash_value;
1319 hash_value = do_hash(key, tab);
1320 st_add_direct_with_hash(tab, key, value, hash_value);
1327st_insert2(st_table *tab, st_data_t key, st_data_t value,
1328 st_data_t (*func)(st_data_t))
1330 st_table_entry *entry;
1333 st_hash_t hash_value;
1337 hash_value = do_hash(key, tab);
1339 rebuild_table_if_necessary(tab);
1340 if (!st_has_bins(tab)) {
1341 bin = find_entry(tab, hash_value, key);
1342 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1344 new_p = bin == UNDEFINED_ENTRY_IND;
1347 bin_ind = UNDEFINED_BIN_IND;
1350 bin = find_table_bin_ptr_and_reserve(tab, &hash_value,
1352 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1354 new_p = bin == UNDEFINED_ENTRY_IND;
1359 ind = tab->entries_bound++;
1360 entry = &tab->entries[ind];
1361 entry->hash = hash_value;
1363 entry->record = value;
1364 if (bin_ind != UNDEFINED_BIN_IND)
1365 set_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
1368 tab->entries[bin].record = value;
1373st_replace_no_check(st_table *new_tab, st_table *old_tab)
1375 *new_tab = *old_tab;
1376 size_t memsize = get_allocated_entries(old_tab) *
sizeof(st_table_entry);
1377 memsize += bins_size(old_tab);
1378 new_tab->entries = (st_table_entry *) malloc(memsize);
1380 if (new_tab->entries == NULL) {
1384 MEMCPY(new_tab->entries, old_tab->entries,
char, memsize);
1392st_replace(st_table *new_tab, st_table *old_tab)
1395 return st_replace_no_check(new_tab, old_tab);
1400st_copy(st_table *old_tab)
1404 new_tab = (st_table *) malloc(
sizeof(st_table));
1406 if (new_tab == NULL)
1410 if (st_replace_no_check(new_tab, old_tab) == NULL) {
1411 st_free_table(new_tab);
1421update_range_for_deleted(st_table *tab, st_index_t n)
1425 if (tab->entries_start == n) {
1426 st_index_t start = n + 1;
1427 st_index_t bound = tab->entries_bound;
1428 st_table_entry *entries = tab->entries;
1429 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
1430 tab->entries_start = start > MAX_ENTRIES_START ? MAX_ENTRIES_START : (
unsigned int)start;
1439st_general_delete(st_table *tab, st_data_t *key, st_data_t *value)
1441 st_table_entry *entry;
1446 hash = do_hash(*key, tab);
1448 if (!st_has_bins(tab)) {
1449 bin = find_entry(tab, hash, *key);
1450 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1452 if (bin == UNDEFINED_ENTRY_IND) {
1453 if (value != 0) *value = 0;
1458 bin_ind = find_table_bin_ind(tab, hash, *key);
1459 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1461 if (bin_ind == UNDEFINED_BIN_IND) {
1462 if (value != 0) *value = 0;
1465 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1466 MARK_BIN_DELETED(tab, bin_ind);
1468 entry = &tab->entries[bin];
1470 if (value != 0) *value = entry->record;
1471 MARK_ENTRY_DELETED(entry);
1473 update_range_for_deleted(tab, bin);
1478st_delete(st_table *tab, st_data_t *key, st_data_t *value)
1480 return st_general_delete(tab, key, value);
1489st_delete_safe(st_table *tab, st_data_t *key, st_data_t *value,
1490 st_data_t never ATTRIBUTE_UNUSED)
1492 return st_general_delete(tab, key, value);
1500st_shift(st_table *tab, st_data_t *key, st_data_t *value)
1502 st_index_t i, bound;
1504 st_table_entry *entries, *curr_entry_ptr;
1507 entries = tab->entries;
1508 bound = tab->entries_bound;
1509 for (i = tab->entries_start; i < bound; i++) {
1510 curr_entry_ptr = &entries[i];
1511 if (! DELETED_ENTRY_P(curr_entry_ptr)) {
1512 st_hash_t entry_hash = curr_entry_ptr->hash;
1513 st_data_t entry_key = curr_entry_ptr->key;
1515 if (value != 0) *value = curr_entry_ptr->record;
1518 if (!st_has_bins(tab)) {
1519 bin = find_entry(tab, entry_hash, entry_key);
1520 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0)) {
1521 entries = tab->entries;
1524 curr_entry_ptr = &entries[bin];
1527 bin_ind = find_table_bin_ind(tab, entry_hash, entry_key);
1528 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0)) {
1529 entries = tab->entries;
1532 curr_entry_ptr = &entries[get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind)
1534 MARK_BIN_DELETED(tab, bin_ind);
1536 MARK_ENTRY_DELETED(curr_entry_ptr);
1538 update_range_for_deleted(tab, i);
1542 if (value != 0) *value = 0;
1548st_cleanup_safe(st_table *tab ATTRIBUTE_UNUSED,
1549 st_data_t never ATTRIBUTE_UNUSED)
1563st_update(st_table *tab, st_data_t key,
1564 st_update_callback_func *func, st_data_t arg)
1566 st_table_entry *entry = NULL;
1568 st_table_entry *entries;
1570 st_data_t value = 0, old_key;
1571 int retval, existing;
1572 st_hash_t hash = do_hash(key, tab);
1575 entries = tab->entries;
1576 if (!st_has_bins(tab)) {
1577 bin = find_entry(tab, hash, key);
1578 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1580 existing = bin != UNDEFINED_ENTRY_IND;
1581 entry = &entries[bin];
1582 bin_ind = UNDEFINED_BIN_IND;
1585 bin_ind = find_table_bin_ind(tab, hash, key);
1586 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1588 existing = bin_ind != UNDEFINED_BIN_IND;
1590 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1591 entry = &entries[bin];
1596 value = entry->record;
1600 unsigned int rebuilds_num = tab->rebuilds_num;
1602 retval = (*func)(&key, &value, arg, existing);
1606 assert(rebuilds_num == tab->rebuilds_num);
1612 st_add_direct_with_hash(tab, key, value, hash);
1615 if (old_key != key) {
1618 entry->record = value;
1622 if (bin_ind != UNDEFINED_BIN_IND)
1623 MARK_BIN_DELETED(tab, bin_ind);
1624 MARK_ENTRY_DELETED(entry);
1626 update_range_for_deleted(tab, bin);
1642st_general_foreach(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg,
1647 st_table_entry *entries, *curr_entry_ptr;
1648 enum st_retval retval;
1649 st_index_t i, rebuilds_num;
1652 int error_p, packed_p = !st_has_bins(tab);
1654 entries = tab->entries;
1657 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1658 curr_entry_ptr = &entries[i];
1659 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1661 key = curr_entry_ptr->key;
1662 rebuilds_num = tab->rebuilds_num;
1663 hash = curr_entry_ptr->hash;
1664 retval = (*func)(key, curr_entry_ptr->record, arg, 0);
1666 if (retval == ST_REPLACE && replace) {
1668 value = curr_entry_ptr->record;
1669 retval = (*replace)(&key, &value, arg, TRUE);
1670 curr_entry_ptr->key = key;
1671 curr_entry_ptr->record = value;
1674 if (rebuilds_num != tab->rebuilds_num) {
1676 entries = tab->entries;
1677 packed_p = !st_has_bins(tab);
1679 i = find_entry(tab, hash, key);
1680 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1682 error_p = i == UNDEFINED_ENTRY_IND;
1685 i = find_table_entry_ind(tab, hash, key);
1686 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1688 error_p = i == UNDEFINED_ENTRY_IND;
1691 if (error_p && check_p) {
1693 retval = (*func)(0, 0, arg, 1);
1696 curr_entry_ptr = &entries[i];
1709 st_data_t key = curr_entry_ptr->key;
1713 bin = find_entry(tab, hash, key);
1714 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
1716 if (bin == UNDEFINED_ENTRY_IND)
1720 bin_ind = find_table_bin_ind(tab, hash, key);
1721 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
1723 if (bin_ind == UNDEFINED_BIN_IND)
1725 bin = get_bin(st_bins_ptr(tab), get_size_ind(tab), bin_ind) - ENTRY_BASE;
1726 MARK_BIN_DELETED(tab, bin_ind);
1728 curr_entry_ptr = &entries[bin];
1729 MARK_ENTRY_DELETED(curr_entry_ptr);
1731 update_range_for_deleted(tab, bin);
1741st_foreach_with_hash(st_table *tab, st_foreach_with_hash_callback_func *func, st_data_t arg)
1743 st_table_entry *entries, *curr_entry_ptr;
1744 enum st_retval retval;
1745 st_index_t i, rebuilds_num;
1748 int packed_p = !st_has_bins(tab);
1750 entries = tab->entries;
1753 for (i = tab->entries_start; i < tab->entries_bound; i++) {
1754 curr_entry_ptr = &entries[i];
1755 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
1757 key = curr_entry_ptr->key;
1758 rebuilds_num = tab->rebuilds_num;
1759 hash = curr_entry_ptr->hash;
1760 retval = (*func)(key, curr_entry_ptr->record, hash, arg);
1762 if (rebuilds_num != tab->rebuilds_num) {
1764 entries = tab->entries;
1765 packed_p = !st_has_bins(tab);
1767 i = find_entry(tab, hash, key);
1768 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1772 i = find_table_entry_ind(tab, hash, key);
1773 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
1777 curr_entry_ptr = &entries[i];
1791st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
1793 return st_general_foreach(tab, func, replace, arg, TRUE);
1797 st_foreach_callback_func *func;
1802apply_functor(st_data_t k, st_data_t v, st_data_t d,
int _)
1804 const struct functor *f = (
void *)d;
1805 return f->func(k, v, f->arg);
1809st_foreach(st_table *tab, st_foreach_callback_func *func, st_data_t arg)
1811 const struct functor f = { func, arg };
1812 return st_general_foreach(tab, apply_functor, 0, (st_data_t)&f, FALSE);
1817st_foreach_check(st_table *tab, st_foreach_check_callback_func *func, st_data_t arg,
1818 st_data_t never ATTRIBUTE_UNUSED)
1820 return st_general_foreach(tab, func, 0, arg, TRUE);
1825static inline st_index_t
1826st_general_keys(st_table *tab, st_data_t *keys, st_index_t size)
1828 st_index_t i, bound;
1829 st_data_t key, *keys_start, *keys_end;
1830 st_table_entry *curr_entry_ptr, *entries = tab->entries;
1832 bound = tab->entries_bound;
1834 keys_end = keys + size;
1835 for (i = tab->entries_start; i < bound; i++) {
1836 if (keys == keys_end)
1838 curr_entry_ptr = &entries[i];
1839 key = curr_entry_ptr->key;
1840 if (! DELETED_ENTRY_P(curr_entry_ptr))
1844 return keys - keys_start;
1848st_keys(st_table *tab, st_data_t *keys, st_index_t size)
1850 return st_general_keys(tab, keys, size);
1855st_keys_check(st_table *tab, st_data_t *keys, st_index_t size,
1856 st_data_t never ATTRIBUTE_UNUSED)
1858 return st_general_keys(tab, keys, size);
1863static inline st_index_t
1864st_general_values(st_table *tab, st_data_t *values, st_index_t size)
1866 st_index_t i, bound;
1867 st_data_t *values_start, *values_end;
1868 st_table_entry *curr_entry_ptr, *entries = tab->entries;
1870 values_start = values;
1871 values_end = values + size;
1872 bound = tab->entries_bound;
1873 for (i = tab->entries_start; i < bound; i++) {
1874 if (values == values_end)
1876 curr_entry_ptr = &entries[i];
1877 if (! DELETED_ENTRY_P(curr_entry_ptr))
1878 *values++ = curr_entry_ptr->record;
1881 return values - values_start;
1885st_values(st_table *tab, st_data_t *values, st_index_t size)
1887 return st_general_values(tab, values, size);
1892st_values_check(st_table *tab, st_data_t *values, st_index_t size,
1893 st_data_t never ATTRIBUTE_UNUSED)
1895 return st_general_values(tab, values, size);
1898#define FNV1_32A_INIT 0x811c9dc5
1903#define FNV_32_PRIME 0x01000193
1906#ifndef UNALIGNED_WORD_ACCESS
1907# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
1908 defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || \
1909 defined(__powerpc64__) || defined(__POWERPC__) || defined(__aarch64__) || \
1910 defined(__mc68020__)
1911# define UNALIGNED_WORD_ACCESS 1
1914#ifndef UNALIGNED_WORD_ACCESS
1915# define UNALIGNED_WORD_ACCESS 0
1921#define BIG_CONSTANT(x,y) ((st_index_t)(x)<<32|(st_index_t)(y))
1922#define ROTL(x,n) ((x)<<(n)|(x)>>(SIZEOF_ST_INDEX_T*CHAR_BIT-(n)))
1924#if ST_INDEX_BITS <= 32
1925#define C1 (st_index_t)0xcc9e2d51
1926#define C2 (st_index_t)0x1b873593
1928#define C1 BIG_CONSTANT(0x87c37b91,0x114253d5);
1929#define C2 BIG_CONSTANT(0x4cf5ad43,0x2745937f);
1931NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_step(st_index_t h, st_index_t k));
1932NO_SANITIZE(
"unsigned-integer-overflow",
static inline st_index_t murmur_finish(st_index_t h));
1933NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash(
const void *ptr,
size_t len, st_index_t h));
1935static inline st_index_t
1936murmur_step(st_index_t h, st_index_t k)
1938#if ST_INDEX_BITS <= 32
1954static inline st_index_t
1955murmur_finish(st_index_t h)
1957#if ST_INDEX_BITS <= 32
1961 const st_index_t c1 = 0x85ebca6b;
1962 const st_index_t c2 = 0xc2b2ae35;
1968 const st_index_t c1 = BIG_CONSTANT(0xbf58476d,0x1ce4e5b9);
1969 const st_index_t c2 = BIG_CONSTANT(0x94d049bb,0x133111eb);
1971#if ST_INDEX_BITS > 64
1988st_hash(
const void *ptr,
size_t len, st_index_t h)
1990 const char *data = ptr;
1994#define data_at(n) (st_index_t)((unsigned char)data[(n)])
1995#define UNALIGNED_ADD_4 UNALIGNED_ADD(2); UNALIGNED_ADD(1); UNALIGNED_ADD(0)
1996#if SIZEOF_ST_INDEX_T > 4
1997#define UNALIGNED_ADD_8 UNALIGNED_ADD(6); UNALIGNED_ADD(5); UNALIGNED_ADD(4); UNALIGNED_ADD(3); UNALIGNED_ADD_4
1998#if SIZEOF_ST_INDEX_T > 8
1999#define UNALIGNED_ADD_16 UNALIGNED_ADD(14); UNALIGNED_ADD(13); UNALIGNED_ADD(12); UNALIGNED_ADD(11); \
2000 UNALIGNED_ADD(10); UNALIGNED_ADD(9); UNALIGNED_ADD(8); UNALIGNED_ADD(7); UNALIGNED_ADD_8
2001#define UNALIGNED_ADD_ALL UNALIGNED_ADD_16
2003#define UNALIGNED_ADD_ALL UNALIGNED_ADD_8
2005#define UNALIGNED_ADD_ALL UNALIGNED_ADD_4
2008 if (
len >=
sizeof(st_index_t)) {
2009#if !UNALIGNED_WORD_ACCESS
2010 int align = (int)((st_data_t)data %
sizeof(st_index_t));
2016#ifdef WORDS_BIGENDIAN
2017# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
2018 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 2)
2020# define UNALIGNED_ADD(n) case SIZEOF_ST_INDEX_T - (n) - 1: \
2021 t |= data_at(n) << CHAR_BIT*(n)
2027#ifdef WORDS_BIGENDIAN
2028 t >>= (CHAR_BIT * align) - CHAR_BIT;
2030 t <<= (CHAR_BIT * align);
2033 data +=
sizeof(st_index_t)-align;
2034 len -=
sizeof(st_index_t)-align;
2036 sl = CHAR_BIT * (SIZEOF_ST_INDEX_T-align);
2037 sr = CHAR_BIT * align;
2039 while (
len >=
sizeof(st_index_t)) {
2040 d = *(st_index_t *)data;
2041#ifdef WORDS_BIGENDIAN
2042 t = (t << sr) | (d >> sl);
2044 t = (t >> sr) | (d << sl);
2046 h = murmur_step(h, t);
2048 data +=
sizeof(st_index_t);
2049 len -=
sizeof(st_index_t);
2052 pack =
len < (size_t)align ? (
int)
len : align;
2055#ifdef WORDS_BIGENDIAN
2056# define UNALIGNED_ADD(n) case (n) + 1: \
2057 d |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2059# define UNALIGNED_ADD(n) case (n) + 1: \
2060 d |= data_at(n) << CHAR_BIT*(n)
2065#ifdef WORDS_BIGENDIAN
2066 t = (t << sr) | (d >> sl);
2068 t = (t >> sr) | (d << sl);
2071 if (
len < (
size_t)align)
goto skip_tail;
2073 h = murmur_step(h, t);
2079#ifdef HAVE_BUILTIN___BUILTIN_ASSUME_ALIGNED
2080#define aligned_data __builtin_assume_aligned(data, sizeof(st_index_t))
2082#define aligned_data data
2086 h = murmur_step(h, *(st_index_t *)aligned_data);
2087 data +=
sizeof(st_index_t);
2088 len -=
sizeof(st_index_t);
2089 }
while (
len >=
sizeof(st_index_t));
2095#if UNALIGNED_WORD_ACCESS && SIZEOF_ST_INDEX_T <= 8 && CHAR_BIT == 8
2097#if SIZEOF_ST_INDEX_T > 4
2098 case 7: t |= data_at(6) << 48;
2099 case 6: t |= data_at(5) << 40;
2100 case 5: t |= data_at(4) << 32;
2102 t |= (st_index_t)*(uint32_t*)aligned_data;
2106 case 3: t |= data_at(2) << 16;
2107 case 2: t |= data_at(1) << 8;
2108 case 1: t |= data_at(0);
2110#ifdef WORDS_BIGENDIAN
2111# define UNALIGNED_ADD(n) case (n) + 1: \
2112 t |= data_at(n) << CHAR_BIT*(SIZEOF_ST_INDEX_T - (n) - 1)
2114# define UNALIGNED_ADD(n) case (n) + 1: \
2115 t |= data_at(n) << CHAR_BIT*(n)
2123 h ^= t; h -= ROTL(t, 7);
2129 return murmur_finish(h);
2133st_hash_uint32(st_index_t h, uint32_t i)
2135 return murmur_step(h, i);
2138NO_SANITIZE(
"unsigned-integer-overflow",
extern st_index_t st_hash_uint(st_index_t h, st_index_t i));
2140st_hash_uint(st_index_t h, st_index_t i)
2145#if SIZEOF_ST_INDEX_T*CHAR_BIT > 8*8
2146 h = murmur_step(h, i >> 8*8);
2148 h = murmur_step(h, i);
2153st_hash_end(st_index_t h)
2155 h = murmur_finish(h);
2161rb_st_hash_start(st_index_t h)
2167strhash(st_data_t arg)
2169 register const char *
string = (
const char *)arg;
2170 return st_hash(
string, strlen(
string), FNV1_32A_INIT);
2174st_locale_insensitive_strcasecmp(
const char *s1,
const char *s2)
2181 if (c1 ==
'\0' || c2 ==
'\0') {
2182 if (c1 !=
'\0')
return 1;
2183 if (c2 !=
'\0')
return -1;
2186 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2187 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2198st_locale_insensitive_strncasecmp(
const char *s1,
const char *s2,
size_t n)
2203 for (i = 0; i < n; i++) {
2206 if (c1 ==
'\0' || c2 ==
'\0') {
2207 if (c1 !=
'\0')
return 1;
2208 if (c2 !=
'\0')
return -1;
2211 if ((
'A' <= c1) && (c1 <=
'Z')) c1 +=
'a' -
'A';
2212 if ((
'A' <= c2) && (c2 <=
'Z')) c2 +=
'a' -
'A';
2224st_strcmp(st_data_t lhs, st_data_t rhs)
2226 const char *s1 = (
char *)lhs;
2227 const char *s2 = (
char *)rhs;
2228 return strcmp(s1, s2);
2232st_locale_insensitive_strcasecmp_i(st_data_t lhs, st_data_t rhs)
2234 const char *s1 = (
char *)lhs;
2235 const char *s2 = (
char *)rhs;
2236 return st_locale_insensitive_strcasecmp(s1, s2);
2239NO_SANITIZE(
"unsigned-integer-overflow", PUREFUNC(
static st_index_t strcasehash(st_data_t)));
2241strcasehash(st_data_t arg)
2243 register const char *
string = (
const char *)arg;
2244 register st_index_t hval = FNV1_32A_INIT;
2250 unsigned int c = (
unsigned char)*
string++;
2251 if ((
unsigned int)(c -
'A') <= (
'Z' -
'A')) c +=
'a' -
'A';
2255 hval *= FNV_32_PRIME;
2261st_numcmp(st_data_t x, st_data_t y)
2267st_numhash(st_data_t n)
2269 enum {s1 = 11, s2 = 3};
2270 return (st_index_t)((n>>s1|(n<<s2)) ^ (n>>s2));
2278st_expand_table(st_table *tab, st_index_t siz)
2283 if (siz <= get_allocated_entries(tab))
2286 tmp = st_init_table_with_size(tab->type, siz);
2287 n = get_allocated_entries(tab);
2288 MEMCPY(tmp->entries, tab->entries, st_table_entry, n);
2289 st_free_entries(tab);
2291 tab->entry_power = tmp->entry_power;
2292 tab->bin_power = tmp->bin_power;
2293 tab->size_ind = tmp->size_ind;
2294 tab->entries = tmp->entries;
2295 tab->rebuilds_num++;
2296 free_fixed_ptr(tmp);
2302st_rehash_linear(st_table *tab)
2304 int eq_p, rebuilt_p;
2306 st_table_entry *p, *q;
2308 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2309 p = &tab->entries[i];
2310 if (DELETED_ENTRY_P(p))
2312 for (j = i + 1; j < tab->entries_bound; j++) {
2313 q = &tab->entries[j];
2314 if (DELETED_ENTRY_P(q))
2316 DO_PTR_EQUAL_CHECK(tab, p, q->hash, q->key, eq_p, rebuilt_p);
2317 if (EXPECT(rebuilt_p, 0))
2321 MARK_ENTRY_DELETED(q);
2323 update_range_for_deleted(tab, j);
2333st_rehash_indexed(st_table *tab)
2335 int eq_p, rebuilt_p;
2338 unsigned int const size_ind = get_size_ind(tab);
2339 initialize_bins(tab);
2340 for (i = tab->entries_start; i < tab->entries_bound; i++) {
2341 st_table_entry *p = &tab->entries[i];
2343#ifdef QUADRATIC_PROBE
2346 st_index_t perturb = p->hash;
2349 if (DELETED_ENTRY_P(p))
2352 ind = hash_bin(p->hash, tab);
2354 st_index_t bin = get_bin(st_bins_ptr(tab), size_ind, ind);
2355 if (EMPTY_OR_DELETED_BIN_P(bin)) {
2357 set_bin(st_bins_ptr(tab), size_ind, ind, i + ENTRY_BASE);
2361 st_table_entry *q = &tab->entries[bin - ENTRY_BASE];
2362 DO_PTR_EQUAL_CHECK(tab, q, p->hash, p->key, eq_p, rebuilt_p);
2363 if (EXPECT(rebuilt_p, 0))
2367 q->record = p->record;
2368 MARK_ENTRY_DELETED(p);
2370 update_range_for_deleted(tab, bin);
2375#ifdef QUADRATIC_PROBE
2376 ind = hash_bin(ind + d, tab);
2379 ind = secondary_hash(ind, tab, &perturb);
2392st_rehash(st_table *tab)
2397 if (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2398 rebuilt_p = st_rehash_linear(tab);
2400 rebuilt_p = st_rehash_indexed(tab);
2401 }
while (rebuilt_p);
2405st_stringify(
VALUE key)
2408 rb_hash_key_str(key) : key;
2414 st_data_t k = st_stringify(key);
2416 e.hash = do_hash(k, tab);
2420 tab->entries[tab->entries_bound++] = e;
2431 for (i = 0; i < argc; ) {
2432 st_data_t k = st_stringify(argv[i++]);
2433 st_data_t v = argv[i++];
2434 st_insert(tab, k, v);
2446 for (i = 0; i < argc; ) {
2447 VALUE key = argv[i++];
2448 VALUE val = argv[i++];
2449 st_insert_single(tab, hash, key, val);
2461 st_index_t n, size = argc / 2;
2462 st_table *tab = RHASH_ST_TABLE(hash);
2464 tab = rb_hash_tbl_raw(hash);
2465 n = tab->entries_bound + size;
2466 st_expand_table(tab, n);
2467 if (UNLIKELY(tab->num_entries))
2468 st_insert_generic(tab, argc, argv, hash);
2470 st_insert_single(tab, hash, argv[0], argv[1]);
2471 else if (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS)
2472 st_insert_linear(tab, argc, argv, hash);
2474 st_insert_generic(tab, argc, argv, hash);
2478rb_st_compact_table(st_table *tab)
2480 st_index_t num = tab->num_entries;
2481 if (REBUILD_THRESHOLD * num <= get_allocated_entries(tab)) {
2483 st_table *new_tab = st_init_table_with_size(tab->type, 2 * num);
2484 rebuild_table_with(new_tab, tab);
2485 rebuild_move_table(new_tab, tab);
2486 rebuild_cleanup(tab);
2494struct set_table_entry {
2500set_ptr_equal_check(
const set_table *tab,
const set_table_entry *entry,
2501 st_hash_t hash_val, st_data_t key,
2502 int *res,
int *rebuilt_p)
2504 unsigned int old_rebuilds_num = tab->rebuilds_num;
2505 *res = entry_equal(tab->type, entry->hash, entry->key, hash_val, key);
2506 *rebuilt_p = old_rebuilds_num != tab->rebuilds_num;
2509#define SET_DO_PTR_EQUAL_CHECK(tab, ptr, hash_val, key, res, rebuilt_p) \
2510 set_ptr_equal_check((tab), (ptr), (hash_val), (key), &(res), &(rebuilt_p))
2513static inline st_hash_t
2514set_do_hash(st_data_t key,
set_table *tab)
2516 st_hash_t hash = (st_hash_t)(tab->type->hash)(key);
2517 return normalize_hash_value(hash);
2521static inline unsigned int
2524 return tab->size_ind;
2528static inline st_index_t
2531 return ((st_index_t) 1)<<tab->bin_power;
2535static inline st_index_t
2538 return set_get_bins_num(tab) - 1;
2543static inline st_index_t
2544set_hash_bin(st_hash_t hash_value,
set_table *tab)
2546 return hash_value & set_bins_mask(tab);
2550static inline st_index_t
2551set_get_allocated_entries(
const set_table *tab)
2553 return ((st_index_t) 1)<<tab->entry_power;
2557set_allocated_entries_size(
const set_table *tab)
2559 return set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2565 return tab->entry_power > MAX_POWER2_FOR_TABLES_WITHOUT_BINS;
2569static inline st_index_t
2572 if (set_has_bins(tab)) {
2573 return features[tab->entry_power].bins_words *
sizeof (st_index_t);
2579static inline st_index_t *
2582 if (set_has_bins(tab)) {
2583 return (st_index_t *)(((
char *)tab->
entries) + set_allocated_entries_size(tab));
2593 memset(set_bins_ptr(tab), 0, set_bins_size(tab));
2600 tab->num_entries = 0;
2601 tab->entries_start = tab->entries_bound = 0;
2602 if (set_bins_ptr(tab) != NULL)
2603 set_initialize_bins(tab);
2609 size_t memsize = set_get_allocated_entries(tab) *
sizeof(set_table_entry);
2610 if (set_has_bins(tab)) {
2611 memsize += set_bins_size(tab);
2617set_init_existing_table_with_size(
set_table *tab,
const struct st_hash_type *type, st_index_t size)
2624 const char *e = getenv(
"ST_HASH_LOG");
2625 if (!e || !*e) init_st = 1;
2634 n = get_power2(size);
2637 tab->entry_power = n;
2638 tab->bin_power = features[n].bin_power;
2639 tab->size_ind = features[n].size_ind;
2641 tab->
entries = (set_table_entry *)malloc(set_entries_memsize(tab));
2642 set_make_tab_empty(tab);
2643 tab->rebuilds_num = 0;
2651set_init_table_with_size(
set_table *tab,
const struct st_hash_type *type, st_index_t size)
2653 if (tab == NULL) tab = malloc(
sizeof(
set_table));
2655 set_init_existing_table_with_size(tab, type, size);
2661set_init_numtable(
void)
2663 return set_init_table_with_size(NULL, &type_numhash, 0);
2667set_init_numtable_with_size(st_index_t size)
2669 return set_init_table_with_size(NULL, &type_numhash, size);
2673set_init_embedded_numtable_with_size(
set_table *tab, st_index_t size)
2675 return set_init_existing_table_with_size(tab, &type_numhash, size);
2679set_table_size(
const struct set_table *tbl)
2681 return tbl->num_entries;
2688 set_make_tab_empty(tab);
2689 tab->rebuilds_num++;
2695 sized_free(tab->
entries, set_entries_memsize(tab));
2703 set_free_embedded_table(tab);
2704 free_fixed_ptr(tab);
2712 + (tab->entry_power <= MAX_POWER2_FOR_TABLES_WITHOUT_BINS ? 0 : set_bins_size(tab))
2713 + set_get_allocated_entries(tab) *
sizeof(set_table_entry));
2717set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2720set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key);
2723set_find_table_bin_ind_direct(
set_table *table, st_hash_t hash_value, st_data_t key);
2726set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
2727 st_data_t key, st_index_t *bin_ind);
2731static void set_rebuild_cleanup(
set_table *
const tab);
2740 if ((2 * tab->num_entries <= set_get_allocated_entries(tab)
2741 && REBUILD_THRESHOLD * tab->num_entries > set_get_allocated_entries(tab))
2742 || tab->num_entries < (1 << MINIMAL_POWER2)) {
2744 tab->num_entries = 0;
2745 if (set_has_bins(tab))
2746 set_initialize_bins(tab);
2747 set_rebuild_table_with(tab, tab);
2754 new_tab = set_init_table_with_size(NULL, tab->type,
2755 2 * tab->num_entries - 1);
2756 set_rebuild_table_with(new_tab, tab);
2757 set_rebuild_move_table(new_tab, tab);
2759 set_rebuild_cleanup(tab);
2766 unsigned int size_ind;
2767 set_table_entry *new_entries;
2768 set_table_entry *curr_entry_ptr;
2772 new_entries = new_tab->
entries;
2775 bins = set_bins_ptr(new_tab);
2776 size_ind = set_get_size_ind(new_tab);
2777 st_index_t bound = tab->entries_bound;
2778 set_table_entry *entries = tab->
entries;
2780 for (i = tab->entries_start; i < bound; i++) {
2781 curr_entry_ptr = &entries[i];
2782 PREFETCH(entries + i + 1, 0);
2783 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
2785 if (&new_entries[ni] != curr_entry_ptr)
2786 new_entries[ni] = *curr_entry_ptr;
2787 if (EXPECT(bins != NULL, 1)) {
2788 bin_ind = set_find_table_bin_ind_direct(new_tab, curr_entry_ptr->hash,
2789 curr_entry_ptr->key);
2790 set_bin(bins, size_ind, bin_ind, ni + ENTRY_BASE);
2792 new_tab->num_entries++;
2796 assert(new_tab->num_entries == tab->num_entries);
2802 sized_free(tab->
entries, set_entries_memsize(tab));
2805 tab->entry_power = new_tab->entry_power;
2806 tab->bin_power = new_tab->bin_power;
2807 tab->size_ind = new_tab->size_ind;
2809 free_fixed_ptr(new_tab);
2813set_rebuild_cleanup(
set_table *
const tab)
2815 tab->entries_start = 0;
2816 tab->entries_bound = tab->num_entries;
2817 tab->rebuilds_num++;
2832static inline st_index_t
2833set_secondary_hash(st_index_t ind,
set_table *tab, st_index_t *perturb)
2836 ind = (ind << 2) + ind + *perturb + 1;
2837 return set_hash_bin(ind, tab);
2844static inline st_index_t
2845set_find_entry(
set_table *tab, st_hash_t hash_value, st_data_t key)
2847 int eq_p, rebuilt_p;
2848 st_index_t i, bound;
2849 set_table_entry *entries;
2851 bound = tab->entries_bound;
2853 for (i = tab->entries_start; i < bound; i++) {
2854 SET_DO_PTR_EQUAL_CHECK(tab, &entries[i], hash_value, key, eq_p, rebuilt_p);
2855 if (EXPECT(rebuilt_p, 0))
2856 return REBUILT_TABLE_ENTRY_IND;
2860 return UNDEFINED_ENTRY_IND;
2872set_find_table_entry_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2874 int eq_p, rebuilt_p;
2876#ifdef QUADRATIC_PROBE
2882 set_table_entry *entries = tab->
entries;
2884 ind = set_hash_bin(hash_value, tab);
2885#ifdef QUADRATIC_PROBE
2888 perturb = hash_value;
2891 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2892 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2893 SET_DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2894 if (EXPECT(rebuilt_p, 0))
2895 return REBUILT_TABLE_ENTRY_IND;
2899 else if (EMPTY_BIN_P(bin))
2900 return UNDEFINED_ENTRY_IND;
2901#ifdef QUADRATIC_PROBE
2902 ind = set_hash_bin(ind + d, tab);
2905 ind = set_secondary_hash(ind, tab, &perturb);
2916set_find_table_bin_ind(
set_table *tab, st_hash_t hash_value, st_data_t key)
2918 int eq_p, rebuilt_p;
2920#ifdef QUADRATIC_PROBE
2926 set_table_entry *entries = tab->
entries;
2928 ind = set_hash_bin(hash_value, tab);
2929#ifdef QUADRATIC_PROBE
2932 perturb = hash_value;
2935 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2936 if (! EMPTY_OR_DELETED_BIN_P(bin)) {
2937 SET_DO_PTR_EQUAL_CHECK(tab, &entries[bin - ENTRY_BASE], hash_value, key, eq_p, rebuilt_p);
2938 if (EXPECT(rebuilt_p, 0))
2939 return REBUILT_TABLE_BIN_IND;
2943 else if (EMPTY_BIN_P(bin))
2944 return UNDEFINED_BIN_IND;
2945#ifdef QUADRATIC_PROBE
2946 ind = set_hash_bin(ind + d, tab);
2949 ind = set_secondary_hash(ind, tab, &perturb);
2959set_find_table_bin_ind_direct(
set_table *tab, st_hash_t hash_value, st_data_t key)
2962#ifdef QUADRATIC_PROBE
2969 ind = set_hash_bin(hash_value, tab);
2970#ifdef QUADRATIC_PROBE
2973 perturb = hash_value;
2976 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
2977 if (EMPTY_OR_DELETED_BIN_P(bin))
2979#ifdef QUADRATIC_PROBE
2980 ind = set_hash_bin(ind + d, tab);
2983 ind = set_secondary_hash(ind, tab, &perturb);
2990#define MARK_SET_BIN_EMPTY(tab, i) (set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, EMPTY_BIN))
3002set_find_table_bin_ptr_and_reserve(
set_table *tab, st_hash_t *hash_value,
3003 st_data_t key, st_index_t *bin_ind)
3005 int eq_p, rebuilt_p;
3007 st_hash_t curr_hash_value = *hash_value;
3008#ifdef QUADRATIC_PROBE
3013 st_index_t entry_index;
3014 st_index_t firset_deleted_bin_ind;
3015 set_table_entry *entries;
3017 ind = set_hash_bin(curr_hash_value, tab);
3018#ifdef QUADRATIC_PROBE
3021 perturb = curr_hash_value;
3023 firset_deleted_bin_ind = UNDEFINED_BIN_IND;
3026 entry_index = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), ind);
3027 if (EMPTY_BIN_P(entry_index)) {
3029 entry_index = UNDEFINED_ENTRY_IND;
3030 if (firset_deleted_bin_ind != UNDEFINED_BIN_IND) {
3032 ind = firset_deleted_bin_ind;
3033 MARK_SET_BIN_EMPTY(tab, ind);
3037 else if (! DELETED_BIN_P(entry_index)) {
3038 SET_DO_PTR_EQUAL_CHECK(tab, &entries[entry_index - ENTRY_BASE], curr_hash_value, key, eq_p, rebuilt_p);
3039 if (EXPECT(rebuilt_p, 0))
3040 return REBUILT_TABLE_ENTRY_IND;
3044 else if (firset_deleted_bin_ind == UNDEFINED_BIN_IND)
3045 firset_deleted_bin_ind = ind;
3046#ifdef QUADRATIC_PROBE
3047 ind = set_hash_bin(ind + d, tab);
3050 ind = set_secondary_hash(ind, tab, &perturb);
3060set_table_lookup(
set_table *tab, st_data_t key)
3063 st_hash_t hash = set_do_hash(key, tab);
3066 if (!set_has_bins(tab)) {
3067 bin = set_find_entry(tab, hash, key);
3068 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3070 if (bin == UNDEFINED_ENTRY_IND)
3074 bin = set_find_table_entry_ind(tab, hash, key);
3075 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3077 if (bin == UNDEFINED_ENTRY_IND)
3086set_rebuild_table_if_necessary(
set_table *tab)
3088 st_index_t bound = tab->entries_bound;
3090 if (bound == set_get_allocated_entries(tab) || tab->entries_start == MAX_ENTRIES_START) {
3091 set_rebuild_table(tab);
3099set_insert(
set_table *tab, st_data_t key)
3101 set_table_entry *entry;
3104 st_hash_t hash_value;
3108 hash_value = set_do_hash(key, tab);
3110 set_rebuild_table_if_necessary(tab);
3111 if (!set_has_bins(tab)) {
3112 bin = set_find_entry(tab, hash_value, key);
3113 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3115 new_p = bin == UNDEFINED_ENTRY_IND;
3118 bin_ind = UNDEFINED_BIN_IND;
3121 bin = set_find_table_bin_ptr_and_reserve(tab, &hash_value,
3123 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3125 new_p = bin == UNDEFINED_ENTRY_IND;
3129 ind = tab->entries_bound++;
3131 entry->hash = hash_value;
3133 if (bin_ind != UNDEFINED_BIN_IND)
3134 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind, ind + ENTRY_BASE);
3144 *new_tab = *old_tab;
3145 size_t memsize = set_allocated_entries_size(old_tab) + set_bins_size(old_tab);
3146 new_tab->
entries = (set_table_entry *)malloc(memsize);
3157 if (set_replace(new_tab, old_tab) == NULL) {
3158 set_free_table(new_tab);
3168set_update_range_for_deleted(
set_table *tab, st_index_t n)
3172 if (tab->entries_start == n) {
3173 st_index_t start = n + 1;
3174 st_index_t bound = tab->entries_bound;
3175 set_table_entry *entries = tab->
entries;
3176 while (start < bound && DELETED_ENTRY_P(&entries[start])) start++;
3177 tab->entries_start = start > MAX_ENTRIES_START ? MAX_ENTRIES_START : (
unsigned int)start;
3184#define MARK_SET_BIN_DELETED(tab, i) \
3186 set_bin(set_bins_ptr(tab), set_get_size_ind(tab), i, DELETED_BIN); \
3192set_table_delete(
set_table *tab, st_data_t *key)
3194 set_table_entry *entry;
3199 hash = set_do_hash(*key, tab);
3201 if (!set_has_bins(tab)) {
3202 bin = set_find_entry(tab, hash, *key);
3203 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3205 if (bin == UNDEFINED_ENTRY_IND) {
3210 bin_ind = set_find_table_bin_ind(tab, hash, *key);
3211 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3213 if (bin_ind == UNDEFINED_BIN_IND) {
3216 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3217 MARK_SET_BIN_DELETED(tab, bin_ind);
3221 MARK_ENTRY_DELETED(entry);
3223 set_update_range_for_deleted(tab, bin);
3236set_general_foreach(
set_table *tab, set_foreach_check_callback_func *func,
3237 set_update_callback_func *replace, st_data_t arg,
3242 set_table_entry *entries, *curr_entry_ptr;
3243 enum st_retval retval;
3244 st_index_t i, rebuilds_num;
3247 int error_p, packed_p = !set_has_bins(tab);
3252 for (i = tab->entries_start; i < tab->entries_bound; i++) {
3253 curr_entry_ptr = &entries[i];
3254 if (EXPECT(DELETED_ENTRY_P(curr_entry_ptr), 0))
3256 key = curr_entry_ptr->key;
3257 rebuilds_num = tab->rebuilds_num;
3258 hash = curr_entry_ptr->hash;
3259 retval = (*func)(key, arg, 0);
3261 if (retval == ST_REPLACE && replace) {
3262 retval = (*replace)(&key, arg, TRUE);
3263 curr_entry_ptr->key = key;
3266 if (rebuilds_num != tab->rebuilds_num) {
3269 packed_p = !set_has_bins(tab);
3271 i = set_find_entry(tab, hash, key);
3272 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3274 error_p = i == UNDEFINED_ENTRY_IND;
3277 i = set_find_table_entry_ind(tab, hash, key);
3278 if (EXPECT(i == REBUILT_TABLE_ENTRY_IND, 0))
3280 error_p = i == UNDEFINED_ENTRY_IND;
3283 if (error_p && check_p) {
3285 retval = (*func)(0, arg, 1);
3288 curr_entry_ptr = &entries[i];
3301 st_data_t key = curr_entry_ptr->key;
3305 bin = set_find_entry(tab, hash, key);
3306 if (EXPECT(bin == REBUILT_TABLE_ENTRY_IND, 0))
3308 if (bin == UNDEFINED_ENTRY_IND)
3312 bin_ind = set_find_table_bin_ind(tab, hash, key);
3313 if (EXPECT(bin_ind == REBUILT_TABLE_BIN_IND, 0))
3315 if (bin_ind == UNDEFINED_BIN_IND)
3317 bin = get_bin(set_bins_ptr(tab), set_get_size_ind(tab), bin_ind) - ENTRY_BASE;
3318 MARK_SET_BIN_DELETED(tab, bin_ind);
3320 curr_entry_ptr = &entries[bin];
3321 MARK_ENTRY_DELETED(curr_entry_ptr);
3323 set_update_range_for_deleted(tab, bin);
3332set_foreach_with_replace(
set_table *tab, set_foreach_check_callback_func *func, set_update_callback_func *replace, st_data_t arg)
3334 return set_general_foreach(tab, func, replace, arg, TRUE);
3338 set_foreach_callback_func *func;
3343set_apply_functor(st_data_t k, st_data_t d,
int _)
3345 const struct set_functor *f = (
void *)d;
3346 return f->func(k, f->arg);
3350set_table_foreach(
set_table *tab, set_foreach_callback_func *func, st_data_t arg)
3352 const struct set_functor f = { func, arg };
3353 return set_general_foreach(tab, set_apply_functor, NULL, (st_data_t)&f, FALSE);
3358set_foreach_check(
set_table *tab, set_foreach_check_callback_func *func, st_data_t arg,
3359 st_data_t never ATTRIBUTE_UNUSED)
3361 return set_general_foreach(tab, func, NULL, arg, TRUE);
3367set_keys(
set_table *tab, st_data_t *keys, st_index_t size)
3369 st_index_t i, bound;
3370 st_data_t key, *keys_start, *keys_end;
3371 set_table_entry *curr_entry_ptr, *entries = tab->
entries;
3373 bound = tab->entries_bound;
3375 keys_end = keys + size;
3376 for (i = tab->entries_start; i < bound; i++) {
3377 if (keys == keys_end)
3379 curr_entry_ptr = &entries[i];
3380 key = curr_entry_ptr->key;
3381 if (! DELETED_ENTRY_P(curr_entry_ptr))
3385 return keys - keys_start;
3391 st_index_t num = tab->num_entries;
3392 if (REBUILD_THRESHOLD * num <= set_get_allocated_entries(tab)) {
3394 set_table *new_tab = set_init_table_with_size(NULL, tab->type, 2 * num);
3395 set_rebuild_table_with(new_tab, tab);
3396 set_rebuild_move_table(new_tab, tab);
3397 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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
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.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t VALUE
Type that represents a Ruby object.