Skip to content

Commit 496b546

Browse files
committed
Improved HashTable with buckets
1 parent 566f65c commit 496b546

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: src/3.HashTableWithBuckets/Program.cs

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ record HashNode(TKey Key, TValue Value)
4949
}
5050

5151
private HashNode?[] _buckets;
52+
private int _count;
5253

5354

5455
public HashTable(int capacity)
@@ -59,6 +60,11 @@ public void Add(TKey key, TValue value)
5960
var index = _computeHash(key);
6061
var node = new HashNode(key, value);
6162

63+
if(_count == _buckets.Length)
64+
{
65+
throw new InvalidOperationException("Hashtable is full");
66+
}
67+
6268
if(_buckets[index] is null)
6369
{
6470
_buckets[index] = node;
@@ -78,6 +84,8 @@ public void Add(TKey key, TValue value)
7884

7985
current.Next = node;
8086
}
87+
88+
_count++;
8189
}
8290

8391
public TValue Get(TKey key)
@@ -122,6 +130,7 @@ public void Remove(TKey key)
122130
{
123131
previous.Next = current.Next;
124132
}
133+
_count--;
125134
return;
126135
}
127136

0 commit comments

Comments
 (0)