Beispiel #1
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");
        }
Beispiel #2
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);
        }
Beispiel #3
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"));
        }
Beispiel #4
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);
        }
Beispiel #5
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"));
        }
Beispiel #6
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);
            }
        }
Beispiel #7
0
        public void CanContainValue()
        {
            MyHashTable hash = new MyHashTable();

            hash.Add(5, "naming");
            hash.Add(7, "conventions");
            hash.Add(8, "always");
            hash.Add(9, "destroy");
            hash.Add(21, "time");

            Assert.True(hash.Contains("always"));
        }
Beispiel #8
0
        public void CanAddKeyValuePairToHashtable()
        {
            MyHashTable hash = new MyHashTable();

            hash.Add(5, "naming");
            hash.Add(7, "conventions");
            hash.Add(9, "destroy");
            hash.Add(21, "time");

            int findValue = hash.FindKey("destroy");

            Assert.Equal(9, findValue);
        }
Beispiel #9
0
        public void AddItem()
        {
            var set = new MyHashTable <string, int>();

            set.Add("Ricardo", 30);
            set.Add("Hadassa", 29);

            Assert.IsTrue(set.Contains("Ricardo"));
            Assert.IsTrue(set.Contains("Hadassa"));
            Assert.IsFalse(set.Contains("Pedro"));
            Assert.AreEqual(30, set["Ricardo"]);
            Assert.AreEqual(29, set["Hadassa"]);
        }
Beispiel #10
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);
        }
Beispiel #11
0
        public void CanHandleCollision()
        {
            MyHashTable hash = new MyHashTable();

            hash.Add(5, "olive");
            hash.Add(7, "ilove");
            hash.Add(8, "evian");
            hash.Add(22, "olive");
            hash.Add(9, "niave");

            int findValue = hash.FindKey("olive");

            Assert.Equal(5, findValue);
        }
Beispiel #12
0
        public void IEnumerator_Test()
        {
            //arrange
            MyHashTable <string> testHT = new MyHashTable <string>(1024);

            testHT.Add("Diana", "Woof");
            testHT.Add("Jazz", "boof");
            string[] exepected = new string[] { "Jazz,boof", "Diana,Woof" };


            //Assert
            //Assert.NotNull(actual);
            Assert.Equal(exepected, testHT);
        }
Beispiel #13
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));
        }
Beispiel #14
0
        public void No_Collision_Test()
        {
            //arrange
            MyHashTable <int> testHT = new MyHashTable <int>(1024);
            int thisIndex            = testHT.myEqualityComparer.GetHashCode("Diana") % testHT.bucketCount;

            //act
            testHT.Add("Diana", 678);
            testHT.Add("Diana", 978);

            //assert
            Assert.NotNull(testHT.buckets[thisIndex].Next);
            //Assert.Equal(978, testHT.buckets[thisIndex].Next.Value);
        }
Beispiel #15
0
        public void TestClear()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);
            hashTable.Add("qwe", 11);
            hashTable.Add("rty", 12);
            hashTable.Add("fgh", 13);

            hashTable.Clear();

            Assert.AreEqual(0, hashTable.Count);
            Assert.AreEqual(16, hashTable.Capacity);
        }
Beispiel #16
0
        public void TestIndexator_Get_Fail()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

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

            int value = hashTable["qwe"];
        }
Beispiel #17
0
        public void TestContainsKey_False()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

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

            Assert.AreEqual(false, hashTable.ContainsKey("qwe"));
        }
Beispiel #18
0
        public static void Main(string[] args)
        {
            int[] array = { 5, 2, 8 };

            MyLinkedList <int> intList = new MyLinkedList <int>(array);

            // intList[2]++;
            // Error CS0200  Property or indexer 'MyLinkedList<int>.this[int]' cannot be assigned to --it is read only

            // intList[0] = 4;
            // Error CS0200  Property or indexer 'MyLinkedList<int>.this[int]' cannot be assigned to --it is read only
            List <int>[] arrayList = new List <int> [1];

            arrayList[0] = new List <int>();
            arrayList[0].Add(15);
            arrayList[0].Add(10);

            MyLinkedList <List <int> > list = new MyLinkedList <List <int> >(arrayList);

            // Изменения не сохраняются
            list[0].Add(35);

            foreach (var item in list)
            {
                // Изменения не сохраняются
                item.Add(100);
            }

            foreach (var item in list)
            {
                foreach (var item2 in item)
                {
                    // 15
                    // 10
                    Console.WriteLine(item2);
                }
            }

            MyHashTable <int, string> hashTable = new MyHashTable <int, string>();

            hashTable.Add(151312, "Ivan");
            hashTable.Add(1512312, "Eugene");

            foreach (var item in hashTable.GetValues())
            {
                Console.WriteLine(item);
            }
        }
