Beispiel #1
0
        public void GivenTwoEntries_WhenBothHaveSameIndex_ShouldStoreBothInLinkedList()
        {
            // Arrange
            var table = new MyHashTable <int, string>();

            // Act
            table.Put(5, "a");
            table.Put(500, "b");

            // Assert
            table.Get(5).Should().Be("a");
            table.Get(500).Should().Be("b");
        }
        /// <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);
        }
Beispiel #3
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);
        }
Beispiel #4
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 #5
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 #6
0
        public void CanGetValueFromHashTable()
        {
            MyHashTable hashTable = new MyHashTable();

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

            Assert.Equal("test", resultNode.Value);
        }
Beispiel #7
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);
        }
        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);
        }
Beispiel #9
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");
        }
Beispiel #10
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 #11
0
        public void HandlesCollisionsProperly(string key, string value)
        {
            MyHashTable <object> collisionTable = new MyHashTable <object>(1);

            for (int i = 0; i < 8; i++)
            {
                collisionTable.Add(key, value);
            }
            string actual = collisionTable.Get(key);

            Assert.Equal(value, actual);
        }
Beispiel #12
0
        public void Get_Works_Test()
        {
            //arrange
            MyHashTable <int> testHT = new MyHashTable <int>(1024);

            testHT.Add("Diana", 678);

            //act
            int actual = testHT.Get("Diana");

            //assert
            Assert.Equal(678, actual);
        }
Beispiel #13
0
        public void GivenAnEntry_WhenAlreadyContainAEntryWithSameKey_ShouldUpdateTheValue()
        {
            // Arrange
            var table = new MyHashTable <int, string>();

            table.Put(1, "Juliana");

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

            // Assert
            table.Get(1).Should().Be("Guilherme");
        }
Beispiel #14
0
 /// <summary>
 /// Method that returns first repeated word in sentence/paragraph.
 /// </summary>
 /// <param name="sentence">string</param>
 /// <returns>string</returns>
 public string MyWord(string sentence)
 {
     string[] words = sentence.Split(' ');
     foreach (string word in words)
     {
         if (HT.Get(word) == "value here")
         {
             return(word);
         }
         HT.Add(word.ToLower(), "value here");
     }
     return(null);
 }
        public void Insert_Items_Testing_Colission()
        {
            var    myHashTable = new MyHashTable <int>(10);
            string key         = "myTest";
            string key2        = "myTest2";
            string key3        = "myTest7";
            int    value       = 5;

            myHashTable.Set(key, 10);
            myHashTable.Set(key2, 2);
            myHashTable.Set(key3, value);

            int result = myHashTable.Get(key3);

            Assert.Equal(value, result);
        }
Beispiel #16
0
        public void Get_With_No_Matching_Key_Throws_Error_Test()
        {
            //arrange
            MyHashTable <int> testHT = new MyHashTable <int>(1024);

            testHT.Add("Diana", 678);



            //assert
            Assert.Throws <KeyNotFoundException>(() =>
            {
                //act
                int actual = testHT.Get("Skip");
            });
        }
 public MyHashTable <object> MyLeftJoint(MyHashTable <object> H1, MyHashTable <object> H2)
 {
     foreach (var bucket in H1.Table)
     {
         if (bucket != null)
         {
             foreach (var node in bucket)
             {
                 if (H2.contains(node.Key))
                 {
                     H1.Add(node.Key, H2.Get(node.Key));
                 }
                 Node newNode = new Node(null, null);
                 H1.Add(newNode.Key, newNode.Value);
             }
         }
     }
     return(H1);
 }
        public void WorksAsExpected()
        {
            MyHashTable <object> tableOne = new MyHashTable <object>(20);
            MyHashTable <object> tableTwo = new MyHashTable <object>(20);

            tableOne.Add("fond", "enamored");
            tableOne.Add("wrath", "anger");
            tableOne.Add("diligent", "employed");
            tableOne.Add("outift", "garb");
            tableOne.Add("guide", "usher");
            tableTwo.Add("wrath", "delight");
            tableTwo.Add("diligent", "idle");
            tableTwo.Add("guide", "follow");
            tableTwo.Add("flow", "jam");
            tableTwo.Add("fond", "averse");
            LeftJoin LJ = new LeftJoin();

            LJ.MyLeftJoint(tableOne, tableTwo);
            Assert.Equal("poop", tableOne.Get("fond"));
        }
Beispiel #19
0
        public static HashSet <string[]> LeftJoin(MyHashTable <string> left, MyHashTable <string> right)
        {
            HashSet <string[]> leftHash = new HashSet <string[]>();
            HashSet <string[]> result   = new HashSet <string[]>();

            foreach (var word in left)
            {
                leftHash.Add(word.Split(','));
            }
            foreach (var entry in leftHash)
            {
                if (right.Contains(entry[0]))
                {
                    result.Add(new string[] { entry[0], entry[1], right.Get(entry[0]) });
                }
                else
                {
                    result.Add(new string[] { entry[0], entry[1], "NULL" });
                }
            }
            return(result);
        }
Beispiel #20
0
        public void IfKeyDoesNotExistReturnsNull()
        {
            MyHashTable hashTable = new MyHashTable();

            Assert.Null(hashTable.Get("test"));
        }
Beispiel #21
0
        public void ReturnsNullIfKeyNotInHashTable()
        {
            MyHashTable <object> table = new MyHashTable <object>(3);

            Assert.Null(table.Get("Null"));
        }
Beispiel #22
0
 public void SetAndGet_WhenCalled_SetsTheValueIntoTheCorrectIndex(string key, int value)
 {
     _hash.Set(key, value);
     Assert.That(_hash.Get(key), Is.EqualTo(value));
 }