Instances1NFData
NFData (CountMinSketch s)Defined in data-sketches-core-0.3.0.0 · DataSketches.Frequencies.CountMin.Internal
:: a typeCtrl KGHC 10.0.0.20260917 · lts/ghc-10.0.x · fc4e598 · 2026-10-11
Moduledata-sketches-0.4.0.1Haskell2010
Count-Min sketch for approximate frequency estimation.
"How many times has this particular item appeared?" — imagine counting ad impressions per user, or requests per API key, when there are too many distinct items to keep an exact counter for each one. Count-Min gives you an approximate count using fixed memory.
It is conservative: estimates may overcount (due to hash collisions) but never undercount. If an item truly appeared 100 times, the sketch will report >= 100. This makes it safe for rate limiting and frequency capping — you might act slightly early, but never too late.
epsilon (ε): error tolerance. The estimate is within ε * N of the
true count with high probability, where N is the total insertions.
delta (δ): failure probability. The probability the estimate exceeds the error bound is at most δ.
Space used is O(1/ε * log(1/δ)).
Items must be pre-hashed to Word64. Apply a hash function to your domain types before calling insert.
Backed by a C implementation (cbits/countmin.c) behind a ForeignPtr.
All sketch memory lives outside the GHC heap.
import qualified DataSketches.Frequencies.CountMin as CM
import Data.Hashable (hash)
main :: IO ()
main = do
sk <- CM.mkCountMinSketch 0.001 0.01 -- ε=0.1%, δ=1%
mapM_ (\_ -> CM.insert sk 42) [1..500]
freq <- CM.estimate sk 42
putStrLn $ "frequency ≈ " ++ show freq -- ≈ 500
Fully mergeable via merge. Both sketches must have been constructed with the same ε and δ (i.e. identical dimensions).
NFData (CountMinSketch s)Defined in data-sketches-core-0.3.0.0 · DataSketches.Frequencies.CountMin.InternalInsert a single occurrence of an item.
Insert multiple occurrences of an item.
Merge the second sketch into the first. Both must have the same dimensions (same epsilon and delta parameters at construction).
Estimate the frequency of an item. May overcount but never undercounts.
Number of columns in the sketch.
Number of rows (hash functions) in the sketch.