public static void DoTest()
        {
            var cuckooTable = new CuckooHashTable<string, int>();

            cuckooTable.Add("Ahmad", 10);
            cuckooTable.Add("Oliver", 11);
            cuckooTable.Add("Konstantinos", 12);
            cuckooTable.Add("Olympos", 13);
            cuckooTable.Add("Bic", 14);
            cuckooTable.Add("Carter", 15);
            cuckooTable.Add("Sameeros", 16);

            var Ahmad = cuckooTable["Ahmad"];
            Debug.Assert(Ahmad == 10);

            var Oliver = cuckooTable["Oliver"];
            Debug.Assert(Oliver == 11);

            var Konstantinos = cuckooTable["Konstantinos"];
            Debug.Assert(Konstantinos == 12);

            var Olympos = cuckooTable["Olympos"];
            Debug.Assert(Olympos == 13);

            var Bic = cuckooTable["Bic"];
            Debug.Assert(Bic == 14);

            var Carter = cuckooTable["Carter"];
            Debug.Assert(Carter == 15);

            var Sameeros = cuckooTable["Sameeros"];
            Debug.Assert(Sameeros == 16);

            cuckooTable.Clear();
        }
        public static void DoTest()
        {
            var cuckooTable = new CuckooHashTable <string, int>();

            cuckooTable.Add("Ahmad", 10);
            cuckooTable.Add("Oliver", 11);
            cuckooTable.Add("Konstantinos", 12);
            cuckooTable.Add("Olympos", 13);
            cuckooTable.Add("Bic", 14);
            cuckooTable.Add("Carter", 15);
            cuckooTable.Add("Sameeros", 16);

            var Ahmad = cuckooTable["Ahmad"];

            Assert.True(Ahmad == 10);

            var Oliver = cuckooTable["Oliver"];

            Assert.True(Oliver == 11);

            var Konstantinos = cuckooTable["Konstantinos"];

            Assert.True(Konstantinos == 12);

            var Olympos = cuckooTable["Olympos"];

            Assert.True(Olympos == 13);

            var Bic = cuckooTable["Bic"];

            Assert.True(Bic == 14);

            var Carter = cuckooTable["Carter"];

            Assert.True(Carter == 15);

            var Sameeros = cuckooTable["Sameeros"];

            Assert.True(Sameeros == 16);

            cuckooTable.Clear();
        }
Beispiel #3
0
        /// <summary>
        /// Generate the MFORTH hash table.
        /// </summary>
        /// <param name="rom">MFORTH ROM.</param>
        /// <param name="phf1">Upon return, will contain the first
        /// PearsonHashFunction used by the hash table.</param>
        /// <param name="phf2">Upon return, will contain the second
        /// PearsonHashFunction used by the hash table.</param>
        /// <returns>The MFORTH hash table.</returns>
        private static CuckooHashTable<Word> GenerateHashTable(ROM rom, out PearsonHashFunction phf1, out PearsonHashFunction phf2)
        {
            // Initialize our random number generator.
            var random = new Random(HashTableRandomSeed);

            // Initialize our hash functions and the hash table.
            var hashFunc1 = phf1 = new PearsonHashFunction(random);
            var hashFunc2 = phf2 = new PearsonHashFunction(random);
            var hashTable = new CuckooHashTable<Word>(
                (w) => HashName(w.Name, hashFunc1, hashFunc2, HashMask),
                (w) => HashName(w.Name, hashFunc2, hashFunc1, HashMask));

            // Generate the hash table.
            #if FINDING_OPTIMAL_HASH_SEED
            int minValuesAtSecondLocation = int.MaxValue;
            #endif
            for (;;)
            {
            #if !FINDING_OPTIMAL_HASH_SEED
                // Display a progress dot.
                Console.Write(".");
            #endif

                // Add all of the words to our hash table; assume that
                // we will be successful.
                bool haveCompleteTable = true;
                foreach (var word in rom.Words)
                {
                    if (!hashTable.TryAddValue(word))
                    {
                        haveCompleteTable = false;
                        break;
                    }
                }

            #if !FINDING_OPTIMAL_HASH_SEED
                // We're done if all of the words were added to the
                // table.
                if (haveCompleteTable)
                {
                    break;
                }

                // We failed to generate a complete hash table; shuffle
                // the hash functions, clear the table, and try again.
                phf1.ShuffleAuxilliaryTable(random);
                phf2.ShuffleAuxilliaryTable(random);
                hashTable.Clear();
            #else
                // Print out the results if this seed produced better
                // results than the previous best seed.
                if (haveCompleteTable
                    && hashTable.NumValuesStoredAtSecondHash < minValuesAtSecondLocation)
                {
                    Console.WriteLine(
                        "Seed: {0}; 1st: {1}; 2nd: {2}",
                        HashTableRandomSeed,
                        hashTable.NumValuesStoredAtFirstHash,
                        hashTable.NumValuesStoredAtSecondHash);
                    minValuesAtSecondLocation = hashTable.NumValuesStoredAtSecondHash;
                }

                // Generate a new seed and re-create all of the hash
                // functions and tables.
                random = new Random(++HashTableRandomSeed);
                hashFunc1 = phf1 = new PearsonHashFunction(random);
                hashFunc2 = phf2 = new PearsonHashFunction(random);
                hashTable = new CuckooHashTable<Word>(
                    (w) => HashName(w.Name, hashFunc1, hashFunc2, HashMask),
                    (w) => HashName(w.Name, hashFunc2, hashFunc1, HashMask));
            #endif
            }

            // Ensure that all of the words in the ROM were added to the
            // hash table.
            if (rom.NumWords != hashTable.Count)
            {
                throw new InvalidDataException(
                    string.Format(
                        "Hash table only has {0} words; expected {1} words.",
                        hashTable.Count,
                        rom.NumWords));
            }

            // Return the hash table.
            return hashTable;
        }
