-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
154 lines (124 loc) · 3.91 KB
/
Program.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
// 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>(5);
hashtable.Add(3, new("Leo", "leo@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(6, new("Jon", "jon@email.fk"));
Console.WriteLine(hashtable.Get(0));
Console.WriteLine(hashtable.Get(1));
Console.WriteLine(hashtable.Get(2));
Console.WriteLine(hashtable.Get(3));
Console.WriteLine(hashtable.Get(4));
hashtable.Remove(0);
hashtable.Remove(1);
hashtable.Remove(2);
hashtable.Remove(3);
hashtable.Remove(4);
hashtable.Remove(5);
hashtable.Remove(6);
// var hashtable = new HashTable<string, Profile>(5);
// 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 _count;
public HashTable(int capacity)
=> _buckets = new HashNode[(int)(capacity * 1.25)]; // 25% more space to avoid collisions
public void Add(TKey key, TValue value)
{
var index = _computeHash(key);
if(_count == _buckets.Length)
{
throw new InvalidOperationException("Hashtable is full");
}
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;
}
}