Beispiel #19
0
        public void TestIndexator_Set_Fail()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

            for (int i = 0; i < count; i++)
            {
                hashTable.Add(i.ToString(), i);
            }
            Assert.AreEqual(10, hashTable["asd"]);

            hashTable["qwe"] = 20;
        }
Beispiel #20
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);
        }
        public void WillReturnEmptyArrayIfNoValuesInTableOne()
        {
            MyHashTable tableOne = new MyHashTable();
            MyHashTable tableTwo = new MyHashTable();

            tableTwo.Add("fond", "averse");
            tableTwo.Add("negative", "positive");

            string[] expected = new string[0];

            List <string> resultList = LeftJoin.Program.LeftJoin(tableOne, tableTwo);

            string[] result = resultList.ToArray();

            Assert.Equal(expected, result);
        }
Beispiel #22
0
        public void Should_Throw_When_Key_Is_Presented_For_Adding()
        {
            //arrange
            var key   = "key";
            var value = "value";
            var map   = new MyHashTable <string, string>();

            map.Add(key, value);


            //act
            Action act = () => map.Add(key, value);

            //assert
            act.ShouldThrow <ArgumentException>();
        }
Beispiel #23
0
        public void Should_Check_Contains_Key_For_Multiple_Entries()
        {
            //arrange
            var key   = "key";
            var value = "value";
            var map   = new MyHashTable <string, string>();

            map.Add(key, value);
            map.Add(key + "1", value + "1");

            //act
            var result = map.ContainsKey(key);

            //assert
            result.ShouldBeEquivalentTo(true);
        }
Beispiel #24
0
        public void TestIndexator_Get_Success()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

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

            int value = hashTable["asd"];

            Assert.AreEqual(10, value);
        }
Beispiel #25
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);
        }
Beispiel #26
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);
            }
        }
Beispiel #27
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);
        }
Beispiel #28
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);
        }
Beispiel #29
0
        public void CanGetValueFromHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

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

            Assert.Equal("test", resultNode.Value);
        }
Beispiel #30
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);
        }
Beispiel #31
0
        public void CanAddNewItem()
        {
            MyHashTable <int, string> myList = new MyHashTable <int, string>();

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

            Assert.Equal("one", s);
        }
        public void WillMergeTwoTablesAddingNullIfNoMatchInTableTwo()
        {
            MyHashTable tableOne = new MyHashTable();
            MyHashTable tableTwo = new MyHashTable();

            tableOne.Add("fond", "enamored");
            tableOne.Add("wrath", "anger");

            tableTwo.Add("fond", "averse");
            tableTwo.Add("negative", "positive");

            string[] expected = new string[] { "fond", "enamored", "averse", "wrath", "anger", null };

            List <string> resultList = LeftJoin.Program.LeftJoin(tableOne, tableTwo);

            string[] result = resultList.ToArray();

            Assert.Equal(expected, result);
        }
        public void WillMergeTwoTables()
        {
            MyHashTable tableOne = new MyHashTable();
            MyHashTable tableTwo = new MyHashTable();

            tableOne.Add("fond", "enamored");
            tableOne.Add("wrath", "anger");

            tableTwo.Add("fond", "averse");
            tableTwo.Add("wrath", "delight");

            string[] expected = new string[] { "fond", "enamored", "averse", "wrath", "anger", "delight" };

            List <string> resultList = LeftJoin.Program.LeftJoin(tableOne, tableTwo);

            string[] result = resultList.ToArray();

            Assert.Equal(expected, result);
        }
Beispiel #34
0
        public void CanRemoveValueFromHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

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

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

            Assert.False(hashTable.Contains("test"));
        }
Beispiel #35
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);
        }
Beispiel #36
0
        public void TestTryGetValue_Fail()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

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

            int  value;
            bool found = hashTable.TryGetValue("qwe", out value);

            Assert.AreEqual(false, found);
            Assert.AreEqual(0, 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);
        }