Skip to content
This repository
Browse code

First version of CustomHasTable (trivial implementation)

  • Loading branch information...
commit a9b2ff6002a904172701c9fc4725862b711aea86 1 parent 6e6fd57
Gergo Bogdan authored May 28, 2012
2  CustomHashTable/CustomHashTable.csproj
@@ -43,6 +43,8 @@
43 43
     <Reference Include="System.Xml" />
44 44
   </ItemGroup>
45 45
   <ItemGroup>
  46
+    <Compile Include="HashTable.cs" />
  47
+    <Compile Include="Person.cs" />
46 48
     <Compile Include="Program.cs" />
47 49
     <Compile Include="Properties\AssemblyInfo.cs" />
48 50
   </ItemGroup>
119  CustomHashTable/HashTable.cs
... ...
@@ -0,0 +1,119 @@
  1
+using System;
  2
+using System.Linq;
  3
+using System.Text;
  4
+
  5
+namespace CustomHashTable
  6
+{
  7
+    class HashTable<T>
  8
+        where T : class
  9
+    {
  10
+
  11
+        public static HashTable<T> Build<T>( System.Collections.Generic.IList<string> keys,  System.Collections.Generic.IList<T> values)
  12
+            where T : class
  13
+        {
  14
+            HashTable<T> ht = new HashTable<T>();
  15
+
  16
+            ht.Rebuild(keys, values);
  17
+
  18
+            return ht;
  19
+        }
  20
+
  21
+        private string[] internalKeys;
  22
+        private int[] indexForValues;
  23
+        private T[] values;
  24
+
  25
+        public int BiggestHashCode { get; set; }
  26
+
  27
+        private HashTable()
  28
+        {
  29
+
  30
+        }
  31
+
  32
+        public void Rebuild(System.Collections.Generic.IList<string> keys, System.Collections.Generic.IList<T> values)
  33
+        {
  34
+            //get all hasCode for keys
  35
+            int[] allHashCodes = new int[keys.Count()];
  36
+
  37
+            this.internalKeys = new string[keys.Count];
  38
+
  39
+            int index = 0;
  40
+            foreach (var item in keys)
  41
+            {
  42
+                allHashCodes[index] = GetHashForKey(item);
  43
+                internalKeys[index] = item;
  44
+                ++index;
  45
+                //Console.WriteLine("Key = [ {0} ] HASH=[ {1} ]", item, allHashCodes[index - 1]); 
  46
+            }
  47
+
  48
+            //this specifies the size of array : indexForValues - THIS NUMBER CAN BE A HUGE ONE
  49
+            this.BiggestHashCode = allHashCodes.OrderByDescending(p => p).FirstOrDefault();
  50
+
  51
+            this.values = new T[this.BiggestHashCode + 1];
  52
+            this.indexForValues = new int[values.Count];
  53
+
  54
+            //inser the elements in the indexForValue and also insert he values in Values array
  55
+            index = 0;
  56
+            for (int cursor = 0; cursor < allHashCodes.Length; ++cursor)
  57
+            {
  58
+                this.indexForValues[index] = allHashCodes[index];
  59
+                this.values[this.indexForValues[index]] = values[index];
  60
+                ++index;
  61
+            }
  62
+        }
  63
+
  64
+
  65
+        public T this[string key]
  66
+        {
  67
+            get 
  68
+            {
  69
+                T element = default(T);
  70
+                int keyIndex = GetKeyIndex(key);
  71
+
  72
+                if (keyIndex != -1)
  73
+                {
  74
+                    if (indexForValues.Length > keyIndex)
  75
+                    {
  76
+                        if (values.Length > indexForValues[keyIndex])
  77
+                        {
  78
+                            element = values[indexForValues[keyIndex]];
  79
+                        }
  80
+                    }
  81
+                }
  82
+
  83
+                return element;
  84
+            }
  85
+        }
  86
+
  87
+        private int GetKeyIndex(string keyValue)
  88
+        {
  89
+            int index = -1;
  90
+
  91
+            for (int cursor = 0; cursor < internalKeys.Length; ++cursor)
  92
+            {
  93
+                if (keyValue == internalKeys[cursor])
  94
+                {
  95
+                    index = cursor;
  96
+                    break;
  97
+                }
  98
+            }
  99
+
  100
+            return index;
  101
+        }
  102
+
  103
+        private int GetHashForKey(string key)
  104
+        {
  105
+            int hashValue = 0;
  106
+
  107
+            if (!string.IsNullOrWhiteSpace(key))
  108
+            {
  109
+                for (int cursor = 0; cursor < key.Length; ++cursor)
  110
+                {
  111
+                    hashValue += ((int)key[cursor]);
  112
+                }
  113
+            }
  114
+
  115
+            return hashValue;
  116
+        }
  117
+             
  118
+    }
  119
+}
23  CustomHashTable/Person.cs
... ...
@@ -0,0 +1,23 @@
  1
