Skip to content

Commit

Permalink
Address more review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
  • Loading branch information
zuiderkwast committed Oct 25, 2024
1 parent 5129254 commit f59ada4
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/hashset.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ void hashsetSetResizePolicy(hashsetResizePolicy policy) {
#if SIZE_MAX == UINT64_MAX /* 64-bit version */

#define ELEMENTS_PER_BUCKET 7
#define BUCKET_BITS_TYPE uint8_t
#define BITS_NEEDED_TO_STORE_POS_WITHIN_BUCKET 3

/* Selecting the number of buckets.
*
Expand Down Expand Up @@ -183,6 +185,8 @@ void hashsetSetResizePolicy(hashsetResizePolicy policy) {
#elif SIZE_MAX == UINT32_MAX /* 32-bit version */

#define ELEMENTS_PER_BUCKET 12
#define BUCKET_BITS_TYPE uint16_t
#define BITS_NEEDED_TO_STORE_POS_WITHIN_BUCKET 4
#define BUCKET_FACTOR 7
#define BUCKET_DIVISOR 64
/* When resizing, we get a fill of at most 76.19% (64 / 7 / 12). */
Expand Down Expand Up @@ -240,16 +244,6 @@ static_assert(MAX_FILL_PERCENT_HARD < 100, "Hard fill factor must be below 100%"
* everfull presence unused hashes unused elements
*/

#if ELEMENTS_PER_BUCKET < 8
#define BUCKET_BITS_TYPE uint8_t
#define BITS_NEEDED_TO_STORE_POS_WITHIN_BUCKET 3
#elif ELEMENTS_PER_BUCKET < 16
#define BUCKET_BITS_TYPE uint16_t
#define BITS_NEEDED_TO_STORE_POS_WITHIN_BUCKET 4
#else
#error "Unexpected value of ELEMENTS_PER_BUCKET"
#endif

typedef struct {
BUCKET_BITS_TYPE everfull : 1;
BUCKET_BITS_TYPE presence : ELEMENTS_PER_BUCKET;
Expand Down Expand Up @@ -652,6 +646,7 @@ static bucket *findBucketForInsert(hashset *s, uint64_t hash, int *pos_in_bucket
assert(s->tables[table]);
size_t mask = expToMask(s->bucket_exp[table]);
size_t bucket_idx = hash & mask;
size_t start_bucket_idx = bucket_idx;
while (1) {
bucket *b = &s->tables[table][bucket_idx];
for (int pos = 0; pos < ELEMENTS_PER_BUCKET; pos++) {
Expand All @@ -662,6 +657,7 @@ static bucket *findBucketForInsert(hashset *s, uint64_t hash, int *pos_in_bucket
return b;
}
bucket_idx = nextCursor(bucket_idx, mask);
assert(bucket_idx != start_bucket_idx); /* Detect infinite loop. */
}
}

Expand Down

0 comments on commit f59ada4

Please sign in to comment.