-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.H
More file actions
169 lines (140 loc) · 4.46 KB
/
hash.H
File metadata and controls
169 lines (140 loc) · 4.46 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
//using namespace std;
class HashTable
{
int INDEX_NUM; // No. of buckets
// Pointer to an array containing buckets
std::pair<std::atomic<uint32_t>,std::list<std::pair<long unsigned int, std::pair<int, int>>>> *table;
public:
HashTable(int index_num); // Constructor
HashTable();
~HashTable(); // Destructor
// inserts a key into hash table
void insertItem(long unsigned int index, long unsigned int cacheLine);
bool findItem(long unsigned int index, long unsigned int cacheLine);
void wait(long unsigned int index);
void signal(long unsigned int index);
std::pair<long unsigned int, std::pair<int, int>>& getItem(long unsigned int index, long unsigned int cacheLine);
// hash function to map values to key
long unsigned int hashFunction(long unsigned int cacheLine) {
return (cacheLine % INDEX_NUM);
}
void displayHash();
void print();
void print_long(long unsigned int index);
HashTable& operator=(HashTable other)
{
std::swap(INDEX_NUM, other.INDEX_NUM);
std::swap(table, other.table);
return *this;
}
};
HashTable::HashTable()
{
INDEX_NUM = 0;
}
HashTable::HashTable(int index_num)
{
std::cerr << "in constructor begins\n";
INDEX_NUM = index_num;
table = new std::pair<std::atomic<uint32_t>,std::list<std::pair<long unsigned int, std::pair<int, int>>>>[INDEX_NUM];
for(int i = 0; i < INDEX_NUM; i++)
table[i].first = 1;
//for(int i = 0; i < INDEX_NUM; i++)
// std::cerr << table[i].first << "\n";
std::cerr << "in constructor ends\n";
}
HashTable::~HashTable()
{
delete[] table;
}
void HashTable::insertItem(long unsigned int index, long unsigned int cacheLine)
{
//long unsigned int index = hashFunction(cacheLine);
table[index].second.push_back({cacheLine, {0, 0}});
}
#if 0
bool HashTable::findItem(long unsigned int index, long unsigned int cacheLine)
{
// get the hash index of key
//long unsigned int index = hashFunction(cacheLine);
auto it = table[index].second.begin();
while (it != table[index].second.end())
{
if (std::get<0>(*it) == cacheLine)
break;
}
// if key is found in hash table, remove it
if (it != table[index].second.end())
return true;
return false;
}
#endif
bool HashTable::findItem(long unsigned int index, long unsigned int cacheLine)
{
// get the hash index of key
//long unsigned int index = hashFunction(cacheLine);
// find the key in (index)th list
std::list <std::pair<long unsigned int, std::pair<int, int>>> :: iterator i;
for (i = table[index].second.begin();
i != table[index].second.end(); i++) {
if (i->first == cacheLine)
break;
}
// if key is found in hash table, remove it
if (i != table[index].second.end())
return true;
return false;
}
void HashTable::wait(long unsigned int index)
{
//while(table[index].first <= 0);
//table[index].first--;
auto oldval = table[index].first.load();
while (oldval == 0 || !table[index].first.compare_exchange_strong(oldval, 0)) {
oldval = table[index].first.load();
//std::cerr << "in wait, val: " << oldval << "\n";
}
}
void HashTable::signal(long unsigned int index)
{
//std::cerr << "in signal\n";
auto val = table[index].first.fetch_add(1, std::memory_order_seq_cst);
//std::cerr << "in signal, val: " << val << "\n";
}
void HashTable::print_long(long unsigned int index)
{
std::cerr << "hello world long\n";
}
void HashTable::print()
{
std::cerr << "hello world\n";
//auto val = table[index].first.fetch_add(1, std::memory_order_seq_cst);
//std::cerr << "in signal, val: " << val << "\n";
}
std::pair<long unsigned int, std::pair<int, int>>& HashTable::getItem(long unsigned int index, long unsigned int cacheLine)
{
// get the hash index of key
//long unsigned int index = hashFunction(cacheLine);
// find the key in (index)th list
std::list <std::pair<long unsigned int, std::pair<int, int>>> :: iterator i;
for (i = table[index].second.begin();
i != table[index].second.end(); i++) {
if (i->first == cacheLine)
break;
}
// if key is found in hash table, remove it
if (i != table[index].second.end()) {
return *i;//table[index];
}
std::pair<long unsigned int, std::pair<int, int>> empty_pair({0, {0, 0}});
return empty_pair;
}
// function to display hash table
void HashTable::displayHash() {
for (int i = 0; i < INDEX_NUM; i++) {
std::cout << i;
for (auto x : table[i].second)
std::cout << " --> cache line: " << x.first << ", a: " << x.second.first << ", b: " << x.second.second;
std::cout << std::endl;
}
}