-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputedChaining.cs
233 lines (215 loc) · 7 KB
/
ComputedChaining.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//table Row for Computed Chaining Table
class ComputedChainingTableRow
{
public int? Key { get; set; }
public int? nof { get; set; }
public ComputedChainingTableRow()
{
Key = null;
nof = null;
}
}
class ComputedChaining
{
private ComputedChainingTableRow[] HashTable { get; set; }
private int ModValue;
//fills all table rows as null
public ComputedChaining(int modValue)
{
ModValue = modValue;
HashTable = new ComputedChainingTableRow[modValue];
for (int i = 0; i < modValue; i++)
{
HashTable[i] = new ComputedChainingTableRow();
}
}
//hash function
private int Hash(int key)
{
return key % ModValue;
}
//quotient function, if quotient is 0, it returns 1 to prevent infinite loop
private int Quotient(int key)
{
int quotient = key / ModValue % ModValue == 0 ? 1 : key / ModValue % ModValue;
return quotient;
}
//finds space for key
private void FindSpace(int key)
{
int hashedIndex = Hash(key);
//find first key that nof column is null
int pointer = hashedIndex;
while (HashTable[pointer].nof != null)
{
int tempNof = HashTable[pointer].nof.Value;
int moveSize = tempNof * Quotient(HashTable[pointer].Key.Value);
pointer = (pointer + moveSize) % ModValue;
}
//find space for key then write nof of keys that found above
int count = 1;
int offset = Quotient(HashTable[pointer].Key.Value);
int iterator = pointer;
iterator = (iterator + offset) % ModValue;
while (iterator != pointer)
{
if (HashTable[iterator].Key == null)
{
HashTable[iterator].Key = key;
break;
}
else
{
count++;
iterator = (iterator + offset) % ModValue;
}
}
HashTable[pointer].nof = count;
}
//inserts key to table
public void Insert(int key)
{
int hashedIndex = Hash(key);
//if table row is null, insert key
if (HashTable[hashedIndex].Key == null)
{
HashTable[hashedIndex].Key = key;
return;
}
//if collision occurs
else
{
//if the key at the location of the collison is its own home adress
if (Hash(HashTable[hashedIndex].Key.Value) == hashedIndex)
{
FindSpace(key);
}
//if the key at the location of the collison is not its own home adress
else
{
//get chain of key that place in the collison occurs
List<int> keyChain = new List<int>();
List<int> indexChain = new List<int>();
int pointer = Hash(HashTable[hashedIndex].Key.Value);
int tempNof;
int moveSize;
while (HashTable[pointer].nof != null)
{
keyChain.Add(HashTable[pointer].Key.Value);
indexChain.Add(pointer);
tempNof = HashTable[pointer].nof.Value;
moveSize = tempNof * Quotient(HashTable[pointer].Key.Value);
pointer = (pointer + moveSize) % ModValue;
}
keyChain.Add(HashTable[pointer].Key.Value);
indexChain.Add(pointer);
//find the breakpoint of the chain
int chainBreakpoint = indexChain.IndexOf(hashedIndex);
//replace the key that came by parameter with the key that caused the collision
HashTable[hashedIndex] = new ComputedChainingTableRow() { Key = key, nof = null };
//delete the nof value of the key just before the breakpoint
HashTable[indexChain[chainBreakpoint - 1]].nof = null;
//delete all rows in the chain after the breakpoint
for (int i = chainBreakpoint + 1; i < indexChain.Count; i++)
{
HashTable[indexChain[i]] = new ComputedChainingTableRow();
}
//find new space for all keys in the chain just deleted
for (int i = chainBreakpoint; i < keyChain.Count; i++)
{
FindSpace(keyChain[i]);
}
}
}
}
//returns index of key
private int GetIndex(int key)
{
int hashedIndex = Hash(key);
if (HashTable[hashedIndex].Key == null)
{
return -1;
}
else if (HashTable[hashedIndex].Key == key)
{
return hashedIndex;
}
else
{
while (HashTable[hashedIndex].nof != null)
{
int tempNof = HashTable[hashedIndex].nof.Value;
int moveSize = tempNof * Quotient(HashTable[hashedIndex].Key.Value);
hashedIndex = (hashedIndex + moveSize) % ModValue;
if (HashTable[hashedIndex].Key == key)
{
return hashedIndex;
}
}
}
return -1;
}
//searches key in table
public void Search(int key)
{
int index = GetIndex(key);
if (index != -1)
{
Console.WriteLine("Key {0} is found on index {1}", key, index);
return;
}
Console.WriteLine("Key {0} is not found", key);
}
//returns number of probes for key
public int GetProb(int key)
{
int hashedIndex = Hash(key);
if (HashTable[hashedIndex].Key == null)
{
return 0;
}
else if (HashTable[hashedIndex].Key == key)
{
return 1;
}
else
{
int count = 1;
while (HashTable[hashedIndex].nof != null)
{
int tempNof = HashTable[hashedIndex].nof.Value;
int moveSize = tempNof * Quotient(HashTable[hashedIndex].Key.Value);
hashedIndex = (hashedIndex + moveSize) % ModValue;
count++;
if (HashTable[hashedIndex].Key == key)
{
return count;
}
}
}
return 0;
}
//returns average number of probes for all keys inserted
public double AverageProbes(List<int> insertNumbers)
{
double totalProbes = 0;
foreach (var item in insertNumbers)
{
totalProbes += GetProb(item);
}
return totalProbes / insertNumbers.Count;
}
//prints table
public void Print()
{
Console.WriteLine("--Computed Chaining--");
string row = string.Format("{0,0}{1,8}{2,8}", "Index", "Key", "nof");
Console.WriteLine(row);
for (int i = 0; i < HashTable.Length; i++)
{
row = string.Format("{0,3}{1,10}{2,7}", i, HashTable[i].Key, HashTable[i].nof);
Console.WriteLine(row);
}
}
}