source: mainline/uspace/lib/cpp/include/impl/deque.hpp@ 46cf7bf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 46cf7bf was 3f7031a, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: fixed deque::deque(deque&&) typo causing push to fail after move

  • Property mode set to 100644
File size: 37.5 KB
Line 
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 <algorithm>
33#include <initializer_list>
34#include <internal/insert_iterator.hpp>
35#include <iterator>
36#include <memory>
37
38namespace std
39{
40 template<class T, class Allocator = allocator<T>>
41 class deque;
42
43 namespace aux
44 {
45 /**
46 * Note: We decided that these iterators contain a
47 * reference to the container and an index, which
48 * allows us to use the already implemented operator[]
49 * on deque and also allows us to conform to the requirement
50 * of the standard that functions such as push_back
51 * invalidate the .end() iterator.
52 */
53
54 template<class T, class Allocator>
55 class deque_iterator;
56
57 template<class T, class Allocator>
58 class deque_const_iterator
59 {
60 public:
61 using size_type = typename deque<T, Allocator>::size_type;
62 using value_type = typename deque<T, Allocator>::value_type;
63 using reference = typename deque<T, Allocator>::const_reference;
64 using difference_type = size_type;
65 using pointer = const value_type*;
66 using iterator_category = random_access_iterator_tag;
67
68 deque_const_iterator(const deque<T, Allocator>& deq, size_type idx)
69 : deq_{deq}, idx_{idx}
70 { /* DUMMY BODY */ }
71
72 deque_const_iterator(const deque_const_iterator& other)
73 : deq_{other.deq_}, idx_{other.idx_}
74 { /* DUMMY BODY */ }
75
76 deque_const_iterator& operator=(const deque_const_iterator& other)
77 {
78 deq_ = other.deq_;
79 idx_ = other.idx_;
80
81 return *this;
82 }
83
84 reference operator*() const
85 {
86 return deq_[idx_];
87 }
88
89 pointer operator->() const
90 {
91 return addressof(deq_[idx_]);
92 }
93
94 deque_const_iterator& operator++()
95 {
96 ++idx_;
97
98 return *this;
99 }
100
101 deque_const_iterator operator++(int)
102 {
103 return deque_const_iterator{deq_, idx_++};
104 }
105
106 deque_const_iterator& operator--()
107 {
108 --idx_;
109
110 return *this;
111 }
112
113 deque_const_iterator operator--(int)
114 {
115 return deque_const_iterator{deq_, idx_--};
116 }
117
118 deque_const_iterator operator+(difference_type n)
119 {
120 return deque_const_iterator{deq_, idx_ + n};
121 }
122
123 deque_const_iterator& operator+=(difference_type n)
124 {
125 idx_ += n;
126
127 return *this;
128 }
129
130 deque_const_iterator operator-(difference_type n)
131 {
132 return deque_const_iterator{deq_, idx_ - n};
133 }
134
135 deque_const_iterator& operator-=(difference_type n)
136 {
137 idx_ -= n;
138
139 return *this;
140 }
141
142 reference operator[](difference_type n) const
143 {
144 return deq_[idx_ + n];
145 }
146
147 difference_type operator-(const deque_const_iterator& rhs)
148 {
149 return idx_ - rhs.idx_;
150 }
151
152 size_type idx() const
153 {
154 return idx_;
155 }
156
157 operator deque_iterator<T, Allocator>()
158 {
159 return deque_iterator{
160 const_cast<deque<T, Allocator>&>(deq_), idx_
161 };
162 }
163
164 private:
165 const deque<T, Allocator>& deq_;
166 size_type idx_;
167 };
168
169 template<class T, class Allocator>
170 bool operator==(const deque_const_iterator<T, Allocator>& lhs,
171 const deque_const_iterator<T, Allocator>& rhs)
172 {
173 return lhs.idx() == rhs.idx();
174 }
175
176 template<class T, class Allocator>
177 bool operator!=(const deque_const_iterator<T, Allocator>& lhs,
178 const deque_const_iterator<T, Allocator>& rhs)
179 {
180 return !(lhs == rhs);
181 }
182
183 template<class T, class Allocator>
184 class deque_iterator
185 {
186 public:
187 using size_type = typename deque<T, Allocator>::size_type;
188 using value_type = typename deque<T, Allocator>::value_type;
189 using reference = typename deque<T, Allocator>::reference;
190 using difference_type = size_type;
191 using pointer = value_type*;
192 using iterator_category = random_access_iterator_tag;
193
194 deque_iterator(deque<T, Allocator>& deq, size_type idx)
195 : deq_{deq}, idx_{idx}
196 { /* DUMMY BODY */ }
197
198 deque_iterator(const deque_iterator& other)
199 : deq_{other.deq_}, idx_{other.idx_}
200 { /* DUMMY BODY */ }
201
202 deque_iterator(const deque_const_iterator<T, Allocator>& other)
203 : deq_{other.deq_}, idx_{other.idx_}
204 { /* DUMMY BODY */ }
205
206 deque_iterator& operator=(const deque_iterator& other)
207 {
208 deq_ = other.deq_;
209 idx_ = other.idx_;
210
211 return *this;
212 }
213
214 deque_iterator& operator=(const deque_const_iterator<T, Allocator>& other)
215 {
216 deq_ = other.deq_;
217 idx_ = other.idx_;
218
219 return *this;
220 }
221
222 reference operator*()
223 {
224 return deq_[idx_];
225 }
226
227 pointer operator->()
228 {
229 return addressof(deq_[idx_]);
230 }
231
232 deque_iterator& operator++()
233 {
234 ++idx_;
235
236 return *this;
237 }
238
239 deque_iterator operator++(int)
240 {
241 return deque_iterator{deq_, idx_++};
242 }
243
244 deque_iterator& operator--()
245 {
246 --idx_;
247
248 return *this;
249 }
250
251 deque_iterator operator--(int)
252 {
253 return deque_iterator{deq_, idx_--};
254 }
255
256 deque_iterator operator+(difference_type n)
257 {
258 return deque_iterator{deq_, idx_ + n};
259 }
260
261 deque_iterator& operator+=(difference_type n)
262 {
263 idx_ += n;
264
265 return *this;
266 }
267
268 deque_iterator operator-(difference_type n)
269 {
270 return deque_iterator{deq_, idx_ - n};
271 }
272
273 deque_iterator& operator-=(difference_type n)
274 {
275 idx_ -= n;
276
277 return *this;
278 }
279
280 reference operator[](difference_type n) const
281 {
282 return deq_[idx_ + n];
283 }
284
285 difference_type operator-(const deque_iterator& rhs)
286 {
287 return idx_ - rhs.idx_;
288 }
289
290 size_type idx() const
291 {
292 return idx_;
293 }
294
295 operator deque_const_iterator<T, Allocator>()
296 {
297 return deque_const_iterator{deq_, idx_};
298 }
299
300 private:
301 deque<T, Allocator>& deq_;
302 size_type idx_;
303 };
304
305 template<class T, class Allocator>
306 bool operator==(const deque_iterator<T, Allocator>& lhs,
307 const deque_iterator<T, Allocator>& rhs)
308 {
309 return lhs.idx() == rhs.idx();
310 }
311
312 template<class T, class Allocator>
313 bool operator!=(const deque_iterator<T, Allocator>& lhs,
314 const deque_iterator<T, Allocator>& rhs)
315 {
316 return !(lhs == rhs);
317 }
318 }
319
320 /**
321 * 23.3.3 deque:
322 */
323
324 template<class T, class Allocator>
325 class deque
326 {
327 public:
328 using allocator_type = Allocator;
329 using value_type = T;
330 using reference = value_type&;
331 using const_reference = const value_type&;
332
333 using iterator = aux::deque_iterator<T, Allocator>;
334 using const_iterator = aux::deque_const_iterator<T, Allocator>;
335 using reverse_iterator = std::reverse_iterator<iterator>;
336 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
337
338 using size_type = typename allocator_traits<allocator_type>::size_type;
339 using difference_type = typename allocator_traits<allocator_type>::difference_type;
340 using pointer = typename allocator_traits<allocator_type>::pointer;
341 using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
342
343 /**
344 * 23.3.3.2, construct/copy/destroy:
345 */
346
347 deque()
348 : deque{allocator_type{}}
349 { /* DUMMY BODY */ }
350
351 explicit deque(const allocator_type& alloc)
352 : allocator_{alloc}, front_bucket_idx_{bucket_size_},
353 back_bucket_idx_{0}, front_bucket_{default_front_},
354 back_bucket_{default_back_}, bucket_count_{default_bucket_count_},
355 bucket_capacity_{default_bucket_capacity_}, size_{}, data_{}
356 {
357 init_();
358 }
359
360 explicit deque(size_type n, const allocator_type& alloc = allocator_type{})
361 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
362 front_bucket_{}, back_bucket_{}, bucket_count_{},
363 bucket_capacity_{}, size_{n}, data_{}
364 {
365 prepare_for_size_(n);
366 init_();
367
368 for (size_type i = 0; i < size_; ++i)
369 allocator_.construct(&(*this)[i]);
370 back_bucket_idx_ = size_ % bucket_size_;
371 }
372
373 deque(size_type n, const value_type& value, const allocator_type& alloc = allocator_type{})
374 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
375 front_bucket_{}, back_bucket_{}, bucket_count_{},
376 bucket_capacity_{}, size_{n}, data_{}
377 {
378 prepare_for_size_(n);
379 init_();
380
381 for (size_type i = 0; i < size_; ++i)
382 (*this)[i] = value;
383 back_bucket_idx_ = size_ % bucket_size_;
384 }
385
386 template<class InputIterator>
387 deque(InputIterator first, InputIterator last,
388 const allocator_type& alloc = allocator_type{})
389 : allocator_{alloc}, front_bucket_idx_{bucket_size_},
390 back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
391 bucket_count_{}, bucket_capacity_{}, size_{},
392 data_{}
393 {
394 copy_from_range_(first, last);
395 }
396
397 deque(const deque& other)
398 : deque{other.begin(), other.end(), other.allocator_}
399 { /* DUMMY BODY */ }
400
401 deque(deque&& other)
402 : allocator_{move(other.allocator_)},
403 front_bucket_idx_{other.front_bucket_idx_},
404 back_bucket_idx_{other.back_bucket_idx_},
405 front_bucket_{other.front_bucket_},
406 back_bucket_{other.back_bucket_},
407 bucket_count_{other.bucket_count_},
408 bucket_capacity_{other.bucket_capacity_},
409 size_{other.size_}, data_{other.data_}
410 {
411 other.data_ = nullptr;
412 other.clear();
413 }
414
415 deque(const deque& other, const allocator_type& alloc)
416 : deque{other.begin(), other.end(), alloc}
417 { /* DUMMY BODY */ }
418
419 deque(deque&& other, const allocator_type& alloc)
420 : allocator_{alloc},
421 front_bucket_idx_{other.front_bucket_idx_},
422 back_bucket_idx_{other.front_bucket_idx_},
423 front_bucket_{other.front_bucket_},
424 back_bucket_{other.back_bucket_},
425 bucket_count_{other.bucket_count_},
426 bucket_capacity_{other.bucket_capacity_},
427 size_{other.size_}, data_{other.data_}
428 {
429 other.data_ = nullptr;
430 other.clear();
431 }
432
433 deque(initializer_list<T> init, const allocator_type& alloc = allocator_type{})
434 : allocator_{alloc}, front_bucket_idx_{bucket_size_},
435 back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
436 bucket_count_{}, bucket_capacity_{}, size_{},
437 data_{}
438 {
439 copy_from_range_(init.begin(), init.end());
440 }
441
442 ~deque()
443 {
444 fini_();
445 }
446
447 deque& operator=(const deque& other)
448 {
449 if (data_)
450 fini_();
451
452 copy_from_range_(other.begin(), other.end());
453
454 return *this;
455 }
456
457 deque& operator=(deque&& other)
458 noexcept(allocator_traits<allocator_type>::is_always_equal::value)
459 {
460 swap(other);
461 other.clear();
462 }
463
464 deque& operator=(initializer_list<T> init)
465 {
466 if (data_)
467 fini_();
468
469 copy_from_range_(init.begin(), init.end());
470
471 return *this;
472 }
473
474 template<class InputIterator>
475 void assign(InputIterator first, InputIterator last)
476 {
477 copy_from_range_(first, last);
478 }
479
480 void assign(size_type n, const T& value)
481 {
482 prepare_for_size_(n);
483 init_();
484 size_ = n;
485
486 auto it = begin();
487 for (size_type i = size_type{}; i < n; ++i)
488 *it++ = value;
489 }
490
491 void assign(initializer_list<T> init)
492 {
493 copy_from_range_(init.begin(), init.end());
494 }
495
496 allocator_type get_allocator() const noexcept
497 {
498 return allocator_;
499 }
500
501 iterator begin() noexcept
502 {
503 return aux::deque_iterator{*this, 0};
504 }
505
506 const_iterator begin() const noexcept
507 {
508 return aux::deque_const_iterator{*this, 0};
509 }
510
511 iterator end() noexcept
512 {
513 return aux::deque_iterator{*this, size_};
514 }
515
516 const_iterator end() const noexcept
517 {
518 return aux::deque_const_iterator{*this, size_};
519 }
520
521 reverse_iterator rbegin() noexcept
522 {
523 return make_reverse_iterator(end());
524 }
525
526 const_reverse_iterator rbegin() const noexcept
527 {
528 return make_reverse_iterator(cend());
529 }
530
531 reverse_iterator rend() noexcept
532 {
533 return make_reverse_iterator(begin());
534 }
535
536 const_reverse_iterator rend() const noexcept
537 {
538 return make_reverse_iterator(cbegin());
539 }
540
541 const_iterator cbegin() const noexcept
542 {
543 return aux::deque_const_iterator{*this, 0};
544 }
545
546 const_iterator cend() const noexcept
547 {
548 return aux::deque_const_iterator{*this, size_};
549 }
550
551 const_reverse_iterator crbegin() const noexcept
552 {
553 return make_reverse_iterator(cend());
554 }
555
556 const_reverse_iterator crend() const noexcept
557 {
558 return make_reverse_iterator(cbegin());
559 }
560
561 /**
562 * 23.3.3.3, capacity:
563 */
564
565 size_type size() const noexcept
566 {
567 return size_;
568 }
569
570 size_type max_size() const noexcept
571 {
572 return allocator_traits<allocator_type>::max_size(allocator_);
573 }
574
575 void resize(size_type sz)
576 {
577 if (sz <= size_)
578 {
579 for (size_type i = 0; i < size_ - sz; ++i)
580 pop_back();
581 }
582 else
583 {
584 value_type value{};
585 for (size_type i = 0; i < sz - size_; ++i)
586 push_back(value);
587 }
588 }
589
590 void resize(size_type sz, const value_type& value)
591 {
592 if (sz <= size_)
593 {
594 for (size_type i = 0; i < size_ - sz; ++i)
595 pop_back();
596 }
597 else
598 {
599 for (size_type i = 0; i < sz - size_; ++i)
600 push_back(value);
601 }
602 }
603
604 void shrink_to_fit()
605 {
606 /**
607 * We lazily allocate buckets and eagerily deallocate them.
608 * We cannot go into smaller pieces as buckets have fixed size.
609 * Because of this, shrink_to_fit has no effect (as permitted
610 * by the standard).
611 */
612 }
613
614 bool empty() const noexcept
615 {
616 return size_ == size_type{};
617 }
618
619 reference operator[](size_type idx)
620 {
621 return data_[get_bucket_index_(idx)][get_element_index_(idx)];
622 }
623
624 const_reference operator[](size_type idx) const
625 {
626 return data_[get_bucket_index_(idx)][get_element_index_(idx)];
627 }
628
629 reference at(size_type idx)
630 {
631 // TODO: bounds checking
632 return operator[](idx);
633 }
634
635 const_reference at(size_type idx) const
636 {
637 // TODO: bounds checking
638 return operator[](idx);
639 }
640
641 reference front()
642 {
643 return *begin();
644 }
645
646 const_reference front() const
647 {
648 return *cbegin();
649 }
650
651 reference back()
652 {
653 auto tmp = end();
654
655 return *(--tmp);
656 }
657
658 const_reference back() const
659 {
660 auto tmp = cend();
661
662 return *(--tmp);
663 }
664
665 /**
666 * 23.3.3.4, modifiers:
667 */
668
669 template<class... Args>
670 void emplace_front(Args&&... args)
671 {
672 if (front_bucket_idx_ == 0)
673 add_new_bucket_front_();
674
675 allocator_traits<allocator_type>::construct(
676 allocator_,
677 &data_[front_bucket_][--front_bucket_idx_],
678 forward<Args>(args)...
679 );
680
681 ++size_;
682 }
683
684 template<class... Args>
685 void emplace_back(Args&&... args)
686 {
687 allocator_traits<allocator_type>::construct(
688 allocator_,
689 &data_[back_bucket_][back_bucket_idx_++],
690 forward<Args>(args)...
691 );
692
693 ++size_;
694
695 if (back_bucket_idx_ >= bucket_size_)
696 add_new_bucket_back_();
697 }
698
699 template<class... Args>
700 iterator emplace(const_iterator position, Args&&... args)
701 {
702 auto idx = position.idx();
703 shift_right_(idx, 1);
704
705 allocator_traits<allocator_type>::construct(
706 allocator_,
707 &data_[get_bucket_index_(idx)][get_element_index_(idx)],
708 forward<Args>(args)...
709 );
710 ++size_;
711
712 return iterator{*this, idx};
713 }
714
715 void push_front(const value_type& value)
716 {
717 if (front_bucket_idx_ == 0)
718 add_new_bucket_front_();
719
720 data_[front_bucket_][--front_bucket_idx_] = value;
721 ++size_;
722 }
723
724 void push_front(value_type&& value)
725 {
726 if (front_bucket_idx_ == 0)
727 add_new_bucket_front_();
728
729 data_[front_bucket_][--front_bucket_idx_] = forward<value_type>(value);
730 ++size_;
731 }
732
733 void push_back(const value_type& value)
734 {
735 data_[back_bucket_][back_bucket_idx_++] = value;
736 ++size_;
737
738 if (back_bucket_idx_ >= bucket_size_)
739 add_new_bucket_back_();
740 }
741
742 void push_back(value_type&& value)
743 {
744 data_[back_bucket_][back_bucket_idx_++] = forward<value_type>(value);
745 ++size_;
746
747 if (back_bucket_idx_ >= bucket_size_)
748 add_new_bucket_back_();
749 }
750
751 iterator insert(const_iterator position, const value_type& value)
752 {
753 auto idx = position.idx();
754 shift_right_(idx, 1);
755
756 /**
757 * Note: One may notice that when working with the deque
758 * iterator, we use its index without any checks.
759 * This is because a valid iterator will always have
760 * a valid index as functions like pop_back or erase
761 * invalidate iterators.
762 */
763 data_[get_bucket_index_(idx)][get_element_index_(idx)] = value;
764 ++size_;
765
766 return iterator{*this, idx};
767 }
768
769 iterator insert(const_iterator position, value_type&& value)
770 {
771 auto idx = position.idx();
772 shift_right_(idx, 1);
773
774 data_[get_bucket_index_(idx)][get_element_index_(idx)] = forward<value_type>(value);
775 ++size_;
776
777 return iterator{*this, idx};
778 }
779
780 iterator insert(const_iterator position, size_type n, const value_type& value)
781 {
782 return insert(
783 position,
784 aux::insert_iterator{0u, value},
785 aux::insert_iterator{n}
786 );
787 }
788
789 template<class InputIterator>
790 iterator insert(const_iterator position, InputIterator first, InputIterator last)
791 {
792 auto idx = position.idx();
793 auto count = distance(first, last);
794
795 if (idx < size_ / 2)
796 {
797 shift_left_(idx, count);
798
799 iterator it{*this, idx - 1};
800 while (first != last)
801 *it++ = *first++;
802 }
803 else
804 {
805 shift_right_(idx, count);
806
807 iterator it{*this, idx};
808 while (first != last)
809 *it++ = *first++;
810 }
811
812 size_ += count;
813
814 return iterator{*this, idx};
815 }
816
817 iterator insert(const_iterator position, initializer_list<value_type> init)
818 {
819 return insert(position, init.begin(), init.end());
820 }
821
822 void pop_back()
823 {
824 if (empty())
825 return;
826
827 if (back_bucket_idx_ == 0)
828 { // Means we gotta pop data_[back_bucket_ - 1][bucket_size_ - 1]!
829 if (data_[back_bucket_])
830 allocator_.deallocate(data_[back_bucket_], bucket_size_);
831
832 --back_bucket_;
833 back_bucket_idx_ = bucket_size_ - 1;
834 }
835 else
836 --back_bucket_idx_;
837
838 allocator_.destroy(&data_[back_bucket_][back_bucket_idx_]);
839 --size_;
840 }
841
842 void pop_front()
843 {
844 if (empty())
845 return;
846
847 if (front_bucket_idx_ >= bucket_size_)
848 { // Means we gotta pop data_[front_bucket_ + 1][0]!
849 if (data_[front_bucket_])
850 allocator_.deallocate(data_[front_bucket_], bucket_size_);
851
852 ++front_bucket_;
853 front_bucket_idx_ = 1;
854
855 allocator_.destroy(&data_[front_bucket_][0]);
856 }
857 else
858 {
859 allocator_.destroy(&data_[front_bucket_][front_bucket_idx_]);
860
861 ++front_bucket_idx_;
862 }
863
864 --size_;
865 }
866
867 iterator erase(const_iterator position)
868 {
869 auto idx = position.idx();
870 copy(
871 iterator{*this, idx + 1},
872 end(),
873 iterator{*this, idx}
874 );
875
876 /**
877 * Note: We need to not only decrement size,
878 * but also take care of any issues caused
879 * by decrement bucket indices, which pop_back
880 * does for us.
881 */
882 pop_back();
883
884 return iterator{*this, idx};
885 }
886
887 iterator erase(const_iterator first, const_iterator last)
888 {
889 if (first == last)
890 return first;
891
892 auto first_idx = first.idx();
893 auto last_idx = last.idx();
894 auto count = distance(first, last);
895
896 copy(
897 iterator{*this, last_idx},
898 end(),
899 iterator{*this, first_idx}
900 );
901
902 for (size_type i = 0; i < count; ++i)
903 pop_back();
904
905 return iterator{*this, first_idx};
906 }
907
908 void swap(deque& other)
909 noexcept(allocator_traits<allocator_type>::is_always_equal::value)
910 {
911 std::swap(allocator_, other.allocator_);
912 std::swap(front_bucket_idx_, other.front_bucket_idx_);
913 std::swap(back_bucket_idx_, other.back_bucket_idx_);
914 std::swap(front_bucket_, other.front_bucket_);
915 std::swap(back_bucket_, other.back_bucket_);
916 std::swap(bucket_count_, other.bucket_count_);
917 std::swap(bucket_capacity_, other.bucket_capacity_);
918 std::swap(size_, other.size_);
919 std::swap(data_, other.data_);
920 }
921
922 void clear() noexcept
923 {
924 if (data_)
925 fini_();
926
927 front_bucket_ = default_front_;
928 back_bucket_ = default_back_;
929 bucket_count_ = default_bucket_count_;
930 bucket_capacity_ = default_bucket_capacity_;
931 size_ = size_type{};
932
933 init_();
934 }
935
936 private:
937 allocator_type allocator_;
938
939 /**
940 * Note: In our implementation, front_bucket_idx_
941 * points at the first element and back_bucket_idx_
942 * points at the one past last element. Because of this,
943 * some operations may be done in inverse order
944 * depending on the side they are applied to.
945 */
946 size_type front_bucket_idx_;
947 size_type back_bucket_idx_;
948 size_type front_bucket_;
949 size_type back_bucket_;
950
951 static constexpr size_type bucket_size_{16};
952 static constexpr size_type default_bucket_count_{2};
953 static constexpr size_type default_bucket_capacity_{4};
954 static constexpr size_type default_front_{1};
955 static constexpr size_type default_back_{2};
956
957 size_type bucket_count_;
958 size_type bucket_capacity_;
959 size_type size_{};
960
961 value_type** data_;
962
963 void init_()
964 {
965 data_ = new value_type*[bucket_capacity_];
966
967 for (size_type i = front_bucket_; i <= back_bucket_; ++i)
968 data_[i] = allocator_.allocate(bucket_size_);
969 }
970
971 void prepare_for_size_(size_type size)
972 {
973 if (data_)
974 fini_();
975
976 bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2;
977
978 front_bucket_ = 0;
979 back_bucket_ = bucket_capacity_ - 1;
980
981 front_bucket_idx_ = bucket_size_;
982 back_bucket_idx_ = size % bucket_size_;
983 }
984
985 template<class Iterator>
986 void copy_from_range_(Iterator first, Iterator last)
987 {
988 size_ = distance(first, last);
989 prepare_for_size_(size_);
990 init_();
991
992 auto it = begin();
993 while (first != last)
994 *it++ = *first++;
995 }
996
997 void ensure_space_front_(size_type idx, size_type count)
998 {
999 auto free_space = bucket_size_ - elements_in_front_bucket_();
1000 if (front_bucket_idx_ == 0)
1001 free_space = 0;
1002
1003 if (count <= free_space)
1004 {
1005 front_bucket_idx_ -= count;
1006 return;
1007 }
1008
1009 count -= free_space;
1010
1011 auto buckets_needed = count / bucket_size_;
1012 if (count % bucket_size_ != 0)
1013 ++buckets_needed;
1014
1015 for (size_type i = size_type{}; i < buckets_needed; ++i)
1016 add_new_bucket_front_();
1017
1018 front_bucket_idx_ = bucket_size_ - (count % bucket_size_);
1019 }
1020
1021 void ensure_space_back_(size_type idx, size_type count)
1022 {
1023 auto free_space = bucket_size_ - back_bucket_idx_;
1024 if (count < free_space)
1025 return;
1026
1027 count -= free_space;
1028 auto buckets_needed = count / bucket_size_;
1029 if (count % bucket_size_ != 0)
1030 ++buckets_needed;
1031
1032 for (size_type i = size_type{}; i < buckets_needed; ++i)
1033 add_new_bucket_back_();
1034
1035 back_bucket_idx_ = (back_bucket_idx_ + count) % bucket_size_;
1036 }
1037
1038 void shift_right_(size_type idx, size_type count)
1039 {
1040 ensure_space_back_(idx, count);
1041
1042 iterator it{*this, idx};
1043 copy_backward(it, end(), end() + count - 1);
1044
1045 }
1046
1047 void shift_left_(size_type idx, size_type count)
1048 {
1049 ensure_space_front_(idx, count);
1050
1051 copy(
1052 iterator{*this, count},
1053 iterator{*this, idx + count - 1},
1054 iterator{*this, 0}
1055 );
1056 }
1057
1058 void fini_()
1059 {
1060 for (size_type i = front_bucket_; i <= back_bucket_; ++i)
1061 allocator_.deallocate(data_[i], bucket_size_);
1062
1063 delete[] data_;
1064 data_ = nullptr;
1065 }
1066
1067 bool has_bucket_space_back_() const
1068 {
1069 return back_bucket_ < bucket_capacity_ - 1;
1070 }
1071
1072 bool has_bucket_space_front_() const
1073 {
1074 return front_bucket_ > 0;
1075 }
1076
1077 void add_new_bucket_back_()
1078 {
1079 if (!has_bucket_space_back_())
1080 expand_();
1081
1082 ++back_bucket_;
1083 ++bucket_count_;
1084 data_[back_bucket_] = allocator_.allocate(bucket_size_);
1085 back_bucket_idx_ = size_type{};
1086 }
1087
1088 void add_new_bucket_front_()
1089 {
1090 if (!has_bucket_space_front_())
1091 expand_();
1092
1093 --front_bucket_;
1094 ++bucket_count_;
1095 data_[front_bucket_] = allocator_.allocate(bucket_size_);
1096 front_bucket_idx_ = bucket_size_;
1097 }
1098
1099 void expand_()
1100 {
1101 bucket_capacity_ *= 2;
1102 value_type** new_data = new value_type*[bucket_capacity_];
1103
1104 /**
1105 * Note: This currently expands both sides whenever one reaches
1106 * its limit. Might be better to expand only one (or both when
1107 * the other is near its limit)?
1108 */
1109 size_type new_front = bucket_capacity_ / 4;
1110 size_type new_back = new_front + bucket_count_ - 1;
1111
1112 for (size_type i = new_front, j = front_bucket_; i <= new_back; ++i, ++j)
1113 new_data[i] = move(data_[j]);
1114 std::swap(data_, new_data);
1115
1116 delete[] new_data;
1117 front_bucket_ = new_front;
1118 back_bucket_ = new_back;
1119 }
1120
1121 size_type get_bucket_index_(size_type idx) const
1122 {
1123 if (idx < elements_in_front_bucket_())
1124 return front_bucket_;
1125
1126 idx -= elements_in_front_bucket_();
1127 return idx / bucket_size_ + front_bucket_ + 1;
1128 }
1129
1130 size_type get_element_index_(size_type idx) const
1131 {
1132 if (idx < elements_in_front_bucket_())
1133 return bucket_size_ - elements_in_front_bucket_() + idx;
1134
1135 idx -= elements_in_front_bucket_();
1136 return idx % bucket_size_;
1137 }
1138
1139 size_type elements_in_front_bucket_() const
1140 {
1141 return bucket_size_ - front_bucket_idx_;
1142 }
1143 };
1144
1145 template<class T, class Allocator>
1146 bool operator==(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1147 {
1148 if (lhs.size() != rhs.size())
1149 return false;
1150
1151 for (decltype(lhs.size()) i = 0; i < lhs.size(); ++i)
1152 {
1153 if (lhs[i] != rhs[i])
1154 return false;
1155 }
1156
1157 return true;
1158 }
1159
1160 template<class T, class Allocator>
1161 bool operator<(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1162 {
1163 auto min_size = min(lhs.size(), rhs.size());
1164 for (decltype(lhs.size()) i = 0; i < min_size; ++i)
1165 {
1166 if (lhs[i] >= rhs[i])
1167 return false;
1168 }
1169
1170 if (lhs.size() == rhs.size())
1171 return true;
1172 else
1173 return lhs.size() < rhs.size();
1174 }
1175
1176 template<class T, class Allocator>
1177 bool operator!=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1178 {
1179 return !(lhs == rhs);
1180 }
1181
1182 template<class T, class Allocator>
1183 bool operator>(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1184 {
1185 return rhs < lhs;
1186 }
1187
1188 template<class T, class Allocator>
1189 bool operator<=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1190 {
1191 return !(rhs < lhs);
1192 }
1193
1194 template<class T, class Allocator>
1195 bool operator>=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1196 {
1197 return !(lhs < rhs);
1198 }
1199
1200 /**
1201 * 23.3.3.5, deque specialized algorithms:
1202 */
1203
1204 template<class T, class Allocator>
1205 void swap(deque<T, Allocator>& lhs, deque<T, Allocator>& rhs)
1206 noexcept(noexcept(lhs.swap(rhs)))
1207 {
1208 lhs.swap(rhs);
1209 }
1210}
1211
1212#endif
Note: See TracBrowser for help on using the repository browser.