示例#1
0
        static void Main(string[] args)
        {
            // TODO Open file for reading
            string[] lines = System.IO.File.ReadAllLines("./../../input/words.txt");
            //string[] lines = System.IO.File.ReadAllLines("./../../input/phil.txt");

            // Create or erase output file
            File.Create("./../../input/output.txt").Close();
            StreamWriter file = new System.IO.StreamWriter("./../../input/output.txt", true);

            // TODO Initialize hash table
            MyHashTable table = new MyHashTable(128);

            // While less than 40% full, add entries to table
            int CurrentLine = 0;

            while (table.CurrentCapacity < (table.Length * .4d))
            {
                table.Add(lines[CurrentLine++]);
            }
            //while (table.CurrentCapacity < (table.Length * .99d)) { table.Add(lines[CurrentLine++]); }

            // TODO Get min, max, and avg #probes for the first 30 words
            CurrentLine = 0;
            int ProbeSum = 0;
            int ProbeMin = table.Length;
            int ProbeMax = -1;

            while (CurrentLine < table.CurrentCapacity)
            {
                MyHashNode retrieved = table.Get(lines[CurrentLine]);
                ProbeSum += retrieved.ProbeCount;
                ProbeMin  = retrieved.ProbeCount < ProbeMin ? retrieved.ProbeCount : ProbeMin;
                ProbeMax  = retrieved.ProbeCount > ProbeMax ? retrieved.ProbeCount : ProbeMax;
                CurrentLine++;
            }
            // TODO Get min, max, and avg #probes for the last 30 words

            // TODO Print table
            Console.WriteLine(table.ToString());
            file.Write(table.ToString());

            //
            file.WriteLine($"Minimum number of probes: {ProbeMin}");
            file.WriteLine($"Maximum number of probes: {ProbeMax}");
            file.WriteLine($"average number of probes: {(double)ProbeSum / table.CurrentCapacity}");
            file.Close();
            Console.WriteLine($"Minimum number of probes: {ProbeMin}");
            Console.WriteLine($"Maximum number of probes: {ProbeMax}");
            Console.WriteLine($"average number of probes: {(double)ProbeSum / table.CurrentCapacity}");
            double a = (double)table.CurrentCapacity / (double)table.Length;
            double E = (1 - a / 2) / (1 - a);

            Console.WriteLine($"Load factor (alpha): {a}");
            Console.WriteLine($"Expected number of probes: {E}");

            // End program
            Console.ReadKey();
            Environment.Exit(0);
        }
        /// <summary>
        /// Takes in two Hash Tables, and performs a Left Join on them.  All values from the first table are added to a list, followed by any values in the second table with a matching key.  If table two does not have a matching key, null is added after the first value.  The key and values are added in the following order: [key, value1, value2].
        /// </summary>
        /// <param name="tableOne">First Hash Table to join.</param>
        /// <param name="tableTwo">Second Hash Table to join.</param>
        /// <returns>List of keys and values.</returns>
        public static List <string> LeftJoin(MyHashTable tableOne, MyHashTable tableTwo)
        {
            List <string> resultList = new List <string>();

            if (tableOne == null)
            {
                return(null);
            }

            foreach (var item in tableOne.Map)
            {
                if (item != null)
                {
                    resultList.Add(item.First.Value.Key);
                    resultList.Add(item.First.Value.Value);

                    if (tableTwo.Contains(item.First.Value.Key))
                    {
                        MyNode node = tableTwo.Get(item.First.Value.Key);
                        resultList.Add(node.Value);
                    }
                    else
                    {
                        resultList.Add(null);
                    }
                }
            }

            return(resultList);
        }
示例#3
0
        public void CanHashAKey(string key, int expected)
        {
            MyHashTable <object> table = new MyHashTable <object>(41);
            int actual = table.Hash(key);

            Assert.Equal(expected, actual);
        }
