-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashing.cs
More file actions
170 lines (141 loc) · 5.36 KB
/
Hashing.cs
File metadata and controls
170 lines (141 loc) · 5.36 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms {
public class Node<T> {
public string Key { get; set; }
public T Value { get; set; }
public Node(string key, T value) {
this.Key = key;
this.Value = value;
}
}
public class HashStructure<T> {
private Node<T> Node { get; set; }
private LinkedList<Node<T>>[] Table { get; set; }
private int _bucketQuantity;
private int _occupiedBuckets;
public HashStructure(int bucketQuantity = 1) {
this._bucketQuantity = bucketQuantity;
Table = new LinkedList<Node<T>>[_bucketQuantity];
}
public T Get(string key) {
int bucket = HashFunction(key);
if (Table[bucket] == null) {
throw new KeyNotFoundException($"Key {key} wasn't found in {nameof(Table)}");
}
foreach (var item in Table[bucket]) {
if (string.Equals(item.Key, key, StringComparison.Ordinal)) {
return item.Value;
}
}
throw new KeyNotFoundException($"Key {key} wasn't found in {nameof(Table)}");
}
public T Remove(string key) {
int bucket = HashFunction(key);
if (Table[bucket] == null) {
throw new KeyNotFoundException($"Key {key} wasn't found in {nameof(Table)}");
}
//Think about resizing logic when removing.
foreach (var item in Table[bucket]) {
if (string.Equals(item.Key, key, StringComparison.Ordinal)) {
Table[bucket].Remove(item);
return item.Value;
}
}
throw new KeyNotFoundException($"Key {key} wasn't found in {nameof(Table)}");
}
public void Add(string key, T value) {
if (string.IsNullOrEmpty(key) || value == null) {
throw new ArgumentNullException();
}
UpdateLoadFactor(this.Table);
Node<T> node = new(key, value);
int bucket = HashFunction(node.Key);
if (bucket < 0 || bucket == null) {
throw new Exception("Invalid Bucket Address");
}
if (this.Table[bucket] == null) {
LinkedList<Node<T>> linkedList = new LinkedList<Node<T>>();
Table[bucket] = linkedList;
linkedList.AddLast(node);
_occupiedBuckets++;
return;
}
else {
var nodeExists = GetNodeIfExisting(node, Table[bucket]);
if (nodeExists == null) {
Table[bucket].AddLast(node);
}
else {
if (!nodeExists.Value.Equals(node.Value)) {
nodeExists.Value = node.Value;
}
}
return;
}
}
private int HashFunction(string key) {
if (key.Length < 1) {
throw new ArgumentException("Key should minimum 1 length.");
}
int asciiValue = AsciiSumOfString(key);
int bucket = asciiValue % _bucketQuantity;
return bucket;
}
private void UpdateLoadFactor(LinkedList<Node<T>>[] table) {
if (this.Table == null) {
throw new ArgumentNullException(nameof(Table), "Table cannot be null");
}
LinkedList<Node<T>>[] resizedTable;
if (table.Length == 0) {
resizedTable = new LinkedList<Node<T>>[1];
this.Table = resizedTable;
return;
}
int totalCapacity = Table.Length;
_occupiedBuckets = 0;
for (int i = 0; i < Table.Length; i++) {
if (Table[i] is not null && Table[i].Count > 0) {
_occupiedBuckets++;
}
}
int thresholdPercent = (_bucketQuantity * 75) / 100;
if (this._occupiedBuckets > thresholdPercent) {
//Increase table
IncreaseTableSize(this.Table);
}
}
public void IncreaseTableSize(LinkedList<Node<T>>[] oldTable) {
LinkedList<Node<T>>[] resizedTable = new LinkedList<Node<T>>[oldTable.Length * 2];
this.Table = resizedTable;
this._bucketQuantity = this.Table.Length - 1;
for (int i = 0; i < oldTable.Length; i++) {
if (oldTable[i] != null) {
foreach (var item in oldTable[i]) {
this.Add(item.Key, item.Value);
}
}
}
}
private int AsciiSumOfString(string text) {
int sum = 0;
foreach (char c in text) {
sum += (int)c;
}
return Math.Min(int.MaxValue, sum);
}
private Node<T>? GetNodeIfExisting(Node<T> node, LinkedList<Node<T>> linkedList) {
bool keyFound = false;
foreach (var item in linkedList) {
if (item.Key == node.Key) {
return item;
}
}
return null;
}
}
}