Exemple #1
0
 public void FactoryCanProduceAllHashFunctionTypes()
 {
     // Make sure factory can produce each HashFunctionId
     foreach (HashFunctionId hashFunctionId in Enum.GetValues(typeof(HashFunctionId)))
     {
         IHashFunction hashFunction = HashFunctionFactory.GetHashFunction(hashFunctionId);
         Assert.IsNotNull(hashFunction, "Factory created a null hash function with ID {0}", hashFunctionId);
     }
 }
        /// <summary>
        ///     Creates a CardinalityEstimator with the given <paramref name="state" />
        /// </summary>
        internal CardinalityEstimator(CardinalityEstimatorState state)
        {
            this.bitsPerIndex = state.BitsPerIndex;
            this.bitsForHll   = 64 - this.bitsPerIndex;
            this.m            = (int)Math.Pow(2, this.bitsPerIndex);
            this.alphaM       = GetAlphaM(this.m);
            this.subAlgorithmSelectionThreshold = GetSubAlgorithmSelectionThreshold(this.bitsPerIndex);

            // Init the hash function
            this.hashFunctionId = state.HashFunctionId;
            this.hashFunction   = HashFunctionFactory.GetHashFunction(this.hashFunctionId);

            // Init the direct count
            this.directCount = state.DirectCount != null ? new HashSet <ulong>(state.DirectCount) : null;

            // Init the sparse representation
            this.isSparse       = state.IsSparse;
            this.lookupSparse   = state.LookupSparse != null ? new Dictionary <ushort, byte>(state.LookupSparse) : null;
            this.lookupDense    = state.LookupDense;
            this.CountAdditions = state.CountAdditions;

            // Each element in the sparse representation takes 15 bytes, and there is some constant overhead
            this.sparseMaxElements = Math.Max(0, this.m / 15 - 10);
            // If necessary, switch to the dense representation
            if (this.sparseMaxElements <= 0)
            {
                SwitchToDenseRepresentation();
            }

            // if DirectCount is not null, populate the HLL lookup with its elements.  This allows serialization to include only directCount
            if (this.directCount != null)
            {
                // since we are re-initializing the object, we need to reset isSparse to true and sparse lookup
                isSparse          = true;
                this.lookupSparse = new Dictionary <ushort, byte>();
                foreach (ulong element in this.directCount)
                {
                    AddElementHash(element);
                }
            }
            else
            {
                this.directCount = null;
            }
        }
Exemple #3
0
        public void EachImplmentationHasUniqueId()
        {
            Array hashFunctionIds = Enum.GetValues(typeof(HashFunctionId));
            // Discover and count all implementations of IHashFunction
            int hashFunctionTypesCount =
                typeof(IHashFunction).Assembly.GetTypes().Count(t => typeof(IHashFunction).IsAssignableFrom(t) && t.IsClass);

            Assert.AreEqual(hashFunctionIds.Length, hashFunctionTypesCount,
                            "Number of IHashFunction implementations must match number of HashFunctionIds");

            // Make sure the IDs are unique
            ISet <HashFunctionId> knownIds = new HashSet <HashFunctionId>();

            foreach (HashFunctionId hashFunctionId in hashFunctionIds)
            {
                IHashFunction hashFunction = HashFunctionFactory.GetHashFunction(hashFunctionId);
                if (knownIds.Contains(hashFunction.HashFunctionId))
                {
                    Assert.Fail("Hash function ID {0} has more than one implementation!", hashFunction.HashFunctionId);
                }
                knownIds.Add(hashFunction.HashFunctionId);
            }
        }
 internal void SetHashFunctionAfterDeserializing(StreamingContext context)
 {
     this.hashFunction = HashFunctionFactory.GetHashFunction(this.hashFunctionId);
 }