static void Main(string[] args) { CustomHash <string> ch = new CustomHash <string>(16); Console.WriteLine("Words:"); int hash = Int32.Parse(Console.ReadLine()); while (hash != -1) { Console.WriteLine("Hex: {0:X}", hash); string word = Console.ReadLine(); ch.Insert((uint)hash, word); // Update hash = Int32.Parse(Console.ReadLine()); } // Now retrieve Console.WriteLine("Find: "); hash = Int32.Parse(Console.ReadLine()); while (hash != -1) { bool cont = ch.Contains((uint)hash); // Print if contains, and value if does Console.WriteLine("Exists: " + cont + ((cont) ? (" Value: " + ch.Get((uint)hash)) : (""))); hash = Int32.Parse(Console.ReadLine()); } Console.WriteLine(ch.ToString()); Console.WriteLine("Press any key to close...."); Console.CursorVisible = false; Console.ReadKey(); }
void Insert(Key <T> key) { size++; uint hash = key.code % tableSize; if (table[hash] == null) { // Input into space table[hash] = key; } else if (table[hash] is Key <T> preValue) { // Enforce no duplicate keys if (preValue.code == key.code) { return; } // Create a new table in that position CustomHash <T> ins = new CustomHash <T>(tableSize); // Change the codes preValue.code = HashShift(preValue.code); key.code = HashShift(key.code); ins.Insert(preValue); ins.Insert(key); table[hash] = ins; } else if (table[hash] is CustomHash <T> pos) { key.code = HashShift(key.code); pos.Insert(key); } }