1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_DEQUE
|
---|
30 | #define LIBCPP_DEQUE
|
---|
31 |
|
---|
32 | #include <initializer_list>
|
---|
33 | #include <iterator>
|
---|
34 | #include <memory>
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 | template<class T, class Allocator = allocator<T>>
|
---|
39 | class deque;
|
---|
40 |
|
---|
41 | namespace aux
|
---|
42 | {
|
---|
43 | /**
|
---|
44 | * Note: We decided that these iterators contain a
|
---|
45 | * reference to the container and an index, which
|
---|
46 | * allows us to use the already implemented operator[]
|
---|
47 | * on deque and also allows us to conform to the requirement
|
---|
48 | * of the standard that functions such as push_back
|
---|
49 | * invalidate the .end() iterator.
|
---|
50 | */
|
---|
51 |
|
---|
52 | template<class T, class Allocator>
|
---|
53 | class deque_const_iterator
|
---|
54 | {
|
---|
55 | public:
|
---|
56 | using size_type = typename deque<T, Allocator>::size_type;
|
---|
57 | using value_type = typename deque<T, Allocator>::value_type;
|
---|
58 | using reference = typename deque<T, Allocator>::const_reference;
|
---|
59 | using difference_type = size_type;
|
---|
60 | using pointer = const value_type*;
|
---|
61 | using iterator_category = random_access_iterator_tag;
|
---|
62 |
|
---|
63 | deque_const_iterator(const deque<T, Allocator>& deq, size_type idx)
|
---|
64 | : deq_{deq}, idx_{idx}
|
---|
65 | { /* DUMMY BODY */ }
|
---|
66 |
|
---|
67 | deque_const_iterator& operator=(const deque_const_iterator& other)
|
---|
68 | {
|
---|
69 | deq_ = other.deq_;
|
---|
70 | idx_ = other.idx_;
|
---|
71 |
|
---|
72 | return *this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | reference operator*() const
|
---|
76 | {
|
---|
77 | return deq_[idx_];
|
---|
78 | }
|
---|
79 |
|
---|
80 | pointer operator->() const
|
---|
81 | {
|
---|
82 | return addressof(deq_[idx_]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | deque_const_iterator& operator++()
|
---|
86 | {
|
---|
87 | ++idx_;
|
---|
88 |
|
---|
89 | return *this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | deque_const_iterator operator++(int)
|
---|
93 | {
|
---|
94 | return deque_const_iterator{deq_, idx_++};
|
---|
95 | }
|
---|
96 |
|
---|
97 | deque_const_iterator& operator--()
|
---|
98 | {
|
---|
99 | --idx_;
|
---|
100 |
|
---|
101 | return *this;
|
---|
102 | }
|
---|
103 |
|
---|
104 | deque_const_iterator operator--(int)
|
---|
105 | {
|
---|
106 | return deque_const_iterator{deq_, idx_--};
|
---|
107 | }
|
---|
108 |
|
---|
109 | deque_const_iterator operator+(difference_type n)
|
---|
110 | {
|
---|
111 | return deque_const_iterator{deq_, idx_ + n};
|
---|
112 | }
|
---|
113 |
|
---|
114 | deque_const_iterator& operator+=(difference_type n)
|
---|
115 | {
|
---|
116 | idx_ += n;
|
---|
117 |
|
---|
118 | return *this;
|
---|
119 | }
|
---|
120 |
|
---|
121 | deque_const_iterator operator-(difference_type n)
|
---|
122 | {
|
---|
123 | return deque_const_iterator{deq_, idx_ - n};
|
---|
124 | }
|
---|
125 |
|
---|
126 | deque_const_iterator& operator-=(difference_type n)
|
---|
127 | {
|
---|
128 | idx_ -= n;
|
---|
129 |
|
---|
130 | return *this;
|
---|
131 | }
|
---|
132 |
|
---|
133 | reference operator[](difference_type n) const
|
---|
134 | {
|
---|
135 | return deq_[idx_ + n];
|
---|
136 | }
|
---|
137 |
|
---|
138 | difference_type operator-(const deque_const_iterator& rhs)
|
---|
139 | {
|
---|
140 | return idx_ - rhs.idx_;
|
---|
141 | }
|
---|
142 |
|
---|
143 | size_type idx() const
|
---|
144 | {
|
---|
145 | return idx_;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private:
|
---|
149 | const deque<T, Allocator>& deq_;
|
---|
150 | size_type idx_;
|
---|
151 | };
|
---|
152 |
|
---|
153 | template<class T, class Allocator>
|
---|
154 | bool operator==(const deque_const_iterator<T, Allocator>& lhs,
|
---|
155 | const deque_const_iterator<T, Allocator>& rhs)
|
---|
156 | {
|
---|
157 | return lhs.idx() == rhs.idx();
|
---|
158 | }
|
---|
159 |
|
---|
160 | template<class T, class Allocator>
|
---|
161 | bool operator!=(const deque_const_iterator<T, Allocator>& lhs,
|
---|
162 | const deque_const_iterator<T, Allocator>& rhs)
|
---|
163 | {
|
---|
164 | return !(lhs == rhs);
|
---|
165 | }
|
---|
166 |
|
---|
167 | template<class T, class Allocator>
|
---|
168 | class deque_iterator
|
---|
169 | {
|
---|
170 | public:
|
---|
171 | using size_type = typename deque<T, Allocator>::size_type;
|
---|
172 | using value_type = typename deque<T, Allocator>::value_type;
|
---|
173 | using reference = typename deque<T, Allocator>::reference;
|
---|
174 | using difference_type = size_type;
|
---|
175 | using pointer = value_type*;
|
---|
176 | using iterator_category = random_access_iterator_tag;
|
---|
177 |
|
---|
178 | deque_iterator(deque<T, Allocator>& deq, size_type idx)
|
---|
179 | : deq_{deq}, idx_{idx}
|
---|
180 | { /* DUMMY BODY */ }
|
---|
181 |
|
---|
182 | deque_iterator(const deque_const_iterator<T, Allocator>& other)
|
---|
183 | : deq_{other.deq_}, idx_{other.idx_}
|
---|
184 | { /* DUMMY BODY */ }
|
---|
185 |
|
---|
186 | deque_iterator& operator=(const deque_iterator& other)
|
---|
187 | {
|
---|
188 | deq_ = other.deq_;
|
---|
189 | idx_ = other.idx_;
|
---|
190 |
|
---|
191 | return *this;
|
---|
192 | }
|
---|
193 |
|
---|
194 | deque_iterator& operator=(const deque_const_iterator<T, Allocator>& other)
|
---|
195 | {
|
---|
196 | deq_ = other.deq_;
|
---|
197 | idx_ = other.idx_;
|
---|
198 |
|
---|
199 | return *this;
|
---|
200 | }
|
---|
201 |
|
---|
202 | reference operator*()
|
---|
203 | {
|
---|
204 | return deq_[idx_];
|
---|
205 | }
|
---|
206 |
|
---|
207 | pointer operator->()
|
---|
208 | {
|
---|
209 | return addressof(deq_[idx_]);
|
---|
210 | }
|
---|
211 |
|
---|
212 | deque_iterator& operator++()
|
---|
213 | {
|
---|
214 | ++idx_;
|
---|
215 |
|
---|
216 | return *this;
|
---|
217 | }
|
---|
218 |
|
---|
219 | deque_iterator operator++(int)
|
---|
220 | {
|
---|
221 | return deque_iterator{deq_, idx_++};
|
---|
222 | }
|
---|
223 |
|
---|
224 | deque_iterator& operator--()
|
---|
225 | {
|
---|
226 | --idx_;
|
---|
227 |
|
---|
228 | return *this;
|
---|
229 | }
|
---|
230 |
|
---|
231 | deque_iterator operator--(int)
|
---|
232 | {
|
---|
233 | return deque_iterator{deq_, idx_--};
|
---|
234 | }
|
---|
235 |
|
---|
236 | deque_iterator operator+(difference_type n)
|
---|
237 | {
|
---|
238 | return deque_iterator{deq_, idx_ + n};
|
---|
239 | }
|
---|
240 |
|
---|
241 | deque_iterator& operator+=(difference_type n)
|
---|
242 | {
|
---|
243 | idx_ += n;
|
---|
244 |
|
---|
245 | return *this;
|
---|
246 | }
|
---|
247 |
|
---|
248 | deque_iterator operator-(difference_type n)
|
---|
249 | {
|
---|
250 | return deque_iterator{deq_, idx_ - n};
|
---|
251 | }
|
---|
252 |
|
---|
253 | deque_iterator& operator-=(difference_type n)
|
---|
254 | {
|
---|
255 | idx_ -= n;
|
---|
256 |
|
---|
257 | return *this;
|
---|
258 | }
|
---|
259 |
|
---|
260 | reference operator[](difference_type n) const
|
---|
261 | {
|
---|
262 | return deq_[idx_ + n];
|
---|
263 | }
|
---|
264 |
|
---|
265 | difference_type operator-(const deque_iterator& rhs)
|
---|
266 | {
|
---|
267 | return idx_ - rhs.idx_;
|
---|
268 | }
|
---|
269 |
|
---|
270 | size_type idx() const
|
---|
271 | {
|
---|
272 | return idx_;
|
---|
273 | }
|
---|
274 |
|
---|
275 | private:
|
---|
276 | deque<T, Allocator>& deq_;
|
---|
277 | size_type idx_;
|
---|
278 | };
|
---|
279 |
|
---|
280 | template<class T, class Allocator>
|
---|
281 | bool operator==(const deque_iterator<T, Allocator>& lhs,
|
---|
282 | const deque_iterator<T, Allocator>& rhs)
|
---|
283 | {
|
---|
284 | return lhs.idx() == rhs.idx();
|
---|
285 | }
|
---|
286 |
|
---|
287 | template<class T, class Allocator>
|
---|
288 | bool operator!=(const deque_iterator<T, Allocator>& lhs,
|
---|
289 | const deque_iterator<T, Allocator>& rhs)
|
---|
290 | {
|
---|
291 | return !(lhs == rhs);
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * 23.3.3 deque:
|
---|
297 | */
|
---|
298 |
|
---|
299 | template<class T, class Allocator>
|
---|
300 | class deque
|
---|
301 | {
|
---|
302 | public:
|
---|
303 | using allocator_type = Allocator;
|
---|
304 | using value_type = T;
|
---|
305 | using reference = value_type&;
|
---|
306 | using const_reference = const value_type&;
|
---|
307 |
|
---|
308 | using iterator = aux::deque_iterator<T, Allocator>;
|
---|
309 | using const_iterator = aux::deque_const_iterator<T, Allocator>;
|
---|
310 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
311 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
312 |
|
---|
313 | using size_type = typename allocator_traits<allocator_type>::size_type;
|
---|
314 | using difference_type = typename allocator_traits<allocator_type>::difference_type;
|
---|
315 | using pointer = typename allocator_traits<allocator_type>::pointer;
|
---|
316 | using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * 23.3.3.2, construct/copy/destroy:
|
---|
320 | */
|
---|
321 |
|
---|
322 | deque()
|
---|
323 | : deque{allocator_type{}}
|
---|
324 | { /* DUMMY BODY */ }
|
---|
325 |
|
---|
326 | explicit deque(const allocator_type& alloc)
|
---|
327 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
328 | back_bucket_idx_{0}, front_bucket_{default_front_},
|
---|
329 | back_bucket_{default_back_}, bucket_count_{default_bucket_count_},
|
---|
330 | bucket_capacity_{default_bucket_capacity_}, size_{}, data_{}
|
---|
331 | {
|
---|
332 | init_();
|
---|
333 | }
|
---|
334 |
|
---|
335 | explicit deque(size_type n, const allocator_type& alloc = allocator_type{})
|
---|
336 | : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
|
---|
337 | front_bucket_{}, back_bucket_{}, bucket_count_{},
|
---|
338 | bucket_capacity_{}, size_{n}, data_{}
|
---|
339 | {
|
---|
340 | prepare_for_size_(n);
|
---|
341 | init_();
|
---|
342 |
|
---|
343 | for (size_type i = 0; i < size_; ++i)
|
---|
344 | allocator_.construct(&(*this)[i]);
|
---|
345 | back_bucket_idx_ = size_ % bucket_size_;
|
---|
346 | }
|
---|
347 |
|
---|
348 | deque(size_type n, const value_type& value, const allocator_type& alloc = allocator_type{})
|
---|
349 | : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
|
---|
350 | front_bucket_{}, back_bucket_{}, bucket_count_{},
|
---|
351 | bucket_capacity_{}, size_{n}, data_{}
|
---|
352 | {
|
---|
353 | prepare_for_size_(n);
|
---|
354 | init_();
|
---|
355 |
|
---|
356 | for (size_type i = 0; i < size_; ++i)
|
---|
357 | (*this)[i] = value;
|
---|
358 | back_bucket_idx_ = size_ % bucket_size_;
|
---|
359 | }
|
---|
360 |
|
---|
361 | template<class InputIterator>
|
---|
362 | deque(InputIterator first, InputIterator last,
|
---|
363 | const allocator_type& alloc = allocator_type{})
|
---|
364 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
365 | back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
|
---|
366 | bucket_count_{}, bucket_capacity_{}, size_{},
|
---|
367 | data_{}
|
---|
368 | {
|
---|
369 | copy_from_range_(first, last);
|
---|
370 | }
|
---|
371 |
|
---|
372 | deque(const deque& other)
|
---|
373 | : deque{other.begin(), other.end(), other.allocator_}
|
---|
374 | { /* DUMMY BODY */ }
|
---|
375 |
|
---|
376 | deque(deque&& other)
|
---|
377 | : allocator_{move(other.allocator_)},
|
---|
378 | front_bucket_idx_{other.front_bucket_idx_},
|
---|
379 | back_bucket_idx_{other.front_bucket_idx_},
|
---|
380 | front_bucket_{other.front_bucket_},
|
---|
381 | back_bucket_{other.back_bucket_},
|
---|
382 | bucket_count_{other.bucket_count_},
|
---|
383 | bucket_capacity_{other.bucket_capacity_},
|
---|
384 | size_{other.size_}, data_{other.data_}
|
---|
385 | {
|
---|
386 | other.data_ = nullptr;
|
---|
387 | other.clear();
|
---|
388 | }
|
---|
389 |
|
---|
390 | deque(const deque& other, const allocator_type& alloc)
|
---|
391 | : deque{other.begin(), other.end(), alloc}
|
---|
392 | { /* DUMMY BODY */ }
|
---|
393 |
|
---|
394 | deque(deque&& other, const allocator_type& alloc)
|
---|
395 | : allocator_{alloc},
|
---|
396 | front_bucket_idx_{other.front_bucket_idx_},
|
---|
397 | back_bucket_idx_{other.front_bucket_idx_},
|
---|
398 | front_bucket_{other.front_bucket_},
|
---|
399 | back_bucket_{other.back_bucket_},
|
---|
400 | bucket_count_{other.bucket_count_},
|
---|
401 | bucket_capacity_{other.bucket_capacity_},
|
---|
402 | size_{other.size_}, data_{other.data_}
|
---|
403 | {
|
---|
404 | other.data_ = nullptr;
|
---|
405 | other.clear();
|
---|
406 | }
|
---|
407 |
|
---|
408 | deque(initializer_list<T> init, const allocator_type& alloc = allocator_type{})
|
---|
409 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
410 | back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
|
---|
411 | bucket_count_{}, bucket_capacity_{}, size_{},
|
---|
412 | data_{}
|
---|
413 | {
|
---|
414 | copy_from_range_(init.begin(), init.end());
|
---|
415 | }
|
---|
416 |
|
---|
417 | ~deque()
|
---|
418 | {
|
---|
419 | fini_();
|
---|
420 | }
|
---|
421 |
|
---|
422 | deque& operator=(const deque& other)
|
---|
423 | {
|
---|
424 | // TODO: implement
|
---|
425 | }
|
---|
426 |
|
---|
427 | deque& operator=(deque&& other)
|
---|
428 | noexcept(allocator_traits<allocator_type>::is_always_equal::value)
|
---|
429 | {
|
---|
430 | swap(other);
|
---|
431 | other.clear();
|
---|
432 | }
|
---|
433 |
|
---|
434 | deque& operator=(initializer_list<T> init)
|
---|
435 | {
|
---|
436 | // TODO: implement
|
---|
437 | }
|
---|
438 |
|
---|
439 | template<class InputIterator>
|
---|
440 | void assign(InputIterator first, InputIterator last)
|
---|
441 | {
|
---|
442 | // TODO: implement
|
---|
443 | }
|
---|
444 |
|
---|
445 | void assign(size_type n, const T& value)
|
---|
446 | {
|
---|
447 | // TODO: implement
|
---|
448 | }
|
---|
449 |
|
---|
450 | void assign(initializer_list<T> init)
|
---|
451 | {
|
---|
452 | // TODO: implement
|
---|
453 | }
|
---|
454 |
|
---|
455 | allocator_type get_allocator() const noexcept
|
---|
456 | {
|
---|
457 | return allocator_;
|
---|
458 | }
|
---|
459 |
|
---|
460 | iterator begin() noexcept
|
---|
461 | {
|
---|
462 | return aux::deque_iterator{*this, 0};
|
---|
463 | }
|
---|
464 |
|
---|
465 | const_iterator begin() const noexcept
|
---|
466 | {
|
---|
467 | return aux::deque_const_iterator{*this, 0};
|
---|
468 | }
|
---|
469 |
|
---|
470 | iterator end() noexcept
|
---|
471 | {
|
---|
472 | return aux::deque_iterator{*this, size_};
|
---|
473 | }
|
---|
474 |
|
---|
475 | const_iterator end() const noexcept
|
---|
476 | {
|
---|
477 | return aux::deque_const_iterator{*this, size_};
|
---|
478 | }
|
---|
479 |
|
---|
480 | reverse_iterator rbegin() noexcept
|
---|
481 | {
|
---|
482 | return make_reverse_iterator(end());
|
---|
483 | }
|
---|
484 |
|
---|
485 | const_reverse_iterator rbegin() const noexcept
|
---|
486 | {
|
---|
487 | return make_reverse_iterator(cend());
|
---|
488 | }
|
---|
489 |
|
---|
490 | reverse_iterator rend() noexcept
|
---|
491 | {
|
---|
492 | return make_reverse_iterator(begin());
|
---|
493 | }
|
---|
494 |
|
---|
495 | const_reverse_iterator rend() const noexcept
|
---|
496 | {
|
---|
497 | return make_reverse_iterator(cbegin());
|
---|
498 | }
|
---|
499 |
|
---|
500 | const_iterator cbegin() const noexcept
|
---|
501 | {
|
---|
502 | return aux::deque_const_iterator{*this, 0};
|
---|
503 | }
|
---|
504 |
|
---|
505 | const_iterator cend() const noexcept
|
---|
506 | {
|
---|
507 | return aux::deque_const_iterator{*this, size_};
|
---|
508 | }
|
---|
509 |
|
---|
510 | const_reverse_iterator crbegin() const noexcept
|
---|
511 | {
|
---|
512 | return make_reverse_iterator(cend());
|
---|
513 | }
|
---|
514 |
|
---|
515 | const_reverse_iterator crend() const noexcept
|
---|
516 | {
|
---|
517 | return make_reverse_iterator(cbegin());
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * 23.3.3.3, capacity:
|
---|
522 | */
|
---|
523 |
|
---|
524 | size_type size() const noexcept
|
---|
525 | {
|
---|
526 | return size_;
|
---|
527 | }
|
---|
528 |
|
---|
529 | size_type max_size() const noexcept
|
---|
530 | {
|
---|
531 | // TODO: implement
|
---|
532 | }
|
---|
533 |
|
---|
534 | void resize(size_type sz)
|
---|
535 | {
|
---|
536 | // TODO: implement
|
---|
537 | }
|
---|
538 |
|
---|
539 | void resize(size_type sz, const value_type& value)
|
---|
540 | {
|
---|
541 | // TODO: implement
|
---|
542 | }
|
---|
543 |
|
---|
544 | void shrink_to_fit()
|
---|
545 | {
|
---|
546 | // TODO: implement
|
---|
547 | }
|
---|
548 |
|
---|
549 | bool empty() const noexcept
|
---|
550 | {
|
---|
551 | return size_ == size_type{};
|
---|
552 | }
|
---|
553 |
|
---|
554 | reference operator[](size_type idx)
|
---|
555 | {
|
---|
556 | return data_[get_bucket_index_(idx)][get_element_index_(idx)];
|
---|
557 | }
|
---|
558 |
|
---|
559 | const_reference operator[](size_type idx) const
|
---|
560 | {
|
---|
561 | return data_[get_bucket_index_(idx)][get_element_index_(idx)];
|
---|
562 | }
|
---|
563 |
|
---|
564 | reference at(size_type idx)
|
---|
565 | {
|
---|
566 | // TODO: bounds checking
|
---|
567 | return operator[](idx);
|
---|
568 | }
|
---|
569 |
|
---|
570 | const_reference at(size_type idx) const
|
---|
571 | {
|
---|
572 | // TODO: bounds checking
|
---|
573 | return operator[](idx);
|
---|
574 | }
|
---|
575 |
|
---|
576 | reference front()
|
---|
577 | {
|
---|
578 | return *begin();
|
---|
579 | }
|
---|
580 |
|
---|
581 | const_reference front() const
|
---|
582 | {
|
---|
583 | return *cbegin();
|
---|
584 | }
|
---|
585 |
|
---|
586 | reference back()
|
---|
587 | {
|
---|
588 | auto tmp = end();
|
---|
589 |
|
---|
590 | return *(--tmp);
|
---|
591 | }
|
---|
592 |
|
---|
593 | const_reference back() const
|
---|
594 | {
|
---|
595 | auto tmp = cend();
|
---|
596 |
|
---|
597 | return *(--tmp);
|
---|
598 | }
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * 23.3.3.4, modifiers:
|
---|
602 | */
|
---|
603 |
|
---|
604 | template<class... Args>
|
---|
605 | void emplace_front(Args&&... args)
|
---|
606 | {
|
---|
607 | // TODO: implement
|
---|
608 | }
|
---|
609 |
|
---|
610 | template<class... Args>
|
---|
611 | void emplace_back(Args&&... args)
|
---|
612 | {
|
---|
613 | // TODO: implement
|
---|
614 | }
|
---|
615 |
|
---|
616 | template<class... Args>
|
---|
617 | iterator emplace(const_iterator position, Args&&... args)
|
---|
618 | {
|
---|
619 | // TODO: implement
|
---|
620 | }
|
---|
621 |
|
---|
622 | void push_front(const value_type& value)
|
---|
623 | {
|
---|
624 | if (front_bucket_idx_ == 0)
|
---|
625 | add_new_bucket_front_();
|
---|
626 |
|
---|
627 | data_[front_bucket_][--front_bucket_idx_] = value;
|
---|
628 | ++size_;
|
---|
629 | }
|
---|
630 |
|
---|
631 | void push_front(value_type&& value)
|
---|
632 | {
|
---|
633 | if (front_bucket_idx_ == 0)
|
---|
634 | add_new_bucket_front_();
|
---|
635 |
|
---|
636 | data_[front_bucket_][--front_bucket_idx_] = forward<value_type>(value);
|
---|
637 | ++size_;
|
---|
638 | }
|
---|
639 |
|
---|
640 | void push_back(const value_type& value)
|
---|
641 | {
|
---|
642 | data_[back_bucket_][back_bucket_idx_++] = value;
|
---|
643 | ++size_;
|
---|
644 |
|
---|
645 | if (back_bucket_idx_ >= bucket_size_)
|
---|
646 | add_new_bucket_back_();
|
---|
647 | }
|
---|
648 |
|
---|
649 | void push_back(value_type&& value)
|
---|
650 | {
|
---|
651 | data_[back_bucket_][back_bucket_idx_++] = forward<value_type>(value);
|
---|
652 | ++size_;
|
---|
653 |
|
---|
654 | if (back_bucket_idx_ >= bucket_size_)
|
---|
655 | add_new_bucket_back_();
|
---|
656 | }
|
---|
657 |
|
---|
658 | iterator insert(const_iterator position, const value_type& value)
|
---|
659 | {
|
---|
660 | // TODO: implement
|
---|
661 | }
|
---|
662 |
|
---|
663 | iterator insert(const_iterator position, value_type&& value)
|
---|
664 | {
|
---|
665 | // TODO: implement
|
---|
666 | }
|
---|
667 |
|
---|
668 | iterator insert(const_iterator position, size_type n, const value_type& value)
|
---|
669 | {
|
---|
670 | // TODO: implement
|
---|
671 | }
|
---|
672 |
|
---|
673 | template<class InputIterator>
|
---|
674 | iterator insert(const_iterator position, InputIterator first, InputIterator last)
|
---|
675 | {
|
---|
676 | // TODO: implement
|
---|
677 | }
|
---|
678 |
|
---|
679 | iterator insert(const_iterator position, initializer_list<value_type> init)
|
---|
680 | {
|
---|
681 | // TODO: implement
|
---|
682 | }
|
---|
683 |
|
---|
684 | void pop_back()
|
---|
685 | {
|
---|
686 | if (empty())
|
---|
687 | return;
|
---|
688 |
|
---|
689 | if (back_bucket_idx_ == 0)
|
---|
690 | { // Means we gotta pop data_[front_bucket_][bucket_size_ - 1]!
|
---|
691 | if (data_[back_bucket_])
|
---|
692 | allocator_.deallocate(data_[back_bucket_], bucket_size_);
|
---|
693 |
|
---|
694 | --back_bucket_;
|
---|
695 | back_bucket_idx_ = bucket_size_ - 1;
|
---|
696 | }
|
---|
697 | else
|
---|
698 | --back_bucket_idx_;
|
---|
699 |
|
---|
700 | allocator_.destroy(&data_[back_bucket_][back_bucket_idx_]);
|
---|
701 | --size_;
|
---|
702 | }
|
---|
703 |
|
---|
704 | void pop_front()
|
---|
705 | {
|
---|
706 | if (empty())
|
---|
707 | return;
|
---|
708 |
|
---|
709 | if (front_bucket_idx_ >= bucket_size_)
|
---|
710 | { // Means we gotta pop data_[front_bucket_][0]!
|
---|
711 | if (data_[front_bucket_])
|
---|
712 | allocator_.deallocate(data_[front_bucket_], bucket_size_);
|
---|
713 |
|
---|
714 | ++front_bucket_;
|
---|
715 | front_bucket_idx_ = 1;
|
---|
716 |
|
---|
717 | allocator_.destroy(&data_[front_bucket_][0]);
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | allocator_.destroy(&data_[front_bucket_][front_bucket_idx_]);
|
---|
722 |
|
---|
723 | ++front_bucket_idx_;
|
---|
724 | }
|
---|
725 |
|
---|
726 | --size_;
|
---|
727 | }
|
---|
728 |
|
---|
729 | iterator erase(const_iterator position)
|
---|
730 | {
|
---|
731 | // TODO: implement
|
---|
732 | }
|
---|
733 |
|
---|
734 | iterator erase(const_iterator first, const_iterator last)
|
---|
735 | {
|
---|
736 | // TODO: implement
|
---|
737 | }
|
---|
738 |
|
---|
739 | void swap(deque& other)
|
---|
740 | noexcept(allocator_traits<allocator_type>::is_always_equal::value)
|
---|
741 | {
|
---|
742 | // TODO: implement
|
---|
743 | }
|
---|
744 |
|
---|
745 | void clear() noexcept
|
---|
746 | {
|
---|
747 | if (data_)
|
---|
748 | fini_();
|
---|
749 |
|
---|
750 | front_bucket_ = default_front_;
|
---|
751 | back_bucket_ = default_back_;
|
---|
752 | bucket_count_ = default_bucket_count_;
|
---|
753 | bucket_capacity_ = default_bucket_capacity_;
|
---|
754 | size_ = size_type{};
|
---|
755 |
|
---|
756 | init_();
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* private: */
|
---|
760 | allocator_type allocator_;
|
---|
761 |
|
---|
762 | /**
|
---|
763 | * Note: In our implementation, front_bucket_idx_
|
---|
764 | * points at the first element and back_bucket_idx_
|
---|
765 | * points at the one past last element. Because of this,
|
---|
766 | * some operations may be done in inverse order
|
---|
767 | * depending on the side they are applied to.
|
---|
768 | */
|
---|
769 | size_type front_bucket_idx_;
|
---|
770 | size_type back_bucket_idx_;
|
---|
771 | size_type front_bucket_;
|
---|
772 | size_type back_bucket_;
|
---|
773 |
|
---|
774 | static constexpr size_type bucket_size_{16};
|
---|
775 | static constexpr size_type default_bucket_count_{2};
|
---|
776 | static constexpr size_type default_bucket_capacity_{4};
|
---|
777 | static constexpr size_type default_front_{1};
|
---|
778 | static constexpr size_type default_back_{2};
|
---|
779 |
|
---|
780 | size_type bucket_count_;
|
---|
781 | size_type bucket_capacity_;
|
---|
782 | size_type size_{};
|
---|
783 |
|
---|
784 | value_type** data_;
|
---|
785 |
|
---|
786 | void init_()
|
---|
787 | {
|
---|
788 | data_ = new value_type*[bucket_capacity_];
|
---|
789 |
|
---|
790 | for (size_type i = front_bucket_; i <= back_bucket_; ++i)
|
---|
791 | data_[i] = allocator_.allocate(bucket_size_);
|
---|
792 | }
|
---|
793 |
|
---|
794 | void prepare_for_size_(size_type size)
|
---|
795 | {
|
---|
796 | if (data_)
|
---|
797 | fini_();
|
---|
798 |
|
---|
799 | if (size < bucket_size_) // Always front_bucket_ != back_bucket_.
|
---|
800 | bucket_count_ = bucket_capacity_ = 2;
|
---|
801 | else if (size % bucket_size_ == 0)
|
---|
802 | bucket_count_ = bucket_capacity_ = size / bucket_size_ + 1;
|
---|
803 | else
|
---|
804 | bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2;
|
---|
805 |
|
---|
806 | front_bucket_ = 0;
|
---|
807 | back_bucket_ = bucket_capacity_ - 1;
|
---|
808 | }
|
---|
809 |
|
---|
810 | template<class Iterator>
|
---|
811 | void copy_from_range_(Iterator first, Iterator last)
|
---|
812 | {
|
---|
813 | size_ = distance(first, last);
|
---|
814 | prepare_for_size_(size_);
|
---|
815 | init_();
|
---|
816 |
|
---|
817 | auto it = begin();
|
---|
818 | while (first != last)
|
---|
819 | *it++ = *first++;
|
---|
820 |
|
---|
821 | // Remainder is the amount of elements in the last bucket.
|
---|
822 | back_bucket_idx_ = size_ % bucket_size_;
|
---|
823 | }
|
---|
824 |
|
---|
825 | void fini_()
|
---|
826 | {
|
---|
827 | for (size_type i = front_bucket_; i <= back_bucket_; ++i)
|
---|
828 | allocator_.deallocate(data_[i], bucket_size_);
|
---|
829 |
|
---|
830 | delete[] data_;
|
---|
831 | data_ = nullptr;
|
---|
832 | }
|
---|
833 |
|
---|
834 | bool has_bucket_space_back_() const
|
---|
835 | {
|
---|
836 | return back_bucket_ < bucket_capacity_ - 1;
|
---|
837 | }
|
---|
838 |
|
---|
839 | bool has_bucket_space_front_() const
|
---|
840 | {
|
---|
841 | return front_bucket_ > 0;
|
---|
842 | }
|
---|
843 |
|
---|
844 | void add_new_bucket_back_()
|
---|
845 | {
|
---|
846 | if (!has_bucket_space_back_())
|
---|
847 | expand_();
|
---|
848 |
|
---|
849 | ++back_bucket_;
|
---|
850 | data_[back_bucket_] = allocator_.allocate(bucket_size_);
|
---|
851 | back_bucket_idx_ = size_type{};
|
---|
852 | }
|
---|
853 |
|
---|
854 | void add_new_bucket_front_()
|
---|
855 | {
|
---|
856 | if (!has_bucket_space_front_())
|
---|
857 | expand_();
|
---|
858 |
|
---|
859 | --front_bucket_;
|
---|
860 | data_[front_bucket_] = allocator_.allocate(bucket_size_);
|
---|
861 | front_bucket_idx_ = bucket_size_;
|
---|
862 | }
|
---|
863 |
|
---|
864 | void expand_()
|
---|
865 | {
|
---|
866 | bucket_capacity_ *= 2;
|
---|
867 | value_type** new_data = new value_type*[bucket_capacity_];
|
---|
868 |
|
---|
869 | size_type new_front = bucket_capacity_ / 4;
|
---|
870 | size_type new_back = bucket_capacity_ - bucket_capacity_ / 4 - 1;
|
---|
871 |
|
---|
872 | for (size_type i = new_front, j = front_bucket_; i <= new_back; ++i, ++j)
|
---|
873 | new_data[i] = move(data_[j]);
|
---|
874 | std::swap(data_, new_data);
|
---|
875 |
|
---|
876 | delete[] new_data;
|
---|
877 | front_bucket_ = new_front;
|
---|
878 | back_bucket_ = new_back;
|
---|
879 | }
|
---|
880 |
|
---|
881 | size_type get_bucket_index_(size_type idx) const
|
---|
882 | {
|
---|
883 | if (idx < elements_in_front_bucket_())
|
---|
884 | return front_bucket_;
|
---|
885 |
|
---|
886 | idx -= elements_in_front_bucket_();
|
---|
887 | return idx / bucket_size_ + front_bucket_ + 1;
|
---|
888 | }
|
---|
889 |
|
---|
890 | size_type get_element_index_(size_type idx) const
|
---|
891 | {
|
---|
892 | if (idx < elements_in_front_bucket_())
|
---|
893 | return bucket_size_ - elements_in_front_bucket_() + idx;
|
---|
894 |
|
---|
895 | idx -= elements_in_front_bucket_();
|
---|
896 | return idx % bucket_size_;
|
---|
897 | }
|
---|
898 |
|
---|
899 | size_type elements_in_front_bucket_() const
|
---|
900 | {
|
---|
901 | return bucket_size_ - front_bucket_idx_;
|
---|
902 | }
|
---|
903 | };
|
---|
904 |
|
---|
905 | template<class T, class Allocator>
|
---|
906 | bool operator==(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
907 | {
|
---|
908 | // TODO: implement
|
---|
909 | return false;
|
---|
910 | }
|
---|
911 |
|
---|
912 | template<class T, class Allocator>
|
---|
913 | bool operator<(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
914 | {
|
---|
915 | // TODO: implement
|
---|
916 | return false;
|
---|
917 | }
|
---|
918 |
|
---|
919 | template<class T, class Allocator>
|
---|
920 | bool operator!=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
921 | {
|
---|
922 | // TODO: implement
|
---|
923 | return false;
|
---|
924 | }
|
---|
925 |
|
---|
926 | template<class T, class Allocator>
|
---|
927 | bool operator>(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
928 | {
|
---|
929 | // TODO: implement
|
---|
930 | return false;
|
---|
931 | }
|
---|
932 |
|
---|
933 | template<class T, class Allocator>
|
---|
934 | bool operator<=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
935 | {
|
---|
936 | // TODO: implement
|
---|
937 | return false;
|
---|
938 | }
|
---|
939 |
|
---|
940 | template<class T, class Allocator>
|
---|
941 | bool operator>=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
942 | {
|
---|
943 | // TODO: implement
|
---|
944 | return false;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /**
|
---|
948 | * 23.3.3.5, deque specialized algorithms:
|
---|
949 | */
|
---|
950 |
|
---|
951 | template<class T, class Allocator>
|
---|
952 | void swap(deque<T, Allocator>& lhs, deque<T, Allocator>& rhs)
|
---|
953 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
954 | {
|
---|
955 | lhs.swap(rhs);
|
---|
956 | }
|
---|
957 | }
|
---|
958 |
|
---|
959 | #endif
|
---|