File tree 1 file changed +9
-0
lines changed
src/3.HashTableWithBuckets
1 file changed +9
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ record HashNode(TKey Key, TValue Value)
49
49
}
50
50
51
51
private HashNode ? [ ] _buckets ;
52
+ private int _count ;
52
53
53
54
54
55
public HashTable ( int capacity )
@@ -59,6 +60,11 @@ public void Add(TKey key, TValue value)
59
60
var index = _computeHash ( key ) ;
60
61
var node = new HashNode ( key , value ) ;
61
62
63
+ if ( _count == _buckets . Length )
64
+ {
65
+ throw new InvalidOperationException ( "Hashtable is full" ) ;
66
+ }
67
+
62
68
if ( _buckets [ index ] is null )
63
69
{
64
70
_buckets [ index ] = node ;
@@ -78,6 +84,8 @@ public void Add(TKey key, TValue value)
78
84
79
85
current . Next = node ;
80
86
}
87
+
88
+ _count ++ ;
81
89
}
82
90
83
91
public TValue Get ( TKey key )
@@ -122,6 +130,7 @@ public void Remove(TKey key)
122
130
{
123
131
previous . Next = current . Next ;
124
132
}
133
+ _count -- ;
125
134
return ;
126
135
}
127
136
You can’t perform that action at this time.
0 commit comments