Skip to content
Jennifer Programming Language

bloom API reference

A Bloom filter: a compact, probabilistic set. add records a string; mightContain tests membership with no false negatives (a member always reports true) but possible false positives (a non-member may report true, with a probability that grows as the filter fills). The bit array is packed into bytes; the hashes positions per item come from double-hashing one SHA-256 digest (pos_i = (h1 + i*h2) mod size), so a single hash yields all k positions.

Value-semantic: add / addAll / union return the updated filter. Called as $f = bloom.add($f, x) on a local they update the bit array in place; otherwise the filter is copied. Over hash + math + binary; runs on both binaries.

Import with import "bloom.j" as bloom;. See the bloom guide for prose and examples.

Contents

NameWhat it is
addAdd an item to the filter.
addAllAdd every item of a list.
deserializeReconstruct a filter from bytes produced by serialize.
mergeAlias for union.
mightContainTest whether an item might be in the filter.
newCreate an empty filter with size bits and hashes hash functions (k).
optimalCreate a filter sized for n expected elements at a target false-positive rate fpr.
serializeSerialize a filter to bytes: a 4-byte big-endian size, a 4-byte big-endian hashes, then the raw bit array.
unionUnion two filters of the same size and k with a bitwise OR.
Filterstruct: A Bloom filter.

Functions

add

jennifer
bloom.add(f as Filter, item as string)

Add an item to the filter.

f
the filter
item
the item to add
Returns Filter
the filter with the item recorded

addAll

jennifer
bloom.addAll(f as Filter, items as list of string)

Add every item of a list.

f
the filter
items
the items to add
Returns Filter
the filter with all items recorded

deserialize

jennifer
bloom.deserialize(b as bytes)

Reconstruct a filter from bytes produced by serialize. The result is identical (same size, k, and bits), so membership is preserved.

b
the encoded filter
Returns Filter
the reconstructed filter
Throws Error
kind "bloom" if the byte layout is malformed

merge

jennifer
bloom.merge(a as Filter, b as Filter)

Alias for union.

a
the first filter
b
the second filter
Returns Filter
a filter holding the members of both
Throws Error
kind "bloom" if the two filters differ in size or k

mightContain

jennifer
bloom.mightContain(f as Filter, item as string)

Test whether an item might be in the filter. False positives are possible; false negatives never happen (a previously added item always returns true).

f
the filter
item
the item to test
Returns bool
true if the item might be present, false if it is definitely absent

new

jennifer
bloom.new(size as int, hashes as int)

Create an empty filter with size bits and hashes hash functions (k).

size
the number of bits (must be >= 1)
hashes
the number of hash positions per item (1 to 255)
Returns Filter
the empty filter
Throws Error
kind "bloom" if size or hashes is out of range

optimal

jennifer
bloom.optimal(n as int, fpr as float)

Create a filter sized for n expected elements at a target false-positive rate fpr. Picks the optimal bit count m = ceil(-(n*ln(fpr))/(ln(2)^2)) and hash count k = round((m/n)*ln(2)) (clamped to 1..255, the range new accepts: a very small fpr would otherwise ask for more than 255 hashes), then returns an empty Filter of that shape.

n
the expected number of elements (must be >= 1)
fpr
the target false-positive rate, in the open interval (0, 1)
Returns Filter
an empty filter sized for the requested load
Throws Error
kind "bloom" if n < 1 or fpr is not in (0, 1)

serialize

jennifer
bloom.serialize(f as Filter)

Serialize a filter to bytes: a 4-byte big-endian size, a 4-byte big-endian hashes, then the raw bit array. deserialize reverses it exactly.

f
the filter to encode
Returns bytes
the encoded filter

union

jennifer
bloom.union(a as Filter, b as Filter)

Union two filters of the same size and k with a bitwise OR. An item present in either input is present in the result.

a
the first filter
b
the second filter
Returns Filter
a filter holding the members of both
Throws Error
kind "bloom" if the two filters differ in size or k

Structs

Filter

A Bloom filter.

jennifer
def struct Filter {
    bits as bytes,
    size as int,
    hashes as int
};
bits
the packed bit array
size
the number of bits
hashes
the number of hash positions per item (k)