forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainedHashTable.cs
654 lines (546 loc) · 21.3 KB
/
ChainedHashTable.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/***
* Chained Hash Table.
*
* A hash table that implements the Separate-Chaining scheme for resolving keys-collisions. It also implements auto-resizing (expansion and contraction).
*/
using System;
using System.Collections.Generic;
using DataStructures.Common;
using DataStructures.Lists;
namespace DataStructures.Dictionaries
{
/// <summary>
/// Hash Table with Chaining.
/// </summary>
public class ChainedHashTable<TKey, TValue> : IDictionary<TKey, TValue> where TKey : IComparable<TKey>
{
/// <summary>
/// Used in the ensure capacity function
/// </summary>
public enum CapacityManagementMode
{
Contract = 0,
Expand = 1
}
/// <summary>
/// INSTANCE VARIABLES.
/// </summary>
private int _size;
private int _freeSlotsCount;
private decimal _slotsLoadFactor;
private const int _defaultCapacity = 8;
private DLinkedList<TKey, TValue>[] _hashTableStore;
private static readonly DLinkedList<TKey, TValue>[] _emptyArray = new DLinkedList<TKey, TValue>[_defaultCapacity];
private List<TKey> _keysCollection { get; set; }
private List<TValue> _valuesCollection { get; set; }
// Keys and Values Comparers
private EqualityComparer<TKey> _keysComparer { get; set; }
private EqualityComparer<TValue> _valuesComparer { get; set; }
// The C# Maximum Array Length (before encountering overflow)
// Reference: http://referencesource.microsoft.com/#mscorlib/system/array.cs,2d2b551eabe74985
private const int MAX_ARRAY_LENGTH = 0X7FEFFFFF;
// Initial hash value.
private const uint INITIAL_HASH = 0x9e3779b9;
/// <summary>
/// CONSTRUCTOR
/// </summary>
public ChainedHashTable()
{
this._size = 0;
this._hashTableStore = _emptyArray;
this._freeSlotsCount = this._hashTableStore.Length;
this._keysComparer = EqualityComparer<TKey>.Default;
this._valuesComparer = EqualityComparer<TValue>.Default;
this._keysCollection = new List<TKey>();
this._valuesCollection = new List<TValue>();
}
/// <summary>
/// Rehash the the current collection elements to a new collection.
/// </summary>
private void _rehash(ref DLinkedList<TKey, TValue>[] newHashTableStore, int oldHashTableSize)
{
// Reset the free slots count
this._freeSlotsCount = newHashTableStore.Length;
for (int i = 0; i < oldHashTableSize; ++i)
{
var chain = _hashTableStore[i];
if (chain != null && chain.Count > 0)
{
var head = chain.Head;
while (head != null)
{
uint hash = _getHashOfKey(head.Key, newHashTableStore.Length);
if (newHashTableStore[hash] == null)
{
_freeSlotsCount--;
newHashTableStore[hash] = new DLinkedList<TKey, TValue>();
}
newHashTableStore[hash].Append(head.Key, head.Value);
head = head.Next;
}
}
}//end-for
}
/// <summary>
/// Contracts the capacity of the keys and values arrays.
/// </summary>
private void _contractCapacity()
{
int oneThird = (_hashTableStore.Length / 3);
int twoThirds = 2 * oneThird;
if (_size <= oneThird)
{
int newCapacity = (_hashTableStore.Length == 0 ? _defaultCapacity : twoThirds);
// Try to expand the size
DLinkedList<TKey, TValue>[] newHashTableStore = new DLinkedList<TKey, TValue>[newCapacity];
// Rehash
if (_size > 0)
{
_rehash(ref newHashTableStore, _hashTableStore.Length);
}//end-if
_hashTableStore = newHashTableStore;
}
}
/// <summary>
/// Expands the capacity of the keys and values arrays.
/// </summary>
private void _expandCapacity(int minCapacity)
{
if (_hashTableStore.Length < minCapacity)
{
int newCapacity = (_hashTableStore.Length == 0 ? _defaultCapacity : _hashTableStore.Length * 2);
// Make sure it doesn't divide by 2 or 10
if (newCapacity % 2 == 0 || newCapacity % 10 == 0)
newCapacity = newCapacity + 1;
// Handle overflow
if (newCapacity >= MAX_ARRAY_LENGTH)
newCapacity = MAX_ARRAY_LENGTH;
else if (newCapacity < minCapacity)
newCapacity = minCapacity;
// Try to expand the size
try
{
DLinkedList<TKey, TValue>[] newHashTableStore = new DLinkedList<TKey, TValue>[newCapacity];
// Rehash
if (_size > 0)
{
_rehash(ref newHashTableStore, _hashTableStore.Length);
}//end-if
_hashTableStore = newHashTableStore;
}
catch (OutOfMemoryException)
{
throw;
}
}
}
/// <summary>
/// A high-level functon that handles both contraction and expansion of the internal collection.
/// </summary>
/// <param name="mode">Contract or Expand.</param>
/// <param name="newSize">The new expansion size.</param>
private void _ensureCapacity(CapacityManagementMode mode, int newSize = -1)
{
// If the size of the internal collection is less than or equal to third of
// ... the total capacity then contract the internal collection
int oneThird = (_hashTableStore.Length / 3);
if (mode == CapacityManagementMode.Contract && _size <= oneThird)
{
_contractCapacity();
}
else if (mode == CapacityManagementMode.Expand && newSize > 0)
{
_expandCapacity(newSize);
}
}
/// <summary>
/// Hash Function.
/// The universal hashing principle method.
/// </summary>
private uint _universalHashFunction(TKey key, int length)
{
if (length < 0)
throw new IndexOutOfRangeException();
// Hashes
uint prehash = 0, hash = INITIAL_HASH;
// Primes
int a = 197, b = 4049, p = 7199369;
prehash = _getPreHashOfKey(key);
hash = Convert.ToUInt32(((a * prehash + b) % p) % length);
return hash;
}
/// <summary>
/// Hash Function.
/// The division method hashing.
/// </summary>
private uint _divisionMethodHashFunction(TKey key, int length)
{
uint prehash = 0, hash = INITIAL_HASH;
if (length < 0)
throw new IndexOutOfRangeException();
if (key is string && key.IsEqualTo(default(TKey)) == false)
{
var stringKey = Convert.ToString(key);
for (int i = 0; i < stringKey.Length; ++i)
{
hash = (hash ^ stringKey[i]) + ((hash << 26) + (hash >> 6));
}
if (hash > length)
hash = Convert.ToUInt32(hash % length);
}
else
{
prehash = _getPreHashOfKey(key);
hash = Convert.ToUInt32((37 * prehash) % length);
}
return hash;
}
/// <summary>
/// Returns an integer that represents the key.
/// Used in the _hashKey function.
/// </summary>
private uint _getPreHashOfKey(TKey key)
{
return Convert.ToUInt32(Math.Abs(_keysComparer.GetHashCode(key)));
}
/// <summary>
/// Returns a key from 0 to m where m is the size of the keys-and-values map. The hash serves as an index.
/// </summary>
private uint _getHashOfKey(TKey key, int length)
{
return _universalHashFunction(key, length);
}
/// <summary>
/// Returns a key from 0 to m where m is the size of the keys-and-values map. The hash serves as an index.
/// Division Method.
/// </summary>
private uint _getHashOfKey(TKey key)
{
return _universalHashFunction(key, _hashTableStore.Length);
}
/// <summary>
/// Return count of elements in the hash table.
/// </summary>
public int Count
{
get { return _size; }
}
/// <summary>
/// Checks if the hash table is readonly.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Checks if the hash table is empty.
/// </summary>
public bool IsEmpty
{
get { return Count == 0; }
}
/// <summary>
/// Checks whether key exists in the hash table.
/// </summary>
public bool ContainsKey(TKey key)
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
return _hashTableStore[hashcode].ContainsKey(key);
}
// else
return false;
}
/// <summary>
/// Checks whether a key-value pair exist in the hash table.
/// </summary>
public bool Contains(KeyValuePair<TKey, TValue> item)
{
// Get hash of the key
var hashcode = _getHashOfKey(item.Key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
try
{
var existingPair = _hashTableStore[hashcode].Find(item.Key);
if (existingPair.Key.IsEqualTo(item.Key) && _valuesComparer.Equals(existingPair.Value, item.Value))
return true;
}
catch (KeyNotFoundException)
{
// do nothing
}
}
// else
return false;
}
/// <summary>
/// List of hash table keys.
/// </summary>
public ICollection<TKey> Keys
{
get { return _keysCollection; }
}
/// <summary>
/// List of hash table values.
/// </summary>
public ICollection<TValue> Values
{
get { return _valuesCollection; }
}
/// <summary>
/// Tries to get the value of key which might not be in the dictionary.
/// </summary>
public bool TryGetValue(TKey key, out TValue value)
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
try
{
var existingPair = _hashTableStore[hashcode].Find(key);
value = existingPair.Value;
return true;
}
catch (KeyNotFoundException)
{
// do nothing
}
}
// NOT FOUND
value = default(TValue);
return false;
}
/// <summary>
/// Gets or sets the value of a key.
/// </summary>
public TValue this[TKey key]
{
get
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
try
{
var existingPair = _hashTableStore[hashcode].Find(key);
return existingPair.Value;
}
catch (KeyNotFoundException)
{
// do nothing
}
}
// NOT FOUND
throw new KeyNotFoundException();
}
set
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
bool exists = _hashTableStore[hashcode].ContainsKey(key);
if (exists == true)
_hashTableStore[hashcode].UpdateValueByKey(key, value);
}
// NOT FOUND
throw new KeyNotFoundException();
}
}
/// <summary>
/// Add a key and value to the hash table.
/// </summary>
public void Add(TKey key, TValue value)
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] == null)
{
// This is an empty slot. Initialize the chain of collisions.
_hashTableStore[hashcode] = new DLinkedList<TKey, TValue>();
// Decrease the number of free slots.
_freeSlotsCount--;
}
else if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
if (_hashTableStore[hashcode].ContainsKey(key) == true)
throw new ArgumentException("Key already exists in the hash table.");
}
_hashTableStore[hashcode].Append(key, value);
_size++;
//Add the key-value to the keys and values collections
_keysCollection.Add(key);
_valuesCollection.Add(value);
_slotsLoadFactor = Decimal.Divide(
Convert.ToDecimal(_size),
Convert.ToDecimal(_hashTableStore.Length));
// Capacity management
if (_slotsLoadFactor.IsGreaterThanOrEqualTo(Convert.ToDecimal(0.90)))
{
_ensureCapacity(CapacityManagementMode.Expand, _hashTableStore.Length + 1);
}
}
/// <summary>
/// Add a key-value pair to the hash table.
/// </summary>
public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
/// <summary>
/// Remove a key from the hash table and return the status.
/// </summary>
public bool Remove(TKey key)
{
// Get hash of the key
var hashcode = _getHashOfKey(key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
try
{
var keyValuePair = _hashTableStore[hashcode].Find(key);
if (keyValuePair.Key.IsEqualTo(key))
{
_hashTableStore[hashcode].RemoveBy(key);
_size--;
// check if no other keys exist in this slot.
if (_hashTableStore[hashcode].Count == 0)
{
// Nullify the chain of collisions at this slot.
_hashTableStore[hashcode] = null;
// Increase the number of free slots.
_freeSlotsCount++;
// Capacity management
_ensureCapacity(CapacityManagementMode.Contract);
}
_keysCollection.Remove(key);
_valuesCollection.Remove(keyValuePair.Value);
return true;
}
}
catch
{
// do nothing
}
}
// else
return false;
}
/// <summary>
/// Remove a key-value pair from the hash table and return the status.
/// </summary>
public bool Remove(KeyValuePair<TKey, TValue> item)
{
// Get hash of the key
var hashcode = _getHashOfKey(item.Key);
// The chain of colliding keys are found at _keysValuesMap[hashcode] as a doubly-linked-list.
if (_hashTableStore[hashcode] != null && _hashTableStore[hashcode].Count > 0)
{
try
{
var keyValuePair = _hashTableStore[hashcode].Find(item.Key);
if (keyValuePair.Key.IsEqualTo(item.Key) && _valuesComparer.Equals(keyValuePair.Value, item.Value))
{
_hashTableStore[hashcode].RemoveBy(item.Key);
_size--;
// check if no other keys exist in this slot.
if (_hashTableStore[hashcode].Count == 0)
{
// Nullify the chain of collisions at this slot.
_hashTableStore[hashcode] = null;
// Increase the number of free slots.
_freeSlotsCount++;
// Capacity management
_ensureCapacity(CapacityManagementMode.Contract);
}
_keysCollection.Remove(keyValuePair.Key);
_valuesCollection.Remove(keyValuePair.Value);
return true;
}
}
catch
{
// do nothing
}
}
// else
return false;
}
/// <summary>
/// Copy the key-value pairs in the hash table to an array starting from the specified index.
/// </summary>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
array = new KeyValuePair<TKey, TValue>[_size];
int i = arrayIndex;
int hashTableIndex = 0;
int countOfElements = (array.Length - arrayIndex);
while (true)
{
KeyValuePair<TKey, TValue> pair;
if (i >= array.Length)
break;
if (_hashTableStore[hashTableIndex] != null && _hashTableStore[hashTableIndex].Count > 0)
{
if (_hashTableStore[hashTableIndex].Count == 1)
{
pair = new KeyValuePair<TKey, TValue>(_hashTableStore[hashTableIndex].First.Key, _hashTableStore[hashTableIndex].First.Value);
array[i] = pair;
i++;
hashTableIndex++;
}
else
{
var headOfChain = _hashTableStore[hashTableIndex].Head;
while (i < array.Length)
{
pair = new KeyValuePair<TKey, TValue>(headOfChain.Key, headOfChain.Value);
array[i] = pair;
i++;
hashTableIndex++;
headOfChain = headOfChain.Next;
}
}//end-if-else
}//end-if
else
{
hashTableIndex++;
}
}
}
/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
// Clear the elements in the store
Array.Clear(_hashTableStore, 0, _hashTableStore.Length);
// Re-initialize to empty collection.
_hashTableStore = _emptyArray;
_size = 0;
_slotsLoadFactor = 0;
_freeSlotsCount = _hashTableStore.Length;
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}