Example #1
0
 public HashTable(IHashFunction hashFunction)
 {
     HashFunction = hashFunction;
     for (int i = 0; i < hashSize; i++)
     {
         hashTable[i] = new List.List<int>();
     }
 }
Example #2
0
 public HashTable(int size, IHashFunction function)
 {
     this.size = size;
     this.array = new List[size];
     for (int i = 0; i < size; ++i)
     {
         this.array[i] = new List();
     }
     this.hashFunction = function;
 }
Example #3
0
 /// <summary>
 /// Class constructor
 /// </summary>
 /// <param name="size"></param>
 public HashTable(int size, IHashFunction hashFunction)
 {
     this.hashFunction = hashFunction;
     hashTableSize = size;
     hashTable = new List[hashTableSize];
     for (int i = 0; i < hashTableSize; ++i)
     {
         hashTable[i] = new List();
     }
 }
Example #4
0
 /// <summary>
 /// Изменение хеш-функции
 /// </summary>
 /// <param name="hasfunstion">хеш-функция</param>
 public void ChangeHashFunction(IHashFunction hasfunstion)
 {
     HashFunction = hasfunstion;
     for (int i = 0; i < 100; i++)
     {
         for (int j = 0; j < hashTable[i].Size(); j++)
         {
             int temp = hashTable[i].ReturnByIndex(0);
             InsertToHashTable(temp);
             hashTable[i].Delete(0);
         }
     }
 }
Example #5
0
 public DatabaseLogger(
     IContext context,
     IOperationExecutive operationExecutive,
     IServiceProvider <TEntity> serviceProvider,
     IMappingDataRepository mappingDataRepository,
     IHashingSerializer <TEntity> hashingSerializer,
     IHashFunction hashFunction,
     ILoggingConfigurationProvider loggingConfigurationProvider,
     ISafeRepository safeRepository)
 {
     this.context                      = context;
     this.operationExecutive           = operationExecutive;
     this.serviceProvider              = serviceProvider;
     this.mappingDataRepository        = mappingDataRepository;
     this.hashingSerializer            = hashingSerializer;
     this.hashFunction                 = hashFunction;
     this.loggingConfigurationProvider = loggingConfigurationProvider;
     this.safeRepository               = safeRepository;
 }
        /// <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)
            {
                foreach (ulong element in this.directCount)
                {
                    AddElementHash(element);
                }
            }
            else
            {
                this.directCount = null;
            }
        }
Example #7
0
        /// <summary>
        /// method to find the primary hash function
        /// </summary>
        /// <param name="list"> containing all of the keys and values.</param>
        /// <param name="hashTable">giving a hash table to fill</param>
        /// <param name="maxKeyLength"> giving the maximum length of any key</param>
        /// <returns> giving the sum of the squares of the sizes of each list in the hash table that was filled.</returns>
        private int FindPrimaryHashFunction(IList <KeyValuePair <string, T> > list, IList <KeyValuePair <string, T> >[] hashTable, int maxKeyLength)
        {
            long sum = 0;

            do
            {
                _primaryHashFunction = new RandomHashFunction(list.Count, maxKeyLength);
                for (int i = 0; i < hashTable.Length; i++)
                {
                    hashTable[i] = new List <KeyValuePair <string, T> >();
                }

                foreach (KeyValuePair <string, T> keyPair in list)
                {
                    hashTable[_primaryHashFunction.Hash(keyPair.Key)].Add(keyPair);
                }
                sum = SumOfSquares(hashTable);
            } while ((double)sum > (list.Count * _maximumSecondaryLengthFactor));
            return((int)sum);
        }