+using System;
  2
+using System.Collections.Generic;
  3
+using System.Linq;
  4
+using System.Text;
  5
+
  6
+namespace CustomHashTable
  7
+{
  8
+    class Person
  9
+    {
  10
+        public Person()
  11
+        {
  12
+
  13
+        }
  14
+
  15
+        public string Name { get; set; }
  16
+        public string PhoneNumber { get; set; }
  17
+
  18
+        public override string ToString()
  19
+        {
  20
+            return string.Format("Name=[{0}], PhoneNumber=[{1}]", this.Name, this.PhoneNumber);
  21
+        }
  22
+    }
  23
+}
100  CustomHashTable/Program.cs
@@ -9,6 +9,106 @@ class Program
9 9
     {
10 10
         static void Main(string[] args)
11 11
         {
  12
+
  13
+            List<string> firstNames = new List<string>()
  14
+            {
  15
+                "Murdok",
  16
+                "John",
  17
+                "Bob",
  18
+                "Steve",
  19
+                "Bill",
  20
+                "Harold",
  21
+                "Mike",
  22
+                "Stiffler",
  23
+                "Jim",
  24
+                "Mike",
  25
+                "Mark",
  26
+                "Jennifer",
  27
+                "Michelle",
  28
+                "Cristine",
  29
+                "Olive",
  30
+                "Helene",
  31
+                "Geril",
  32
+                "Catherin",
  33
+                "Fred",
  34
+                "Lisa",
  35
+                "Manuela",
  36
+                "Clarck",
  37
+                "Denis",
  38
+                "Emmerson",
  39
+                "Michael",
  40
+                "Spike",
  41
+            };
  42
+
  43
+            List<string> lastNames = new List<string>()
  44
+            {
  45
+                "Big",
  46
+                "Small",
  47
+                "Elephant",
  48
+                "Ball",
  49
+                "Black",
  50
+                "Wild",
  51
+                "White",
  52
+                "Legend",
  53
+                "Rose",
  54
+                "Filipo",
  55
+                "Neard",
  56
+                "Schwarzenegger",
  57
+                "Sylvester",
  58
+                "Minesweeper",
  59
+                "Courage",
  60
+                "Nest",
  61
+                "Nice",
  62
+                "Baseball",
  63
+                "Red",
  64
+                "Tall",
  65
+                "Bear",
  66
+                "Lazy",
  67
+                "Fruit",
  68
+                "Nechlace",
  69
+                "Ellison",
  70
+            };
  71
+            Random rnd = new Random();
  72
+
  73
+            List<Person> samplePersons = new List<Person>();
  74
+            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
  75
+
  76
+            int nr = int.Parse(args[0]);
  77
+
  78
+            for (int i = 0; i < nr; i++)
  79
+            {
  80
+                int fnIndex = rnd.Next() % firstNames.Count;
  81
+                int lnIndex = rnd.Next() % lastNames.Count;
  82
+                Person p = new Person()
  83
+                {
  84
+                    Name = firstNames[fnIndex] + " " + lastNames[lnIndex],
  85
+                    PhoneNumber = (rnd.Next() % 999999999).ToString()
  86
+                };
  87
+                samplePersons.Add(p);
  88
+            }
  89
+
  90
+            watch.Start();
  91
+            var ht = HashTable<Person>.Build<Person>(samplePersons.Select(p => p.Name).ToList(), samplePersons);
  92
+            watch.Stop();
  93
+
  94
+            Console.WriteLine("HashTable build took : [ {0,10} ms ]", watch.ElapsedMilliseconds);
  95
+            Console.WriteLine("The Biggest HashCode found : [ {0} ]", ht.BiggestHashCode);
  96
+
  97
+            for (int i = 0; i < 25; i++)
  98
+            {
  99
+                int rndPerson = rnd.Next() % samplePersons.Count;
  100
+                
  101
+                watch.Reset();
  102
+                watch.Start();
  103
+                var personFound = ht[samplePersons[rndPerson].Name];
  104
+                watch.Stop();
  105
+
  106
+                Console.WriteLine("FOUND IN : [ {1,6} ms ], {0}",personFound.ToString(), watch.ElapsedMilliseconds);
  107
+
  108
+            }
  109
+            
  110
+            //Console.ReadLine();
  111
+
12 112
         }
13 113
     }
14 114
 }
BIN  wordCounter/wordCounter.suo
Binary file not shown

0 notes on commit a9b2ff6

Please sign in to comment.
Something went wrong with that request. Please try again.