-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConcurrentLinkedList.h
136 lines (115 loc) · 3.78 KB
/
ConcurrentLinkedList.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
136
#pragma once
#include <atomic>
#include <memory>
#include <optional>
#include "Iterator.h"
template<typename T>
class ConcurrentLinkedList {
private:
class Node {
public:
std::optional<T> dat;
std::atomic<std::shared_ptr<Node>> next;
Node() : dat(std::nullopt), next(nullptr) {}
explicit Node(T e) : dat(e), next(nullptr) {}
Node(T e, std::shared_ptr<Node> _next) : dat(e), next(_next) {}
};
const std::shared_ptr<Node> head;
public:
ConcurrentLinkedList() : head(std::make_shared<Node>()) {
}
~ConcurrentLinkedList() = default;
void push_head(T e) {
std::shared_ptr<Node> E = std::make_shared<Node>(e);
while (true) {
std::shared_ptr<Node> original_head = head->next.load();
E->next = original_head;
if (head->next.compare_exchange_weak(original_head, E))
break;
}
}
std::optional<T> get_head() const {
auto _head = head->next.load();
if (_head == nullptr) return std::nullopt;
else return _head->dat;
}
std::optional<T> poll_head() {
while (true) {
std::shared_ptr<Node> original_head = head->next;
if (original_head == nullptr) return std::nullopt;
if (head->next.compare_exchange_weak(original_head, original_head->next))
return original_head->dat;
}
}
bool empty() const {
return this->head->next.load() == nullptr;
}
bool remove(T e) {
std::shared_ptr<Node> c = head;
while (true) {
std::shared_ptr<Node> c_next = c->next.load();
if (c_next == nullptr) break;
if (c_next->dat == e) {
if (c->next.compare_exchange_weak(c_next, c_next->next.load()))
return true;
} else {
c = c->next;
}
}
return false;
}
class LinkedListIterator : public RemovableIterator<T> {
private:
std::shared_ptr<Node> c;
std::shared_ptr<Node> c_next;
ConcurrentLinkedList& linkedList;
public:
explicit LinkedListIterator(ConcurrentLinkedList& linkedList_) : linkedList(linkedList_) {
c = nullptr;
c_next = nullptr;
}
T current() const override {
return c_next->dat.value();
}
bool MoveNext() override {
if (c == nullptr) {
c = linkedList.head;
} else {
if (c_next == c->next.load())
c = c->next;
}
if (c == nullptr) return false;
c_next = c->next;
if (c_next == nullptr) return false;
return true;
}
bool remove() override {
if (c == nullptr || c_next == nullptr) return false;
if (c->next.compare_exchange_weak(c_next, c_next->next.load())) {
return true;
} else {
return false;
}
}
bool remove(T e) override {
std::shared_ptr<Node> c = this->c;
while (true) {
std::shared_ptr<Node> c_next = c->next.load();
if (c_next == nullptr) break;
if (c_next->dat == e) {
if (c->next.compare_exchange_weak(c_next, c_next->next.load()))
return true;
} else {
c = c->next;
}
}
return false;
}
};
std::unique_ptr<RemovableIterator<T>> getRemovableIterator() {
return std::make_unique<LinkedListIterator>(*this);
}
std::unique_ptr<Iterator<T>> getIterator() const {
return std::make_unique<LinkedListIterator>(const_cast<ConcurrentLinkedList&>(*this));
}
};