-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.h
312 lines (244 loc) · 7.84 KB
/
deque.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
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
311
312
#pragma once
#include <cstring>
#include <cstdlib>
#include <iterator>
template<class T>
class Deque {
private:
template<class IT, class Container>
class deque_iterator : public std::iterator<std::random_access_iterator_tag, IT> {
public:
deque_iterator() :
container_(nullptr),
index_(0)
{}
template<class IT2, class Container2>
deque_iterator(const deque_iterator<IT2, Container2>& other) :
container_(other.container_),
index_(other.index_)
{}
template<class IT2, class Container2>
deque_iterator(const std::reverse_iterator< deque_iterator<IT2, Container2> >& other) :
container_(other.base().container_),
index_(other.base().index_)
{}
template<class IT2, class Container2>
deque_iterator& operator=(const deque_iterator<IT2, Container2>& other) {
this->container_ = other.container_;
this->index_ = other.index_;
return *this;
}
template<class IT2, class Container2>
deque_iterator& operator=(const std::reverse_iterator< deque_iterator<IT2, Container2> >& other) {
this->container_ = other.container_;
this->index_ = other.index_;
return *this;
}
deque_iterator& operator+=(typename deque_iterator::difference_type diff) {
index_ += diff;
return *this;
}
deque_iterator& operator-=(typename deque_iterator::difference_type diff) {
return *this += -diff;
}
deque_iterator& operator++() {
return *this += 1;
}
deque_iterator operator++(int) {
deque_iterator tmp(*this);
*this += 1;
return tmp;
}
deque_iterator& operator--() {
return *this += -1;
}
deque_iterator operator--(int) {
deque_iterator tmp(*this);
*this -= 1;
return tmp;
}
deque_iterator operator+(typename deque_iterator::difference_type diff) const {
return deque_iterator(*this) += diff;
}
deque_iterator operator-(typename deque_iterator::difference_type diff) const {
return deque_iterator(*this) -= diff;
}
typename deque_iterator::difference_type operator-(const deque_iterator& other) const {
return this->index_ - other.index_;
}
template<class IT2, class Container2>
bool operator==(const deque_iterator<IT2, Container2>& other) const {
return index_ == other.index_;
}
template<class IT2, class Container2>
bool operator!=(const deque_iterator<IT2, Container2>& other) const {
return index_ != other.index_;
}
template<class IT2, class Container2>
bool operator<(const deque_iterator<IT2, Container2>& other) const {
return *this - other < 0;
}
template<class IT2, class Container2>
bool operator<=(const deque_iterator<IT2, Container2>& other) const {
return *this - other <= 0;
}
template<class IT2, class Container2>
bool operator>(const deque_iterator<IT2, Container2>& other) const {
return *this - other > 0;
}
template<class IT2, class Container2>
bool operator>=(const deque_iterator<IT2, Container2>& other) const {
return *this - other >= 0;
}
typename deque_iterator::reference operator*() const {
return (*container_)[index_];
}
typename deque_iterator::pointer operator->() const {
return &(*container_)[index_];
}
typename deque_iterator::reference operator[](typename deque_iterator::difference_type diff) const {
return *(*this + diff);
}
private:
friend Container;
Container* container_;
int index_;
};
public:
typedef deque_iterator<T, Deque> iterator;
typedef deque_iterator<const T, const Deque> const_iterator;
typedef std::reverse_iterator< iterator > reverse_iterator;
typedef std::reverse_iterator< const_iterator > const_reverse_iterator;
Deque() :
left_(0),
right_(0),
capacity_(1),
data_(new T[capacity_])
{}
Deque(const Deque& other) :
left_(other.left_),
right_(other.right_),
capacity_(other.capacity_),
data_(new T[capacity_])
{
memcpy(data_, other.data_, capacity_ * sizeof(T));
}
~Deque() {
delete[] data_;
}
size_t size() const {
return right_ >= left_ ? right_ - left_ : capacity_ - left_ + right_;
}
bool empty() const {
return size() == 0;
}
void push_back(const T& value) {
if (size() + 1 >= capacity_)
grow();
data_[right_++] = value;
right_ %= capacity_;
}
void pop_back() {
if (size() < capacity_ / 4)
shrink();
right_ = (right_ + capacity_ - 1) % capacity_;
}
void push_front(const T& value) {
if (size() + 1 >= capacity_)
grow();
left_ = (left_ + capacity_ - 1) % capacity_;
data_[left_] = value;
}
void pop_front() {
if (size() < capacity_ / 4)
shrink();
left_ = (left_ + 1) % capacity_;
}
T& operator[](size_t index) {
return data_[(left_ + index) % capacity_];
}
const T& operator[](size_t index) const {
return data_[(left_ + index) % capacity_];
}
T& front() {
return (*this)[0];
}
const T& front() const {
return (*this)[0];
}
T& back() {
return (*this)[size() - 1];
}
const T& back() const {
return (*this)[size() - 1];
}
iterator begin() {
return get_iterator(0);
}
iterator end() {
return get_iterator(size());
}
const_iterator cbegin() const {
return get_citerator(0);
}
const_iterator cend() const {
return get_citerator(size());
}
const_iterator begin() const {
return cbegin();
}
const_iterator end() const {
return cend();
}
reverse_iterator rbegin() {
return reverse_iterator(end());
}
reverse_iterator rend() {
return reverse_iterator(begin());
}
const_reverse_iterator crbegin() const {
return const_reverse_iterator(cend());
}
const_reverse_iterator crend() const {
return const_reverse_iterator(cbegin());
}
const_reverse_iterator rbegin() const {
return crbegin();
}
const_reverse_iterator rend() const {
return crend();
}
private:
void resize(size_t new_capacity) {
T* new_data = new T[new_capacity];
for (size_t i = left_, j = 0; i != right_; i = (i + 1) % capacity_, ++j)
new_data[j] = data_[i];
right_ = size();
left_ = 0;
delete[] data_;
data_ = new_data;
capacity_ = new_capacity;
}
void grow() {
resize(capacity_ * 2);
}
void shrink() {
resize(capacity_ / 2);
}
iterator get_iterator(size_t index) {
iterator it;
it.container_ = this;
it.index_ = index;
return it;
}
const_iterator get_citerator(size_t index) const {
const_iterator it;
it.container_ = this;
it.index_ = index;
return it;
}
size_t left_;
size_t right_;
size_t capacity_;
T* data_;
};