bloom

Space-efficient probabilistic Bloom filter with capacity-based sizing.

v1.0.0Data structuresbundledimport "bloom";
Overview Installation Documentation Examples

Overview

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 offline zero dependencies probabilistic filter

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

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install bloom

This adds the library to hphp.yaml, installs it into .hphp/packages/bloom/ and pins the exact version in hphp.lock:

require:
  bloom: "^1.0"

Use it

import "bloom";

bloom is bundled with the compiler, so import "bloom"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install bloom          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install bloom
Packagebloom
Version1.0.0 (follows the compiler)
Entry filebloom.hphp
Sourcepackages/lib/bloom ↗

Documentation

API Reference

Factory Functions

Class BloomFilter

Error Handling

All invalid parameters throw an Exception with a descriptive bloom: prefix:

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.