/// <summary> /// Creates a new Bloom filter optimized to store n items with a specified target /// false-positive rate. /// </summary> /// <param name="n">Number of items to store.</param> /// <param name="fpRate">Desired false positive rate.</param> public BloomFilter(uint n, double fpRate) { var m = Utils.OptimalM(n, fpRate); var k = Utils.OptimalK(fpRate); Buckets = new Buckets(m, 1); Hash = Defaults.GetDefaultHashAlgorithm(); this.m = m; this.k = k; }
/// <summary> /// Creates a new special case of Stable Bloom Filter which is a traditional /// Bloom filter with m bits and an optimal number of hash functions for the /// target false-positive rate. Unlike the stable variant, data is not evicted /// and a cell contains a maximum of 1 hash value. /// </summary> /// <param name="m">Number of cells to decrement</param> /// <param name="fpRate">Desired false-positive rate</param> /// <returns></returns> public static StableBloomFilter NewUnstableBloomFilter(uint m, double fpRate) { var cells = new Buckets(m, 1); var k = Utils.OptimalK(fpRate); return(new StableBloomFilter { Hash = Defaults.GetDefaultHashAlgorithm(), M = m, k = k, p = 0, Max = cells.MaxBucketValue(), cells = cells, IndexBuffer = new uint[k] }); }
/// <summary> /// Creates a new partitioned Bloom filter optimized to store n items with a /// specified target false-positive rate. /// </summary> /// <param name="n">Number of items</param> /// <param name="fpRate">Desired false-positive rate</param> public PartitionedBloomFilter(uint n, double fpRate) { var m = Utils.OptimalM(n, fpRate); var k = Utils.OptimalK(fpRate); var partitions = new Buckets[k]; var s = (uint)Math.Ceiling((double)m / (double)k); for (uint i = 0; i < k; i++) { partitions[i] = new Buckets(s, 1); } this.Partitions = partitions; this.Hash = Defaults.GetDefaultHashAlgorithm(); this.M = m; this.k = k; this.S = s; }
/// <summary> /// Creates a new Stable Bloom Filter with m cells and d bits allocated per cell /// optimized for the target false-positive rate. Use NewDefaultStableFilter if /// you don't want to calculate d. /// </summary> /// <param name="m">Number of cells to decrement</param> /// <param name="d">Bits per cell</param> /// <param name="fpRate">Desired false-positive rate</param> public StableBloomFilter(uint m, byte d, double fpRate) { var k = Utils.OptimalK(fpRate) / 2; if (k > m) { k = m; } else if (k <= 0) { k = 1; } var cells = new Buckets(m, d); this.Hash = Defaults.GetDefaultHashAlgorithm(); this.M = m; this.k = k; this.p = OptimalStableP(m, k, d, fpRate); this.Max = cells.MaxBucketValue(); this.cells = cells; this.IndexBuffer = new uint[k]; }