Skip to content

Commit 09b3725

Browse files
committed
add rehash
1 parent acdb899 commit 09b3725

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/hash.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ static R_INLINE size_t hash_index2(SEXP key, uintptr_t multiplier) {
7171
return ((((uintptr_t)key) >> 6) & 0x0fffffff) * multiplier;
7272
}
7373

74+
void hash_rehash(hashtab *h) {
75+
size_t new_size = h->size * 2;
76+
hashtab *new_h = hash_create_(new_size, 0.5);
77+
78+
for (size_t i = 0; i < h->size; ++i) {
79+
if (h->tb1[i].key) hash_set(new_h, h->tb1[i].key, h->tb1[i].value);
80+
if (h->tb2[i].key) hash_set(new_h, h->tb2[i].key, h->tb2[i].value);
81+
}
82+
*h = *new_h;
83+
}
7484

7585
void hash_set(hashtab *h, SEXP key, R_xlen_t value) {
7686
size_t max_relocations = h->size;
@@ -94,7 +104,9 @@ void hash_set(hashtab *h, SEXP key, R_xlen_t value) {
94104
h->tb2[idx2] = item;
95105
item = temp;
96106
}
97-
internal_error(__func__, "Cuckoo hashing cycle detected, rehash needed");
107+
// need to rehash
108+
hash_rehash(h);
109+
hash_set(h, key, value);
98110
}
99111

100112
R_xlen_t hash_lookup(const hashtab *h, SEXP key, R_xlen_t ifnotfound) {

0 commit comments

Comments
 (0)