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

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

cpp: finished capacity related functions

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