Example #1
0
        public static void Main()
        {
            var hashtable = new HashTable<int, string>();
            hashtable.Add(5, "five");
            hashtable.Add(3, "tree");
            hashtable.Add(-6, "minus six");
            Console.WriteLine(hashtable.Find(-6));
            Console.WriteLine("All elements");
            foreach (var item in hashtable)
            {
                Console.WriteLine("Key: {0} => Value: {1}", item.Key, item.Value);
            }

            hashtable.Remove(3);
            Console.WriteLine("3 removed from table");
            try
            {
                Console.WriteLine("Searching for 3 in table");
                hashtable.Find(3);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Hash table has {0} elements", hashtable.Count);
        }
    static void Main()
    {
        var grades = new HashTable<string, int>();
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.Add("Peter", 3);
        grades.Add("Maria", 6);
        grades["George"] = 5;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        grades.AddOrReplace("Peter", 33);
        grades.AddOrReplace("Tanya", 4);
        grades["George"] = 55;
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
        Console.WriteLine("Values: " + string.Join(", ", grades.Values));
        Console.WriteLine("Count = " + string.Join(", ", grades.Count));
        Console.WriteLine("--------------------");

        grades.Remove("Peter");
        grades.Remove("George");
        grades.Remove("George");
        Console.WriteLine("Grades:" + string.Join(",", grades));
        Console.WriteLine("--------------------");

        Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
        Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
        Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
        Console.WriteLine("--------------------");
    }
        public void AddDuplicateKeyElementsTest()
        {
            HashTable<int, char> alphabet = new HashTable<int, char>();

            alphabet.Add(1, 'A');
            alphabet.Add(1, 'a');
        }
        public void InsertValueByDuplicatedKey_ExpectedException()
        {
            HashTable<int, int> hashTable = new HashTable<int, int>(10);

            hashTable.Add(1, 1);
            hashTable.Add(1, 2);
        }
        public void AddShouldThrowExceptionWhenTheSameKeyIsAlreadyPresent()
        {
            var hashtable = new HashTable<string, int>();

            hashtable.Add("gosho", 12);
            hashtable.Add("gosho", 2);
        }
        static void Main(string[] args)
        {
            HashTable<int, string> table = new HashTable<int, string>();

            for (int i = 0; i < 17; i++)
            {
                table.Add(i, (i + 1).ToString());
            }

            Console.WriteLine(table[15]);

            table.Clear();

            for (int i = 0; i < 3000; i++)
            {
                table.Add(i, (i + 1).ToString());
            }

            Console.WriteLine(table[1457]);

            //table.Add(1, "test");
            //table.Add(2, "test");
            ////table.Remove(1);
            //Console.WriteLine(table[1]);
        }
        internal static void Main()
        {
            string decorationLine = new string('-', Console.WindowWidth);
            Console.Write(decorationLine);
            Console.WriteLine("***Presenting the functionality of the data structure 'Hash table'***");
            Console.Write(decorationLine);

            HashTable<string, int> clubsPoints = new HashTable<string, int>();

            Console.WriteLine("---Add operation---");
            clubsPoints.Add("Rabbits-O!", 15);
            clubsPoints.Add("Frogssy", 19);
            clubsPoints.Add("Puma Runners", 17);
            clubsPoints.Add("Lion Kings", 33);
            Console.WriteLine("Count = " + clubsPoints.Count);
            Console.WriteLine();

            Console.WriteLine("---Iterator functionality---");
            PrintClubsPoints(clubsPoints);
            Console.WriteLine();

            Console.WriteLine("---Remove operation---");
            bool isRemoved = clubsPoints.Remove("Frogssy");
            Console.WriteLine("Are 'Frogssy' removed: " + isRemoved);
            Console.WriteLine("Count = " + clubsPoints.Count);
            Console.WriteLine("Hash table after removal:");
            PrintClubsPoints(clubsPoints);
            Console.WriteLine();

            Console.WriteLine("---Indexer---");
            Console.WriteLine("'Lion Kings' points so far: " + clubsPoints["Lion Kings"]);
            clubsPoints["Rabbits-O!"] += 3;
            Console.WriteLine("'Rabbits-O!' points after a win: " + clubsPoints["Rabbits-O!"]);
            Console.WriteLine();

            Console.WriteLine("---Find operation---");
            Console.WriteLine("Info about 'Puma Runners'" + clubsPoints.Find("Puma Runners"));
            Console.WriteLine();

            Console.WriteLine("---ContainsKey operation---");
            Console.WriteLine("Is there a club 'Birdy'? - " + clubsPoints.ContainsKey("Birdy"));
            Console.WriteLine("Is there a club 'Rabbits-O!'? - " + clubsPoints.ContainsKey("Rabbits-O!"));
            Console.WriteLine();

            Console.WriteLine("---Keys property---");
            Console.Write("All clubs names are: ");
            Console.WriteLine(string.Join(", ", clubsPoints.Keys));
            Console.WriteLine();

            Console.WriteLine("---Values property---");
            Console.Write("All club's points are: ");
            Console.WriteLine(string.Join(", ", clubsPoints.Values));
            Console.WriteLine();

            Console.WriteLine("---Clear operation---");
            clubsPoints.Clear();
            Console.WriteLine("Elements count after clearing: " + clubsPoints.Count);
        }
        public void SetNonExistingElementTest()
        {
            HashTable<int, char> alphabet = new HashTable<int, char>();
            alphabet.Add(1, 'A');
            alphabet.Add(2, 'B');
            alphabet.Add(3, 'C');

            alphabet[4] = 'D';
        }
        public void GetNonExistingElementTest()
        {
            HashTable<int, char> alphabet = new HashTable<int, char>();
            alphabet.Add(1, 'A');
            alphabet.Add(2, 'B');
            alphabet.Add(3, 'C');

            char dLetter = alphabet[4];
        }
        public void AddingNewKeyPairShouldCountCorrectly()
        {
            var hashTable = new HashTable<string, int>();
            hashTable.Add("Ivan", 1);
            hashTable.Add("Ivan1", 2);
            hashTable.Add("Ivan2", 3);

            Assert.AreEqual(hashTable.Count, 3);
        }
        public void TestIfAddingASameKeyThrows()
        {
            var table = new HashTable<int, int>();

            table.Add(1, 11);
            table.Add(1, 12);
            var count = table.Count;
            Assert.AreEqual(8, count);
        }
Example #12
0
        private static void Main(string[] args)
        {
            HashTable<int, string> test = new HashTable<int, string>();
            test.Add(5, "Pesho");
            test.Add(45, "Gosho");
            test.Add(18, "Mariika");

            Console.WriteLine("Test: " + string.Join(", ", test));
        }
    public void Add_EmptyHashTable_Duplicates_ShouldThrowException()
    {
        // Arrange
        var hashTable = new HashTable<string, string>();

        // Act
        hashTable.Add("Peter", "first");
        hashTable.Add("Peter", "second");
    }
Example #14
0
 public void HashTableTest()
 {
     HashTable<string, int> table = new HashTable<string, int>(100);
     table.Add("brad", 100);
     table.Add("rich", 200);
     table.Remove("rich");
     Assert.AreEqual(100, table.Find("brad"));
     Assert.AreEqual(0, table.Find("rich"));
 }
        public void FindNonExistingEntryTest()
        {
            HashTable<string, int> studentMarks = new HashTable<string, int>();
            studentMarks.Add("Ivan", 5);
            studentMarks.Add("Pesho", 4);
            studentMarks.Add("Lili", 6);
            studentMarks.Add("Gosho", 3);

            studentMarks.Find("Marin");
        }
        public void CountShouldReturnCorrectResult()
        {
            var table = new HashTable<string, int>();
            table.Add("pesho", 2);
            table.Add("gosho", 3);
            table.Add("stamat", 3);
            table.Remove("gosho");

            Assert.AreEqual(2, table.Count);
        }
 public void Add_GivenKeyAndValue_PairInATable()
 {
     var hashTable = new HashTable<int, int>();
     hashTable.Add(5, 2);
     hashTable.Add(48, 30);
     hashTable.Add(11, 7);
     hashTable.Add(78, 25);
     hashTable.Add(7, 58);
     Assert.AreEqual(7, hashTable.SearchByKey(58));
 }
 public void Remove_GivenKey_ReturnedExpectedValue()
 {
     var hashTable = new HashTable<int, int>();
     hashTable.Add(31, 2);
     hashTable.Add(21, 30);
     hashTable.Add(11, 7);
     hashTable.Add(41, 25);
     hashTable.Add(7, 58);
     Assert.AreEqual(7, hashTable.Remove(11));
 }
        public void FindWithNullKeyTest()
        {
            HashTable<string, int> studentMarks = new HashTable<string, int>();
            studentMarks.Add("Ivan", 5);
            studentMarks.Add("Pesho", 4);
            studentMarks.Add("Lili", 6);
            studentMarks.Add("Gosho", 3);

            studentMarks.Find(null);
        }
        public void Add_Get()
        {
            HashTable<int, string> table = new HashTable<int, string>();
            table.Add(1, "Hello ");
            table.Add(2, "to ");
            table.Add(3, "Everyone");

            string result = table.Get(1) + table.Get(2) + table.Get(3);

            Assert.AreEqual("Hello to Everyone", result);
        }
        public void ContainsKeyFalseTest()
        {
            HashTable<string, int> studentMarks = new HashTable<string, int>();
            studentMarks.Add("Pesho", 4);
            studentMarks.Add("Lili", 6);
            studentMarks.Add("Gosho", 3);

            bool containsIvanStudent = studentMarks.ContainsKey("Ivan");

            Assert.IsFalse(containsIvanStudent);
        }
        public void AddingValuesToHashTable()
        {
            HashTable<int, int> hashTable = new HashTable<int, int>(10);

            hashTable.Add(1, 1);
            hashTable.Add(11, 2);
            hashTable.Add(2, 2);
            hashTable.Add(3, 3);

            Assert.AreEqual(hashTable.Count, 4);
        }
        public void HashTable_GetValue_Key_11_ExpectedValue_2()
        {
            HashTable<int, int> hashTable = new HashTable<int, int>(10);

            hashTable.Add(1, 1);
            hashTable.Add(11, 2);
            hashTable.Add(2, 2);
            hashTable.Add(3, 3);

            Assert.AreEqual(hashTable.GetValue(11), 2);
        }
 public void Remove_GivenTheSameKeySecondTime_ThrownExpectedException()
 {
     var hashTable = new HashTable<int, int>();
     hashTable.Add(31, 2);
     hashTable.Add(21, 30);
     hashTable.Add(11, 7);
     hashTable.Add(41, 25);
     hashTable.Add(7, 58);
     hashTable.Remove(11);
     hashTable.Remove(11);
 }
Example #25
0
 public void Test_Add_ShouldIncreaseCapacityWhenNeeded()
 {
     hashTable = new HashTable<string, string>(4);
     var initialCapacity = hashTable.Capacity;
     hashTable.Add(key1, value1);
     hashTable.Add(key2, value2);
     hashTable.Add(key3, value3);
     var currentCapacity = hashTable.Capacity;
     var expectedCapacity = initialCapacity * 2;
     Assert.AreEqual(expectedCapacity, currentCapacity, "Hash table should increase capacity when range exceeded!");
 }
        public void GetExistingElementTest()
        {
            HashTable<int, char> alphabet = new HashTable<int, char>();
            alphabet.Add(1, 'A');
            alphabet.Add(2, 'B');
            alphabet.Add(3, 'C');

            char bLetter = alphabet[2];

            Assert.AreEqual(3, alphabet.Count);
            Assert.AreEqual('B', bLetter);
        }
        public static void Main(string[] args)
        {
            var table = new HashTable<int, string>();
            table.Add(1, "a");
            table.Add(2, "b");
            table.Add(3, "pesho");

            foreach (var pair in table)
            {
                Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
            }
        }
        public void SetExistingElementTest()
        {
            HashTable<int, char> alphabet = new HashTable<int, char>();
            alphabet.Add(1, 'A');
            alphabet.Add(2, 'B');
            alphabet.Add(3, 'C');

            alphabet[2] = 'b';

            Assert.AreEqual(3, alphabet.Count);
            Assert.AreEqual('b', alphabet[2]);
        }
Example #29
0
        static void Main(string[] args)
        {
            
            HashSet<int> p = new HashSet<int>();
            HashTable<int, int> hashTable = new HashTable<int, int>(2);
            hashTable.Add(3, 4);
            hashTable.Add(7, 4);
            Console.WriteLine(hashTable.Count);
            hashTable.Remove(3);
            Console.WriteLine(hashTable.Count);
            hashTable.Add(3, 4);
            hashTable.Add(8, 4);
            hashTable.Add(13, 4);
            hashTable.Add(3333, 4);
            hashTable.Add(124, 4);
            hashTable.Add(412412, 4);
            hashTable[8] = 25;
            hashTable[8] = -45;
            Console.WriteLine(hashTable.Count);
            foreach (var item in hashTable)
            {
                Console.WriteLine("Key {0}, Value {1}", item.Key, item.Value);
            }
            // Console.WriteLine(hashTable.Count);

            // Console.WriteLine(hashTable.Find(3));

            IEnumerable<int> keys = hashTable.Keys;

            foreach (var key in keys)
            {
                Console.Write(key + ",");
            }
            Console.WriteLine();
            hashTable.Add(17, 34);
            hashTable.Remove(8);
            hashTable.Remove(3333);
            hashTable.Add(-4, 23);
            keys = hashTable.Keys;

            foreach (var key in keys)
            {
                Console.Write(key + ",");
            }
            Console.WriteLine();
            hashTable.Clear();
            Console.WriteLine(hashTable.Count);
            foreach (var item in hashTable)
            {
                Console.WriteLine("Key {0}, Value {1}", item.Key, item.Value);
            }
        }
        public void Enumerate()
        {
            HashTable<int, string> table = new HashTable<int, string>();
            table.Add(1, "Hello ");
            table.Add(2, "to ");
            table.Add(3, "Everyone");

            string result = string.Empty;
            foreach (var value in table)
                result += value;

            Assert.AreEqual("Hello to Everyone", result);
        }
Example #31
0
 private static void AddValuesToHashTable(HashTable <string, string> hashTable)
 {
     hashTable.Add("1", "item 1");
     hashTable.Add("2", "item 2");
     hashTable.Add("7", "item 7");
 }
Example #32
0
 public void GetOneValue()
 {
     testTable.Add("Monday", "1");
     Assert.Equal("1", testTable.Get("Monday"));
 }
Example #33
0
        static void Main()
        {
            var badHashTable = new BadHashTable <int>(100);

            badHashTable.Add(5);
            badHashTable.Add(55);
            badHashTable.Add(555);

            badHashTable.DisplayHashTable();

            Console.WriteLine();
            Console.WriteLine($"Таблица содержит значение 555? {badHashTable.Search(555)}");
            Console.WriteLine($"Таблица содержит значение 2? {badHashTable.Search(2)}");
            Console.WriteLine();


            var person1   = new Person("Коля", 20);
            var person2   = new Person("Толя", 25);
            var person3   = new Person("Оля", 22, 1);
            var person4   = new Person("Валя", 20, 1);
            var person5   = new Person("Валя", 20, 0);
            var person6   = new Person("Витя", 20, 0);
            var hashTable = new HashTable <int, Person>(10);

            hashTable.Add(person1.Id, person1);
            hashTable.Add(person2.Id, person2);
            hashTable.Add(person3.Id, person3);
            hashTable.Add(person4.Id, person4);
            hashTable.Add(person5.Id, person5);
            hashTable.Add(person6.Id, person6);

            hashTable.DisplayHashTable();
            Console.WriteLine();

            Console.WriteLine($"Таблица содержит {person1}  по ключу {person1.Id}? {hashTable.Search(person1.Id, person1)}");
            Console.WriteLine($"Таблица содержит {person4}  по ключу {person4.Id}? {hashTable.Search(person4.Id, person4)}");
            Console.WriteLine();


            var superHash = new SuperHash.SuperHash <Person>(100);


            superHash.Add(person1);
            superHash.Add(person2);
            superHash.Add(person3);
            superHash.Add(person4);
            superHash.Add(person5);
            superHash.Add(person6);


            superHash.DisplayHashTable();

            Console.WriteLine();


            Console.WriteLine($"Таблица содержит значение {new Person("Коля", 20, 0)}? {superHash.Search(new Person("Коля", 20, 0))}");
            Console.WriteLine($"Таблица содержит значение {new Person("Коля", 20, 1)}? {superHash.Search(new Person("Коля", 20, 1))}");
            Console.WriteLine($"Таблица содержит значение {person5}? {superHash.Search(person5)}");

            Console.WriteLine();
        }
Example #34
0
 public void Add(T value)
 {
     hashTable.Add(value.GetHashCode(), value);
 }
 public void AddTest()
 {
     hashTable.Add("Alex");
     Assert.IsTrue(hashTable.Contains("Alex"));
 }
Example #36
0
        public void TestAdd_NullKey()
        {
            HashTable <string, int> hashTable = new HashTable <string, int>();

            hashTable.Add(null, 5);
        }
Example #37
0
    static void Main(string[] args)
    {
        HashTable <string, string> table = new HashTable <string, string>(10);

        table.Add("testKey", "testValue");
        table.Add("greetings", "Hello world !!!");
        table.Add("phone", "0000000");
        table.Add("testKey", "testValue");
        table.Add("greetings", "Hello world !!!");
        table.Add("phone", "0000000");
        table.Add("testKey", "testValue");
        table.Add("greetings", "Hello world !!!");
        table.Add("phone", "0000000");
        table.Add("testKey", "testValue");
        table.Add("greetings", "Hello world !!!");
        table.Add("phone", "0000000");

        var test = table.GetValue("testKey");

        System.Console.WriteLine($"before remove: {test}");

        table.Remove("testKey");

        var test2 = table.GetValue("testKey");

        System.Console.WriteLine($"after remove: {test2}");
        table.Clear();

        System.Console.ReadKey();
    }
Example #38
0
 public void Add(TKey key, TValue value)
 {
     table.Add(key, value);
 }
Example #39
0
        public void TestHashTableClear()
        {
            HashTable <string, int> hashTable = new HashTable <string, int>(3, 0.9f);

            hashTable.Add("m", 10);
            hashTable.Add("b", 2000);
            hashTable.Add("t", 0);
            hashTable.Add("o", 33);
            hashTable.Add("z", 0);
            hashTable.Add("k", -812);
            hashTable.Add("g", 0);
            hashTable.Add("a5", 91);
            hashTable.Add("c", 0);
            hashTable.Add("a2", 22);
            hashTable.Add("a7", 66);
            hashTable.Add("i", 707);
            hashTable.Add("h", -15);

            hashTable.Clear();

            Assert.IsFalse(hashTable.ContainsKey("m"));
            Assert.IsFalse(hashTable.ContainsKey("b"));
            Assert.IsFalse(hashTable.ContainsKey("t"));
            Assert.IsFalse(hashTable.ContainsKey("o"));
            Assert.IsFalse(hashTable.ContainsKey("z"));
            Assert.IsFalse(hashTable.ContainsKey("k"));
            Assert.IsFalse(hashTable.ContainsKey("g"));
            Assert.IsFalse(hashTable.ContainsKey("a5"));
            Assert.IsFalse(hashTable.ContainsKey("c"));
            Assert.IsFalse(hashTable.ContainsKey("a2"));
            Assert.IsFalse(hashTable.ContainsKey("a7"));
            Assert.IsFalse(hashTable.ContainsKey("i"));
            Assert.IsFalse(hashTable.ContainsKey("h"));

            Assert.AreEqual(0, hashTable.Count);
        }
Example #40
0
    static void Main()
    {
        //exercise 2
        HashTable<string, string> phoneBook = new HashTable<string, string>();

        string input = Console.ReadLine();

        while (input != "search")
        {
            string[] splitted = input.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

            var name = splitted[0];
            var phone = splitted[1];

            if (!phoneBook.ContainsKey(name))
            {
                phoneBook.Add(name, phone);
            }

            input = Console.ReadLine();
            if (input == "search")
            {
                string input2 = Console.ReadLine();
                while (input2 != "")
                {
                    if (!phoneBook.ContainsKey(input2))
                    {
                        Console.WriteLine("Contact {0} does not exist.", input2);
                    }
                    else
                    {
                        Console.Write(input2);
                        Console.WriteLine(" -> " + phoneBook[input2]);
                    }

                    input2 = Console.ReadLine();
                }
                
            }


        //exercise 1;
        /*HashTable<char, int> grades = new HashTable<char, int>();

    string input = Console.ReadLine();
    for (int i = 0; i < input.Length; i++)
    {
        var current = input[i];
        if (!grades.ContainsKey(current))
        {
            grades.Add(current, 0);
        }

        grades[current]++;

    }

    foreach (var keyValue in grades.OrderBy(x=> x.Key))
    {
        Console.WriteLine(keyValue.Key + ": " + keyValue.Value + " time/s");
    }*/



        //Lab
        /*HashTable<string, int> grades = new HashTable<string, int>();

    Console.WriteLine("Grades:" + string.Join(",", grades));
    Console.WriteLine("---------------");

    grades.Add("Peter", 3);
        var n = grades.Find("Peter");
        Console.WriteLine("************" + n);

    grades.Add("Maria", 6);
    grades["George"] = 5;
    Console.WriteLine("Grades:" + string.Join(",", grades));
    Console.WriteLine("--------------------");

    grades.AddOrReplace("Peter", 33);
    grades.AddOrReplace("Tanya", 4);
    grades["George"] = 55;
    Console.WriteLine("Grades:" + string.Join(",", grades));
    Console.WriteLine("--------------------");

    Console.WriteLine("Keys: " + string.Join(", ", grades.Keys));
    Console.WriteLine("Values: " + string.Join(", ", grades.Values));
    Console.WriteLine("Count = " + string.Join(", ", grades.Count));
    Console.WriteLine("--------------------");

    grades.Remove("Peter");
    grades.Remove("George");
    grades.Remove("George");
    Console.WriteLine("Grades:" + string.Join(",", grades));
    Console.WriteLine("--------------------");

    Console.WriteLine("ContainsKey[\"Tanya\"] = " + grades.ContainsKey("Tanya"));
    Console.WriteLine("ContainsKey[\"George\"] = " + grades.ContainsKey("George"));
    Console.WriteLine("Grades[\"Tanya\"] = " + grades["Tanya"]);
    Console.WriteLine("--------------------");*/
    }
   
}
Example #41
0
        public void HashTable_Add_Method_Exception()
        {
            var ht = new HashTable();

            Assert.Throws <ArgumentNullException>(() => ht.Add(null, "test"));
        }
Example #42
0
        public void TestHashTable()
        {
            HashTable <string, int> hashTable = new HashTable <string, int>();

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

            if (hashTable.LookUp("643") != 643)
            {
                Assert.Equal(643, hashTable.LookUp("643"));
            }

            if (hashTable.LookUp("56") != 56)
            {
                Assert.Equal(56, hashTable.LookUp("56"));
            }

            if (hashTable.LookUp("275") != 275)
            {
                Assert.True(false);
            }

            if (hashTable.LookUp("477") != 477)
            {
                Assert.True(false);
            }

            ChainableHashTable <string, int> chainableHashTable = new ChainableHashTable <string, int>();

            for (int i = 1; i < 1000; i++)
            {
                chainableHashTable.Add(i.ToString(), i);
            }

            if (hashTable.LookUp("643") != 643)
            {
                Assert.Equal(643, hashTable.LookUp("643"));
            }

            if (hashTable.LookUp("56") != 56)
            {
                Assert.Equal(56, hashTable.LookUp("56"));
            }

            if (hashTable.LookUp("275") != 275)
            {
                Assert.True(false);
            }

            if (hashTable.LookUp("477") != 477)
            {
                Assert.True(false);
            }

            TreeHashTable <string, int> treeHashTable = new TreeHashTable <string, int>();

            for (int i = 1; i < 1000; i++)
            {
                treeHashTable.Add(i.ToString(), i);
            }

            if (treeHashTable.LookUp("643") != 643)
            {
                Assert.Equal(643, treeHashTable.LookUp("643"));
            }

            if (treeHashTable.LookUp("56") != 56)
            {
                Assert.Equal(56, treeHashTable.LookUp("56"));
            }

            if (treeHashTable.LookUp("275") != 275)
            {
                Assert.True(false);
            }

            if (treeHashTable.LookUp("477") != 477)
            {
                Assert.True(false);
            }
        }
Example #43
0
        public void AddItemThat_Is_Not_In_The_HashTable()
        {
            _hashTable.Add("42");

            _hashTable.Should().BeEquivalentTo("42");
        }
        public ChartForm(int Num, DataGridView OutFileView, DataGridView ParametersView)
        {
            InitializeComponent();
            double A, GoodA = 0;
            int    N, GoodMulN = 0, GoodDivN = 0;
            int    MinCollisMul = 0, MinCollisDiv = 0, MaxCollisMul = 0, MaxCollisDiv = 0;

            for (int i = 1; i < Num; i++)
            {
                A = Convert.ToDouble(ParametersView[i, 0].Value);
                N = Convert.ToInt32(ParametersView[i, 1].Value);

                HashTable Division       = new HashTable(N, A, "/");
                HashTable Multiplication = new HashTable(N, A, "*");

                for (int j = 0; j < OutFileView.Rows.Count; j++)
                {
                    Auto autoM = new Auto(Convert.ToString(OutFileView[0, j].Value),
                                          Convert.ToString(OutFileView[1, j].Value),
                                          Convert.ToString(OutFileView[2, j].Value),
                                          Convert.ToInt32(OutFileView[3, j].Value));
                    Auto autoD = new Auto(Convert.ToString(OutFileView[0, j].Value),
                                          Convert.ToString(OutFileView[1, j].Value),
                                          Convert.ToString(OutFileView[2, j].Value),
                                          Convert.ToInt32(OutFileView[3, j].Value));

                    Multiplication.Add(autoM);
                    Division.Add(autoD);
                }
                int CollisMul = NumCollisions(Multiplication);
                int CollisDiv = NumCollisions(Division);

                if (i == 1)
                {
                    MinCollisMul = CollisMul;
                    GoodMulN     = N;
                    GoodA        = A;
                }
                if (i == 1)
                {
                    MinCollisDiv = CollisDiv;
                    GoodDivN     = N;
                }
                if (CollisMul < MinCollisMul)
                {
                    MinCollisMul = CollisMul;
                    GoodMulN     = N;
                    GoodA        = A;
                }
                if (CollisDiv < MinCollisDiv)
                {
                    MinCollisDiv = CollisDiv;
                    GoodDivN     = N;
                }


                if (CollisMul > MaxCollisMul)
                {
                    MaxCollisMul = CollisMul;
                }
                if (CollisDiv > MaxCollisDiv)
                {
                    MaxCollisDiv = CollisDiv;
                }

                this.chart1.Series["Division"].Points.AddXY("Количество\nколлизий\nN=" + N + " A=" + A, CollisDiv);
                this.chart1.Series["Multiplication"].Points.AddXY("Количество\nколлизий\nN=" + N + " A=" + A, CollisMul);
            }

            BoxMinDiv.Text = Convert.ToString(MinCollisDiv);
            BoxMinMul.Text = Convert.ToString(MinCollisMul);
            BoxMaxDiv.Text = Convert.ToString(MaxCollisDiv);
            BoxMaxMul.Text = Convert.ToString(MaxCollisMul);
            BoxNDiv.Text   = Convert.ToString(GoodDivN);
            BoxNMul.Text   = Convert.ToString(GoodMulN);
            BoxAMul.Text   = Convert.ToString(GoodA);
        }
Example #45
0
 public void CreateTestHashTable()
 {
     hashTable = new HashTable(10);
     hashTable.Add("1", "testString1");
 }
Example #46
0
        public void Test_Add_ShouldIncreaseCount()
        {
            hashTable.Add(key1, value1);
            var hashTableCount = hashTable.Count;
            var expectedCount  = 1;

            Assert.AreEqual(expectedCount, hashTableCount, "Hash table should increase count when adding!");
        }
Example #47
0
        public void AddTest()
        {
            for (int i = -2; i < 4; ++i)
            {
                hashTable.Add(i);
            }

            for (int i = -2; i < 4; ++i)
            {
                Assert.IsTrue(hashTable.Exists(i));
            }

            Assert.IsFalse(hashTable.Exists(9));
            Assert.IsFalse(hashTable.Exists(-15));
        }
Example #48
0
    public static void Main()
    {
        HashTable <string, int> marks = new HashTable <string, int>();

        marks.Add("Pesho", 6);
        marks.Add("Pesho", 2);
        marks.Add("Gosho", 3);
        marks.Add("Petranka", 2);
        marks.Add("Stoqnka", 5);
        marks.Add("Kirilka", 4);
        marks.Remove("Stoqnka");
        marks["Pesho"] = 3;

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

        Console.WriteLine(marks.Find("Pesho"));
        Console.WriteLine(marks["Pesho"]);
        Console.WriteLine();

        marks.Clear();

        marks.Add("Pesho", 6);
        marks.Add("Pesho", 2);
        marks.Add("Gosho", 3);
        marks.Add("George", 2);
        marks.Add("Stanley", 5);
        marks.Add("Kate", 4);
        marks.Remove("Stanley");
        marks["Pesho"] = 3;

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

        Console.WriteLine(marks.Find("Pesho"));
        Console.WriteLine(marks["Pesho"]);
    }
        static void Main(string[] args)
        {
            Console.WriteLine("*********Create new list*************");
            var list = new LinkedList <string>()
            {
                "item1", "item2", "item3"
            };

            foreach (var element in list)
            {
                Console.Write($"{element} ");
            }

            Console.WriteLine("\n****Added item4****");
            list.Add("item4");
            foreach (var element in list)
            {
                Console.Write($"{element} ");
            }

            Console.WriteLine("\n****Added item0 to the begining****");
            list.AddAt("item0", 0);
            foreach (var element in list)
            {
                Console.Write($"{element} ");
            }

            Console.WriteLine("\n****Removed first item0 ****");
            list.RemoveAt(0);
            foreach (var element in list)
            {
                Console.Write($"{element} ");
            }

            Console.WriteLine("\n****Removed item2 ****");

            list.Remove("item2");
            foreach (var element in list)
            {
                Console.Write($"{element} ");
            }
            Console.WriteLine("\n****Element and their position****");
            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine($"{list.ElementAt(i)} at {i} position ");
            }

            Console.WriteLine("*********Create new hash table*************");

            var hashtable = new HashTable(5);

            hashtable.Add("1", "testString1");
            hashtable.Add("2", "testString2");
            hashtable.Add("3", "testString3");

            for (int i = 0; i < hashtable.Size; i++)
            {
                Console.WriteLine(hashtable[i]);
            }

            Console.WriteLine($"Hashtable contains '2' key - {hashtable.Contains("2")}");
            Console.WriteLine($"Hashtable contains '4' key - {hashtable.Contains("4")}");

            object value;
            var    result = hashtable.TryGet("1", out value);

            Console.WriteLine($"Hashtable contains '1' key - {hashtable.Contains("4")} with {value} value");

            Console.ReadKey();
        }
