Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binary fuse filter: allow size=0,size=1,size=2 and add tests #27

Merged
merged 1 commit into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions include/binaryfusefilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ static inline double binary_fuse_calculate_size_factor(uint32_t arity,
// size should be at least 2.
static inline bool binary_fuse8_allocate(uint32_t size,
binary_fuse8_t *filter) {
if(size <= 1) { return false; }
uint32_t arity = 3;
filter->SegmentLength = binary_fuse_calculate_segment_length(arity, size);
filter->SegmentLength = size == 0 ? 4 : binary_fuse_calculate_segment_length(arity, size);
if (filter->SegmentLength > 262144) {
filter->SegmentLength = 262144;
}
filter->SegmentLengthMask = filter->SegmentLength - 1;
double sizeFactor = binary_fuse_calculate_size_factor(arity, size);
uint32_t capacity = (uint32_t)(round((double)size * sizeFactor));
uint32_t capacity = size <= 1 ? 0 : (uint32_t)(round((double)size * sizeFactor));
uint32_t initSegmentCount =
(capacity + filter->SegmentLength - 1) / filter->SegmentLength -
(arity - 1);
Expand Down Expand Up @@ -410,14 +409,13 @@ static inline bool binary_fuse16_contain(uint64_t key,
// size should be at least 2.
static inline bool binary_fuse16_allocate(uint32_t size,
binary_fuse16_t *filter) {
if(size <= 1) { return false; }
uint32_t arity = 3;
filter->SegmentLength = binary_fuse_calculate_segment_length(arity, size);
filter->SegmentLength = size == 0 ? 4 : binary_fuse_calculate_segment_length(arity, size);
if (filter->SegmentLength > 262144) {
filter->SegmentLength = 262144;
}
filter->SegmentLengthMask = filter->SegmentLength - 1;
double sizeFactor = binary_fuse_calculate_size_factor(arity, size);
double sizeFactor = size <= 1 ? 0 : binary_fuse_calculate_size_factor(arity, size);
uint32_t capacity = (uint32_t)(round((double)size * sizeFactor));
uint32_t initSegmentCount =
(capacity + filter->SegmentLength - 1) / filter->SegmentLength -
Expand Down
10 changes: 9 additions & 1 deletion tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,12 @@ int main() {
printf("\n");
printf("======\n");
}
}

// test small edge-case binary fuse input sizes
testbinaryfuse8(0);
testbinaryfuse8(1);
testbinaryfuse8(2);
testbinaryfuse16(0);
testbinaryfuse16(1);
testbinaryfuse16(2);
}