示例#4
0
        public static void NumberTable()
        {
            MyHashTable<int, int> number_table = new MyHashTable<int, int>();

            int[] first_list = new int[] { 5, 8, 19, 39, 13, 99, 40, 38, 84, 66, 75, 1, 45, 92 };

            int[] second_list = new int[] { 6, 9, 20, 43, 14, 100, 41, 37, 85, 67, 76, 2, 46, 3 };

            foreach (int number in first_list)
                number_table.Add(new KeyValuePair<int, int>(number, number));

            foreach (int number in second_list)
                number_table.Add(number, number);

            number_table[55] = 4;

            Console.WriteLine("Values in the Interger Hashtable: {0}\n", string.Join(", ", number_table));

            Console.WriteLine("Keys in the Integer Hashtable: {0}\n", string.Join(", ", number_table.Keys));

            Console.WriteLine("Values in the Integer Hashtable: {0}\n", string.Join(", ", number_table.Values));

            Console.WriteLine("Does the Hashtable contain the Key 101: {0}\n", number_table.ContainsKey(101));

            TestCount(29, number_table.Count, "Element count doesn't match!");
            Console.WriteLine("Elements in the Hashtable: {0}\n", number_table.Count);

            Console.WriteLine("---------------------------------------------------------\n");
        }
示例#5
0
        public static void ExecuteSqlTran(MyHashTable SQLStringList, string id)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionStringLocalTransaction))
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                SqlTransaction trans = connection.BeginTransaction();
                cmd.Transaction = trans;
                bool flag = false;
                int  ids  = 0;
                if (!string.IsNullOrEmpty(id))
                {
                    flag = true;
                }
                try
                {
                    foreach (string entry in SQLStringList.Keys)
                    {
                        string         cmdText  = entry;
                        SqlParameter[] cmdParms = (SqlParameter[])SQLStringList[entry];
                        if (ids > 0)
                        {
                            for (int i = 0; i < cmdParms.Length; i++)
                            {
                                if (cmdParms[i].ParameterName == id.Trim())
                                {
                                    cmdParms[i].Value = ids;
                                    ids  = 0;
                                    flag = false;
                                }
                            }
                        }

                        PrepareCommand(cmd, connection, trans, cmdText, cmdParms);
                        if (flag)
                        {
                            ids = Convert.ToInt32(cmd.ExecuteScalar());
                        }
                        else
                        {
                            cmd.ExecuteNonQuery();
                        }

                        cmd.Parameters.Clear();
                    }
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message, ex);
                    trans.Rollback();
                    throw;
                }
            }
        }
示例#6
0
        public void CanHashValueToKey()
        {
            MyHashTable hashTable = new MyHashTable();

            int hashKey = hashTable.Hash("test");

            Assert.InRange(hashKey, 0, 1024);
        }
示例#7
0
        /// <summary>
        /// Takes in two Binary Search Trees and uses a HashTable to find any values that are shared between the trees.  Returns a List of all common values.  If no values are shared returns null.
        /// </summary>
        /// <param name="treeOne">First Tree to compare against.</param>
        /// <param name="treeTwo">Second Tree to compare against.</param>
        /// <returns>List of shared values, or null if no values shared.</returns>
        public static List <int> TreeIntersection(MyBinarySearchTree treeOne, MyBinarySearchTree treeTwo)
        {
            if (treeOne.Root == null || treeTwo.Root == null)
            {
                return(null);
            }

            MyHashTable         hashTable  = new MyHashTable();
            List <int>          returnList = new List <int>();
            Queue <Node <int> > queue      = new Queue <Node <int> >();

            queue.Enqueue(treeOne.Root);

            while (queue.Count != 0)
            {
                Node <int> node = queue.Dequeue();

                if (node.LChild != null)
                {
                    queue.Enqueue(node.LChild);
                }
                if (node.RChild != null)
                {
                    queue.Enqueue(node.RChild);
                }

                hashTable.Add(node.Value.ToString(), "");
            }

            queue.Enqueue(treeTwo.Root);

            while (queue.Count != 0)
            {
                Node <int> node = queue.Dequeue();

                if (node.LChild != null)
                {
                    queue.Enqueue(node.LChild);
                }
                if (node.RChild != null)
                {
                    queue.Enqueue(node.RChild);
                }

                if (hashTable.Contains(node.Value.ToString()))
                {
                    returnList.Add(node.Value);
                    hashTable.Remove(node.Value.ToString(), "");
                }
            }

            if (returnList.Count > 0)
            {
                return(returnList);
            }

            return(null);
        }
