-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyLockFreeMap.cpp
More file actions
177 lines (171 loc) · 5.53 KB
/
myLockFreeMap.cpp
File metadata and controls
177 lines (171 loc) · 5.53 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
172
173
174
175
176
177
#include <iostream>
#include <atomic>
#include <memory>
#include <thread>
#include <vector>
#include <mutex>
template <typename Key, typename Value>
class LockFreeList {
public:
LockFreeList() {
head = std::make_unique<Node>();
size_ = 0;
}
Value& push_front(const Key& key, const Value& val = Value{}) {
auto ptr = std::make_unique<Node>(key, val);
std::unique_lock<std::mutex> lk(head->m);
ptr->next = std::move(head->next);
head->next = std::move(ptr);
size_++;
return *(head->next->data);
}
template <typename F>
void for_each(F& f) {
Node* curr = head.get();
std::unique_lock<std::mutex> lk(head->m);
while (curr->next.get()) {
std::unique_lock<std::mutex> next_lock(curr->next->m);
const Node* next = curr->next.get();
*next->data = f(*next->data);
curr = next;
lk = std::move(next_lock);
}
}
void remove_if(const Key& key, std::size_t& count) {
Node* curr = head.get();
std::unique_lock<std::mutex> lk(head->m);
while (curr->next.get()) {
std::unique_lock<std::mutex> next_lock(curr->next->m);
const Node* next = curr->next.get();
if (next->key == key) {
curr->next = std::move(next->next);
count--;
return;
}
else {
curr = next;
lk = std::move(next_lock);
}
}
}
Value& find_first(const Key& key, std::size_t& count) {
const Node* curr = head.get();
std::unique_lock<std::mutex> lk(head->m);
while (Node* next = curr->next.get()) {
std::unique_lock<std::mutex> next_lock(next->m);
if (next->key == key) {
return *next->data;
}
else {
curr = next;
lk = std::move(next_lock);
}
}
lk.unlock();
Value& val = push_front(key);
count++;
size_++;
return val;
}
Value& find_first(const Key& key) {
Node* curr = head.get();
std::unique_lock<std::mutex> lk(head->m);
while (curr->next.get()) {
std::unique_lock<std::mutex> next_lock(curr->next->m);
const Node* next = curr->next.get();
if (next->key == key) {
return *next->data;
}
}
throw std::out_of_range{};
}
void find_first(const Key& key, std::size_t& count, const Value& val) {
const Node* curr = head.get();
std::unique_lock<std::mutex> lk(head->m);
while (curr->next) {
std::unique_lock<std::mutex> next_lock(curr->next->m);
const Node* next = curr->next.get();
if (next->key == key) {
next->data = std::make_unique<Node>(val);
return;
}
else {
curr = next;
lk = std::move(next_lock);
}
}
push_front(key, val);
count++;
size_++;
}
// void operator=(const Value& val) {
// replace(key, n_elements, val);
// return;
// }
std::size_t size() {
return size_;
}
private:
struct Node {
Key key;
std::unique_ptr<Value> data = nullptr;
std::unique_ptr<Node> next = nullptr;
std::mutex m;
Node() = default;
Node(const Key& key_, const Value& val_) {
key = key_;
data = std::make_unique<Value>(val_);
}
};
std::unique_ptr<Node> head;
std::size_t size_;
};
template <typename Key, typename Value, typename Hash = std::hash<Key>>
class LockFreeUnorderedMap {
public:
LockFreeUnorderedMap(int buckets_){
hasher = Hash{};
buckets = buckets_;
map.resize(buckets);
}
LockFreeUnorderedMap() {
std::cout << "hi";
hasher = Hash{};
buckets = 17;
map.resize(buckets);
}
Value& operator[](const Key& key) noexcept {
int b = hasher(key) % buckets;
int x = 1;
return map[b].find_first(key, n_elements);
}
Value& at(const Key& key) const {
int b = hasher(key) % buckets;
std::cout << 1;
return map[b].find_first(key, n_elements);
}
void insert(const Key& key, const Value& val) {
map[hasher(key) % buckets].push_front(key, val);
n_elements++;
}
std::size_t count(const Key& key) {
int b = hasher(key) % buckets;
return map[b].size() > 0;
}
std::size_t size() const {
return n_elements;
}
private:
std::size_t n_elements = 0;
std::size_t buckets = 17;
std::vector<LockFreeList<Key, Value>> map;
Hash hasher;
};
int main() {
LockFreeUnorderedMap<int, int> map;
std::cout << map.count(1);
std::cout << map[1];
map[1] = 1;
std::cout << map[1];
std::cout << map.size();
}