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

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

cpp: added swap

  • Property mode set to 100644
File size: 31.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 <initializer_list>
33#include <iterator>
34#include <memory>
35
36namespace 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 if (data_)
425 fini_();
426
427 copy_from_range_(other.begin(), other.end());
428
429 return *this;
430 }
431
432 deque& operator=(deque&& other)
433 noexcept(allocator_traits<allocator_type>::is_always_equal::value)
434 {
435 swap(other);
436 other.clear();
437 }
438
439 deque& operator=(initializer_list<T> init)
440 {
441 if (data_)
442 fini_();
443
444 copy_from_range_(init.begin(), init.end());
445
446 return *this;
447 }
448
449 template<class InputIterator>
450 void assign(InputIterator first, InputIterator last)
451 {
452 copy_from_range_(first, last);
453 }
454
455 void assign(size_type n, const T& value)
456 {
457 prepare_for_size_(n);
458 init_();
459 size_ = n;
460
461 auto it = begin();
462 for (size_type i = size_type{}; i < n; ++i)
463 *it++ = value;
464 }
465
466 void assign(initializer_list<T> init)
467 {
468 copy_from_range_(init.begin(), init.end());
469 }
470
471 allocator_type get_allocator() const noexcept
472 {
473 return allocator_;
474 }
475
476 iterator begin() noexcept
477 {
478 return aux::deque_iterator{*this, 0};
479 }
480
481 const_iterator begin() const noexcept
482 {
483 return aux::deque_const_iterator{*this, 0};
484 }
485
486 iterator end() noexcept
487 {
488 return aux::deque_iterator{*this, size_};
489 }
490
491 const_iterator end() const noexcept
492 {
493 return aux::deque_const_iterator{*this, size_};
494 }
495
496 reverse_iterator rbegin() noexcept
497 {
498 return make_reverse_iterator(end());
499 }
500
501 const_reverse_iterator rbegin() const noexcept
502 {
503 return make_reverse_iterator(cend());
504 }
505
506 reverse_iterator rend() noexcept
507 {
508 return make_reverse_iterator(begin());
509 }
510
511 const_reverse_iterator rend() const noexcept
512 {
513 return make_reverse_iterator(cbegin());
514 }
515
516 const_iterator cbegin() const noexcept
517 {
518 return aux::deque_const_iterator{*this, 0};
519 }
520
521 const_iterator cend() const noexcept
522 {
523 return aux::deque_const_iterator{*this, size_};
524 }
525
526 const_reverse_iterator crbegin() const noexcept
527 {
528 return make_reverse_iterator(cend());
529 }
530
531 const_reverse_iterator crend() const noexcept
532 {
533 return make_reverse_iterator(cbegin());
534 }
535
536 /**
537 * 23.3.3.3, capacity:
538 */
539
540 size_type size() const noexcept
541 {
542 return size_;
543 }
544
545 size_type max_size() const noexcept
546 {
547 return allocator_traits<allocator_type>::max_size(allocator_);
548 }
549
550 void resize(size_type sz)
551 {
552 if (sz <= size_)
553 {
554 for (size_type i = 0; i < size_ - sz; ++i)
555 pop_back();
556 }
557 else
558 {
559 value_type value{};
560 for (size_type i = 0; i < sz - size_; ++i)
561 push_back(value);
562 }
563 }
564
565 void resize(size_type sz, const value_type& value)
566 {
567 if (sz <= size_)
568 {
569 for (size_type i = 0; i < size_ - sz; ++i)
570 pop_back();
571 }
572 else
573 {
574 for (size_type i = 0; i < sz - size_; ++i)
575 push_back(value);
576 }
577 }
578
579 void shrink_to_fit()
580 {
581 /**
582 * We lazily allocate buckets and eagerily deallocate them.
583 * We cannot go into smaller pieces as buckets have fixed size.
584 * Because of this, shrink_to_fit has no effect (as permitted
585 * by the standard).
586 */
587 }
588
589 bool empty() const noexcept
590 {
591 return size_ == size_type{};
592 }
593
594 reference operator[](size_type idx)
595 {
596 return data_[get_bucket_index_(idx)][get_element_index_(idx)];
597 }
598
599 const_reference operator[](size_type idx) const
600 {
601 return data_[get_bucket_index_(idx)][get_element_index_(idx)];
602 }
603
604 reference at(size_type idx)
605 {
606 // TODO: bounds checking
607 return operator[](idx);
608 }
609
610 const_reference at(size_type idx) const
611 {
612 // TODO: bounds checking
613 return operator[](idx);
614 }
615
616 reference front()
617 {
618 return *begin();
619 }
620
621 const_reference front() const
622 {
623 return *cbegin();
624 }
625
626 reference back()
627 {
628 auto tmp = end();
629
630 return *(--tmp);
631 }
632
633 const_reference back() const
634 {
635 auto tmp = cend();
636
637 return *(--tmp);
638 }
639
640 /**
641 * 23.3.3.4, modifiers:
642 */
643
644 template<class... Args>
645 void emplace_front(Args&&... args)
646 {
647 if (front_bucket_idx_ == 0)
648 add_new_bucket_front_();
649
650 allocator_traits<allocator_type>::construct(
651 allocator_,
652 &data_[front_bucket_][--front_bucket_idx_],
653 forward<Args>(args)...
654 );
655
656 ++size_;
657 }
658
659 template<class... Args>
660 void emplace_back(Args&&... args)
661 {
662 allocator_traits<allocator_type>::construct(
663 allocator_,
664 &data_[back_bucket_][back_bucket_idx_++],
665 forward<Args>(args)...
666 );
667
668 ++size_;
669
670 if (back_bucket_idx_ >= bucket_size_)
671 add_new_bucket_back_();
672 }
673
674 template<class... Args>
675 iterator emplace(const_iterator position, Args&&... args)
676 {
677 // TODO: implement
678 }
679
680 void push_front(const value_type& value)
681 {
682 if (front_bucket_idx_ == 0)
683 add_new_bucket_front_();
684
685 data_[front_bucket_][--front_bucket_idx_] = value;
686 ++size_;
687 }
688
689 void push_front(value_type&& value)
690 {
691 if (front_bucket_idx_ == 0)
692 add_new_bucket_front_();
693
694 data_[front_bucket_][--front_bucket_idx_] = forward<value_type>(value);
695 ++size_;
696 }
697
698 void push_back(const value_type& value)
699 {
700 data_[back_bucket_][back_bucket_idx_++] = value;
701 ++size_;
702
703 if (back_bucket_idx_ >= bucket_size_)
704 add_new_bucket_back_();
705 }
706
707 void push_back(value_type&& value)
708 {
709 data_[back_bucket_][back_bucket_idx_++] = forward<value_type>(value);
710 ++size_;
711
712 if (back_bucket_idx_ >= bucket_size_)
713 add_new_bucket_back_();
714 }
715
716 iterator insert(const_iterator position, const value_type& value)
717 {
718 // TODO: implement
719 }
720
721 iterator insert(const_iterator position, value_type&& value)
722 {
723 // TODO: implement
724 }
725
726 iterator insert(const_iterator position, size_type n, const value_type& value)
727 {
728 // TODO: implement
729 }
730
731 template<class InputIterator>
732 iterator insert(const_iterator position, InputIterator first, InputIterator last)
733 {
734 // TODO: implement
735 }
736
737 iterator insert(const_iterator position, initializer_list<value_type> init)
738 {
739 // TODO: implement
740 }
741
742 void pop_back()
743 {
744 if (empty())
745 return;
746
747 if (back_bucket_idx_ == 0)
748 { // Means we gotta pop data_[front_bucket_][bucket_size_ - 1]!
749 if (data_[back_bucket_])
750 allocator_.deallocate(data_[back_bucket_], bucket_size_);
751
752 --back_bucket_;
753 back_bucket_idx_ = bucket_size_ - 1;
754 }
755 else
756 --back_bucket_idx_;
757
758 allocator_.destroy(&data_[back_bucket_][back_bucket_idx_]);
759 --size_;
760 }
761
762 void pop_front()
763 {
764 if (empty())
765 return;
766
767 if (front_bucket_idx_ >= bucket_size_)
768 { // Means we gotta pop data_[front_bucket_][0]!
769 if (data_[front_bucket_])
770 allocator_.deallocate(data_[front_bucket_], bucket_size_);
771
772 ++front_bucket_;
773 front_bucket_idx_ = 1;
774
775 allocator_.destroy(&data_[front_bucket_][0]);
776 }
777 else
778 {
779 allocator_.destroy(&data_[front_bucket_][front_bucket_idx_]);
780
781 ++front_bucket_idx_;
782 }
783
784 --size_;
785 }
786
787 iterator erase(const_iterator position)
788 {
789 // TODO: implement
790 }
791
792 iterator erase(const_iterator first, const_iterator last)
793 {
794 // TODO: implement
795 }
796
797 void swap(deque& other)
798 noexcept(allocator_traits<allocator_type>::is_always_equal::value)
799 {
800 std::swap(allocator_, other.allocator_);
801 std::swap(front_bucket_idx_, other.front_bucket_idx_);
802 std::swap(back_bucket_idx_, other.back_bucket_idx_);
803 std::swap(front_bucket_, other.front_bucket_);
804 std::swap(back_bucket_, other.back_bucket_);
805 std::swap(bucket_count_, other.bucket_count_);
806 std::swap(bucket_capacity_, other.bucket_capacity_);
807 std::swap(size_, other.size_);
808 std::swap(data_, other.data_);
809 }
810
811 void clear() noexcept
812 {
813 if (data_)
814 fini_();
815
816 front_bucket_ = default_front_;
817 back_bucket_ = default_back_;
818 bucket_count_ = default_bucket_count_;
819 bucket_capacity_ = default_bucket_capacity_;
820 size_ = size_type{};
821
822 init_();
823 }
824
825 private:
826 allocator_type allocator_;
827
828 /**
829 * Note: In our implementation, front_bucket_idx_
830 * points at the first element and back_bucket_idx_
831 * points at the one past last element. Because of this,
832 * some operations may be done in inverse order
833 * depending on the side they are applied to.
834 */
835 size_type front_bucket_idx_;
836 size_type back_bucket_idx_;
837 size_type front_bucket_;
838 size_type back_bucket_;
839
840 static constexpr size_type bucket_size_{16};
841 static constexpr size_type default_bucket_count_{2};
842 static constexpr size_type default_bucket_capacity_{4};
843 static constexpr size_type default_front_{1};
844 static constexpr size_type default_back_{2};
845
846 size_type bucket_count_;
847 size_type bucket_capacity_;
848 size_type size_{};
849
850 value_type** data_;
851
852 void init_()
853 {
854 data_ = new value_type*[bucket_capacity_];
855
856 for (size_type i = front_bucket_; i <= back_bucket_; ++i)
857 data_[i] = allocator_.allocate(bucket_size_);
858 }
859
860 void prepare_for_size_(size_type size)
861 {
862 if (data_)
863 fini_();
864
865 if (size < bucket_size_) // Always front_bucket_ != back_bucket_.
866 bucket_count_ = bucket_capacity_ = 2;
867 else if (size % bucket_size_ == 0)
868 bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2;
869 else
870 bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2;
871
872 front_bucket_ = 0;
873 back_bucket_ = bucket_capacity_ - 1;
874
875 front_bucket_idx_ = bucket_size_;
876 back_bucket_idx_ = size % bucket_size_;
877 }
878
879 template<class Iterator>
880 void copy_from_range_(Iterator first, Iterator last)
881 {
882 size_ = distance(first, last);
883 prepare_for_size_(size_);
884 init_();
885
886 auto it = begin();
887 while (first != last)
888 *it++ = *first++;
889 }
890
891 void fini_()
892 {
893 for (size_type i = front_bucket_; i <= back_bucket_; ++i)
894 allocator_.deallocate(data_[i], bucket_size_);
895
896 delete[] data_;
897 data_ = nullptr;
898 }
899
900 bool has_bucket_space_back_() const
901 {
902 return back_bucket_ < bucket_capacity_ - 1;
903 }
904
905 bool has_bucket_space_front_() const
906 {
907 return front_bucket_ > 0;
908 }
909
910 void add_new_bucket_back_()
911 {
912 if (!has_bucket_space_back_())
913 expand_();
914
915 ++back_bucket_;
916 data_[back_bucket_] = allocator_.allocate(bucket_size_);
917 back_bucket_idx_ = size_type{};
918 }
919
920 void add_new_bucket_front_()
921 {
922 if (!has_bucket_space_front_())
923 expand_();
924
925 --front_bucket_;
926 data_[front_bucket_] = allocator_.allocate(bucket_size_);
927 front_bucket_idx_ = bucket_size_;
928 }
929
930 void expand_()
931 {
932 bucket_capacity_ *= 2;
933 value_type** new_data = new value_type*[bucket_capacity_];
934
935 size_type new_front = bucket_capacity_ / 4;
936 size_type new_back = bucket_capacity_ - bucket_capacity_ / 4 - 1;
937
938 for (size_type i = new_front, j = front_bucket_; i <= new_back; ++i, ++j)
939 new_data[i] = move(data_[j]);
940 std::swap(data_, new_data);
941
942 delete[] new_data;
943 front_bucket_ = new_front;
944 back_bucket_ = new_back;
945 }
946
947 size_type get_bucket_index_(size_type idx) const
948 {
949 if (idx < elements_in_front_bucket_())
950 return front_bucket_;
951
952 idx -= elements_in_front_bucket_();
953 return idx / bucket_size_ + front_bucket_ + 1;
954 }
955
956 size_type get_element_index_(size_type idx) const
957 {
958 if (idx < elements_in_front_bucket_())
959 return bucket_size_ - elements_in_front_bucket_() + idx;
960
961 idx -= elements_in_front_bucket_();
962 return idx % bucket_size_;
963 }
964
965 size_type elements_in_front_bucket_() const
966 {
967 return bucket_size_ - front_bucket_idx_;
968 }
969 };
970
971 template<class T, class Allocator>
972 bool operator==(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
973 {
974 // TODO: implement
975 return false;
976 }
977
978 template<class T, class Allocator>
979 bool operator<(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
980 {
981 // TODO: implement
982 return false;
983 }
984
985 template<class T, class Allocator>
986 bool operator!=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
987 {
988 // TODO: implement
989 return false;
990 }
991
992 template<class T, class Allocator>
993 bool operator>(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
994 {
995 // TODO: implement
996 return false;
997 }
998
999 template<class T, class Allocator>
1000 bool operator<=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1001 {
1002 // TODO: implement
1003 return false;
1004 }
1005
1006 template<class T, class Allocator>
1007 bool operator>=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
1008 {
1009 // TODO: implement
1010 return false;
1011 }
1012
1013 /**
1014 * 23.3.3.5, deque specialized algorithms:
1015 */
1016
1017 template<class T, class Allocator>
1018 void swap(deque<T, Allocator>& lhs, deque<T, Allocator>& rhs)
1019 noexcept(noexcept(lhs.swap(rhs)))
1020 {
1021 lhs.swap(rhs);
1022 }
1023}
1024
1025#endif
Note: See TracBrowser for help on using the repository browser.