示例#8
0
        public static void Main()
        {
            var myhashtable = new MyHashTable<int, string>();

            myhashtable.Add(3, "ar");

            myhashtable[2] = "asd";

            var indexCheck = myhashtable[2];

            Console.WriteLine("toString:");
            Console.WriteLine(myhashtable);

            Console.WriteLine("indexer:");
            Console.WriteLine(myhashtable[2]);
            Console.WriteLine(indexCheck);

            Console.WriteLine("keys:");
            var keysChecker = myhashtable.Keys;

            foreach (var key in keysChecker)
            {
                Console.WriteLine(key);
            }

            Console.WriteLine("count:");
            Console.WriteLine(myhashtable.Count);
            Console.WriteLine("remove:");
            myhashtable.Remove(4);

            Console.WriteLine(myhashtable[2]);

            myhashtable.Remove(2);

            Console.WriteLine(myhashtable[2]);
            Console.WriteLine("count:");
            Console.WriteLine(myhashtable.Count);

            string res;
            var findChecker = myhashtable.Find(3, out res);
            Console.WriteLine("Find value by key 3:");
            Console.WriteLine(res);
            Console.WriteLine(findChecker);

            Console.WriteLine(myhashtable);
            Console.WriteLine("clear");
            myhashtable.Clear();
            Console.WriteLine(myhashtable);
            Console.WriteLine("----");
            Console.WriteLine("resize");

            for (int i = 0; i < 100; i++)
            {
                myhashtable.Add(i, i.ToString());
            }

            Console.WriteLine(myhashtable);
        }
示例#9
0
        public void CanAddToHashTable(string key, string value)
        {
            MyHashTable <object> table = new MyHashTable <object>(1);

            table.Add(key, value);
            string actual = table.Get(key);

            Assert.Equal(value, actual);
        }
示例#10
0
        public void ReturnsFalseIfKeyExistsInHashTable(string key)
        {
            MyHashTable <object> table = new MyHashTable <object>(50);

            table.Add(key, "value");
            bool actual = table.contains("False!!!!!");

            Assert.False(actual);
        }
示例#11
0
        public void CanGetValueFromHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

            hashTable.Add("test", "test");
            MyNode resultNode = hashTable.Get("test");

            Assert.Equal("test", resultNode.Value);
        }
示例#12
0
        public void CanProperlyGetAValueIfAKeyExistsInTheHashTable(string key, string value)
        {
            MyHashTable <object> table = new MyHashTable <object>(50);

            table.Add(key, value);
            string actual = table.Get(key);

            Assert.Equal(value, actual);
        }
示例#13
0
        public void CanAddNewItem()
        {
            MyHashTable <int, string> myList = new MyHashTable <int, string>();

            myList.Add(1, "one");
            string s = myList[1];

            Assert.Equal("one", s);
        }
示例#14
0
        public void IndexatorsNullAsValueTest()
        {
            MyHashTable <string, bool?> ht = new MyHashTable <string, bool?>(1);
            bool?expectedValue             = null;

            ht["ABC"] = null;
            bool?actualValue = ht["ABC"];

            Assert.AreEqual(expectedValue, actualValue);
        }
示例#15
0
        public void TestAdd_Duplicates()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 1);
            hashTable.Add("asd", 10);

            Assert.AreEqual(1, hashTable.Count);
            Assert.AreEqual(10, hashTable.Find("asd"));
        }
示例#16
0
        public void CanAddKeyValuePairToHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

            hashTable.Add("test", "test");
            MyNode resultNode = hashTable.Get("test");

            Assert.Equal("test", resultNode.Key);
            Assert.Equal("test", resultNode.Value);
        }
