posts: bloom-filter: add construction

This commit is contained in:
Bruno BELANYI 2024-07-14 17:55:15 +01:00
parent 4abcd27ee7
commit 84ce6ea494

View file

@ -37,3 +37,28 @@ the _false positive_ rate of membership is quite low.
I won't be going into those calculations here, but they are quite trivial to I won't be going into those calculations here, but they are quite trivial to
compute, or one can just look up appropriate values for their use case. compute, or one can just look up appropriate values for their use case.
## Implementation
I'll be using Python, which has the nifty ability of representing bitsets
through its built-in big integers quite easily.
We'll be assuming a `BIT_COUNT` of 64 here, but the implementation can easily be
tweaked to use a different number, or even change it at construction time.
### Representation
A `BloomFilter` is just a set of bits and a list of hash functions.
```python
BIT_COUNT = 64
class BloomFilter[T]:
_bits: int
_hash_functions: list[Callable[[T], int]]
def __init__(self, hash_functions: list[Callable[[T], int]]) -> None:
# Filter is initially empty
self._bits = 0
self._hash_functions = hash_functions
```