Example #50
0
        static void Main(string[] args)
        {
            Console.WriteLine("Задание №1" + "\r\n");

            int[] MyArray = new int[10] {
                1, 5, 8, 9, 34, 98, 23, 1, 45, 28
            };

            Console.Write("Исходный массив: ");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(Convert.ToString(MyArray[i]) + " ");
            }

            //демонстрация бинарного поиска
            Console.WriteLine("\r\n" + "1.Нахождение индекса элемента 98 с помощью бинарного поиска: " + BinarySearch(MyArray, 98, 0, MyArray.Length).ToString());

            //демонстрация поиска по бинарному дереву
            var binaryTree = new BinaryTree();

            Random rand = new Random();

            for (int i = 0; i < 9; i++)
            {
                binaryTree.Add(new BinaryTreeNode(rand.Next(0, 50)));
            }

            Console.WriteLine("\r\n" + "2.Бинарное дерево, поиск элемента и его удаление");
            binaryTree.PrintTree();

            Console.WriteLine("Добавим новый элемент");
            binaryTree.Add(new BinaryTreeNode(66));
            binaryTree.PrintTree();

            Console.WriteLine("Удалим его");
            binaryTree.Remove(66);
            binaryTree.PrintTree();

            //демонстрация фиббоначиева поиска
            Console.WriteLine("\r\n" + "3.Фиббоначиев поиск" + "\r\n");

            FibonacciNumber fib  = Fibonacci.find(21);
            FibonacciNumber fib2 = new FibonacciNumber(8);

            Console.WriteLine("Индекс числа 21 = " + fib.Index);
            Console.WriteLine("Значение элемента с индексм 8 = " + fib2.Number);


            Console.WriteLine("\r\n" + "4.Интерпаляционный поиск" + "\r\n");
            Console.Write("Исходный массив: ");
            for (int i = 0; i < 10; i++)
            {
                Console.Write(Convert.ToString(MyArray[i]) + " ");
            }

            Console.WriteLine("\r\n" + "Индекс числа 9 = " + InterpolationSearch(MyArray, 9));


            Console.WriteLine("\r\n" + "Задание №2" + "\r\n");
            Console.WriteLine("1.Поиск по map, в которой коллизии решаются простым рехэшированием");

            Map <int, string> MyMap = new Map <int, string>();

            MyMap.Add(new ItemMap <int, string>(1, "Один"));
            MyMap.Add(new ItemMap <int, string>(1, "Один"));
            MyMap.Add(new ItemMap <int, string>(2, "Два"));
            MyMap.Add(new ItemMap <int, string>(3, "Три"));
            MyMap.Add(new ItemMap <int, string>(4, "Четыре"));
            MyMap.Add(new ItemMap <int, string>(5, "Пять"));
            MyMap.Add(new ItemMap <int, string>(101, "Сто один"));
            Console.WriteLine("Исходная карта:");
            foreach (var item in MyMap)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\r\n" + "Ищем число c ключем  7 " + (MyMap.Search(7) ?? "Не найдено"));
            Console.WriteLine("Ищем число c ключем 101 " + (MyMap.Search(101) ?? "Не найдено"));

            MyMap.Remove(1);
            MyMap.Remove(101);

            Console.WriteLine("\r\n" + "Удалили 1 и 101 ");
            foreach (var item in MyMap)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\r\n" + "2.Поиск по map, в которой коллизии решаются случайным рехэшированием");

            Map <int, string> MyMap2 = new Map <int, string>();

            MyMap2.Add(new ItemMap <int, string>(2, "Два"));
            MyMap2.Add(new ItemMap <int, string>(2, "Два"));
            MyMap2.Add(new ItemMap <int, string>(9, "Девять"));
            MyMap2.Add(new ItemMap <int, string>(3, "Три"));
            MyMap2.Add(new ItemMap <int, string>(4, "Четыре"));
            MyMap2.Add(new ItemMap <int, string>(5, "Пять"));
            MyMap2.Add(new ItemMap <int, string>(102, "Сто два"));
            Console.WriteLine("Исходная карта:");
            foreach (var item in MyMap2)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\r\n" + "Ищем число c ключем  7 " + (MyMap2.Search(7) ?? "Не найдено"));
            Console.WriteLine("Ищем число c ключем 102 " + (MyMap2.Search(102) ?? "Не найдено"));

            MyMap2.Remove(2);
            MyMap2.Remove(102);

            Console.WriteLine("\r\n" + "Удалили 2 и 102 ");
            foreach (var item in MyMap2)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("\r\n" + "3.Поиск по хэш таблице, в которой коллизии решаются методом цепочек");
            Console.WriteLine("Исходная таблица:");

            HashTable <int> MyHashTable = new HashTable <int>(5);

            MyHashTable.Add(2);
            MyHashTable.Add(24);
            MyHashTable.Add(65);
            MyHashTable.Add(8);
            MyHashTable.Add(51);
            MyHashTable.Add(61);

            MyHashTable.PrintHashTable();

            Console.WriteLine("\r\n" + "Содержит ли хэш таблица число 42 " + MyHashTable.Search(42));
            Console.WriteLine("Содержит ли хэш таблица число 8 " + MyHashTable.Search(8));

            MyHashTable.Delate(24);
            MyHashTable.Delate(51);

            Console.WriteLine("\r\n" + "Удалили 24 и 51 ");
            MyHashTable.PrintHashTable();

            Console.WriteLine("\r\n" + "Задание №3");
            Console.WriteLine("Расстановка ферзей на поле : (ферзь = 1)" + "\r\n");
            Chess MyChess = new Chess();

            MyChess.Arrange();


            Console.ReadLine();
        }