示例#17
0
        public void Get_Hash_Code_Works()
        {
            //arrange
            MyHashTable <int> testHT = new MyHashTable <int>(1024);
            //act
            int testHash = testHT.myEqualityComparer.GetHashCode("Brennan");

            //assert
            Assert.Equal(33760, testHash);
        }
示例#18
0
        public void Should_Create_With_Default_Length()
        {
            //arrange

            //act
            MyHashTable <int, int> map = new MyHashTable <int, int>();

            //assert
            map.Count.ShouldBeEquivalentTo(0);
        }
示例#19
0
        public void IndexatorsInvalidTest()
        {
            MyHashTable <float, int> ht = new MyHashTable <float, int>(1);
            float key           = 3.7F;
            int   expectedValue = default(int);

            int actualValue = ht[key];

            Assert.AreEqual(expectedValue, actualValue);
        }
示例#20
0
        public void Min_Bucket_Count_Test()
        {
            //arrange
            MyHashTable <int> testHT = new MyHashTable <int>(124);

            //act
            int thisBucketCount = testHT.bucketCount;

            Assert.Equal(1024, thisBucketCount);
        }
示例#21
0
        public void IndexatorsNullAsKeyTest()
        {
            MyHashTable <string, bool> ht = new MyHashTable <string, bool>(1);
            bool expectedValue            = default(bool);

            ht[null] = false;
            bool actualValue = ht[null];

            Assert.AreEqual(expectedValue, actualValue);
        }
示例#22
0
        public void CanRemoveValueFromHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

            hashTable.Add("test", "test");

            hashTable.Remove("test", "test");

            Assert.False(hashTable.Contains("test"));
        }
        public void TestCount()
        {
            var hashTable = new MyHashTable<string, int>
            {
                { "five", 5 },
                { "six", 6 }
            };

            Assert.AreEqual(2, hashTable.Count);
        }
        public void TestFind()
        {
            var hashTable = new MyHashTable<string, int>
            {
                { "five", 5 },
                { "six", 6 }
            };

            Assert.AreEqual(5, hashTable.Find("five"));
        }
示例#25
0
        public void Insert_An_Item_And_Get_It_Back()
        {
            var    myHashTable = new MyHashTable <int>(10);
            string key         = "myTest";
            int    value       = 1000;

            myHashTable.Set(key, value);
            int result = myHashTable.Get(key);

            Assert.Equal(value, result);
        }
示例#26
0
        public void ShouldAddItemToHashTable()
        {
            var myHashTable = new MyHashTable(allocatedSizeOfTable);
            int hashCode    = Hash.GenerateHash("a", allocatedSizeOfTable);

            myHashTable.Set("a", "a");

            Assert.IsNotNull(myHashTable);
            Assert.IsTrue(myHashTable.LookpUp.Count == 1);
            Assert.IsNotNull(myHashTable.LookpUp.ContainsKey(hashCode));
        }
示例#27
0
        public void IndexatorsValidTest()
        {
            MyHashTable <string, int> ht = new MyHashTable <string, int>(5);
            string key           = "ABC";
            int    expectedValue = -123;

            ht[key] = expectedValue;
            int actualValue = ht[key];

            Assert.AreEqual(expectedValue, actualValue);
        }
示例#28
0
        public void GivenAnEntry_WhenNotContainAEntryWithSameKey_ShouldAddTheEntryInArray()
        {
            // Arrange
            var table = new MyHashTable <int, string>();

            // Act
            table.Put(1, "Guilherme");

            // Assert
            table.Get(1).Should().Be("Guilherme");
        }
示例#29
0
    static void Main(string[] args)
    {
        string[,] initialize = { { "building", "A-51" }, { "apartment", "210" }, { "wow", "nerf Druids" } };

        MyHashTable myhashTable = new MyHashTable(initialize);

        Console.WriteLine(myhashTable["building"].ToString());
        Console.WriteLine(myhashTable["apartment"].ToString());
        Console.WriteLine(myhashTable["wow"].ToString());
        Console.ReadKey();
    }
