···11-# Bloomf - Efficient Bloom filters for OCaml [](https://ci.ocamllabs.io/github/mirage/bloomf)
11+# Bloom - Bloom filters for OCaml
22+23Bloom filters are memory and time efficient data structures allowing
34probabilistic membership queries in a set.
4556A query negative result ensures that the element is not present in the set,
67while a positive result might be a false positive, i.e. the element might not be
77-present and the BF membership query can return true anyway.
88+present and the Bloom filter membership query can return true anyway.
99+1010+Internal parameters of the Bloom filter allow to control its false positive rate
1111+depending on the expected number of elements in it.
1212+1313+## Install
1414+1515+```
1616+opam install bloom
1717+```
1818+1919+Alternatively, you can build from sources with `dune build`.
2020+2121+## Usage
2222+2323+### Generic interface
2424+2525+```ocaml
2626+(* Create a Bloom filter expecting 1000 elements with 1% error rate *)
2727+let bf = Bloom.v ~error_rate:0.01 1000
2828+2929+(* Add elements *)
3030+let () = Bloom.add bf "hello"
3131+let () = Bloom.add bf "world"
3232+3333+(* Query membership *)
3434+let _ = Bloom.mem bf "hello" (* true *)
3535+let _ = Bloom.mem bf "other" (* probably false *)
3636+3737+(* Estimate the number of elements *)
3838+let _ = Bloom.size_estimate bf
3939+```
4040+4141+### Functorial interface
4242+4343+The functorial interface lets you provide a custom hash function:
4444+4545+```ocaml
4646+module My_bloom = Bloom.Make (struct
4747+ type t = string
4848+ let hash = Hashtbl.hash
4949+end)
85099-Internal parameters of the BF allow to control its false positive rate depending
1010-on the expected number of elements in it.
5151+let bf = My_bloom.v ~error_rate:0.01 1000
5252+let () = My_bloom.add bf "hello"
5353+let _ = My_bloom.mem bf "hello" (* true *)
5454+```
11551212-Online documentation is available [here](https://mirage.github.io/bloomf/).
5656+### Set operations
5757+5858+Bloom filters support lossless union and intersection:
5959+6060+```ocaml
6161+let bf1 = Bloom.v ~error_rate:0.01 1000
6262+let bf2 = Bloom.v ~error_rate:0.01 1000
6363+let () = Bloom.add bf1 "a"
6464+let () = Bloom.add bf2 "b"
6565+let combined = Bloom.union bf1 bf2
6666+let _ = Bloom.mem combined "a" (* true *)
6767+let _ = Bloom.mem combined "b" (* true *)
6868+```
13691414-## Install
7070+### Serialization
15711616-The latest version of `bloomf` is available on opam with `opam install bloomf`.
7272+Bloom filters can be serialized to and from bytes:
17731818-Alternatively, you can build from sources with `make` or `dune build`.
7474+```ocaml
7575+let bytes = Bloom.to_bytes bf
7676+let bf' = Bloom.of_bytes bytes (* ('a t, [`Msg of string]) result *)
7777+```
19782079## Tests
2180···27862887## Benchmarks
29883030-Micro benchmarks are provided for `create`, `add`, `mem` and `size_estimate`
8989+Micro benchmarks are provided for `v`, `add`, `mem` and `size_estimate`
3190operations. Expected error rate is 0.01.
32913333-They preform OLS regression analysis using the development version of
9292+They perform OLS regression analysis using the development version of
3493[bechamel](https://github.com/dinosaure/bechamel). To reproduce them, pin
3594`bechamel` then run `dune build @bench`.