forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAddressingHashTable.cs
395 lines (331 loc) · 11.7 KB
/
OpenAddressingHashTable.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructures.Dictionaries
{
/// <summary>
/// Open Addressing Data Structure
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class OpenAddressingHashTable<TKey, TValue> : IDictionary<TKey, TValue> where TKey : IComparable<TKey>
{
/// <summary>
/// Open Addressing Entry
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
private class OAHashEntry<TKey, TValue> where TKey : IComparable<TKey>
{
public TKey key { get; set; }
public TValue value { get; set; }
public bool occupied { get; set; }
public OAHashEntry(TKey Key, TValue Value, bool occp)
{
key = Key;
value = Value;
occupied = occp;
}
}
private int _size { get; set; }
private double _loadFactor { get; set; }
private int _inTable { get; set; }
private OAHashEntry<TKey, TValue>[] _table { get; set; }
private List<TKey> _keys { get; set; }
private List<TValue> _values { get; set; }
/// <summary>
/// CONSTRUCTOR
/// </summary>
///
public OpenAddressingHashTable(int size)
{
_size = size;
_loadFactor = 0.40;
_inTable = 0;
_table = new OAHashEntry<TKey, TValue>[_size];
_keys = new List<TKey>();
_values = new List<TValue>();
//initialize all values to -1
for (int i = 0; i < _table.Length; i++)
{
//initialize each slot
_table[i] = new OAHashEntry<TKey, TValue>(default(TKey), default(TValue), false);
}
}
//doubles the size of the table
private void _expand()
{
//will hold contents of _table to copy over
OAHashEntry<TKey, TValue>[] temp = new OAHashEntry<TKey, TValue>[_size];
temp = _table;
//double the size and rehash
_size *= 2;
OAHashEntry<TKey, TValue>[] exp = new OAHashEntry<TKey, TValue>[_size];
for (int i = 0; i < exp.Length; i++)
{
//initialize each slot
exp[i] = new OAHashEntry<TKey, TValue>(default(TKey), default(TValue), false);
}
_inTable = 0;
_table = exp;
//rehash over the newly sized table
for (int i = 0; i < temp.Length; i++)
{
//this should rehash since the size is now doubled so the hashing will be different
//inserts the key into _table
Add(temp[i].key, temp[i].value);
}
}
//rehashes table
private void _rehash()
{
//will hold contents of _table to copy over
OAHashEntry<TKey, TValue>[] temp = new OAHashEntry<TKey, TValue>[_size];
temp = _table;
OAHashEntry<TKey, TValue>[] rehash = new OAHashEntry<TKey, TValue>[_size];
for (int i = 0; i < rehash.Length; i++)
{
//initialize each slot
rehash[i] = new OAHashEntry<TKey, TValue>(default(TKey), default(TValue), false);
}
_inTable = 0;
_table = rehash;
//rehash over the newly sized table
for (int i = 0; i < temp.Length; i++)
{
//this should rehash since the size is now doubled so the hashing will be different
//inserts the key into _table
Add(temp[i].key, temp[i].value);
}
}
private int _double_hash(TKey key, int i)
{
int hash_value = 0;
int second_hash_value = 0;
int slot = 0;
//grabs hash value for a string
if (typeof(TKey) == typeof(string))
{
//https://stackoverflow.com/questions/4092393/value-of-type-t-cannot-be-converted-to
TKey newTkeyString = (TKey)(object)key;
string newTkeyString2 = (string)(object)newTkeyString;
//https://stackoverflow.com/questions/400733/how-to-get-ascii-value-of-string-in-c-sharp
byte[] asciiBytes = Encoding.ASCII.GetBytes(newTkeyString2);
int string_value = 0;
foreach (byte bite in asciiBytes)
{
string_value += (int)bite;
}
//calculates first hash values
hash_value = Convert.ToInt32(string_value) % _size;
//calculate second hash value
second_hash_value = 1 + (Convert.ToInt32(string_value) % (_size - 1));
}
//grabs a hash value for a char
else if (typeof(TKey) == typeof(char))
{
//https://stackoverflow.com/questions/4092393/value-of-type-t-cannot-be-converted-to
TKey newTkeyChar = (TKey)(object)key;
char newTkeyChar2 = (char)(object)newTkeyChar;
int char_value = (int)newTkeyChar2;
//calculates first hash values
hash_value = Convert.ToInt32(char_value) % _size;
//calculate second hash value
second_hash_value = 1 + (Convert.ToInt32(char_value) % (_size - 1));
}
else
{
//calculates first hash values
hash_value = Convert.ToInt32(key) % _size;
//calculate second hash value
second_hash_value = 1 + (Convert.ToInt32(key) % (_size - 1));
}
//slot index based on first hash value, second hash value as an offset based on a counter, will also guarentee that the slot will be within the range 0 to size
slot = (hash_value + (i * second_hash_value)) % _size;
return slot;
}
public void Add(TKey key, TValue value)
{
int i = 0;
//makes sure there are no duplicate keys
if (ContainsKey(key))
{
return;
}
do
{
//calculate index
int index = _double_hash(key, i);
if (_table[index].occupied == false)
{
var newEntry = new OAHashEntry<TKey, TValue>(key, value, true);
_keys.Add(key);
_values.Add(value);
//set value and key
_table[index] = newEntry;
//increment how many items are in the table
_inTable++;
break;
}
else
{
i++;
}
} while (i != _size);
//every slot is in the table is occupied
if (_inTable == _size)
{
//expand and rehash
_expand();
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
//returns value
public TValue this[TKey key]
{
get{
int index = search(key);
if (index != -1)
{
return _table[index].value;
}
throw new KeyNotFoundException();
}
set {
if (ContainsKey(key) == true)
{
int index = search(key);
_table[index].value = value;
throw new KeyNotFoundException();
}
throw new KeyNotFoundException();
}
}
//removes key-value pair from the table
public bool Remove(TKey key)
{
if (ContainsKey(key))
{
//find position and reset values
int index = search(key);
_keys.Clear();
_values.Clear();
_table[index].key = default(TKey);
_table[index].value = default(TValue);
_table[index].occupied = false;
//number of items in the table decreases
_inTable--;
//rehash table --necessary for Open Addressing since keys could have different positions due to this key
_rehash();
return true;
}
else
{
return false;
}
}
//removes key-value pair from the table
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return Remove(item.Key);
}
//clears table of all values
public void Clear()
{
_keys.Clear();
_values.Clear();
for (int i = 0; i < _table.Length; i++)
{
_table[i].key = default(TKey);
_table[i].value = default(TValue);
_table[i].occupied = false;
}
_inTable = 0;
}
//Tries to get the value of key which might not be in the dictionary.
public bool TryGetValue(TKey key, out TValue value)
{
int index = search(key);
if (index != -1)
{
value = _table[index].value;
return true;
}
//not found
value = default(TValue);
return false;
}
//finds the key and returns index in the table
public int search(TKey key)
{
int i = 0;
do
{
//calculate index
int index = _double_hash(key, i);
if (IComparable.Equals(_table[index].key, key))
{
return index;
}
i++;
} while (i < _size);
return -1;
}
//returns true if the key is in the table
public bool ContainsKey(TKey key)
{
if (search(key) != -1)
{
return true;
}
return false;
}
//returns true if the key is in the table
public bool Contains(KeyValuePair<TKey, TValue> item)
{
if (ContainsKey(item.Key))
{
return true;
}
return false;
}
//returns the number of items in the table
public int Count
{
get { return _inTable;}
}
//returns bool depending on whether or not the table is read only
public bool IsReadOnly
{
get { return false; }
}
//returns a list of keys in the table
public ICollection<TKey> Keys
{
get { return _keys; }
}
//returns a list of values in the table
public ICollection<TValue> Values
{
get { return _values; }
}
//----------------------------------------------------------------------
//-----------------------NOT IMPLEMENTED YET----------------------------
//----------------------------------------------------------------------
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}