示例#30
0
        public void CanRemoveValueFromHashTableWithCollisions()
        {
            MyHashTable hashTable = new MyHashTable();

            hashTable.Add("test", "testOne");
            hashTable.Add("test", "testTwo");

            hashTable.Remove("test", "testOne");

            Assert.False(hashTable.ContainsUnique("test", "testOne"));
        }
示例#31
0
        public void CanAddMultipleValuesWithSameKeyToHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

            hashTable.Add("test", "testOne");
            hashTable.Add("test", "testTwo");
            MyNode resultNode = hashTable.Get("test");

            Assert.Equal("test", resultNode.Key);
            Assert.Equal("testTwo", resultNode.Value);
        }
示例#32
0
        public void IncludeFalseTest()
        {
            MyHashTable <string, int> ht = new MyHashTable <string, int>(5);
            string key = "ABC";

            ht[key] = 22;
            bool expected = false;

            bool actual = ht.Include(key + "E");

            Assert.AreEqual(expected, actual);
        }
示例#33
0
        public void IncludeNullTest()
        {
            MyHashTable <string, int> ht = new MyHashTable <string, int>(5);
            string key = "QWERTY";

            ht[key] = 22;
            bool expected = false;

            bool actual = ht.Include(null);

            Assert.AreEqual(expected, actual);
        }
示例#34
0
        private static void TestMyHashTable()
        {
            var foo = new MyHashTable<string, int>();
            foo.Add("one", 1);
            foo.Add("two", 2);
            foo.Add("three", 3);

            foreach(var item in foo.Values())
            {
                Console.WriteLine(item);
            }
        }
示例#35
0
        public void ShouldGetFirstItemByKey()
        {
            var myHashTable = new MyHashTable(allocatedSizeOfTable);
            int hashCode    = Hash.GenerateHash("a", allocatedSizeOfTable);

            myHashTable.Set("a", "a");
            myHashTable.Set("a", "a2");

            string result = myHashTable.GetFirst("a");

            Assert.AreEqual(result, "a");
        }
        public void TestRemove()
        {
            var hashTable = new MyHashTable<string, int>
            {
                { "five", 5 },
                { "six", 6 }
            };

            hashTable.Remove("five");
            hashTable.Remove("six");

            Assert.AreEqual(0, hashTable.Count);
        }
