LibBloomFilter
Bloom Filter for WoW Lua 5.1 environment - Probabilistic set membership testing with minimal memory footprint.
Features
- Efficiently tests whether an element is a member of a set.
- Low memory usage, suitable for constrained environments.
- Simple API for adding and checking elements.
- Configurable false positive rate.
- Compatible with World of Warcraft Lua 5.1 environment.
Installation
To install LibBloomFilter, simply download the LibBloomFilter.lua file and include it in your WoW addon folder. Then, you can load it using LibStub in your addon code.
local LibBloomFilter = LibStub("LibBloomFilter")
Usage
-- Create a new Bloom Filter with expected 1000 values and 1% false positive rate
local filter = LibBloomFilter.New(1000, 0.01)
-- Add values to the filter
for i = 1, 1000 do
filter:Insert("value" .. i)
end
-- Check for membership
for i = 1, 1200 do
local value = "value" .. i
if filter:Contains(value) then
print(value .. " is possibly in the set.")
else
print(value .. " is definitely not in the set.")
end
end
-- Export the filter state, so you can serialize it
local state = filter:Export()
-- Import the filter state into a new filter
local newFilter = LibBloomFilter.Import(state)
API
LibBloomFilter.New(capacity, seed, falsePositiveRate)
Create a new Bloom Filter instance.
capacity (number): Capacity of the filter (expected number of values).
seed (number, optional): Seed for hash functions (default: 0).
falsePositiveRate (number, optional): Desired false positive rate (between 0 and 1, default: 0.01 which means 1%).
- Returns: LibBloomFilter - The new Bloom Filter instance.
filter:Insert(value)
Insert a value into filter.
value (any): Value to insert.
filter:Contains(value)
Determine if a value is possibly in the filter.
value (any): Value to check.
- Returns: boolean - True if value might be in the set, false if definitely not.
filter:Clear()
Clear all values from the filter.
filter:Export()
Export the current state of the filter.
- Returns: LibBloomFilterState - Compact representation of the filter.
LibBloomFilter.Import(state)
Import a new Bloom Filter from a compact representation.
state (LibBloomFilterState): Compact representation of the filter.
- Returns: LibBloomFilter - The imported Bloom Filter instance.
filter:EstimateFalsePositiveRate()
Estimate the current false positive rate (FPR) of the filter based on current load factor.
- Returns: number - Estimated false positive rate.
License
This library is released under the MIT License. See the LICENSE file for details.