Example #51
0
        static void Main(string[] args)
        {
            HashTable table = new HashTable();

            //Add a set of key/value pairs
            table.Add("Cat", 20);
            table.Add("Zebra", 245);
            table.Add("Tele", 3409);
            table.Add("Frank", 6);

            //Get Hash values of all added keys
            Console.WriteLine("----HashTable-----");
            Console.WriteLine(table.GetHash("Cat") + " is Cat");
            Console.WriteLine(table.GetHash("Zebra") + " is Zebra");
            Console.WriteLine(table.GetHash("Tele") + " is Tele");
            Console.WriteLine(table.GetHash("Frank") + " is Frank");
            Console.WriteLine("-----");

            //Check if keys exist in the Hash table
            Console.WriteLine(table.ContainsKey("Cat"));
            Console.WriteLine(table.ContainsKey("Zebra"));
            Console.WriteLine(table.ContainsKey("Tele"));
            Console.WriteLine(table.ContainsKey("Frank"));
            Console.WriteLine("Key Doe: currently isn't in the table");
            Console.WriteLine(table.ContainsKey("Doe"));
            Console.WriteLine("-----");

            //Find the values of a given keys
            Console.WriteLine(table.FindValue("Cat"));
            Console.WriteLine(table.FindValue("Zebra"));
            Console.WriteLine(table.FindValue("Tele"));
            Console.WriteLine(table.FindValue("Frank"));
            Console.WriteLine("-----");

            //Add a key that creates collision
            table.Add("Doe", 25);
            Console.WriteLine(table.GetHash("Doe") + " is Doe");
            Console.WriteLine(table.GetHash("Cat") + " is Cat");
            Console.WriteLine(table.ContainsKey("Doe"));
            Console.WriteLine("-----");

            //Can still find a value even if the buckets are linked
            Console.WriteLine(table.FindValue("Cat"));
            Console.WriteLine(table.FindValue("Doe"));
            Console.WriteLine(table.ContainsKey("Cat"));
            Console.WriteLine(table.ContainsKey("Doe"));
            Console.WriteLine("-----");
            try
            {
                table.Add("Cat", 50);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("----HashSet-----");

            //HashSet
            HashSet hashSet = new HashSet();

            hashSet.Add("Cat", 20);
            hashSet.Add("Zebra", 245);

            Console.WriteLine(hashSet.ContainsKey("Cat"));
            Console.WriteLine(hashSet.ContainsKey("Zebra"));
            Console.WriteLine(hashSet.ContainsKey("Doe"));
            Console.WriteLine("---");
            //Throw error with collision
            try
            {
                hashSet.Add("Doe", 5);
            }
            catch (Exception)
            {
                Console.WriteLine("Index already has a value");
            }
            Console.WriteLine(table.FindValue("Cat"));
            Console.WriteLine(table.FindValue("Zebra"));
        }
Example #52
0
        public void CanSaveValueByKey()
        {
            var hash = new HashTable();

            hash.Add("Key1", "Value");
        }
 public void Initialize()
 {
     hashTable = new HashTable();
     hashTable.Add("Jacob");
     hashTable.Add("William");
     hashTable.Add("Christopher");
     hashTable.Add("Ryan");
     hashTable.Add("David");
     hashTable.Add("Alexander");
     hashTable.Add("James");
     hashTable.Add("Jonathan");
     hashTable.Add("Jose");
     hashTable.Add("Kevin");
     hashTable.Add("Luke");
     hashTable.Add("Cameron");
 }
Example #54
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                HashTable <string, string> ht = new HashTable <string, string>();

                WriteMsg("HashTable Capacity: " + ht.Capacity);
                WriteMsg("HashTable WhenEnlarge: " + ht.WhenEnlarge);
                WriteMsg("HashTable EnlargeRatio: " + ht.EnlargeRatio);
                WriteLine();

                string key   = "aa";
                string value = "小明";
                uint   i     = ht.Add(key, value);
                WriteAddMsg(key, value, i);

                key   = "bb";
                value = "小刚";
                i     = ht.Add(key, value);
                WriteAddMsg(key, value, i);

                key   = "哈哈";
                value = "小红";
                i     = ht.Add(key, value);
                WriteAddMsg(key, value, i);


                key   = "ccc";
                value = "小花";
                i     = ht.Add(key, value);
                WriteAddMsg(key, value, i);

                key   = "ddd";
                value = "小白";
                i     = ht.Add(key, value);
                WriteAddMsg(key, value, i);

                WriteLine();

                key   = "aa";
                value = ht[key];
                WriteGetMsg(key, value);

                key   = "bb";
                value = ht[key];
                WriteGetMsg(key, value);

                key   = "哈哈";
                value = ht[key];
                WriteGetMsg(key, value);

                key   = "ccc";
                value = ht[key];
                WriteGetMsg(key, value);

                key   = "ddd";
                value = ht[key];
                WriteGetMsg(key, value);

                WriteLine();
            }
            catch (Exception ex)
            {
                WriteMsg(ex.ToString());
                WriteLine();
            }
        }
