A Compact Approximator is a space-efficient probabilistic data structure that maps keys to values using a generalization of Bloom filters. Instead of bits, it stores values from a lattice (e.g., integers, timestamps, counters), allowing efficient tracking of upper bounds across keys.
This Go library implements a customizable compact approximator with pluggable hash functions.
- ✅ Associate approximate values with keys
- ⚙️ Use
maxon insert,minon lookup (lattice semantics) - 🔁 Customizable hash algorithms (FNV-1a, SHA-256)
- 🧪 Thoroughly tested
- 🪪 MIT licensed
- 📁 Includes real-world usage examples
- Library is written by AI and inspected by me.
go get github.com/didip/compactapproxpackage main
import (
"fmt"
"github.com/didip/compactapprox/approx"
)
func main() {
approx, err := approx.NewCompactApproximator(1024, 4, approx.FNV)
if err != nil {
panic(err)
}
key := []byte("user:567")
approx.Insert(key, 123)
approx.Insert(key, 110)
value := approx.Get(key)
fmt.Printf("Approximate value for key %s: %d\n", key, value)
}- Uses an array of
uint64values (instead of bits). Insert(key, value)updateskpositions in the array using:table[i] = max(table[i], value)
Get(key)reads the samekpositions and returns:min(table[i])
This provides an upper bound approximation of the value associated with a key.
You can choose which hash function to use at creation:
approx.NewCompactApproximator(numBuckets, numHashes, approx.FNV)
approx.NewCompactApproximator(numBuckets, numHashes, approx.SHA256)FNV: Fast and simple (default)SHA256: Cryptographic-quality hash (slower, more uniform)
Run tests with:
go test ./approxcompactapproximator/
├── LICENSE
├── go.mod
├── README.md
├── .github/workflows/test.yml
├── approx/
│ ├── approx.go
│ └── approx_test.go
└── examples/
├── basic/main.go
└── sensor/main.go
This project is licensed under the MIT License.
Feel free to open issues or pull requests. Suggestions and improvements welcome!