ayazhafiz / xorf

Xor filters - efficient probabilistic hashsets. Faster and smaller than bloom and cuckoo filters.
MIT License
129 stars 27 forks source link

panic when the number of keys is 1 #47

Closed coocood closed 2 years ago

coocood commented 2 years ago

The following code panics with message attempt to subtract with overflow

BinaryFuse8::try_from(&vec![1u64])
ayazhafiz commented 2 years ago

Happy to take a patch for this.

coocood commented 2 years ago

It's caused by capacity is zero, then (capacity + segment_length - 1) / segment_length is zero, less than (arity - 1)

            let capacity: u32 = if size > 1 {
                (size as f64 * size_factor).round() as u32
            } else { 0 };
            let init_segment_count = (capacity + segment_length - 1) / segment_length - (arity - 1);

Any suggestion on how to fix it? Thank you.