Beispiel #4
0
        /// <summary>
        /// Generate the MFORTH hash table.
        /// </summary>
        /// <param name="rom">MFORTH ROM.</param>
        /// <param name="phf1">Upon return, will contain the first
        /// PearsonHashFunction used by the hash table.</param>
        /// <param name="phf2">Upon return, will contain the second
        /// PearsonHashFunction used by the hash table.</param>
        /// <returns>The MFORTH hash table.</returns>
        private static CuckooHashTable <Word> GenerateHashTable(ROM rom, out PearsonHashFunction phf1, out PearsonHashFunction phf2)
        {
            // Initialize our random number generator.
            var random = new Random(HashTableRandomSeed);

            // Initialize our hash functions and the hash table.
            var hashFunc1 = phf1 = new PearsonHashFunction(random);
            var hashFunc2 = phf2 = new PearsonHashFunction(random);
            var hashTable = new CuckooHashTable <Word>(
                (w) => HashName(w.Name, hashFunc1, hashFunc2, HashMask),
                (w) => HashName(w.Name, hashFunc2, hashFunc1, HashMask));

            // Generate the hash table.
#if FINDING_OPTIMAL_HASH_SEED
            int minValuesAtSecondLocation = int.MaxValue;
#endif
            for (;;)
            {
#if !FINDING_OPTIMAL_HASH_SEED
                // Display a progress dot.
                Console.Write(".");
#endif

                // Add all of the words to our hash table; assume that
                // we will be successful.
                bool haveCompleteTable = true;
                foreach (var word in rom.Words)
                {
                    if (!hashTable.TryAddValue(word))
                    {
                        haveCompleteTable = false;
                        break;
                    }
                }

#if !FINDING_OPTIMAL_HASH_SEED
                // We're done if all of the words were added to the
                // table.
                if (haveCompleteTable)
                {
                    break;
                }

                // We failed to generate a complete hash table; shuffle
                // the hash functions, clear the table, and try again.
                phf1.ShuffleAuxilliaryTable(random);
                phf2.ShuffleAuxilliaryTable(random);
                hashTable.Clear();
#else
                // Print out the results if this seed produced better
                // results than the previous best seed.
                if (haveCompleteTable &&
                    hashTable.NumValuesStoredAtSecondHash < minValuesAtSecondLocation)
                {
                    Console.WriteLine(
                        "Seed: {0}; 1st: {1}; 2nd: {2}",
                        HashTableRandomSeed,
                        hashTable.NumValuesStoredAtFirstHash,
                        hashTable.NumValuesStoredAtSecondHash);
                    minValuesAtSecondLocation = hashTable.NumValuesStoredAtSecondHash;
                }

                // Generate a new seed and re-create all of the hash
                // functions and tables.
                random    = new Random(++HashTableRandomSeed);
                hashFunc1 = phf1 = new PearsonHashFunction(random);
                hashFunc2 = phf2 = new PearsonHashFunction(random);
                hashTable = new CuckooHashTable <Word>(
                    (w) => HashName(w.Name, hashFunc1, hashFunc2, HashMask),
                    (w) => HashName(w.Name, hashFunc2, hashFunc1, HashMask));
#endif
            }

            // Ensure that all of the words in the ROM were added to the
            // hash table.
            if (rom.NumWords != hashTable.Count)
            {
                throw new InvalidDataException(
                          string.Format(
                              "Hash table only has {0} words; expected {1} words.",
                              hashTable.Count,
                              rom.NumWords));
            }

            // Return the hash table.
            return(hashTable);
        }