public V Remove(K key) { int index = GetBucketIndex(key); HashNode <K, V> rootNode = array[index] as HashNode <K, V>; HashNode <K, V> prevNode = null; V retVal = default(V); HashNode <K, V> temp = rootNode; while (temp != null) { if (temp.key.Equals(key)) { if (prevNode == null) { /// Case when root node is to be removed array[index] = null; retVal = temp.Value; } else { prevNode.Next = temp.Next; retVal = temp.Value; } } prevNode = temp; temp = temp.Next; } Size--; return(retVal); }
public V Get(K key) { int index = GetBucketIndex(key); HashNode <K, V> node = array[index] as HashNode <K, V>; V retVal = default(V); while (node != null) { if (node.key.Equals(key)) { retVal = node.Value; break; } node = node.Next; } return(retVal); }
public void Add(K key, V value) { int index = GetBucketIndex(key); HashNode <K, V> node = array[index] as HashNode <K, V>; while (node != null) { if (node.key.Equals(key)) { // If key is already present then update existing value node.Value = value; return; } node = node.Next; } // In case if given key is not present then it needs to be added node = array[index] as HashNode <K, V>; HashNode <K, V> newNode = new HashNode <K, V>(key, value); newNode.Next = node; array[index] = newNode; Size++; // Check the load factor if it exceeds the limit (0.7) then double the capacity and again add each key, value pair double loadFactor = Size / capacity; if (loadFactor >= 0.7) { ArrayList temp = array; foreach (HashNode <K, V> tempNode in temp) { HashNode <K, V> currentNode = tempNode; while (currentNode != null) { Add(currentNode.key, currentNode.Value); currentNode = currentNode.Next; } } } }
static void Main(string[] args) { Console.Write("Enter the size of the hashtable: "); int hashTableSize = Convert.ToInt32(Console.ReadLine()); float expectedResults = -1; string fileName = ""; do { Console.Write("Enter name of input file: "); fileName = Console.ReadLine(); if (!File.Exists(fileName)) { Console.WriteLine("File not found"); } }while (!File.Exists(fileName)); string[] input = File.ReadAllLines(fileName); HashNode[] hashtable = new HashNode[hashTableSize]; #region 40% Linear OldHash Console.WriteLine("\nFilling to 40% capacity with old hash algorithm linear probing"); int entries = 0; for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Linear OldHash Console.WriteLine("\nFilling to 87% capacity with old hash algorithm linear probing"); entries = 0; for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region Random Collision handling OldHash #region 40% Random Console.WriteLine("\nFilling to 40% capacity with old hash algorithm random probing"); for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Random Console.WriteLine("\nFilling to 87% capacity with old hash algorithm random probing"); // 87% entries = 0; for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #endregion #region New Hash Algo Console.WriteLine("\nNew Hashing Function"); #region 40% Linear Console.WriteLine("\nFilling to 40% capacity with new hash algorithm linear probing"); for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Linear Console.WriteLine("\nFilling to 87% capacity with new hash algorithm linear probing"); // 87% Linear entries = 0; for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 40% Random Console.WriteLine("\nFilling to 40% capacity with new hash algorithm random probing"); for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Random Console.WriteLine("\nFilling to 87% capacity with new hash algorithm random probing"); entries = 0; for (int i = 0; i < hashTableSize; i++) { hashtable[i] = new HashNode(); } while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashtable, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashtable); PrintProbeResults(hashtable, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #endregion /*========================================= * Relative File * =========================================*/ Console.WriteLine("\n\nStarting Relative File Processing"); Console.Write("Enter the new size of the hashtable: "); hashTableSize = Convert.ToInt32(Console.ReadLine()); string hashTableFileName = "hashtable40LO.txt"; StreamWriter sw = new StreamWriter(hashTableFileName, false); #region 40% Linear OldHash Console.WriteLine("\nFilling to 40% capacity with old hash algorithm linear probing"); entries = 0; for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Linear OldHash Console.WriteLine("\nFilling to 87% capacity with old hash algorithm linear probing"); hashTableFileName = "hashtable87LO.txt"; sw = new StreamWriter(hashTableFileName, false); entries = 0; for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region Random Collision handling OldHash #region 40% Random Console.WriteLine("\nFilling to 40% capacity with old hash algorithm random probing"); hashTableFileName = "hashtable40RO.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Random Console.WriteLine("\nFilling to 87% capacity with old hash algorithm random probing"); // 87% entries = 0; hashTableFileName = "hashtable87RO.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHashOld(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #endregion #region New Hash Algo Console.WriteLine("\nNew Hashing Function"); #region 40% Linear Console.WriteLine("\nFilling to 40% capacity with new hash algorithm linear probing"); hashTableFileName = "hashtable40LN.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Linear Console.WriteLine("\nFilling to 87% capacity with new hash algorithm linear probing"); // 87% Linear entries = 0; hashTableFileName = "hashtable87LN.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Linear); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Linear); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Linear); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 40% Random Console.WriteLine("\nFilling to 40% capacity with new hash algorithm random probing"); hashTableFileName = "hashtable40RN.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); entries = 0; while (entries < hashTableSize * 0.4f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #region 87% Random Console.WriteLine("\nFilling to 87% capacity with new hash algorithm random probing"); entries = 0; hashTableFileName = "hashtable87RN.txt"; sw = new StreamWriter(hashTableFileName, false); for (int i = 0; i < hashTableSize; i++) { sw.Write("{0,-50}", i + ",,,"); } sw.Flush(); sw.Close(); while (entries < hashTableSize * 0.87f && entries < input.Length) { int ha = GetHash(input[entries], hashTableSize); InsertIntoHashTable(hashTableFileName, hashTableSize, input[entries], ha, ProbeMode.Random); entries++; } PrintHashTable(hashTableFileName, hashTableSize); PrintProbeResults(hashTableFileName, hashTableSize, input, 30, entries, ProbeMode.Random); expectedResults = CalculateExpectedProbes(entries, hashTableSize, ProbeMode.Random); Console.WriteLine("Theoretical probe count: " + expectedResults); #endregion #endregion Console.Write("Press any key to continue..."); Console.Read(); }