- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HashImpplementation
- {
- public class HashEntry<K,V>
- {
- public HashEntry(K key,V value)
- {
- this.Key = key;
- this.Value = value;
- }
- public K Key { get; set; }
- public V Value { get; set; }
- }
- }
- ///////////////////////////////////////////
- using HashImpplementation;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Dictionary
- {
- public class HashTable<K, V>
- {
- private int maxSize = 64;
- private List<HashEntry<K, V>> table;
- private int Hash(K key)
- {
- int index = 7;
- int asciiVal = 0;
- for (int i = 0; i <key.ToString().Length ; i++)
- {
- asciiVal = (int)key.ToString()[i] * i;
- index = index * 31 + asciiVal;
- }
- return index % maxSize;
- }
- public HashTable(int maxSize)
- {
- this.maxSize = maxSize;
- table = new List<HashEntry<K, V>>();
- }
- public void Insert(HashEntry<K,V> items)
- {
- int hashVal = Hash(items.Key);
- table.Add(items);
- //table[hashVal].Add(new HashEntry<K, V>(key,value));
- }
- public V Get(K key)
- {
- int hashVal = Hash(key);
- // linq
- HashEntry<K,V> result = table.FirstOrDefault(x => x.Key.Equals(key));
- return result.Value;
- /// List<HashEntry<K, V>> allEntryes = table[hashVal];
- /* foreach (HashEntry<K,V> entry in allEntryes)
- {
- if (entry.Key.Equals(key))
- {
- return entry.Value;
- }
- }
- return default(V);*/
- }
- }
- }
- ///////////////////////////////////////
- using HashImpplementation;
- using System;
- namespace Dictionary
- {
- class Program
- {
- static void Main(string[] args)
- {
- HashTable<string, int> notes = new HashTable<string, int>(5);
- HashEntry<string, int> items = new HashEntry<string, int>("Boby", 25);
- HashEntry<string, int> items2 = new HashEntry<string, int>("Even", 27);
- HashEntry<string, int> items3 = new HashEntry<string, int>("Billy", 29);
- HashEntry<string, int> items5 = new HashEntry<string, int>("Jack", 12);
- HashEntry<string, int> items6 = new HashEntry<string, int>("Alex", 34);
- notes.Insert(items);
- notes.Insert(items2);
- notes.Insert(items3);
- notes.Insert(items5);
- notes.Insert(items6);
- Console.WriteLine(notes.Get("Even"));
- }
- }
- }