Facebook
From Chartreuse Marmoset, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 42
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HashImpplementation
  8. {
  9.     public class HashEntry<K,V>
  10.     {
  11.         public HashEntry(K key,V value)
  12.         {
  13.             this.Key = key;
  14.             this.Value = value;
  15.         }
  16.  
  17.         public K Key { get; set; }
  18.         public V Value { get; set; }
  19.     }
  20. }
  21. ///////////////////////////////////////////
  22.  
  23.  
  24. using HashImpplementation;
  25. using System;
  26. using System.Collections;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31.  
  32. namespace Dictionary
  33. {
  34.     public class HashTable<K, V>
  35.     {
  36.  
  37.         private  int maxSize = 64;
  38.         private List<HashEntry<K, V>> table;
  39.         private int Hash(K key)
  40.         {
  41.             int index = 7;
  42.             int asciiVal = 0;
  43.             for (int i = 0; i <key.ToString().Length ; i++)
  44.             {
  45.                 asciiVal = (int)key.ToString()[i] * i;
  46.                 index = index * 31 + asciiVal;
  47.             }
  48.             return index % maxSize;
  49.  
  50.         }
  51.         public HashTable(int maxSize)
  52.         {
  53.             this.maxSize = maxSize;
  54.             table = new List<HashEntry<K, V>>();
  55.         }
  56.  
  57.  
  58.         public void Insert(HashEntry<K,V> items)
  59.         {
  60.             int hashVal = Hash(items.Key);
  61.             table.Add(items);
  62.             //table[hashVal].Add(new  HashEntry<K, V>(key,value));
  63.            
  64.  
  65.         }
  66.         public V Get(K key)
  67.         {
  68.             int hashVal = Hash(key);
  69.             // linq
  70.           HashEntry<K,V> result =   table.FirstOrDefault(x => x.Key.Equals(key));
  71.             return result.Value;
  72.             /// List<HashEntry<K, V>> allEntryes = table[hashVal];
  73.  
  74.  
  75.           /*  foreach (HashEntry<K,V> entry in allEntryes)
  76.             {
  77.                 if (entry.Key.Equals(key))
  78.                 {
  79.                     return entry.Value;
  80.                 }
  81.             }
  82.             return default(V);*/
  83.         }
  84.      
  85.     }
  86.  
  87. }
  88.  
  89.  
  90. ///////////////////////////////////////
  91. using HashImpplementation;
  92. using System;
  93.  
  94.  
  95. namespace Dictionary
  96. {
  97.     class Program
  98.     {
  99.         static void Main(string[] args)
  100.         {
  101.               HashTable<string, int> notes = new HashTable<string, int>(5);
  102.             HashEntry<string, int> items = new HashEntry<string, int>("Boby", 25);
  103.             HashEntry<string, int> items2 = new HashEntry<string, int>("Even", 27);
  104.             HashEntry<string, int> items3 = new HashEntry<string, int>("Billy", 29);
  105.             HashEntry<string, int> items5 = new HashEntry<string, int>("Jack", 12);
  106.             HashEntry<string, int> items6 = new HashEntry<string, int>("Alex", 34);
  107.  
  108.             notes.Insert(items);
  109.             notes.Insert(items2);
  110.             notes.Insert(items3);
  111.             notes.Insert(items5);
  112.             notes.Insert(items6);
  113.             Console.WriteLine(notes.Get("Even"));
  114.  
  115.    
  116.  
  117.  
  118.            
  119.         }
  120.     }
  121. }
  122.