Example #8
0
        public MinHashAlgorithm(int hashCount, int groupSize, int windowSize, int blockSize = 0, int step = 0)
        {
            _hashCount  = hashCount;
            _groupSize  = groupSize;
            _windowSize = windowSize;
            _blockSize  = blockSize;
            _step       = step;
            var factory = MurmurHash3Factory.Instance;

            _hashFunctions = new IHashFunction[_hashCount];
            for (var i = 0; i < _hashCount; i++)
            {
                var config = new MurmurHash3Config
                {
                    HashSizeInBits = 32,
                    Seed           = (uint.MaxValue / (uint)hashCount) * ((uint)i)
                };
                _hashFunctions[i] = factory.Create(config);
            }

            _groupHashFunction = CityHashFactory.Instance.Create();
        }
Example #9
0
        private static byte[] GenerateHashImpl(
            ReadOnlySpan <byte> bytes,
            IHashFunction hashFunction)
        {
            var blockSizeBytes = hashFunction.BlockSizeBytes;
            var blockCount     = bytes.Length / blockSizeBytes;
            var remainder      = bytes.Length % blockSizeBytes;

            for (var blockIndex = 0; blockIndex < blockCount; blockIndex++)
            {
                var start = blockIndex * blockSizeBytes;
                var block = bytes.Slice(start, blockSizeBytes);
                hashFunction.AddCompleteBlock(block);
            }

            if (remainder > 0)
            {
                hashFunction.AddRemainder(bytes.Slice(bytes.Length - remainder));
            }

            return(hashFunction.GetFinalHashValue());
        }
Example #10
0
        /// <summary>
        /// method to determine if a given hash function mapping is perfect
        /// </summary>
        /// <param name="secondHashfunction">giving the secondary hash function to test.</param>
        /// <param name="firstTargetLoc">giving the first array location of the target secondary hash table</param>
        /// <param name="secondHashTableLength">giving the length of the target secondary hash table</param>
        /// <param name="elements">elements to place in the target secondary hash table.</param>
        /// <returns>indicating whether the mapping is perfect </returns>
        private bool IsPerfect(IHashFunction secondHashfunction, int firstTargetLoc, int secondHashTableLength, IList <KeyValuePair <string, T> > elements)
        {
            int loc;

            foreach (KeyValuePair <string, T> keyPair in elements)
            {
                loc = secondHashfunction.Hash(keyPair.Key) + firstTargetLoc;
                if (_secondaryTables[loc].Key == null)
                {
                    _secondaryTables[loc] = keyPair;
                }
                else
                {
                    for (int i = firstTargetLoc; i < firstTargetLoc + secondHashTableLength; i++)
                    {
                        _secondaryTables[i] = default(KeyValuePair <string, T>);
                    }
                    return(false);
                }
            }
            return(true);
        }
Example #11
0
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
        protected OpenAddressingHashTable(int size, IHashFunction hashFunction)
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
        {
            // Disable CS8618  because it was complaining about
            // "_array" not being assigned even though it is assigned
            // in the "Resize" method.

            if (size < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }

            // Even numbers works best with the resize logic.
            if (size % 2 != 0)
            {
                throw new ArgumentException(nameof(size));
            }

            this.HashFunction = hashFunction ?? throw new ArgumentNullException(nameof(hashFunction));

            this.Resize(size);
        }
Example #12
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);
            }
        }