Example #55
0
        static void Main(string[] args)
        {
            HashTable hashTable = new HashTable();

            hashTable.Add("Qwerty");
        }
Example #56
0
        //public int Capacity
        //{
        //    get
        //    {
        //        return this.set.Capacity;
        //    }
        //}

        public void Add(T value)
        {
            set.Add(value, true);
        }
Example #57
0
        public static void Main(string[] args)
        {
            HashTable <string, int> myHashTable = new HashTable <string, int>();

            myHashTable.Add("one", 1);
            myHashTable.Add("two", 2);
            myHashTable.Add("three", 3);
            myHashTable.Add("four", 4);
            myHashTable.Add("five", 5);
            myHashTable.Add("six", 6);
            myHashTable.Add("seven", 7);
            myHashTable.Add("eight", 8);
            myHashTable.Add("nine", 9);
            myHashTable.Add("ten", 10);
            myHashTable.Add("eleven", 11);
            myHashTable.Add("twelve", 12);
            myHashTable.Add("thirteen", 13);
            myHashTable.Add("fourteen", 14);
            myHashTable.Add("fifteen", 15);
            myHashTable.Add("sixteen", 16);

            var searched = myHashTable.Find("nine");

            myHashTable.Remove("nine");
            myHashTable.Clear();
        }
