-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
198 lines (159 loc) · 4.81 KB
/
Program.cs
File metadata and controls
198 lines (159 loc) · 4.81 KB
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
// This is a simple implementation of a hash table using open addressing.
// This implementation isn't complete and it's not efficient.
// It's just a simple example to show how a hash table works.
var hashtable = new HashTable<int, Profile>();
hashtable.Add(7, new("Sam", "sam@email.fk"));
hashtable.Add(3, new("Leo", "leo@email.fk"));
hashtable.Add(6, new("Jon", "jon@email.fk"));
//hashtable.Add(3, new("Lia", "lia@email.fk"));
hashtable.Add(4, new("Eva", "eva@email.fk"));
hashtable.Add(0, new("Bob", "bob@email.fk"));
hashtable.Add(1, new("Sue", "sue@email.fk"));
hashtable.Add(2, new("Ana", "ana@email.fk"));
hashtable.Add(5, new("Max", "max@email.fk"));
hashtable.Add(8, new("Kim", "kim@email.fk"));
for (int i = 0; i < 9; i++)
{
Console.WriteLine($"{i} -> {hashtable.Get(i)}");
}
for (int i = 0; i < 9; i++)
{
hashtable.Remove(i);
}
//var hashtable = new HashTable<string, Profile>();
//hashtable.Add("lo", new("Leo", "leo@email.fk"));
//hashtable.Add("la", new("Lia", "lia@email.fk"));
//hashtable.Add("ea", new("Eva", "eva@email.fk"));
//hashtable.Add("bb", new("Bob", "bob@email.fk"));
//hashtable.Add("se", new("Sue", "sue@email.fk"));
//hashtable.Add("aa", new("Ana", "ana@email.fk"));
//hashtable.Add("mx", new("Max", "max@email.fk"));
record Profile(string Name, string Email);
class HashTable<TKey, TValue>
{
record HashNode(TKey Key, TValue Value)
{
public HashNode? Next { get; set; }
}
private HashNode?[] _buckets;
private int _capacity;
private int _count;
public HashTable()
{
_capacity = 4;
_buckets = new HashNode[_capacity];
}
public void Add(TKey key, TValue value)
{
_resizeIfNeeded();
var index = _computeHash(key);
var node = new HashNode(key, value);
if(_buckets[index] is null)
{
_buckets[index] = node;
}
else
{
var current = _buckets[index];
if(current.Key.Equals(key))
{
throw new InvalidOperationException("Key already exists");
}
while(current.Next is not null)
{
current = current.Next;
if(current.Key.Equals(key))
{
throw new InvalidOperationException("Key already exists");
}
}
current.Next = node;
}
_count++;
}
public TValue Get(TKey key)
{
var index = _computeHash(key);
var current = _buckets[index];
while(current is not null)
{
if(current.Key.Equals(key))
{
return current.Value;
}
current = current.Next;
}
throw new KeyNotFoundException();
}
public void Remove(TKey key)
{
var index = _computeHash(key);
var current = _buckets[index];
if(current is null)
{
throw new KeyNotFoundException();
}
HashNode? previous = null;
while(current is not null)
{
if(current.Key.Equals(key))
{
if(previous is null)
{
_buckets[index] = current.Next;
}
else
{
previous.Next = current.Next;
}
_count--;
return;
}
previous = current;
current = current.Next;
}
}
private int _computeHash(TKey key)
{
var hash = key.GetHashCode();
hash &= 0x7FFFFFFF; // Remove sinal -> 0x7FFFFFFF = 1111111111111111111111111111111 = int.MaxValue
hash %= _buckets.Length;
return hash;
}
// Also known as rehashing
// Complexity: Time: O(n), Space: O(2n) -> O(n)
private void _resizeIfNeeded()
{
const float LOAD_FACTOR = 0.75f;
if((float)_count / _capacity < LOAD_FACTOR)
{
return;
}
_capacity *= 2; // Double the size
var newBuckets = new HashNode[_capacity];
foreach(var node in _buckets)
{
var current = node;
while(current is not null)
{
var index = _computeHash(current.Key);
var newNode = new HashNode(current.Key, current.Value);
if(newBuckets[index] is null)
{
newBuckets[index] = newNode;
}
else
{
var pivot = newBuckets[index];
while(pivot.Next is not null)
{
pivot = pivot.Next;
}
pivot.Next = newNode;
}
current = current.Next;
}
}
_buckets = newBuckets;
}
}