-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque.hh
297 lines (263 loc) · 7.47 KB
/
deque.hh
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
#pragma once
#include <algorithm>
namespace ns {
template <typename ITEM> class deque {
public:
deque();
explicit deque(int);
deque(int, const ITEM);
deque(const deque &other);
deque(deque &&other);
deque &operator=(const deque &rhs);
deque &operator=(deque &&rhs);
~deque() { destruct(); }
using value_type = ITEM;
using reference = value_type &;
using const_reference = const value_type &;
using size_type = int;
constexpr ITEM &operator[](int i) const { return seq[(head + i) % cap]; }
constexpr ITEM &back() const { return seq[tail]; }
constexpr ITEM &front() const { return seq[head]; }
constexpr int size() const { return sz; }
constexpr int capacity() const { return cap; }
constexpr bool empty() const { return sz == 0; }
void reserve(int);
template <class... Args> void push_back(Args &&...args);
void push_back(ITEM &&item);
void pop_back();
template <class... Args> void push_front(Args &&...args);
void pop_front();
void clear();
class iterator {
friend class deque;
public:
using difference_type = std::ptrdiff_t;
using value_type = ITEM;
using pointer = ITEM *;
using reference = ITEM &;
using iterator_category = std::forward_iterator_tag;
constexpr ITEM &operator*() const { return deck[idx]; }
bool operator==(const iterator &rhs) const { return idx == rhs.idx; }
bool operator!=(const iterator &rhs) const { return idx != rhs.idx; }
iterator &operator++();
iterator operator++(int);
private:
iterator(deque &deck, int idx) : deck{deck}, idx{idx} {}
deque &deck;
int idx;
};
class const_iterator {
friend class deque;
public:
using difference_type = std::ptrdiff_t;
using value_type = ITEM;
using pointer = const ITEM *;
using reference = const ITEM &;
using iterator_category = std::forward_iterator_tag;
constexpr ITEM &operator*() const { return deck[idx]; }
bool operator==(const const_iterator &rhs) const { return idx == rhs.idx; }
bool operator!=(const const_iterator &rhs) const { return idx != rhs.idx; }
const_iterator &operator++();
const_iterator operator++(int);
private:
const_iterator(const deque &deck, int idx) : deck{deck}, idx{idx} {}
const deque &deck;
int idx;
};
iterator begin() { return iterator(*this, 0); }
iterator end() { return iterator(*this, sz); }
const_iterator begin() const { return const_iterator(*this, 0); }
const_iterator end() const { return const_iterator(*this, sz); }
private:
int const init_cap{4};
int sz;
int cap;
int head;
int tail;
ITEM *seq;
ITEM *construct(const deque &src);
void destruct();
};
template <typename ITEM>
deque<ITEM>::deque()
: sz{0}, cap{init_cap}, head{0}, tail{0},
seq{(ITEM *)operator new(cap * sizeof(ITEM))} {}
template <typename ITEM>
deque<ITEM>::deque(int n)
: sz{n}, cap{n > init_cap ? n : init_cap}, head{0}, tail{sz - 1},
seq{(ITEM *)operator new(cap * sizeof(ITEM))} {
for (int i = 0; i < n; ++i)
new (seq + i) ITEM();
}
template <typename ITEM>
deque<ITEM>::deque(int n, const ITEM item)
: sz{n}, cap{n > init_cap ? n : init_cap}, head{0}, tail{sz - 1},
seq{(ITEM *)operator new(cap * sizeof(ITEM))} {
for (int i = 0; i < n; ++i)
new (seq + i) ITEM(item);
}
template <typename ITEM>
deque<ITEM>::deque(const deque &other)
: sz{other.sz}, cap{other.cap}, head{0}, tail{sz - 1},
seq{construct(other)} {}
template <typename ITEM>
deque<ITEM>::deque(deque &&other)
: sz{other.sz}, cap{other.cap}, head{other.head}, tail{other.tail},
seq{other.seq} {
other.seq = nullptr;
other.sz = 0;
}
template <typename ITEM> deque<ITEM> &deque<ITEM>::operator=(const deque &rhs) {
if (&rhs == this)
return *this;
destruct();
sz = rhs.sz;
cap = rhs.cap;
head = 0;
tail = sz - 1;
seq = construct(rhs);
return *this;
}
template <typename ITEM> deque<ITEM> &deque<ITEM>::operator=(deque &&rhs) {
if (&rhs == this)
return *this;
destruct();
sz = rhs.sz;
cap = rhs.cap;
head = rhs.head;
tail = rhs.tail;
seq = rhs.seq;
rhs.seq = nullptr;
rhs.sz = 0;
return *this;
}
template <typename ITEM> void deque<ITEM>::reserve(int n) {
if (n >= sz) {
ITEM *next{(ITEM *)operator new(n * sizeof(ITEM))};
for (int i = 0; i < sz; ++i) {
new (next + i) ITEM(std::move(seq[(head + i) % cap]));
(seq + (head + i) % cap)->~ITEM();
}
operator delete(seq);
seq = next;
cap = n;
head = 0;
tail = sz - 1;
}
}
// 对sz等于0和sz大于0两种情况分别处理
template <typename ITEM>
template <class... Args>
void deque<ITEM>::push_back(Args &&...args) {
if (sz == cap)
reserve(cap << 1);
++sz;
if (sz > 1)
if (++tail == cap)
tail = 0;
// std::construct_at(seq + tail, std::forward<Args>(args)...);
new (seq + tail) ITEM(std::forward<Args>(args)...);
}
// 对sz等于0和sz大于0两种情况分别处理
template <typename ITEM> void deque<ITEM>::push_back(ITEM &&item) {
if (sz == cap)
reserve(cap << 1);
++sz;
if (sz > 1)
if (++tail == cap)
tail = 0;
// std::construct_at(seq + tail, std::forward<Args>(args)...);
new (seq + tail) ITEM(std::forward<ITEM>(item));
}
// 对sz等于1和sz大于1两种情况分别处理
template <typename ITEM> void deque<ITEM>::pop_back() {
// std::destroy_at(seq + tail);
(seq + tail)->~ITEM();
--sz;
if (sz > 0) {
if (--tail == -1)
tail = cap - 1;
if (sz == cap >> 2)
reserve(cap >> 1);
}
}
// 对sz等于0和sz大于0两种情况分别处理
template <typename ITEM>
template <class... Args>
void deque<ITEM>::push_front(Args &&...args) {
if (sz == cap)
reserve(cap << 1);
++sz;
if (sz > 1)
if (--head == -1)
head = cap - 1;
// std::construct_at(seq + head, std::forward<Args>(args)...);
new (seq + head) ITEM(std::forward<Args>(args)...);
}
// 对sz等于1和sz大于1两种情况分别处理
template <typename ITEM> void deque<ITEM>::pop_front() {
// std::destroy_at(seq + head);
(seq + head)->~ITEM();
--sz;
if (sz > 0) {
if (++head == cap)
head = 0;
if (sz == cap >> 2)
reserve(cap >> 1);
}
}
template <typename ITEM> void deque<ITEM>::clear() {
destruct();
sz = 0;
cap = init_cap;
head = tail = 0;
seq = (ITEM *)operator new(cap * sizeof(ITEM));
}
template <typename ITEM>
typename deque<ITEM>::iterator &deque<ITEM>::iterator::operator++() {
++idx;
return *this;
}
template <typename ITEM>
typename deque<ITEM>::iterator deque<ITEM>::iterator::operator++(int) {
iterator x{*this};
++idx;
return x;
}
template <typename ITEM>
typename deque<ITEM>::const_iterator &
deque<ITEM>::const_iterator::operator++() {
++idx;
return *this;
}
template <typename ITEM>
typename deque<ITEM>::const_iterator
deque<ITEM>::const_iterator::operator++(int) {
const_iterator x{*this};
++idx;
return x;
}
template <typename ITEM> ITEM *deque<ITEM>::construct(const deque &src) {
ITEM *a{(ITEM *)operator new(cap * sizeof(ITEM))};
for (int i = 0; i < sz; ++i)
new (a + i) ITEM(src[i]);
return a;
}
template <typename ITEM> void deque<ITEM>::destruct() {
for (int i = 0; i < sz; ++i)
(seq + (head + i) % cap)->~ITEM();
operator delete(seq);
}
// queue
template <typename ITEM> class queue : private deque<ITEM> {
public:
ITEM &front() { return deque<ITEM>::front(); }
ITEM &back() { return deque<ITEM>::back(); }
void enqueue(const ITEM e) { deque<ITEM>::push_back(e); }
ITEM dequeue() {
ITEM x = deque<ITEM>::front();
deque<ITEM>::pop_front();
return x;
}
};
} // namespace ns