A zero-dependency, space-efficient probabilistic BloomFilter for HolyPHP with configurable bit size and hash count, or automatic optimal sizing from expected capacity and target false positive rate.
bundled — works offlinezero dependenciesprobabilisticfilter
Quick start
import "bloom";
// Create a filter sized for 1,000 items with a 1% target false positive rate
$bf = bloom_from_capacity(1000, 0.01);
$bf->add("alice@example.com");
$bf->add("bob@example.com");
println($bf->mightContain("alice@example.com")); // true
println($bf->mightContain("charlie@example.com")); // false (with ~99% probability)
println($bf->estimatedFillRatio());
$bf->clear();
println($bf->mightContain("alice@example.com")); // false
bloom_new(int $bitSize, int $hashCount): BloomFilter Creates a new BloomFilter with the given bit array size and number of hash functions.
bloom_from_capacity(int $expectedItems, float $falsePositiveRate): BloomFilter Computes the optimal bitSize ($m = \lceil -n \ln(p) / (\ln 2)^2 \rceil$) and hashCount ($k = \text{round}((m/n) \ln 2)$) for the expected number of items and desired false positive rate, returning a configured BloomFilter.
Class BloomFilter
new BloomFilter(int $bitSize, int $hashCount) Constructs a filter with $bitSize bits (packed 30 bits per integer word) and $hashCount hash probes using Kirsch–Mitzenmacher double hashing.
add(string $item): void Inserts $item into the filter.
mightContain(string $item): bool Returns false if $item is definitely not in the set, or true if $item is probably in the set (no false negatives).
clear(): void Resets all bits and counters to zero.
estimatedFillRatio(): float Returns the fraction of bits currently set (setBitCount() / bitSize()), in [0.0, 1.0].
estimatedFalsePositiveRate(): float Returns the estimated current false positive probability (pow(estimatedFillRatio(), hashCount())).
bitSize(): int Returns the configured number of bits.
hashCount(): int Returns the configured number of hash functions.
setBitCount(): int Returns the number of bits currently set to 1.
itemCount(): int Returns the number of add() calls since creation or the last clear().
Error Handling
All invalid parameters throw an Exception with a descriptive bloom: prefix:
new BloomFilter(0, 3) → bloom: bitSize must be >= 1, got 0
new BloomFilter(64, 0) → bloom: hashCount must be >= 1, got 0
bloom_from_capacity(0, 0.01) → bloom: expectedItems must be >= 1, got 0
bloom_from_capacity(100, 0.0) → bloom: falsePositiveRate must be between 0.0 and 1.0 (exclusive), got 0
bloom_from_capacity(100, 1.0) → bloom: falsePositiveRate must be between 0.0 and 1.0 (exclusive), got 1
Examples
01Quick start
Bundled with the compiler — import "bloom"; works offline in any file, or add it to a project with hphp install bloom.
import "bloom";
// Create a filter sized for 1,000 items with a 1% target false positive rate
$bf = bloom_from_capacity(1000, 0.01);
$bf->add("alice@example.com");
$bf->add("bob@example.com");
println($bf->mightContain("alice@example.com")); // true
println($bf->mightContain("charlie@example.com")); // false (with ~99% probability)
println($bf->estimatedFillRatio());
$bf->clear();
println($bf->mightContain("alice@example.com")); // false
Try it
hphp install bloom
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_bloom.hphp.