예제 #1
0
        /// <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");
                }
            }
        }
예제 #2
0
 /// <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));
 }
예제 #3
0
 /// <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));
 }
예제 #4
0
 /// <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();
 }
예제 #5
0
 /// <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();
 }
예제 #6
0
 /// <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))
 {
 }
예제 #7
0
 /// <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)
 {
 }
예제 #8
0
 /// <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);
 }
예제 #9
0
 /// <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);
 }
예제 #10
0
 /// <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));
 }
예제 #11
0
 /// <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));
 }
예제 #12
0
 /// <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))
 {
 }
예제 #13
0
 /// <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)))
 {
 }