-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.cpp
More file actions
171 lines (148 loc) · 4.91 KB
/
Copy pathHashTable.cpp
File metadata and controls
171 lines (148 loc) · 4.91 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
#include <iostream>
#include <vector>
#include <list>
#include <mutex>
#include <memory>
#include <thread>
#include <fstream>
template<typename T>
class Node {
public:
T* value;
int depth;
std::mutex mutex;
int key;
Node<T>* parent; //implement similarily to node in tdgit7
Node(T* val){
value = val;
parent = nullptr;
depth = 0;
key = val->hash();
}
Node(T* val, Node<T>* par) : value(val), key(val->hash()), parent(par){
depth = parent->depth +1;
}
Node(){};
};
template<typename T>
class BaseHashSet {
public:
std::vector<std::list<Node<T>*>> table;
int max_depth;
int count;
BaseHashSet(){};
//QUESTION
//Why do we need to initialize one Node in every bucket ?
BaseHashSet(int max_depth) : table(max_depth), count(0) {
for (int i = 0; i < max_depth; i++) {
table[i].emplace_back(new Node<T>(new T()));
}
this->max_depth = max_depth;
}
size_t get_max_depth() const {return this->max_depth;}
size_t get_count() const {return this->count;}
bool add(Node<T> *x) {
int myBucket = x->value->hash() % table.size(); //hashtable,
std::lock_guard<std::mutex> lock(table[myBucket].front()->mutex);
for (const auto& node : table[myBucket]) {
if (node->value == x->value) {
return false; // Element already exists
}
}
table[myBucket].emplace_back(x);
//std::cout << table[myBucket].size() << std::endl;
return true;
}
bool contains(T* x) {
int x_key = x->hash();
int myBucket = x_key % table.size(); //hash defined in website class
std::lock_guard<std::mutex> lock(table[myBucket].front()->mutex);
for (const auto& node : table[myBucket]) {
if (node->key == x_key) {
return true;
}
}
return false;
}
bool contains(std::string url){
int url_key = std::hash<std::string>{}(url);
int myBucket = url_key % table.size(); //hash defined in website class
std::lock_guard<std::mutex> lock(table[myBucket].front()->mutex);
for (const auto& node : table[myBucket]) {
if (node->key == url_key) {
return true;
}
}
return false;
}
Node<T>* get(T* x){
int x_key = x->hash();
int myBucket = x_key % table.size(); //hash defined in website class
std::lock_guard<std::mutex> lock(table[myBucket].front()->mutex);
for (Node<T>* node : table[myBucket]) {
if (node->key == x_key) {
return node;
}
}
return nullptr;
}
Node<T>* get(std::string url){
int url_key = std::hash<std::string>{}(url);
int myBucket = url_key % table.size(); //hash defined in website class
std::lock_guard<std::mutex> lock(table[myBucket].front()->mutex);
for (Node<T>* node : table[myBucket]) {
if (node->key == url_key) {
return node;
}
}
return nullptr;
}
void printHashtable(){
for (size_t i = 0; i < table.size(); ++i) {
std::cout << "Bucket " << i << ": ";
std::lock_guard<std::mutex> lock(table[i].front()->mutex);
for (const auto& node : table[i]) {
node->value->print();
if (node->parent != nullptr){
std::cout << ", Parent: ";
node->parent->value->print();}
std::cout <<" " <<std::endl;
}
std::cout << std::endl;
}
}
void writeHashtableToFile(const std::string& filename) {
std::ofstream outputFile(filename);
if (!outputFile.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return;
}
for (size_t i = 0; i < table.size(); ++i) {
outputFile << "Bucket " << i << ": ";
std::lock_guard<std::mutex> lock(table[i].front()->mutex);
for (const auto& node : table[i]) {
outputFile << node->value->toString();
if (node->parent != nullptr) {
outputFile << ", Parent: " << node->parent->value->toString();
}
outputFile << std::endl;
}
outputFile << std::endl;
}
outputFile.close();
}
};
template<typename T>
class StripedHashSet : public BaseHashSet<T> {
private:
std::vector<std::mutex> locks;
public:
StripedHashSet(){};
StripedHashSet(int max_depth) : BaseHashSet<T>(max_depth), locks(max_depth) {}
void acquire(T x) {
locks[std::hash<T>{}(x) % locks.size()].lock();
}
void release(T x) {
locks[std::hash<T>{}(x) % locks.size()].unlock();
}
};