Example #13
0
 public HashMultiset(MultisetAbstract <int> multiset, IHashFunction hashFunction, int k)
 {
     _hashFunction = hashFunction;
     _sortedSet    = new List <List <BigInteger> >();
     for (int i = 0; i < multiset.GetLength(); i += 1)
     {
         var m    = multiset.GetMultiset(i);
         var temp = new List <BigInteger>();
         m.ForEach(item =>
         {
             var hash = hashFunction.GetHash(item);
             if ((temp.Count() < k && !temp.Any(x => x == hash) || (temp.Count() >= k && temp[k - 1] > hash)))
             {
                 temp.Add(hash);
                 if (temp.Count() - k > 100)
                 {
                     temp = temp.OrderBy(x => x).Take(k).ToList();
                 }
             }
         });
         _sortedSet.Add(temp.OrderBy(x => x).Take(k).ToList());
     }
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static byte[] ComputeHash(this IHashFunction hashFunction, byte data)
 {
     return(hashFunction.ComputeHash(
                new[] { data }));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <param name="desizedHashSize">Desired size of resulting hash, in bits.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 /// <remarks>
 /// UTF-8 encoding used to convert string to bytes.
 /// </remarks>
 public static byte[] ComputeHash(this IHashFunction hashFunction, string data, int desizedHashSize)
 {
     return(hashFunction.ComputeHash(
                Encoding.UTF8.GetBytes(data),
                desizedHashSize));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <param name="desizedHashSize">Desired size of resulting hash, in bits.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static byte[] ComputeHash(this IHashFunction hashFunction, sbyte data, int desizedHashSize)
 {
     return(hashFunction.ComputeHash(
                new[] { (byte)data },
                desizedHashSize));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 /// <remarks>
 /// UTF-8 encoding used to convert string to bytes.
 /// </remarks>
 public static byte[] ComputeHash(this IHashFunction hashFunction, string data)
 {
     return(hashFunction.ComputeHash(
                Encoding.UTF8.GetBytes(data)));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static IHashValue ComputeHash(this IHashFunction hashFunction, sbyte data)
 {
     return(hashFunction.ComputeHash(
                new[] { (byte)data }));
 }
Example #19
0
        private IHttpClient client = new HttpClientString(); // Http Client

        public PwnedPasswordsValidation(IHashFunction hashFunction, string url)
        {
            _hashFunction = hashFunction;
            _url          = url;
        }
Example #20
0
 public HashTable(IHashFunction hashFunction) : this(20, hashFunction)
 {
 }
Example #21
0
        public void HashFunctions()
        {
            Logger.Log("Analyzing hash functions", Logger.TabChange.Increase);

            var hashes = new IHashFunction[] {
                /*new HashFunction1(), new HashFunction2(), new HashFunction3(), new HashFunction4(),
                new HashFunction5(), new HashFunction6(), new HashFunction7(),*/ new HashFunction8() };

            for (int i = 1; i <= hashes.Length; i++)
            {
                Logger.Log("Analyzing hash function #" + i, Logger.TabChange.Increase);

                var results = EveryDistinctName.GroupBy(n => hashes[i-1].HashName(n)).Select(g => g.ToArray()).OrderBy(s => s.Length).ToArray();

                Logger.Log("Function #" + i + " produces " + results.Length + " unique names");

                File.WriteAllLines("Hash_Function_" + i + ".txt", results.Select(group => group.Aggregate(hashes[i - 1].HashName(group.First()) + " (" + group.Length + "): ", (ag, c) => ag + ',' + "\"" + c + "\"")));

                Logger.Log("Hash function #" + i + " analyzed", Logger.TabChange.Decrease);
            }

            Logger.Log("Hash functions analyzed", Logger.TabChange.Decrease);
        }
 public AuthenticateService(IUsersRepository users, IOptions <AppSettings> appSettings, IHashFunction hash)
 {
     _repository  = users;
     _appSettings = appSettings.Value;
     _hash        = hash;
 }
        /// <summary>
        /// Computes hash value for given data.
        /// </summary>
        /// <param name="hashFunction">Hash function to use.</param>
        /// <param name="data">Data to be hashed.</param>
        /// <param name="desiredHashSize">Desired size of resulting hash, in bits.</param>
        /// <returns>
        /// Hash value of the data as byte array.
        /// </returns>
        public static IHashValue ComputeHash(this IHashFunction hashFunction, byte[] data, int desiredHashSize)
        {
            var hash             = new BigInteger();
            var desiredHashBytes = (desiredHashSize + 7) / 8;

            var seededData = new byte[data.Length + 4];

            Array.Copy(data, 0, seededData, 4, data.Length);

            var hashesNeeded = (desiredHashSize + (hashFunction.HashSizeInBits - 1)) / hashFunction.HashSizeInBits;

            // Compute as many hashes as needed
            for (int x = 0; x < Math.Max(hashesNeeded, 1); ++x)
            {
                byte[] currentData;

                if (x != 0)
                {
                    Array.Copy(BitConverter.GetBytes(x), seededData, 4);
                    currentData = seededData;
                }
                else
                {
                    // Use original data for first
                    currentData = data;
                }


                var elementHash = new BigInteger(
                    hashFunction.ComputeHash(currentData)
                    .Hash
                    .Concat(new[] { (byte)0 })
                    .ToArray());

                hash |= elementHash << (x * hashFunction.HashSizeInBits);
            }


            // XOr-fold the extra bits
            if (hashesNeeded * hashFunction.HashSizeInBits != desiredHashSize)
            {
                var mask = ((new BigInteger(1) << desiredHashSize) - 1);

                hash = (hash ^ (hash >> desiredHashSize)) & mask;
            }


            // Convert to array that contains desiredHashSize bits
            var hashBytes = hash.ToByteArray();

            // Account for missing or extra bytes.
            if (hashBytes.Length != desiredHashBytes)
            {
                var buffer = new byte[desiredHashBytes];
                Array.Copy(hashBytes, buffer, Math.Min(hashBytes.Length, desiredHashBytes));

                hashBytes = buffer;
            }

            return(new HashValue(hashBytes, desiredHashSize));
        }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <param name="desiredHashSize">Desired size of resulting hash, in bits.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static IHashValue ComputeHash(this IHashFunction hashFunction, ulong data, int desiredHashSize)
 {
     return(hashFunction.ComputeHash(
                BitConverter.GetBytes(data),
                desiredHashSize));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <param name="desiredHashSize">Desired size of resulting hash, in bits.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static IHashValue ComputeHash(this IHashFunction hashFunction, byte data, int desiredHashSize)
 {
     return(hashFunction.ComputeHash(
                new[] { data },
                desiredHashSize));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <param name="desizedHashSize">Desired size of resulting hash, in bits.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static byte[] ComputeHash(this IHashFunction hashFunction, ushort data, int desizedHashSize)
 {
     return(hashFunction.ComputeHash(
                BitConverter.GetBytes(data),
                desizedHashSize));
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static byte[] ComputeHash(this IHashFunction hashFunction, int data)
 {
     return(hashFunction.ComputeHash(
                BitConverter.GetBytes(data)));
 }
 protected abstract IHashTable <string> CreateInstance(int initialSize, IHashFunction hashFunction);
Example #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HashTable"/> class.
 /// </summary>
 /// <param name="hashFunction">Hash function</param>
 public HashTable(IHashFunction hashFunction)
 {
     this.hashFunction = hashFunction;
     size = 20;
     CreateBuckets();
 }
Example #30
0
 public UsersService(Func <ParrotWingsDBContext> contextFactory, IHashFunction hash)
 {
     _hash           = hash;
     _contextFactory = contextFactory;
 }
 /// <summary>
 /// Computes hash value for given data.
 /// </summary>
 /// <param name="hashFunction">Hash function to use.</param>
 /// <param name="data">Data to be hashed.</param>
 /// <returns>
 /// Hash value of the data as byte array.
 /// </returns>
 public static IHashValue ComputeHash(this IHashFunction hashFunction, float data)
 {
     return(hashFunction.ComputeHash(
                BitConverter.GetBytes(data)));
 }
Example #32
0
 public DoubleHashingHashTable(int size, IHashFunction hashFunction) : base(size, hashFunction)
 {
 }
 internal void SetHashFunctionAfterDeserializing(StreamingContext context)
 {
     this.hashFunction = HashFunctionFactory.GetHashFunction(this.hashFunctionId);
 }
Example #34
0
        public DoubleHash(IHashFunction hash, int t)
        {
            m = 1UL << t;

            g = hash;
        }
Example #35
0
 protected override IHashTable <string> CreateInstance(int initialSize, IHashFunction hashFunction)
 {
     return(new LinearProbingHashTable <string>(initialSize, hashFunction));
 }
Example #36
0
 public LoginContext(IHashFunction hashFunction)
 {
     _hashFunction = hashFunction;
 }