/// <summary> /// Creates a new Bloom Filter given capacity, error rate, bit array size and number of hash functions /// </summary> /// <param name="capacity">Number of expected elements to be inserted</param> /// <param name="secondaryHashFunction">Secondary hash Functions to be used to generate combination of hashes. Dont pass GetHashCode</param> /// <param name="hashBitsSize">Size of bit array to be stored</param> /// <param name="errorRate">Expected error rate for the bloom filter</param> /// <param name="numberOfHashFunctions">Number of hash functions to be used while storing and retrieving value in bloom filter</param> public BloomFilter(int capacity, HashFunction secondaryHashFunction, int hashBitsSize, float errorRate, int numberOfHashFunctions) { if (capacity < 1) { throw new ArgumentOutOfRangeException(nameof(capacity), capacity, "Number of elements to be inserted in bloom filter must be > 0"); } if (hashBitsSize < 1) { throw new ArgumentOutOfRangeException(nameof(hashBitsSize), hashBitsSize, "Size of HashBits in bloom filter is expected to be >0"); } if (errorRate <= 0 || errorRate >= 1) { throw new ArgumentOutOfRangeException(nameof(errorRate), errorRate, "Error rate is expected to be between (0.0,1.0)"); } if (numberOfHashFunctions < 1) { throw new ArgumentOutOfRangeException(nameof(numberOfHashFunctions), numberOfHashFunctions, "Number of Hash Functions in bloom filter is expected to be >0"); } this.HashBits = new BitArray(hashBitsSize); this.NumberOfHashFunctions = numberOfHashFunctions; if (secondaryHashFunction != null) { this.SecondaryHashFunction = secondaryHashFunction; } else { if (typeof(T) == typeof(string)) { this.SecondaryHashFunction = HashString; } else { throw new ArgumentNullException("HashFunction", "Non null values are expected for each hash function in bloom filter"); } } }
/// <summary> /// Creates a BloomFilter for the specified expected element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expectedElements">The expected elements.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(int expectedElements, HashFunction hashFunction) { return(Build <T>(expectedElements, 0.01, hashFunction)); }
/// <summary> /// Creates a BloomFilter for the specified expected element /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> /// <returns></returns> public static Filter <T> Build <T>(int expectedElements, double errorRate, HashFunction hashFunction) { return(new FilterMemory <T>(expectedElements, errorRate, hashFunction)); }
/// <summary> /// Initializes a new instance of the <see cref="Filter{T}"/> class. /// </summary> /// <param name="name"></param> /// <param name="capacity">The capacity.</param> /// <param name="hashes">The hashes.</param> /// <param name="hashFunction">The hash function.</param> /// <exception cref="ArgumentOutOfRangeException"> /// capacity - capacity must be > 0 /// or /// hashes - hashes must be > 0 /// </exception> /// <exception cref="ArgumentNullException">hashFunction</exception> public Filter(string name, int capacity, int hashes, HashFunction hashFunction) : base(name, capacity, hashes, hashFunction) { CheckElementType(); }
/// <summary> /// Initializes a new instance of the <see cref="Filter{T}"/> class. /// </summary> /// <param name="name"></param> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> /// <exception cref="ArgumentOutOfRangeException"> /// expectedElements - expectedElements must be > 0 /// or /// errorRate /// </exception> /// <exception cref="ArgumentNullException">hashFunction</exception> public Filter(string name, int expectedElements, double errorRate, HashFunction hashFunction) : base(name, expectedElements, errorRate, hashFunction) { CheckElementType(); }
/// <summary> /// Creates a new Bloom filter, using the optimal size for the underlying data structure based on the desired capacity and error rate, as well as the optimal number of hash functions. /// </summary> /// <param name="capacity">The anticipated number of items to be added to the filter. More than this number of items can be added, but the error rate will exceed what is expected.</param> /// <param name="errorRate">The accepable false-positive rate (e.g., 0.01F = 1%)</param> /// <param name="hashFunction">The function to hash the input values. Do not use GetHashCode(). If it is null, and T is string or int a hash function will be provided for you.</param> public Filter(int capacity, float errorRate, HashFunction hashFunction) : this(capacity, errorRate, hashFunction, bestM(capacity, errorRate), bestK(capacity, errorRate)) { }
/// <summary> /// Creates a new Bloom filter, specifying an error rate of 1/capacity, using the optimal size for the underlying data structure based on the desired capacity and error rate, as well as the optimal number of hash functions. /// </summary> /// <param name="capacity">The anticipated number of items to be added to the filter. More than this number of items can be added, but the error rate will exceed what is expected.</param> /// <param name="hashFunction">The function to hash the input values. Do not use GetHashCode(). If it is null, and T is string or int a hash function will be provided for you.</param> public Filter(int capacity, HashFunction hashFunction) : this(capacity, bestErrorRate(capacity), hashFunction) { }
/// <summary> /// Initializes a new instance of the <see cref="FilterMemory{T}"/> class. /// </summary> /// <param name="size">The size.</param> /// <param name="hashes">The hashes.</param> /// <param name="hashFunction">The hash function.</param> public FilterMemory(int size, int hashes, HashFunction hashFunction) : base(size, hashes, hashFunction) { _hashBits = new BitArray(Capacity); }
/// <summary> /// Initializes a new instance of the <see cref="FilterMemory{T}"/> class. /// </summary> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> public FilterMemory(int expectedElements, double errorRate, HashFunction hashFunction) : base(expectedElements, errorRate, hashFunction) { _hashBits = new BitArray(Capacity); }
/// <summary> /// Creates a BloomFilter for the specified expected element /// </summary> /// <param name="expectedElements">The expected elements.</param> /// <param name="errorRate">The error rate.</param> /// <param name="hashFunction">The hash function.</param> /// <param name="name"></param> /// <returns></returns> public static IBloomFilter Build(int expectedElements, double errorRate, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName) { return(new FilterMemory(name, expectedElements, errorRate, hashFunction)); }
/// <summary> /// Creates a BloomFilter for the specified expected element /// </summary> /// <param name="expectedElements">The expected elements.</param> /// <param name="hashFunction">The hash function.</param> /// <param name="name"></param> /// <returns></returns> public static IBloomFilter Build(int expectedElements, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName) { return(Build(expectedElements, 0.01, hashFunction, name)); }
/// <summary> /// Creates a new Bloom Filter given capacity, error rate and number of hash functions /// BitArray size is calculated for given parameters /// </summary> /// <param name="capacity">Number of expected elements to be inserted</param> /// <param name="secondaryHashFunction">Secondary hash Functions to be used to generate combination of hashes. Dont pass GetHashCode</param> /// <param name="errorRate">Expected error rate for the bloom filter</param> /// <param name="numberOfHashFunctions">Number of hash functions to be used while storing and retrieving value in bloom filter</param> public BloomFilter(int capacity, HashFunction secondaryHashFunction, float errorRate, int numberOfHashFunctions) : this(capacity, secondaryHashFunction, OptimalNumberOfHashBits(capacity, errorRate), errorRate, OptimalNumberOfHashes(capacity, errorRate)) { }
/// <summary> /// Creates a new Bloom Filter given capacity and size of bitarray /// Error Rate and Number of hash functions are calculated for given parameters /// </summary> /// <param name="capacity">Number of expected elements to be inserted</param> /// <param name="secondaryHashFunction">Secondary hash Functions to be used to generate combination of hashes. Dont pass GetHashCode</param> /// <param name="hashBitsSize">Size of bit array to be stored</param> public BloomFilter(int capacity, HashFunction secondaryHashFunction, int hashBitsSize) : this(capacity, secondaryHashFunction, hashBitsSize, OptimalErrorRate(capacity), OptimalNumberOfHashes(capacity, OptimalErrorRate(capacity))) { }