Example #58
0
 public void IsBelongsTrueInHashTableTest(HashTable hashTable)
 {
     hashTable.Add("999989");
     Assert.IsTrue(hashTable.IsBelongs("999989"));
 }
Example #59
0
 public void CanAddOneKeyValue()
 {
     hashtable.Add("A1", "pen");
     Assert.True(hashtable.Contains("A1"));
 }
Example #60
0
        public void Add_ThirdteenItems()
        {
            HashTable <string, int> hashTable = new HashTable <string, int>();

            hashTable.Add("Pessfsho", 6);
            hashTable.Add("Pesdfsho1", 6);
            hashTable.Add("Peqeqwsho2", 6);
            hashTable.Add("Peawsho3", 6);
            hashTable.Add("Pesqddho4", 6);
            hashTable.Add("Pwdesho5", 6);
            hashTable.Add("Pescszho6", 6);
            hashTable.Add("Pesaaho7", 6);
            hashTable.Add("Pezxcsho8", 6);
            hashTable.Add("Pesqwdho9", 6);
            hashTable.Add("Pescscho10", 6);
            hashTable.Add("Pescsaaho11", 6);
            hashTable.Add("Pesadaho12", 6);
            hashTable.Add("Pesadwho13", 6);

            Assert.IsTrue(hashTable.Count == 14);
        }