示例#37
0
        public static void HeroTable()
        {
            MyHashTable<string, int> hero_table = new MyHashTable<string, int>();

            List<Hero> adc_list = new List<Hero>();
            adc_list.Add(new Hero { Name = "Ashe", Attack = 57 });
            adc_list.Add(new Hero { Name = "Jinx", Attack = 58 });
            adc_list.Add(new Hero { Name = "Varus", Attack = 55 });
            adc_list.Add(new Hero { Name = "Vayne", Attack = 56 });
            adc_list.Add(new Hero { Name = "Kalista", Attack = 63 });
            adc_list.Add(new Hero { Name = "Jhin", Attack = 53 });
            adc_list.Add(new Hero { Name = "Caitlyn", Attack = 54 });
            adc_list.Add(new Hero { Name = "Draven", Attack = 56 });
            adc_list.Add(new Hero { Name = "Graves", Attack = 61 });
            adc_list.Add(new Hero { Name = "Lucian", Attack = 57 });

            List<Hero> bruiser_list = new List<Hero>();
            bruiser_list.Add(new Hero { Name = "Garen", Attack = 58 });
            bruiser_list.Add(new Hero { Name = "Fiora", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Darius", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Vi", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Wukong", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Shyvana", Attack = 61 });
            bruiser_list.Add(new Hero { Name = "Olaf", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Pantheon", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Riven", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Illaoi", Attack = 60 });

            foreach (Hero adc in adc_list)
            {
                hero_table.Add(new KeyValuePair<string, int>(adc.Name, adc.Attack));
            }

            foreach (Hero bruiser in bruiser_list)
            {
                hero_table.Add(bruiser.Name, bruiser.Attack);
            }

            hero_table["Irelia"] = 62;

            Console.WriteLine("Values in the Hero Hashtable: {0}\n", string.Join(", ", hero_table));

            Console.WriteLine("Keys in the Hero Hashtable: {0}\n", string.Join(", ", hero_table.Keys));

            Console.WriteLine("Values in the Hero Hashtable: {0}\n", string.Join(", ", hero_table.Values));

            Console.WriteLine("Does the Hashtable contain the Key 'Ahri': {0}\n", hero_table.ContainsKey("Ahri"));

            TestCount(21, hero_table.Count, "Element count doesn't match!");
            Console.WriteLine("Elements in the Hashtable: {0}\n", hero_table.Count);
        }
示例#38
0
        static void Main()
        {
            MyHashTable<string, int> ht = new MyHashTable<string, int>();

            ht.Add("Wizard", 10);
            ht.Add("Stronger", 20);

            ht["Barbarian"] = 12;
            ht["Warrior"] = 12;

            Console.WriteLine(ht["Wizard"]);
            ht["Wizard"] = 22;
            Console.WriteLine(string.Join(Environment.NewLine, ht));
        }
示例#39
0
        private static void Main()
        {
            var hashTable = new MyHashTable<string, int>();

            hashTable.Add("five", 5);
            hashTable.Add("six", 6);

            Console.WriteLine(hashTable.Find("five"));
            // throws exception
            //Console.WriteLine(hashTable.Find("seven"));

            //test auto grow

            for (int i = 0; i < 16; i++)
            {
                hashTable.Add(i.ToString(), i);
                Console.WriteLine(hashTable.Count);
            }

            Console.WriteLine(hashTable.Find("five"));
            Console.WriteLine(hashTable.Find("9"));

            Console.WriteLine(hashTable.Count);
            hashTable.Remove("9");
            Console.WriteLine(hashTable.Count);

            Console.WriteLine("test indexator");
            Console.WriteLine(hashTable["10"]);
            hashTable["10"]++;
            Console.WriteLine(hashTable["10"]);
            // throws exception
            //Console.WriteLine(hashTable.Find("9"));

            Console.WriteLine("Test HashTable.Keys enumerator:");
            foreach (var key in hashTable.Keys)
            {
                Console.WriteLine(key);
            }

            Console.WriteLine("Test HashTable enumerator:");
            foreach (var pair in hashTable)
            {
                Console.WriteLine("{0} -> {1}", pair.Key, pair.Value);
            }
        }
        public static void Main()
        {
            var table = new MyHashTable<string, string>();
            table.Add("first", "Pesho");
            table.Add("second", "Gosho");
            table.Add("third", "Tosho");
            table.Add("fourth", "Ivan");
            Console.WriteLine(table.Count);

            foreach (var pair in table)
            {
                Console.WriteLine("{0} -> {1}", pair.Key, pair.Value);
            }

            string first;
            table.Find("first", out first);
            Console.WriteLine(first);

            table.Remove("second");
            Console.WriteLine(table.Count);
            foreach (var pair in table)
            {
                Console.WriteLine("{0} -> {1}", pair.Key, pair.Value);
            }

            Console.WriteLine(table["fourth"]);

            string[] keys = table.Keys;
            foreach (var key in keys)
            {
                Console.WriteLine(key);

            }

            table.Clear();
            Console.WriteLine(table.Count);
        }
        public void TestRemoveShouldThrowException()
        {
            var hashTable = new MyHashTable<string, int>
            {
                { "five", 5 },
                { "six", 6 }
            };

            hashTable.Remove("five");
            hashTable.Remove("six");
            hashTable.Remove("missing");
        }
        public void TestCreation()
        {
            var hashTable = new MyHashTable<string, int>();

            Assert.AreEqual(0, hashTable.Count);
        }