Skip to content

Commit

Permalink
Add numBucketPoitions helper
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Söderqvist <[email protected]>
  • Loading branch information
zuiderkwast committed Nov 13, 2024
1 parent b4cd7d1 commit e43a34a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/hashset.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,12 @@ static inline uint8_t highBits(uint64_t hash) {
return hash >> (CHAR_BIT * 7);
}

static inline int numBucketPoitions(bucket *b) {
return ELEMENTS_PER_BUCKET - (b->chained ? 1 : 0);
}

static inline int bucketIsFull(bucket *b) {
int num_positions = ELEMENTS_PER_BUCKET - (b->chained ? 1 : 0);
return b->presence == (1 << num_positions) - 1;
return b->presence == (1 << numBucketPoitions(b)) - 1;
}

/* Returns non-zero if the position within the bucket is occupied. */
Expand Down Expand Up @@ -461,9 +464,8 @@ static bucket *bucketDefrag(bucket *prev, bucket *b, void *(*defragfn)(void *))

/* Rehashes one bucket. */
static void rehashBucket(hashset *s, bucket *b) {
int num_positions = ELEMENTS_PER_BUCKET - (b->chained ? 1 : 0);
int pos;
for (pos = 0; pos < num_positions; pos++) {
for (pos = 0; pos < numBucketPoitions(b); pos++) {
if (!isPositionFilled(b, pos)) continue; /* empty */
void *element = b->elements[pos];
uint8_t h2 = b->hashes[pos];
Expand Down Expand Up @@ -645,8 +647,7 @@ static bucket *findBucket(hashset *s, uint64_t hash, const void *key, int *pos_i
bucket *b = &s->tables[table][bucket_idx];
do {
/* Find candidate elements with presence flag set and matching h2 hash. */
int num_positions = ELEMENTS_PER_BUCKET - (b->chained ? 1 : 0);
for (int pos = 0; pos < num_positions; pos++) {
for (int pos = 0; pos < numBucketPoitions(b); pos++) {
if (isPositionFilled(b, pos) && b->hashes[pos] == h2) {
/* It's a candidate. */
void *element = b->elements[pos];
Expand Down

0 comments on commit e43a34a

Please sign in to comment.