-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTables.cs
More file actions
98 lines (75 loc) · 2.83 KB
/
HashTables.cs
File metadata and controls
98 lines (75 loc) · 2.83 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
using Hashes;
using System.Numerics;
class HashTable{
private IHashFunct HashFunct { get; set; }
private int l { get; set; }
private int size { get; set; }
private List<(ulong, int)>[] arrayOfLists { get; set; }
public HashTable(IHashFunct HashFunct, int l){
this.HashFunct = HashFunct;
this.l = l;
size = (1 << l);
arrayOfLists = new List<(ulong, int)>[size];
for (int i = 0; i < size; i++){
arrayOfLists[i] = new List<(ulong, int)>();
}
}
public int get(ulong x){
// Hashing indgående værdi
ulong hash = HashFunct.HashFunction(x,l);
List<(ulong, int)> list = arrayOfLists[(ulong)hash];
// Gennemgår listen for at finde matchende nøgle
foreach ((ulong, int) tuple in list){
// Hvis nøglen matcher, returner den tilsvarende værdi
if (tuple.Item1 == x){
return tuple.Item2;
}
}
// Hvis nøglen ikke findes, returner 0
return 0;
}
public void set(ulong x, int v){
ulong hashed_val_index = HashFunct.HashFunction(x, l) % (ulong)size;
// Tjek om nøglen allerede er i tabellen
for (int i = 0; i < arrayOfLists[hashed_val_index].Count; i++)
{
if (arrayOfLists[hashed_val_index][i].Item1 == x)
{
// Nøglen findes allerede, opdater værdien
arrayOfLists[hashed_val_index][i] = (x, v);
return;
}
}
// Nøglen findes ikke, tilføj den til listen på indekspositionen
arrayOfLists[hashed_val_index].Add((x, v));
}
public void increment(ulong x, int d){
ulong hash = HashFunct.HashFunction(x, l);
List<(ulong, int)> listVar = arrayOfLists[hash];
for (int i = 0; i < arrayOfLists[hash].Count; i++){
if (arrayOfLists[hash][i].Item1 == x){
arrayOfLists[hash][i] = (x, (arrayOfLists[hash][i].Item2 + d));
return;
}
}
arrayOfLists[hash].Add((x,d));
}
public BigInteger quadratic_sum(IEnumerable<Tuple<ulong, int>> stream){
//Console.WriteLine("Start");
foreach (Tuple< ulong, int > key in stream){
this.increment(key.Item1, key.Item2);
}
// Console.WriteLine("Done incrementing.\n");
ulong counter = 0;
//for hver indgang i hash tabellen
foreach (List<(ulong, int)> list in arrayOfLists){
//Console.WriteLine("Secondloop");
//Console.WriteLine(list.Count);
for (int i = 0; i<list.Count; i++ ){
counter += (ulong)list[i].Item2 * (ulong)list[i].Item2;
//Console.WriteLine("Second loop inner");
}
}
return counter;
}
}