-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.h
135 lines (112 loc) · 3.12 KB
/
HashTable.h
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
#include <stdlib.h>
#include "SinglyLinkedListNode.h"
#pragma once
template <typename keyT, typename valueT>
class HashTable
{
private:
SinglyLinkedListNode<keyT>** keys;
SinglyLinkedListNode<valueT>** values;
bool* contains_values;
int bucket_count;
public:
HashTable(int bucket_count)
{
this->bucket_count = bucket_count;
this->keys = (SinglyLinkedListNode<keyT>**)malloc(sizeof(SinglyLinkedListNode<keyT>*) * bucket_count);
this->values = (SinglyLinkedListNode<valueT>**)malloc(sizeof(SinglyLinkedListNode<valueT>*) * bucket_count);
this->contains_values = (bool*)malloc(sizeof(bool) * bucket_count);
for (size_t i = 0; i < bucket_count && contains_values; i++)
{
this->contains_values[i] = false;
}
}
~HashTable()
{
this->free();
}
SinglyLinkedListNode<keyT>* GetKeys()
{
auto output = SinglyLinkedListNode<keyT>(0);
for (size_t i = 0; i < bucket_count; i++)
{
if (contains_values[i])
output.AddRange(keys[i]->Clone());
}
return output.next;
}
size_t GetHash(keyT key)
{
if (typeid(keyT) == typeid(short) || typeid(keyT) == typeid(int) || typeid(keyT) == typeid(size_t) || typeid(keyT) == typeid(long) || typeid(keyT) == typeid(long long))
{
int hash = key;
if (key) hash = hash * (hash < bucket_count) + (bucket_count % hash) * (hash >= bucket_count);
hash -= bucket_count * (hash == bucket_count);
return hash;
}
else
{
return 0;
}
}
void Add(keyT key, valueT value)
{
size_t bucketI = GetHash(key);
InsertAtBucket(key, value, bucketI);
}
void Remove(keyT key)
{
size_t bucket_i = GetHash(key);
bool is_found = contains_values[bucket_i];
if (!is_found) return;
int node_i = keys[bucket_i]->GetValueIndex(key);
is_found = node_i != -1;
if (!is_found) return;
keys[bucket_i] = keys[bucket_i]->RemoveNode(node_i);
values[bucket_i] = values[bucket_i]->RemoveNode(node_i);
bool error = (keys[bucket_i] == 0 || values[bucket_i] == 0) && (void*)keys[bucket_i] != (void*)values[bucket_i];
if (error) throw;
if (keys[bucket_i] == 0)
contains_values[bucket_i] = false;
}
valueT Get(keyT key, bool& is_found)
{
int bucket_i = GetHash(key);
is_found = contains_values[bucket_i];
if (!is_found)
return 0;
int node_i = keys[bucket_i]->GetValueIndex(key);
is_found = node_i != -1;
if (!is_found)
return 0;
return GetValueAtBucket(bucket_i, node_i);
}
void free()
{
for (size_t i = 0; i < bucket_count && keys && values; i++)
{
keys[i]->free();
values[i]->free();
}
std::free(keys);
std::free(values);
std::free(contains_values);
}
private:
void InsertAtBucket(keyT key, valueT value, int bucket_i)
{
if (!this->contains_values[bucket_i])
{
this->keys[bucket_i] = new SinglyLinkedListNode<keyT>(key);
this->values[bucket_i] = new SinglyLinkedListNode<valueT>(value);
this->contains_values[bucket_i] = true;
return;
}
this->keys[bucket_i]->GetLastNode()->next = new SinglyLinkedListNode<keyT>(key);
this->values[bucket_i]->GetLastNode()->next = new SinglyLinkedListNode<valueT>(value);
}
valueT GetValueAtBucket(int bucket_i, int node_i)
{
return values[bucket_i]->GetIndex(node_i)->value;
}
};