-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.hpp
310 lines (243 loc) · 7.87 KB
/
Deque.hpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#ifndef DEQUE_HPP
#define DEQUE_HPP
#include <deque>
#include <initializer_list>
#include <memory>
#include <stdexcept>
template<typename T>
class Deque final {
public:
using size_type = std::size_t;
private:
struct Node {
T value{};
std::shared_ptr<Node> prev{};
std::shared_ptr<Node> next{};
Node()
: value{}, next{} {
}
explicit Node(T input_value)
: value{input_value}, prev{nullptr}, next{nullptr} {
}
explicit Node(T input_value, std::shared_ptr<Node> input_prev, std::shared_ptr<Node> input_next)
: value{input_value}, prev{input_prev},
next{input_next} {
}
};
void range_check_exclusive(const size_type pos) const {
if (pos >= m_size) {
throw std::out_of_range(
"Deque::range_check_exclusive: pos (which is " + std::to_string(pos) + ") >= this->m_size() (which is "
+
std::to_string(m_size) + ")");
}
}
void range_check_inclusive(const size_type pos) const {
if (pos > m_size) {
throw std::out_of_range(
"Deque::range_check_inclusive: pos (which is " + std::to_string(pos) + ") > this->m_size() (which is " +
std::to_string(m_size) + ")");
}
}
std::shared_ptr<Node> m_head{};
std::shared_ptr<Node> m_tail{};
size_type m_size{};
public:
Deque(std::initializer_list<T> lst = {}) noexcept
: m_head{}, m_tail{}, m_size{} {
if (!std::empty(lst)) {
for (const T &elem: lst) {
this->push_back(elem);
}
}
}
Deque(const Deque<T> &deque) noexcept {
for (std::shared_ptr<Node> current = deque.m_head; current != nullptr; current = current->next) {
this->push_back(current->value);
}
}
~Deque() noexcept {
clear();
}
Deque<T> &operator=(const Deque<T> &deque) noexcept {
if (this == &deque) {
return *this;
}
this->m_head = deque.m_head;
this->m_tail = deque.m_tail;
this->m_size = deque.m_size;
return *this;
}
friend std::ostream &operator<<(std::ostream &out, const Deque<T> &deque) {
if (deque.m_size == 0) {
return out;
}
for (std::shared_ptr<Node> current = deque.m_head; current != nullptr; current = current->next) {
out << current->value << (current->next ? ", " : ".");
}
return out;
}
T &operator[](const size_type pos) {
this->range_check_exclusive(pos);
if (pos <= m_size - pos) {
std::shared_ptr<Node> current{m_head};
for (size_type i = 0; i < pos; ++i) {
current = current->next;
}
return current->value;
} else {
std::shared_ptr<Node> current{m_tail};
for (size_type i = 0; i < m_size - pos - 1; ++i) {
current = current->prev;
}
return current->value;
}
}
const T &operator[](const size_type pos) const {
this->range_check_exclusive(pos);
if (pos <= m_size - pos) {
std::shared_ptr<Node> current{m_head};
for (size_type i = 0; i < pos; ++i) {
current = current->next;
}
return current->value;
} else {
std::shared_ptr<Node> current{m_tail};
for (size_type i = 0; i < m_size - pos - 1; ++i) {
current = current->prev;
}
return current->value;
}
}
[[nodiscard]] bool empty() const {
return m_size == 0;
}
[[nodiscard]] size_type size() const {
return m_size;
}
const T &back() const {
return m_tail->value;
}
const T &front() const {
return m_head->value;
}
void clear() noexcept {
m_head = nullptr;
m_tail = nullptr;
m_size = 0;
}
void push_back(const T &value) noexcept {
std::shared_ptr<Node> temp{std::make_shared<Node>(value, m_tail, nullptr)};
if (m_size == 0) {
m_head = temp;
} else {
m_tail->next = temp;
}
m_tail = temp;
m_size += 1;
}
void pop_back() noexcept {
std::shared_ptr<Node> temp{m_tail->prev};
if (m_size == 1) {
m_head = nullptr;
} else {
temp->next = nullptr;
}
m_tail = temp;
m_size -= 1;
}
void push_front(const T &value) noexcept {
std::shared_ptr<Node> temp{std::make_shared<Node>(value, nullptr, m_head)};
if (m_size == 0) {
m_tail = temp;
} else {
m_head->prev = temp;
}
m_head = temp;
m_size += 1;
}
void pop_front() noexcept {
std::shared_ptr<Node> temp{m_head->next};
if (m_size == 1) {
m_tail = nullptr;
} else {
temp->prev = nullptr;
}
m_head = temp;
m_size -= 1;
}
size_type erase(const size_type pos) {
this->range_check_exclusive(pos);
if (pos <= m_size - pos) {
std::shared_ptr<Node> dummy{std::make_shared<Node>(0, nullptr, m_head)};
std::shared_ptr<Node> current{dummy};
for (size_type i = 0; i < pos; ++i) {
current = current->next;
}
current->next = current->next->next;
current->next->prev = current;
dummy->next->prev = nullptr;
m_head = dummy->next;
} else {
std::shared_ptr<Node> dummy{std::make_shared<Node>(0, m_tail, nullptr)};
std::shared_ptr<Node> current{dummy};
for (size_type i = pos; i < m_size - 1; ++i) {
current = current->prev;
}
current->prev = current->prev->prev;
current->prev->next = current;
dummy->prev->next = nullptr;
m_tail = dummy->prev;
}
return pos;
}
size_type erase(const size_type first, const size_type last) {
this->range_check_exclusive(first);
for (size_type i = first; i <= last; ++i) {
erase(first);
}
return first;
}
size_type insert(const size_type pos, const T &value) {
this->range_check_inclusive(pos);
if (pos <= m_size - pos) {
std::shared_ptr<Node> dummy{std::make_shared<Node>(0, nullptr, m_head)};
std::shared_ptr<Node> current{dummy};
for (size_type i = 0; i < pos; ++i) {
current = current->next;
}
std::shared_ptr<Node> temp{std::make_shared<Node>(value, current, current->next)};
temp->prev->next = temp;
temp->next->prev = temp;
dummy->next->prev = nullptr;
m_head = dummy->next;
} else {
std::shared_ptr<Node> dummy{std::make_shared<Node>(0, m_tail, nullptr)};
std::shared_ptr<Node> current{dummy};
for (size_type i = pos; i < m_size; ++i) {
current = current->prev;
}
std::shared_ptr<Node> temp{std::make_shared<Node>(value, current->prev, current)};
temp->prev->next = temp;
temp->next->prev = temp;
dummy->prev->next = nullptr;
m_tail = dummy->prev;
}
return pos;
}
size_type insert(const size_type pos, const size_type count, const T &value) {
this->range_check_inclusive(pos);
for (size_type i = 0; i < count; ++i) {
this->insert(pos, value);
}
return pos;
}
size_type insert(const size_type pos, std::initializer_list<T> arr) {
this->range_check_inclusive(pos);
for (const T &elem: arr) {
this->insert(pos, elem);
}
return pos;
}
};
#endif