1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_CONTAINERS_CIRCULAR_DEQUE_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_CONTAINERS_CIRCULAR_DEQUE_H_
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker #include <algorithm>
9*635a8641SAndroid Build Coastguard Worker #include <cstddef>
10*635a8641SAndroid Build Coastguard Worker #include <iterator>
11*635a8641SAndroid Build Coastguard Worker #include <type_traits>
12*635a8641SAndroid Build Coastguard Worker #include <utility>
13*635a8641SAndroid Build Coastguard Worker
14*635a8641SAndroid Build Coastguard Worker #include "base/containers/vector_buffer.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/template_util.h"
18*635a8641SAndroid Build Coastguard Worker
19*635a8641SAndroid Build Coastguard Worker // base::circular_deque is similar to std::deque. Unlike std::deque, the
20*635a8641SAndroid Build Coastguard Worker // storage is provided in a flat circular buffer conceptually similar to a
21*635a8641SAndroid Build Coastguard Worker // vector. The beginning and end will wrap around as necessary so that
22*635a8641SAndroid Build Coastguard Worker // pushes and pops will be constant time as long as a capacity expansion is
23*635a8641SAndroid Build Coastguard Worker // not required.
24*635a8641SAndroid Build Coastguard Worker //
25*635a8641SAndroid Build Coastguard Worker // The API should be identical to std::deque with the following differences:
26*635a8641SAndroid Build Coastguard Worker //
27*635a8641SAndroid Build Coastguard Worker // - ITERATORS ARE NOT STABLE. Mutating the container will invalidate all
28*635a8641SAndroid Build Coastguard Worker // iterators.
29*635a8641SAndroid Build Coastguard Worker //
30*635a8641SAndroid Build Coastguard Worker // - Insertions may resize the vector and so are not constant time (std::deque
31*635a8641SAndroid Build Coastguard Worker // guarantees constant time for insertions at the ends).
32*635a8641SAndroid Build Coastguard Worker //
33*635a8641SAndroid Build Coastguard Worker // - Container-wide comparisons are not implemented. If you want to compare
34*635a8641SAndroid Build Coastguard Worker // two containers, use an algorithm so the expensive iteration is explicit.
35*635a8641SAndroid Build Coastguard Worker //
36*635a8641SAndroid Build Coastguard Worker // If you want a similar container with only a queue API, use base::queue in
37*635a8641SAndroid Build Coastguard Worker // base/containers/queue.h.
38*635a8641SAndroid Build Coastguard Worker //
39*635a8641SAndroid Build Coastguard Worker // Constructors:
40*635a8641SAndroid Build Coastguard Worker // circular_deque();
41*635a8641SAndroid Build Coastguard Worker // circular_deque(size_t count);
42*635a8641SAndroid Build Coastguard Worker // circular_deque(size_t count, const T& value);
43*635a8641SAndroid Build Coastguard Worker // circular_deque(InputIterator first, InputIterator last);
44*635a8641SAndroid Build Coastguard Worker // circular_deque(const circular_deque&);
45*635a8641SAndroid Build Coastguard Worker // circular_deque(circular_deque&&);
46*635a8641SAndroid Build Coastguard Worker // circular_deque(std::initializer_list<value_type>);
47*635a8641SAndroid Build Coastguard Worker //
48*635a8641SAndroid Build Coastguard Worker // Assignment functions:
49*635a8641SAndroid Build Coastguard Worker // circular_deque& operator=(const circular_deque&);
50*635a8641SAndroid Build Coastguard Worker // circular_deque& operator=(circular_deque&&);
51*635a8641SAndroid Build Coastguard Worker // circular_deque& operator=(std::initializer_list<T>);
52*635a8641SAndroid Build Coastguard Worker // void assign(size_t count, const T& value);
53*635a8641SAndroid Build Coastguard Worker // void assign(InputIterator first, InputIterator last);
54*635a8641SAndroid Build Coastguard Worker // void assign(std::initializer_list<T> value);
55*635a8641SAndroid Build Coastguard Worker //
56*635a8641SAndroid Build Coastguard Worker // Random accessors:
57*635a8641SAndroid Build Coastguard Worker // T& at(size_t);
58*635a8641SAndroid Build Coastguard Worker // const T& at(size_t) const;
59*635a8641SAndroid Build Coastguard Worker // T& operator[](size_t);
60*635a8641SAndroid Build Coastguard Worker // const T& operator[](size_t) const;
61*635a8641SAndroid Build Coastguard Worker //
62*635a8641SAndroid Build Coastguard Worker // End accessors:
63*635a8641SAndroid Build Coastguard Worker // T& front();
64*635a8641SAndroid Build Coastguard Worker // const T& front() const;
65*635a8641SAndroid Build Coastguard Worker // T& back();
66*635a8641SAndroid Build Coastguard Worker // const T& back() const;
67*635a8641SAndroid Build Coastguard Worker //
68*635a8641SAndroid Build Coastguard Worker // Iterator functions:
69*635a8641SAndroid Build Coastguard Worker // iterator begin();
70*635a8641SAndroid Build Coastguard Worker // const_iterator begin() const;
71*635a8641SAndroid Build Coastguard Worker // const_iterator cbegin() const;
72*635a8641SAndroid Build Coastguard Worker // iterator end();
73*635a8641SAndroid Build Coastguard Worker // const_iterator end() const;
74*635a8641SAndroid Build Coastguard Worker // const_iterator cend() const;
75*635a8641SAndroid Build Coastguard Worker // reverse_iterator rbegin();
76*635a8641SAndroid Build Coastguard Worker // const_reverse_iterator rbegin() const;
77*635a8641SAndroid Build Coastguard Worker // const_reverse_iterator crbegin() const;
78*635a8641SAndroid Build Coastguard Worker // reverse_iterator rend();
79*635a8641SAndroid Build Coastguard Worker // const_reverse_iterator rend() const;
80*635a8641SAndroid Build Coastguard Worker // const_reverse_iterator crend() const;
81*635a8641SAndroid Build Coastguard Worker //
82*635a8641SAndroid Build Coastguard Worker // Memory management:
83*635a8641SAndroid Build Coastguard Worker // void reserve(size_t); // SEE IMPLEMENTATION FOR SOME GOTCHAS.
84*635a8641SAndroid Build Coastguard Worker // size_t capacity() const;
85*635a8641SAndroid Build Coastguard Worker // void shrink_to_fit();
86*635a8641SAndroid Build Coastguard Worker //
87*635a8641SAndroid Build Coastguard Worker // Size management:
88*635a8641SAndroid Build Coastguard Worker // void clear();
89*635a8641SAndroid Build Coastguard Worker // bool empty() const;
90*635a8641SAndroid Build Coastguard Worker // size_t size() const;
91*635a8641SAndroid Build Coastguard Worker // void resize(size_t);
92*635a8641SAndroid Build Coastguard Worker // void resize(size_t count, const T& value);
93*635a8641SAndroid Build Coastguard Worker //
94*635a8641SAndroid Build Coastguard Worker // Positional insert and erase:
95*635a8641SAndroid Build Coastguard Worker // void insert(const_iterator pos, size_type count, const T& value);
96*635a8641SAndroid Build Coastguard Worker // void insert(const_iterator pos,
97*635a8641SAndroid Build Coastguard Worker // InputIterator first, InputIterator last);
98*635a8641SAndroid Build Coastguard Worker // iterator insert(const_iterator pos, const T& value);
99*635a8641SAndroid Build Coastguard Worker // iterator insert(const_iterator pos, T&& value);
100*635a8641SAndroid Build Coastguard Worker // iterator emplace(const_iterator pos, Args&&... args);
101*635a8641SAndroid Build Coastguard Worker // iterator erase(const_iterator pos);
102*635a8641SAndroid Build Coastguard Worker // iterator erase(const_iterator first, const_iterator last);
103*635a8641SAndroid Build Coastguard Worker //
104*635a8641SAndroid Build Coastguard Worker // End insert and erase:
105*635a8641SAndroid Build Coastguard Worker // void push_front(const T&);
106*635a8641SAndroid Build Coastguard Worker // void push_front(T&&);
107*635a8641SAndroid Build Coastguard Worker // void push_back(const T&);
108*635a8641SAndroid Build Coastguard Worker // void push_back(T&&);
109*635a8641SAndroid Build Coastguard Worker // T& emplace_front(Args&&...);
110*635a8641SAndroid Build Coastguard Worker // T& emplace_back(Args&&...);
111*635a8641SAndroid Build Coastguard Worker // void pop_front();
112*635a8641SAndroid Build Coastguard Worker // void pop_back();
113*635a8641SAndroid Build Coastguard Worker //
114*635a8641SAndroid Build Coastguard Worker // General:
115*635a8641SAndroid Build Coastguard Worker // void swap(circular_deque&);
116*635a8641SAndroid Build Coastguard Worker
117*635a8641SAndroid Build Coastguard Worker namespace base {
118*635a8641SAndroid Build Coastguard Worker
119*635a8641SAndroid Build Coastguard Worker template <class T>
120*635a8641SAndroid Build Coastguard Worker class circular_deque;
121*635a8641SAndroid Build Coastguard Worker
122*635a8641SAndroid Build Coastguard Worker namespace internal {
123*635a8641SAndroid Build Coastguard Worker
124*635a8641SAndroid Build Coastguard Worker // Start allocating nonempty buffers with this many entries. This is the
125*635a8641SAndroid Build Coastguard Worker // external capacity so the internal buffer will be one larger (= 4) which is
126*635a8641SAndroid Build Coastguard Worker // more even for the allocator. See the descriptions of internal vs. external
127*635a8641SAndroid Build Coastguard Worker // capacity on the comment above the buffer_ variable below.
128*635a8641SAndroid Build Coastguard Worker constexpr size_t kCircularBufferInitialCapacity = 3;
129*635a8641SAndroid Build Coastguard Worker
130*635a8641SAndroid Build Coastguard Worker template <typename T>
131*635a8641SAndroid Build Coastguard Worker class circular_deque_const_iterator {
132*635a8641SAndroid Build Coastguard Worker public:
133*635a8641SAndroid Build Coastguard Worker using difference_type = std::ptrdiff_t;
134*635a8641SAndroid Build Coastguard Worker using value_type = T;
135*635a8641SAndroid Build Coastguard Worker using pointer = const T*;
136*635a8641SAndroid Build Coastguard Worker using reference = const T&;
137*635a8641SAndroid Build Coastguard Worker using iterator_category = std::random_access_iterator_tag;
138*635a8641SAndroid Build Coastguard Worker
circular_deque_const_iterator()139*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator() : parent_deque_(nullptr), index_(0) {
140*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
141*635a8641SAndroid Build Coastguard Worker created_generation_ = 0;
142*635a8641SAndroid Build Coastguard Worker #endif // DCHECK_IS_ON()
143*635a8641SAndroid Build Coastguard Worker }
144*635a8641SAndroid Build Coastguard Worker
145*635a8641SAndroid Build Coastguard Worker // Dereferencing.
146*635a8641SAndroid Build Coastguard Worker const T& operator*() const {
147*635a8641SAndroid Build Coastguard Worker CheckUnstableUsage();
148*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndex(index_);
149*635a8641SAndroid Build Coastguard Worker return parent_deque_->buffer_[index_];
150*635a8641SAndroid Build Coastguard Worker }
151*635a8641SAndroid Build Coastguard Worker const T* operator->() const {
152*635a8641SAndroid Build Coastguard Worker CheckUnstableUsage();
153*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndex(index_);
154*635a8641SAndroid Build Coastguard Worker return &parent_deque_->buffer_[index_];
155*635a8641SAndroid Build Coastguard Worker }
156*635a8641SAndroid Build Coastguard Worker const value_type& operator[](difference_type i) const { return *(*this + i); }
157*635a8641SAndroid Build Coastguard Worker
158*635a8641SAndroid Build Coastguard Worker // Increment and decrement.
159*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator& operator++() {
160*635a8641SAndroid Build Coastguard Worker Increment();
161*635a8641SAndroid Build Coastguard Worker return *this;
162*635a8641SAndroid Build Coastguard Worker }
163*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator operator++(int) {
164*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator ret = *this;
165*635a8641SAndroid Build Coastguard Worker Increment();
166*635a8641SAndroid Build Coastguard Worker return ret;
167*635a8641SAndroid Build Coastguard Worker }
168*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator& operator--() {
169*635a8641SAndroid Build Coastguard Worker Decrement();
170*635a8641SAndroid Build Coastguard Worker return *this;
171*635a8641SAndroid Build Coastguard Worker }
172*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator operator--(int) {
173*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator ret = *this;
174*635a8641SAndroid Build Coastguard Worker Decrement();
175*635a8641SAndroid Build Coastguard Worker return ret;
176*635a8641SAndroid Build Coastguard Worker }
177*635a8641SAndroid Build Coastguard Worker
178*635a8641SAndroid Build Coastguard Worker // Random access mutation.
179*635a8641SAndroid Build Coastguard Worker friend circular_deque_const_iterator operator+(
180*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& iter,
181*635a8641SAndroid Build Coastguard Worker difference_type offset) {
182*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator ret = iter;
183*635a8641SAndroid Build Coastguard Worker ret.Add(offset);
184*635a8641SAndroid Build Coastguard Worker return ret;
185*635a8641SAndroid Build Coastguard Worker }
186*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator& operator+=(difference_type offset) {
187*635a8641SAndroid Build Coastguard Worker Add(offset);
188*635a8641SAndroid Build Coastguard Worker return *this;
189*635a8641SAndroid Build Coastguard Worker }
190*635a8641SAndroid Build Coastguard Worker friend circular_deque_const_iterator operator-(
191*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& iter,
192*635a8641SAndroid Build Coastguard Worker difference_type offset) {
193*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator ret = iter;
194*635a8641SAndroid Build Coastguard Worker ret.Add(-offset);
195*635a8641SAndroid Build Coastguard Worker return ret;
196*635a8641SAndroid Build Coastguard Worker }
197*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator& operator-=(difference_type offset) {
198*635a8641SAndroid Build Coastguard Worker Add(-offset);
199*635a8641SAndroid Build Coastguard Worker return *this;
200*635a8641SAndroid Build Coastguard Worker }
201*635a8641SAndroid Build Coastguard Worker
202*635a8641SAndroid Build Coastguard Worker friend std::ptrdiff_t operator-(const circular_deque_const_iterator& lhs,
203*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
204*635a8641SAndroid Build Coastguard Worker lhs.CheckComparable(rhs);
205*635a8641SAndroid Build Coastguard Worker return lhs.OffsetFromBegin() - rhs.OffsetFromBegin();
206*635a8641SAndroid Build Coastguard Worker }
207*635a8641SAndroid Build Coastguard Worker
208*635a8641SAndroid Build Coastguard Worker // Comparisons.
209*635a8641SAndroid Build Coastguard Worker friend bool operator==(const circular_deque_const_iterator& lhs,
210*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
211*635a8641SAndroid Build Coastguard Worker lhs.CheckComparable(rhs);
212*635a8641SAndroid Build Coastguard Worker return lhs.index_ == rhs.index_;
213*635a8641SAndroid Build Coastguard Worker }
214*635a8641SAndroid Build Coastguard Worker friend bool operator!=(const circular_deque_const_iterator& lhs,
215*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
216*635a8641SAndroid Build Coastguard Worker return !(lhs == rhs);
217*635a8641SAndroid Build Coastguard Worker }
218*635a8641SAndroid Build Coastguard Worker friend bool operator<(const circular_deque_const_iterator& lhs,
219*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
220*635a8641SAndroid Build Coastguard Worker lhs.CheckComparable(rhs);
221*635a8641SAndroid Build Coastguard Worker return lhs.OffsetFromBegin() < rhs.OffsetFromBegin();
222*635a8641SAndroid Build Coastguard Worker }
223*635a8641SAndroid Build Coastguard Worker friend bool operator<=(const circular_deque_const_iterator& lhs,
224*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
225*635a8641SAndroid Build Coastguard Worker return !(lhs > rhs);
226*635a8641SAndroid Build Coastguard Worker }
227*635a8641SAndroid Build Coastguard Worker friend bool operator>(const circular_deque_const_iterator& lhs,
228*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
229*635a8641SAndroid Build Coastguard Worker lhs.CheckComparable(rhs);
230*635a8641SAndroid Build Coastguard Worker return lhs.OffsetFromBegin() > rhs.OffsetFromBegin();
231*635a8641SAndroid Build Coastguard Worker }
232*635a8641SAndroid Build Coastguard Worker friend bool operator>=(const circular_deque_const_iterator& lhs,
233*635a8641SAndroid Build Coastguard Worker const circular_deque_const_iterator& rhs) {
234*635a8641SAndroid Build Coastguard Worker return !(lhs < rhs);
235*635a8641SAndroid Build Coastguard Worker }
236*635a8641SAndroid Build Coastguard Worker
237*635a8641SAndroid Build Coastguard Worker protected:
238*635a8641SAndroid Build Coastguard Worker friend class circular_deque<T>;
239*635a8641SAndroid Build Coastguard Worker
circular_deque_const_iterator(const circular_deque<T> * parent,size_t index)240*635a8641SAndroid Build Coastguard Worker circular_deque_const_iterator(const circular_deque<T>* parent, size_t index)
241*635a8641SAndroid Build Coastguard Worker : parent_deque_(parent), index_(index) {
242*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
243*635a8641SAndroid Build Coastguard Worker created_generation_ = parent->generation_;
244*635a8641SAndroid Build Coastguard Worker #endif // DCHECK_IS_ON()
245*635a8641SAndroid Build Coastguard Worker }
246*635a8641SAndroid Build Coastguard Worker
247*635a8641SAndroid Build Coastguard Worker // Returns the offset from the beginning index of the buffer to the current
248*635a8641SAndroid Build Coastguard Worker // item.
OffsetFromBegin()249*635a8641SAndroid Build Coastguard Worker size_t OffsetFromBegin() const {
250*635a8641SAndroid Build Coastguard Worker if (index_ >= parent_deque_->begin_)
251*635a8641SAndroid Build Coastguard Worker return index_ - parent_deque_->begin_; // On the same side as begin.
252*635a8641SAndroid Build Coastguard Worker return parent_deque_->buffer_.capacity() - parent_deque_->begin_ + index_;
253*635a8641SAndroid Build Coastguard Worker }
254*635a8641SAndroid Build Coastguard Worker
255*635a8641SAndroid Build Coastguard Worker // Most uses will be ++ and -- so use a simplified implementation.
Increment()256*635a8641SAndroid Build Coastguard Worker void Increment() {
257*635a8641SAndroid Build Coastguard Worker CheckUnstableUsage();
258*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndex(index_);
259*635a8641SAndroid Build Coastguard Worker index_++;
260*635a8641SAndroid Build Coastguard Worker if (index_ == parent_deque_->buffer_.capacity())
261*635a8641SAndroid Build Coastguard Worker index_ = 0;
262*635a8641SAndroid Build Coastguard Worker }
Decrement()263*635a8641SAndroid Build Coastguard Worker void Decrement() {
264*635a8641SAndroid Build Coastguard Worker CheckUnstableUsage();
265*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndexOrEnd(index_);
266*635a8641SAndroid Build Coastguard Worker if (index_ == 0)
267*635a8641SAndroid Build Coastguard Worker index_ = parent_deque_->buffer_.capacity() - 1;
268*635a8641SAndroid Build Coastguard Worker else
269*635a8641SAndroid Build Coastguard Worker index_--;
270*635a8641SAndroid Build Coastguard Worker }
Add(difference_type delta)271*635a8641SAndroid Build Coastguard Worker void Add(difference_type delta) {
272*635a8641SAndroid Build Coastguard Worker CheckUnstableUsage();
273*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
274*635a8641SAndroid Build Coastguard Worker if (delta <= 0)
275*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndexOrEnd(index_);
276*635a8641SAndroid Build Coastguard Worker else
277*635a8641SAndroid Build Coastguard Worker parent_deque_->CheckValidIndex(index_);
278*635a8641SAndroid Build Coastguard Worker #endif
279*635a8641SAndroid Build Coastguard Worker // It should be valid to add 0 to any iterator, even if the container is
280*635a8641SAndroid Build Coastguard Worker // empty and the iterator points to end(). The modulo below will divide
281*635a8641SAndroid Build Coastguard Worker // by 0 if the buffer capacity is empty, so it's important to check for
282*635a8641SAndroid Build Coastguard Worker // this case explicitly.
283*635a8641SAndroid Build Coastguard Worker if (delta == 0)
284*635a8641SAndroid Build Coastguard Worker return;
285*635a8641SAndroid Build Coastguard Worker
286*635a8641SAndroid Build Coastguard Worker difference_type new_offset = OffsetFromBegin() + delta;
287*635a8641SAndroid Build Coastguard Worker DCHECK(new_offset >= 0 &&
288*635a8641SAndroid Build Coastguard Worker new_offset <= static_cast<difference_type>(parent_deque_->size()));
289*635a8641SAndroid Build Coastguard Worker index_ = (new_offset + parent_deque_->begin_) %
290*635a8641SAndroid Build Coastguard Worker parent_deque_->buffer_.capacity();
291*635a8641SAndroid Build Coastguard Worker }
292*635a8641SAndroid Build Coastguard Worker
293*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
CheckUnstableUsage()294*635a8641SAndroid Build Coastguard Worker void CheckUnstableUsage() const {
295*635a8641SAndroid Build Coastguard Worker DCHECK(parent_deque_);
296*635a8641SAndroid Build Coastguard Worker // Since circular_deque doesn't guarantee stability, any attempt to
297*635a8641SAndroid Build Coastguard Worker // dereference this iterator after a mutation (i.e. the generation doesn't
298*635a8641SAndroid Build Coastguard Worker // match the original) in the container is illegal.
299*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(created_generation_, parent_deque_->generation_)
300*635a8641SAndroid Build Coastguard Worker << "circular_deque iterator dereferenced after mutation.";
301*635a8641SAndroid Build Coastguard Worker }
CheckComparable(const circular_deque_const_iterator & other)302*635a8641SAndroid Build Coastguard Worker void CheckComparable(const circular_deque_const_iterator& other) const {
303*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(parent_deque_, other.parent_deque_);
304*635a8641SAndroid Build Coastguard Worker // Since circular_deque doesn't guarantee stability, two iterators that
305*635a8641SAndroid Build Coastguard Worker // are compared must have been generated without mutating the container.
306*635a8641SAndroid Build Coastguard Worker // If this fires, the container was mutated between generating the two
307*635a8641SAndroid Build Coastguard Worker // iterators being compared.
308*635a8641SAndroid Build Coastguard Worker DCHECK_EQ(created_generation_, other.created_generation_);
309*635a8641SAndroid Build Coastguard Worker }
310*635a8641SAndroid Build Coastguard Worker #else
CheckUnstableUsage()311*635a8641SAndroid Build Coastguard Worker inline void CheckUnstableUsage() const {}
CheckComparable(const circular_deque_const_iterator &)312*635a8641SAndroid Build Coastguard Worker inline void CheckComparable(const circular_deque_const_iterator&) const {}
313*635a8641SAndroid Build Coastguard Worker #endif // DCHECK_IS_ON()
314*635a8641SAndroid Build Coastguard Worker
315*635a8641SAndroid Build Coastguard Worker const circular_deque<T>* parent_deque_;
316*635a8641SAndroid Build Coastguard Worker size_t index_;
317*635a8641SAndroid Build Coastguard Worker
318*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
319*635a8641SAndroid Build Coastguard Worker // The generation of the parent deque when this iterator was created. The
320*635a8641SAndroid Build Coastguard Worker // container will update the generation for every modification so we can
321*635a8641SAndroid Build Coastguard Worker // test if the container was modified by comparing them.
322*635a8641SAndroid Build Coastguard Worker uint64_t created_generation_;
323*635a8641SAndroid Build Coastguard Worker #endif // DCHECK_IS_ON()
324*635a8641SAndroid Build Coastguard Worker };
325*635a8641SAndroid Build Coastguard Worker
326*635a8641SAndroid Build Coastguard Worker template <typename T>
327*635a8641SAndroid Build Coastguard Worker class circular_deque_iterator : public circular_deque_const_iterator<T> {
328*635a8641SAndroid Build Coastguard Worker using base = circular_deque_const_iterator<T>;
329*635a8641SAndroid Build Coastguard Worker
330*635a8641SAndroid Build Coastguard Worker public:
331*635a8641SAndroid Build Coastguard Worker friend class circular_deque<T>;
332*635a8641SAndroid Build Coastguard Worker
333*635a8641SAndroid Build Coastguard Worker using difference_type = std::ptrdiff_t;
334*635a8641SAndroid Build Coastguard Worker using value_type = T;
335*635a8641SAndroid Build Coastguard Worker using pointer = T*;
336*635a8641SAndroid Build Coastguard Worker using reference = T&;
337*635a8641SAndroid Build Coastguard Worker using iterator_category = std::random_access_iterator_tag;
338*635a8641SAndroid Build Coastguard Worker
339*635a8641SAndroid Build Coastguard Worker // Expose the base class' constructor.
circular_deque_iterator()340*635a8641SAndroid Build Coastguard Worker circular_deque_iterator() : circular_deque_const_iterator<T>() {}
341*635a8641SAndroid Build Coastguard Worker
342*635a8641SAndroid Build Coastguard Worker // Dereferencing.
343*635a8641SAndroid Build Coastguard Worker T& operator*() const { return const_cast<T&>(base::operator*()); }
344*635a8641SAndroid Build Coastguard Worker T* operator->() const { return const_cast<T*>(base::operator->()); }
345*635a8641SAndroid Build Coastguard Worker T& operator[](difference_type i) {
346*635a8641SAndroid Build Coastguard Worker return const_cast<T&>(base::operator[](i));
347*635a8641SAndroid Build Coastguard Worker }
348*635a8641SAndroid Build Coastguard Worker
349*635a8641SAndroid Build Coastguard Worker // Random access mutation.
350*635a8641SAndroid Build Coastguard Worker friend circular_deque_iterator operator+(const circular_deque_iterator& iter,
351*635a8641SAndroid Build Coastguard Worker difference_type offset) {
352*635a8641SAndroid Build Coastguard Worker circular_deque_iterator ret = iter;
353*635a8641SAndroid Build Coastguard Worker ret.Add(offset);
354*635a8641SAndroid Build Coastguard Worker return ret;
355*635a8641SAndroid Build Coastguard Worker }
356*635a8641SAndroid Build Coastguard Worker circular_deque_iterator& operator+=(difference_type offset) {
357*635a8641SAndroid Build Coastguard Worker base::Add(offset);
358*635a8641SAndroid Build Coastguard Worker return *this;
359*635a8641SAndroid Build Coastguard Worker }
360*635a8641SAndroid Build Coastguard Worker friend circular_deque_iterator operator-(const circular_deque_iterator& iter,
361*635a8641SAndroid Build Coastguard Worker difference_type offset) {
362*635a8641SAndroid Build Coastguard Worker circular_deque_iterator ret = iter;
363*635a8641SAndroid Build Coastguard Worker ret.Add(-offset);
364*635a8641SAndroid Build Coastguard Worker return ret;
365*635a8641SAndroid Build Coastguard Worker }
366*635a8641SAndroid Build Coastguard Worker circular_deque_iterator& operator-=(difference_type offset) {
367*635a8641SAndroid Build Coastguard Worker base::Add(-offset);
368*635a8641SAndroid Build Coastguard Worker return *this;
369*635a8641SAndroid Build Coastguard Worker }
370*635a8641SAndroid Build Coastguard Worker
371*635a8641SAndroid Build Coastguard Worker // Increment and decrement.
372*635a8641SAndroid Build Coastguard Worker circular_deque_iterator& operator++() {
373*635a8641SAndroid Build Coastguard Worker base::Increment();
374*635a8641SAndroid Build Coastguard Worker return *this;
375*635a8641SAndroid Build Coastguard Worker }
376*635a8641SAndroid Build Coastguard Worker circular_deque_iterator operator++(int) {
377*635a8641SAndroid Build Coastguard Worker circular_deque_iterator ret = *this;
378*635a8641SAndroid Build Coastguard Worker base::Increment();
379*635a8641SAndroid Build Coastguard Worker return ret;
380*635a8641SAndroid Build Coastguard Worker }
381*635a8641SAndroid Build Coastguard Worker circular_deque_iterator& operator--() {
382*635a8641SAndroid Build Coastguard Worker base::Decrement();
383*635a8641SAndroid Build Coastguard Worker return *this;
384*635a8641SAndroid Build Coastguard Worker }
385*635a8641SAndroid Build Coastguard Worker circular_deque_iterator operator--(int) {
386*635a8641SAndroid Build Coastguard Worker circular_deque_iterator ret = *this;
387*635a8641SAndroid Build Coastguard Worker base::Decrement();
388*635a8641SAndroid Build Coastguard Worker return ret;
389*635a8641SAndroid Build Coastguard Worker }
390*635a8641SAndroid Build Coastguard Worker
391*635a8641SAndroid Build Coastguard Worker private:
circular_deque_iterator(const circular_deque<T> * parent,size_t index)392*635a8641SAndroid Build Coastguard Worker circular_deque_iterator(const circular_deque<T>* parent, size_t index)
393*635a8641SAndroid Build Coastguard Worker : circular_deque_const_iterator<T>(parent, index) {}
394*635a8641SAndroid Build Coastguard Worker };
395*635a8641SAndroid Build Coastguard Worker
396*635a8641SAndroid Build Coastguard Worker } // namespace internal
397*635a8641SAndroid Build Coastguard Worker
398*635a8641SAndroid Build Coastguard Worker template <typename T>
399*635a8641SAndroid Build Coastguard Worker class circular_deque {
400*635a8641SAndroid Build Coastguard Worker private:
401*635a8641SAndroid Build Coastguard Worker using VectorBuffer = internal::VectorBuffer<T>;
402*635a8641SAndroid Build Coastguard Worker
403*635a8641SAndroid Build Coastguard Worker public:
404*635a8641SAndroid Build Coastguard Worker using value_type = T;
405*635a8641SAndroid Build Coastguard Worker using size_type = std::size_t;
406*635a8641SAndroid Build Coastguard Worker using difference_type = std::ptrdiff_t;
407*635a8641SAndroid Build Coastguard Worker using reference = value_type&;
408*635a8641SAndroid Build Coastguard Worker using const_reference = const value_type&;
409*635a8641SAndroid Build Coastguard Worker using pointer = value_type*;
410*635a8641SAndroid Build Coastguard Worker using const_pointer = const value_type*;
411*635a8641SAndroid Build Coastguard Worker
412*635a8641SAndroid Build Coastguard Worker using iterator = internal::circular_deque_iterator<T>;
413*635a8641SAndroid Build Coastguard Worker using const_iterator = internal::circular_deque_const_iterator<T>;
414*635a8641SAndroid Build Coastguard Worker using reverse_iterator = std::reverse_iterator<iterator>;
415*635a8641SAndroid Build Coastguard Worker using const_reverse_iterator = std::reverse_iterator<const_iterator>;
416*635a8641SAndroid Build Coastguard Worker
417*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
418*635a8641SAndroid Build Coastguard Worker // Constructor
419*635a8641SAndroid Build Coastguard Worker
420*635a8641SAndroid Build Coastguard Worker constexpr circular_deque() = default;
421*635a8641SAndroid Build Coastguard Worker
422*635a8641SAndroid Build Coastguard Worker // Constructs with |count| copies of |value| or default constructed version.
circular_deque(size_type count)423*635a8641SAndroid Build Coastguard Worker circular_deque(size_type count) { resize(count); }
circular_deque(size_type count,const T & value)424*635a8641SAndroid Build Coastguard Worker circular_deque(size_type count, const T& value) { resize(count, value); }
425*635a8641SAndroid Build Coastguard Worker
426*635a8641SAndroid Build Coastguard Worker // Range constructor.
427*635a8641SAndroid Build Coastguard Worker template <class InputIterator>
circular_deque(InputIterator first,InputIterator last)428*635a8641SAndroid Build Coastguard Worker circular_deque(InputIterator first, InputIterator last) {
429*635a8641SAndroid Build Coastguard Worker assign(first, last);
430*635a8641SAndroid Build Coastguard Worker }
431*635a8641SAndroid Build Coastguard Worker
432*635a8641SAndroid Build Coastguard Worker // Copy/move.
circular_deque(const circular_deque & other)433*635a8641SAndroid Build Coastguard Worker circular_deque(const circular_deque& other) : buffer_(other.size() + 1) {
434*635a8641SAndroid Build Coastguard Worker assign(other.begin(), other.end());
435*635a8641SAndroid Build Coastguard Worker }
circular_deque(circular_deque && other)436*635a8641SAndroid Build Coastguard Worker circular_deque(circular_deque&& other) noexcept
437*635a8641SAndroid Build Coastguard Worker : buffer_(std::move(other.buffer_)),
438*635a8641SAndroid Build Coastguard Worker begin_(other.begin_),
439*635a8641SAndroid Build Coastguard Worker end_(other.end_) {
440*635a8641SAndroid Build Coastguard Worker other.begin_ = 0;
441*635a8641SAndroid Build Coastguard Worker other.end_ = 0;
442*635a8641SAndroid Build Coastguard Worker }
443*635a8641SAndroid Build Coastguard Worker
circular_deque(std::initializer_list<value_type> init)444*635a8641SAndroid Build Coastguard Worker circular_deque(std::initializer_list<value_type> init) { assign(init); }
445*635a8641SAndroid Build Coastguard Worker
~circular_deque()446*635a8641SAndroid Build Coastguard Worker ~circular_deque() { DestructRange(begin_, end_); }
447*635a8641SAndroid Build Coastguard Worker
448*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
449*635a8641SAndroid Build Coastguard Worker // Assignments.
450*635a8641SAndroid Build Coastguard Worker //
451*635a8641SAndroid Build Coastguard Worker // All of these may invalidate iterators and references.
452*635a8641SAndroid Build Coastguard Worker
453*635a8641SAndroid Build Coastguard Worker circular_deque& operator=(const circular_deque& other) {
454*635a8641SAndroid Build Coastguard Worker if (&other == this)
455*635a8641SAndroid Build Coastguard Worker return *this;
456*635a8641SAndroid Build Coastguard Worker
457*635a8641SAndroid Build Coastguard Worker reserve(other.size());
458*635a8641SAndroid Build Coastguard Worker assign(other.begin(), other.end());
459*635a8641SAndroid Build Coastguard Worker return *this;
460*635a8641SAndroid Build Coastguard Worker }
461*635a8641SAndroid Build Coastguard Worker circular_deque& operator=(circular_deque&& other) noexcept {
462*635a8641SAndroid Build Coastguard Worker if (&other == this)
463*635a8641SAndroid Build Coastguard Worker return *this;
464*635a8641SAndroid Build Coastguard Worker
465*635a8641SAndroid Build Coastguard Worker // We're about to overwrite the buffer, so don't free it in clear to
466*635a8641SAndroid Build Coastguard Worker // avoid doing it twice.
467*635a8641SAndroid Build Coastguard Worker ClearRetainCapacity();
468*635a8641SAndroid Build Coastguard Worker buffer_ = std::move(other.buffer_);
469*635a8641SAndroid Build Coastguard Worker begin_ = other.begin_;
470*635a8641SAndroid Build Coastguard Worker end_ = other.end_;
471*635a8641SAndroid Build Coastguard Worker
472*635a8641SAndroid Build Coastguard Worker other.begin_ = 0;
473*635a8641SAndroid Build Coastguard Worker other.end_ = 0;
474*635a8641SAndroid Build Coastguard Worker
475*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
476*635a8641SAndroid Build Coastguard Worker return *this;
477*635a8641SAndroid Build Coastguard Worker }
478*635a8641SAndroid Build Coastguard Worker circular_deque& operator=(std::initializer_list<value_type> ilist) {
479*635a8641SAndroid Build Coastguard Worker reserve(ilist.size());
480*635a8641SAndroid Build Coastguard Worker assign(std::begin(ilist), std::end(ilist));
481*635a8641SAndroid Build Coastguard Worker return *this;
482*635a8641SAndroid Build Coastguard Worker }
483*635a8641SAndroid Build Coastguard Worker
assign(size_type count,const value_type & value)484*635a8641SAndroid Build Coastguard Worker void assign(size_type count, const value_type& value) {
485*635a8641SAndroid Build Coastguard Worker ClearRetainCapacity();
486*635a8641SAndroid Build Coastguard Worker reserve(count);
487*635a8641SAndroid Build Coastguard Worker for (size_t i = 0; i < count; i++)
488*635a8641SAndroid Build Coastguard Worker emplace_back(value);
489*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
490*635a8641SAndroid Build Coastguard Worker }
491*635a8641SAndroid Build Coastguard Worker
492*635a8641SAndroid Build Coastguard Worker // This variant should be enabled only when InputIterator is an iterator.
493*635a8641SAndroid Build Coastguard Worker template <typename InputIterator>
494*635a8641SAndroid Build Coastguard Worker typename std::enable_if<::base::internal::is_iterator<InputIterator>::value,
495*635a8641SAndroid Build Coastguard Worker void>::type
assign(InputIterator first,InputIterator last)496*635a8641SAndroid Build Coastguard Worker assign(InputIterator first, InputIterator last) {
497*635a8641SAndroid Build Coastguard Worker // Possible future enhancement, dispatch on iterator tag type. For forward
498*635a8641SAndroid Build Coastguard Worker // iterators we can use std::difference to preallocate the space required
499*635a8641SAndroid Build Coastguard Worker // and only do one copy.
500*635a8641SAndroid Build Coastguard Worker ClearRetainCapacity();
501*635a8641SAndroid Build Coastguard Worker for (; first != last; ++first)
502*635a8641SAndroid Build Coastguard Worker emplace_back(*first);
503*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
504*635a8641SAndroid Build Coastguard Worker }
505*635a8641SAndroid Build Coastguard Worker
assign(std::initializer_list<value_type> value)506*635a8641SAndroid Build Coastguard Worker void assign(std::initializer_list<value_type> value) {
507*635a8641SAndroid Build Coastguard Worker reserve(std::distance(value.begin(), value.end()));
508*635a8641SAndroid Build Coastguard Worker assign(value.begin(), value.end());
509*635a8641SAndroid Build Coastguard Worker }
510*635a8641SAndroid Build Coastguard Worker
511*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
512*635a8641SAndroid Build Coastguard Worker // Accessors.
513*635a8641SAndroid Build Coastguard Worker //
514*635a8641SAndroid Build Coastguard Worker // Since this class assumes no exceptions, at() and operator[] are equivalent.
515*635a8641SAndroid Build Coastguard Worker
at(size_type i)516*635a8641SAndroid Build Coastguard Worker const value_type& at(size_type i) const {
517*635a8641SAndroid Build Coastguard Worker DCHECK(i < size());
518*635a8641SAndroid Build Coastguard Worker size_t right_size = buffer_.capacity() - begin_;
519*635a8641SAndroid Build Coastguard Worker if (begin_ <= end_ || i < right_size)
520*635a8641SAndroid Build Coastguard Worker return buffer_[begin_ + i];
521*635a8641SAndroid Build Coastguard Worker return buffer_[i - right_size];
522*635a8641SAndroid Build Coastguard Worker }
at(size_type i)523*635a8641SAndroid Build Coastguard Worker value_type& at(size_type i) {
524*635a8641SAndroid Build Coastguard Worker return const_cast<value_type&>(
525*635a8641SAndroid Build Coastguard Worker const_cast<const circular_deque*>(this)->at(i));
526*635a8641SAndroid Build Coastguard Worker }
527*635a8641SAndroid Build Coastguard Worker
528*635a8641SAndroid Build Coastguard Worker value_type& operator[](size_type i) { return at(i); }
529*635a8641SAndroid Build Coastguard Worker const value_type& operator[](size_type i) const {
530*635a8641SAndroid Build Coastguard Worker return const_cast<circular_deque*>(this)->at(i);
531*635a8641SAndroid Build Coastguard Worker }
532*635a8641SAndroid Build Coastguard Worker
front()533*635a8641SAndroid Build Coastguard Worker value_type& front() {
534*635a8641SAndroid Build Coastguard Worker DCHECK(!empty());
535*635a8641SAndroid Build Coastguard Worker return buffer_[begin_];
536*635a8641SAndroid Build Coastguard Worker }
front()537*635a8641SAndroid Build Coastguard Worker const value_type& front() const {
538*635a8641SAndroid Build Coastguard Worker DCHECK(!empty());
539*635a8641SAndroid Build Coastguard Worker return buffer_[begin_];
540*635a8641SAndroid Build Coastguard Worker }
541*635a8641SAndroid Build Coastguard Worker
back()542*635a8641SAndroid Build Coastguard Worker value_type& back() {
543*635a8641SAndroid Build Coastguard Worker DCHECK(!empty());
544*635a8641SAndroid Build Coastguard Worker return *(--end());
545*635a8641SAndroid Build Coastguard Worker }
back()546*635a8641SAndroid Build Coastguard Worker const value_type& back() const {
547*635a8641SAndroid Build Coastguard Worker DCHECK(!empty());
548*635a8641SAndroid Build Coastguard Worker return *(--end());
549*635a8641SAndroid Build Coastguard Worker }
550*635a8641SAndroid Build Coastguard Worker
551*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
552*635a8641SAndroid Build Coastguard Worker // Iterators.
553*635a8641SAndroid Build Coastguard Worker
begin()554*635a8641SAndroid Build Coastguard Worker iterator begin() { return iterator(this, begin_); }
begin()555*635a8641SAndroid Build Coastguard Worker const_iterator begin() const { return const_iterator(this, begin_); }
cbegin()556*635a8641SAndroid Build Coastguard Worker const_iterator cbegin() const { return const_iterator(this, begin_); }
557*635a8641SAndroid Build Coastguard Worker
end()558*635a8641SAndroid Build Coastguard Worker iterator end() { return iterator(this, end_); }
end()559*635a8641SAndroid Build Coastguard Worker const_iterator end() const { return const_iterator(this, end_); }
cend()560*635a8641SAndroid Build Coastguard Worker const_iterator cend() const { return const_iterator(this, end_); }
561*635a8641SAndroid Build Coastguard Worker
rbegin()562*635a8641SAndroid Build Coastguard Worker reverse_iterator rbegin() { return reverse_iterator(end()); }
rbegin()563*635a8641SAndroid Build Coastguard Worker const_reverse_iterator rbegin() const {
564*635a8641SAndroid Build Coastguard Worker return const_reverse_iterator(end());
565*635a8641SAndroid Build Coastguard Worker }
crbegin()566*635a8641SAndroid Build Coastguard Worker const_reverse_iterator crbegin() const { return rbegin(); }
567*635a8641SAndroid Build Coastguard Worker
rend()568*635a8641SAndroid Build Coastguard Worker reverse_iterator rend() { return reverse_iterator(begin()); }
rend()569*635a8641SAndroid Build Coastguard Worker const_reverse_iterator rend() const {
570*635a8641SAndroid Build Coastguard Worker return const_reverse_iterator(begin());
571*635a8641SAndroid Build Coastguard Worker }
crend()572*635a8641SAndroid Build Coastguard Worker const_reverse_iterator crend() const { return rend(); }
573*635a8641SAndroid Build Coastguard Worker
574*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
575*635a8641SAndroid Build Coastguard Worker // Memory management.
576*635a8641SAndroid Build Coastguard Worker
577*635a8641SAndroid Build Coastguard Worker // IMPORTANT NOTE ON reserve(...): This class implements auto-shrinking of
578*635a8641SAndroid Build Coastguard Worker // the buffer when elements are deleted and there is "too much" wasted space.
579*635a8641SAndroid Build Coastguard Worker // So if you call reserve() with a large size in anticipation of pushing many
580*635a8641SAndroid Build Coastguard Worker // elements, but pop an element before the queue is full, the capacity you
581*635a8641SAndroid Build Coastguard Worker // reserved may be lost.
582*635a8641SAndroid Build Coastguard Worker //
583*635a8641SAndroid Build Coastguard Worker // As a result, it's only worthwhile to call reserve() when you're adding
584*635a8641SAndroid Build Coastguard Worker // many things at once with no intermediate operations.
reserve(size_type new_capacity)585*635a8641SAndroid Build Coastguard Worker void reserve(size_type new_capacity) {
586*635a8641SAndroid Build Coastguard Worker if (new_capacity > capacity())
587*635a8641SAndroid Build Coastguard Worker SetCapacityTo(new_capacity);
588*635a8641SAndroid Build Coastguard Worker }
589*635a8641SAndroid Build Coastguard Worker
capacity()590*635a8641SAndroid Build Coastguard Worker size_type capacity() const {
591*635a8641SAndroid Build Coastguard Worker // One item is wasted to indicate end().
592*635a8641SAndroid Build Coastguard Worker return buffer_.capacity() == 0 ? 0 : buffer_.capacity() - 1;
593*635a8641SAndroid Build Coastguard Worker }
594*635a8641SAndroid Build Coastguard Worker
shrink_to_fit()595*635a8641SAndroid Build Coastguard Worker void shrink_to_fit() {
596*635a8641SAndroid Build Coastguard Worker if (empty()) {
597*635a8641SAndroid Build Coastguard Worker // Optimize empty case to really delete everything if there was
598*635a8641SAndroid Build Coastguard Worker // something.
599*635a8641SAndroid Build Coastguard Worker if (buffer_.capacity())
600*635a8641SAndroid Build Coastguard Worker buffer_ = VectorBuffer();
601*635a8641SAndroid Build Coastguard Worker } else {
602*635a8641SAndroid Build Coastguard Worker SetCapacityTo(size());
603*635a8641SAndroid Build Coastguard Worker }
604*635a8641SAndroid Build Coastguard Worker }
605*635a8641SAndroid Build Coastguard Worker
606*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
607*635a8641SAndroid Build Coastguard Worker // Size management.
608*635a8641SAndroid Build Coastguard Worker
609*635a8641SAndroid Build Coastguard Worker // This will additionally reset the capacity() to 0.
clear()610*635a8641SAndroid Build Coastguard Worker void clear() {
611*635a8641SAndroid Build Coastguard Worker // This can't resize(0) because that requires a default constructor to
612*635a8641SAndroid Build Coastguard Worker // compile, which not all contained classes may implement.
613*635a8641SAndroid Build Coastguard Worker ClearRetainCapacity();
614*635a8641SAndroid Build Coastguard Worker buffer_ = VectorBuffer();
615*635a8641SAndroid Build Coastguard Worker }
616*635a8641SAndroid Build Coastguard Worker
empty()617*635a8641SAndroid Build Coastguard Worker bool empty() const { return begin_ == end_; }
618*635a8641SAndroid Build Coastguard Worker
size()619*635a8641SAndroid Build Coastguard Worker size_type size() const {
620*635a8641SAndroid Build Coastguard Worker if (begin_ <= end_)
621*635a8641SAndroid Build Coastguard Worker return end_ - begin_;
622*635a8641SAndroid Build Coastguard Worker return buffer_.capacity() - begin_ + end_;
623*635a8641SAndroid Build Coastguard Worker }
624*635a8641SAndroid Build Coastguard Worker
625*635a8641SAndroid Build Coastguard Worker // When reducing size, the elements are deleted from the end. When expanding
626*635a8641SAndroid Build Coastguard Worker // size, elements are added to the end with |value| or the default
627*635a8641SAndroid Build Coastguard Worker // constructed version. Even when using resize(count) to shrink, a default
628*635a8641SAndroid Build Coastguard Worker // constructor is required for the code to compile, even though it will not
629*635a8641SAndroid Build Coastguard Worker // be called.
630*635a8641SAndroid Build Coastguard Worker //
631*635a8641SAndroid Build Coastguard Worker // There are two versions rather than using a default value to avoid
632*635a8641SAndroid Build Coastguard Worker // creating a temporary when shrinking (when it's not needed). Plus if
633*635a8641SAndroid Build Coastguard Worker // the default constructor is desired when expanding usually just calling it
634*635a8641SAndroid Build Coastguard Worker // for each element is faster than making a default-constructed temporary and
635*635a8641SAndroid Build Coastguard Worker // copying it.
resize(size_type count)636*635a8641SAndroid Build Coastguard Worker void resize(size_type count) {
637*635a8641SAndroid Build Coastguard Worker // SEE BELOW VERSION if you change this. The code is mostly the same.
638*635a8641SAndroid Build Coastguard Worker if (count > size()) {
639*635a8641SAndroid Build Coastguard Worker // This could be slighly more efficient but expanding a queue with
640*635a8641SAndroid Build Coastguard Worker // identical elements is unusual and the extra computations of emplacing
641*635a8641SAndroid Build Coastguard Worker // one-by-one will typically be small relative to calling the constructor
642*635a8641SAndroid Build Coastguard Worker // for every item.
643*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(count - size());
644*635a8641SAndroid Build Coastguard Worker while (size() < count)
645*635a8641SAndroid Build Coastguard Worker emplace_back();
646*635a8641SAndroid Build Coastguard Worker } else if (count < size()) {
647*635a8641SAndroid Build Coastguard Worker size_t new_end = (begin_ + count) % buffer_.capacity();
648*635a8641SAndroid Build Coastguard Worker DestructRange(new_end, end_);
649*635a8641SAndroid Build Coastguard Worker end_ = new_end;
650*635a8641SAndroid Build Coastguard Worker
651*635a8641SAndroid Build Coastguard Worker ShrinkCapacityIfNecessary();
652*635a8641SAndroid Build Coastguard Worker }
653*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
654*635a8641SAndroid Build Coastguard Worker }
resize(size_type count,const value_type & value)655*635a8641SAndroid Build Coastguard Worker void resize(size_type count, const value_type& value) {
656*635a8641SAndroid Build Coastguard Worker // SEE ABOVE VERSION if you change this. The code is mostly the same.
657*635a8641SAndroid Build Coastguard Worker if (count > size()) {
658*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(count - size());
659*635a8641SAndroid Build Coastguard Worker while (size() < count)
660*635a8641SAndroid Build Coastguard Worker emplace_back(value);
661*635a8641SAndroid Build Coastguard Worker } else if (count < size()) {
662*635a8641SAndroid Build Coastguard Worker size_t new_end = (begin_ + count) % buffer_.capacity();
663*635a8641SAndroid Build Coastguard Worker DestructRange(new_end, end_);
664*635a8641SAndroid Build Coastguard Worker end_ = new_end;
665*635a8641SAndroid Build Coastguard Worker
666*635a8641SAndroid Build Coastguard Worker ShrinkCapacityIfNecessary();
667*635a8641SAndroid Build Coastguard Worker }
668*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
669*635a8641SAndroid Build Coastguard Worker }
670*635a8641SAndroid Build Coastguard Worker
671*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
672*635a8641SAndroid Build Coastguard Worker // Insert and erase.
673*635a8641SAndroid Build Coastguard Worker //
674*635a8641SAndroid Build Coastguard Worker // Insertion and deletion in the middle is O(n) and invalidates all existing
675*635a8641SAndroid Build Coastguard Worker // iterators.
676*635a8641SAndroid Build Coastguard Worker //
677*635a8641SAndroid Build Coastguard Worker // The implementation of insert isn't optimized as much as it could be. If
678*635a8641SAndroid Build Coastguard Worker // the insertion requires that the buffer be grown, it will first be grown
679*635a8641SAndroid Build Coastguard Worker // and everything moved, and then the items will be inserted, potentially
680*635a8641SAndroid Build Coastguard Worker // moving some items twice. This simplifies the implemntation substantially
681*635a8641SAndroid Build Coastguard Worker // and means less generated templatized code. Since this is an uncommon
682*635a8641SAndroid Build Coastguard Worker // operation for deques, and already relatively slow, it doesn't seem worth
683*635a8641SAndroid Build Coastguard Worker // the benefit to optimize this.
684*635a8641SAndroid Build Coastguard Worker
insert(const_iterator pos,size_type count,const T & value)685*635a8641SAndroid Build Coastguard Worker void insert(const_iterator pos, size_type count, const T& value) {
686*635a8641SAndroid Build Coastguard Worker ValidateIterator(pos);
687*635a8641SAndroid Build Coastguard Worker
688*635a8641SAndroid Build Coastguard Worker // Optimize insert at the beginning.
689*635a8641SAndroid Build Coastguard Worker if (pos == begin()) {
690*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(count);
691*635a8641SAndroid Build Coastguard Worker for (size_t i = 0; i < count; i++)
692*635a8641SAndroid Build Coastguard Worker push_front(value);
693*635a8641SAndroid Build Coastguard Worker return;
694*635a8641SAndroid Build Coastguard Worker }
695*635a8641SAndroid Build Coastguard Worker
696*635a8641SAndroid Build Coastguard Worker iterator insert_cur(this, pos.index_);
697*635a8641SAndroid Build Coastguard Worker iterator insert_end;
698*635a8641SAndroid Build Coastguard Worker MakeRoomFor(count, &insert_cur, &insert_end);
699*635a8641SAndroid Build Coastguard Worker while (insert_cur < insert_end) {
700*635a8641SAndroid Build Coastguard Worker new (&buffer_[insert_cur.index_]) T(value);
701*635a8641SAndroid Build Coastguard Worker ++insert_cur;
702*635a8641SAndroid Build Coastguard Worker }
703*635a8641SAndroid Build Coastguard Worker
704*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
705*635a8641SAndroid Build Coastguard Worker }
706*635a8641SAndroid Build Coastguard Worker
707*635a8641SAndroid Build Coastguard Worker // This enable_if keeps this call from getting confused with the (pos, count,
708*635a8641SAndroid Build Coastguard Worker // value) version when value is an integer.
709*635a8641SAndroid Build Coastguard Worker template <class InputIterator>
710*635a8641SAndroid Build Coastguard Worker typename std::enable_if<::base::internal::is_iterator<InputIterator>::value,
711*635a8641SAndroid Build Coastguard Worker void>::type
insert(const_iterator pos,InputIterator first,InputIterator last)712*635a8641SAndroid Build Coastguard Worker insert(const_iterator pos, InputIterator first, InputIterator last) {
713*635a8641SAndroid Build Coastguard Worker ValidateIterator(pos);
714*635a8641SAndroid Build Coastguard Worker
715*635a8641SAndroid Build Coastguard Worker size_t inserted_items = std::distance(first, last);
716*635a8641SAndroid Build Coastguard Worker if (inserted_items == 0)
717*635a8641SAndroid Build Coastguard Worker return; // Can divide by 0 when doing modulo below, so return early.
718*635a8641SAndroid Build Coastguard Worker
719*635a8641SAndroid Build Coastguard Worker // Make a hole to copy the items into.
720*635a8641SAndroid Build Coastguard Worker iterator insert_cur;
721*635a8641SAndroid Build Coastguard Worker iterator insert_end;
722*635a8641SAndroid Build Coastguard Worker if (pos == begin()) {
723*635a8641SAndroid Build Coastguard Worker // Optimize insert at the beginning, nothing needs to be shifted and the
724*635a8641SAndroid Build Coastguard Worker // hole is the |inserted_items| block immediately before |begin_|.
725*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(inserted_items);
726*635a8641SAndroid Build Coastguard Worker insert_end = begin();
727*635a8641SAndroid Build Coastguard Worker begin_ =
728*635a8641SAndroid Build Coastguard Worker (begin_ + buffer_.capacity() - inserted_items) % buffer_.capacity();
729*635a8641SAndroid Build Coastguard Worker insert_cur = begin();
730*635a8641SAndroid Build Coastguard Worker } else {
731*635a8641SAndroid Build Coastguard Worker insert_cur = iterator(this, pos.index_);
732*635a8641SAndroid Build Coastguard Worker MakeRoomFor(inserted_items, &insert_cur, &insert_end);
733*635a8641SAndroid Build Coastguard Worker }
734*635a8641SAndroid Build Coastguard Worker
735*635a8641SAndroid Build Coastguard Worker // Copy the items.
736*635a8641SAndroid Build Coastguard Worker while (insert_cur < insert_end) {
737*635a8641SAndroid Build Coastguard Worker new (&buffer_[insert_cur.index_]) T(*first);
738*635a8641SAndroid Build Coastguard Worker ++insert_cur;
739*635a8641SAndroid Build Coastguard Worker ++first;
740*635a8641SAndroid Build Coastguard Worker }
741*635a8641SAndroid Build Coastguard Worker
742*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
743*635a8641SAndroid Build Coastguard Worker }
744*635a8641SAndroid Build Coastguard Worker
745*635a8641SAndroid Build Coastguard Worker // These all return an iterator to the inserted item. Existing iterators will
746*635a8641SAndroid Build Coastguard Worker // be invalidated.
insert(const_iterator pos,const T & value)747*635a8641SAndroid Build Coastguard Worker iterator insert(const_iterator pos, const T& value) {
748*635a8641SAndroid Build Coastguard Worker return emplace(pos, value);
749*635a8641SAndroid Build Coastguard Worker }
insert(const_iterator pos,T && value)750*635a8641SAndroid Build Coastguard Worker iterator insert(const_iterator pos, T&& value) {
751*635a8641SAndroid Build Coastguard Worker return emplace(pos, std::move(value));
752*635a8641SAndroid Build Coastguard Worker }
753*635a8641SAndroid Build Coastguard Worker template <class... Args>
emplace(const_iterator pos,Args &&...args)754*635a8641SAndroid Build Coastguard Worker iterator emplace(const_iterator pos, Args&&... args) {
755*635a8641SAndroid Build Coastguard Worker ValidateIterator(pos);
756*635a8641SAndroid Build Coastguard Worker
757*635a8641SAndroid Build Coastguard Worker // Optimize insert at beginning which doesn't require shifting.
758*635a8641SAndroid Build Coastguard Worker if (pos == cbegin()) {
759*635a8641SAndroid Build Coastguard Worker emplace_front(std::forward<Args>(args)...);
760*635a8641SAndroid Build Coastguard Worker return begin();
761*635a8641SAndroid Build Coastguard Worker }
762*635a8641SAndroid Build Coastguard Worker
763*635a8641SAndroid Build Coastguard Worker // Do this before we make the new iterators we return.
764*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
765*635a8641SAndroid Build Coastguard Worker
766*635a8641SAndroid Build Coastguard Worker iterator insert_begin(this, pos.index_);
767*635a8641SAndroid Build Coastguard Worker iterator insert_end;
768*635a8641SAndroid Build Coastguard Worker MakeRoomFor(1, &insert_begin, &insert_end);
769*635a8641SAndroid Build Coastguard Worker new (&buffer_[insert_begin.index_]) T(std::forward<Args>(args)...);
770*635a8641SAndroid Build Coastguard Worker
771*635a8641SAndroid Build Coastguard Worker return insert_begin;
772*635a8641SAndroid Build Coastguard Worker }
773*635a8641SAndroid Build Coastguard Worker
774*635a8641SAndroid Build Coastguard Worker // Calling erase() won't automatically resize the buffer smaller like resize
775*635a8641SAndroid Build Coastguard Worker // or the pop functions. Erase is slow and relatively uncommon, and for
776*635a8641SAndroid Build Coastguard Worker // normal deque usage a pop will normally be done on a regular basis that
777*635a8641SAndroid Build Coastguard Worker // will prevent excessive buffer usage over long periods of time. It's not
778*635a8641SAndroid Build Coastguard Worker // worth having the extra code for every template instantiation of erase()
779*635a8641SAndroid Build Coastguard Worker // to resize capacity downward to a new buffer.
erase(const_iterator pos)780*635a8641SAndroid Build Coastguard Worker iterator erase(const_iterator pos) { return erase(pos, pos + 1); }
erase(const_iterator first,const_iterator last)781*635a8641SAndroid Build Coastguard Worker iterator erase(const_iterator first, const_iterator last) {
782*635a8641SAndroid Build Coastguard Worker ValidateIterator(first);
783*635a8641SAndroid Build Coastguard Worker ValidateIterator(last);
784*635a8641SAndroid Build Coastguard Worker
785*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
786*635a8641SAndroid Build Coastguard Worker
787*635a8641SAndroid Build Coastguard Worker // First, call the destructor on the deleted items.
788*635a8641SAndroid Build Coastguard Worker if (first.index_ == last.index_) {
789*635a8641SAndroid Build Coastguard Worker // Nothing deleted. Need to return early to avoid falling through to
790*635a8641SAndroid Build Coastguard Worker // moving items on top of themselves.
791*635a8641SAndroid Build Coastguard Worker return iterator(this, first.index_);
792*635a8641SAndroid Build Coastguard Worker } else if (first.index_ < last.index_) {
793*635a8641SAndroid Build Coastguard Worker // Contiguous range.
794*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[first.index_], &buffer_[last.index_]);
795*635a8641SAndroid Build Coastguard Worker } else {
796*635a8641SAndroid Build Coastguard Worker // Deleted range wraps around.
797*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[first.index_],
798*635a8641SAndroid Build Coastguard Worker &buffer_[buffer_.capacity()]);
799*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[0], &buffer_[last.index_]);
800*635a8641SAndroid Build Coastguard Worker }
801*635a8641SAndroid Build Coastguard Worker
802*635a8641SAndroid Build Coastguard Worker if (first.index_ == begin_) {
803*635a8641SAndroid Build Coastguard Worker // This deletion is from the beginning. Nothing needs to be copied, only
804*635a8641SAndroid Build Coastguard Worker // begin_ needs to be updated.
805*635a8641SAndroid Build Coastguard Worker begin_ = last.index_;
806*635a8641SAndroid Build Coastguard Worker return iterator(this, last.index_);
807*635a8641SAndroid Build Coastguard Worker }
808*635a8641SAndroid Build Coastguard Worker
809*635a8641SAndroid Build Coastguard Worker // In an erase operation, the shifted items all move logically to the left,
810*635a8641SAndroid Build Coastguard Worker // so move them from left-to-right.
811*635a8641SAndroid Build Coastguard Worker iterator move_src(this, last.index_);
812*635a8641SAndroid Build Coastguard Worker iterator move_src_end = end();
813*635a8641SAndroid Build Coastguard Worker iterator move_dest(this, first.index_);
814*635a8641SAndroid Build Coastguard Worker for (; move_src < move_src_end; move_src++, move_dest++) {
815*635a8641SAndroid Build Coastguard Worker buffer_.MoveRange(&buffer_[move_src.index_],
816*635a8641SAndroid Build Coastguard Worker &buffer_[move_src.index_ + 1],
817*635a8641SAndroid Build Coastguard Worker &buffer_[move_dest.index_]);
818*635a8641SAndroid Build Coastguard Worker }
819*635a8641SAndroid Build Coastguard Worker
820*635a8641SAndroid Build Coastguard Worker end_ = move_dest.index_;
821*635a8641SAndroid Build Coastguard Worker
822*635a8641SAndroid Build Coastguard Worker // Since we did not reallocate and only changed things after the erase
823*635a8641SAndroid Build Coastguard Worker // element(s), the input iterator's index points to the thing following the
824*635a8641SAndroid Build Coastguard Worker // deletion.
825*635a8641SAndroid Build Coastguard Worker return iterator(this, first.index_);
826*635a8641SAndroid Build Coastguard Worker }
827*635a8641SAndroid Build Coastguard Worker
828*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
829*635a8641SAndroid Build Coastguard Worker // Begin/end operations.
830*635a8641SAndroid Build Coastguard Worker
push_front(const T & value)831*635a8641SAndroid Build Coastguard Worker void push_front(const T& value) { emplace_front(value); }
push_front(T && value)832*635a8641SAndroid Build Coastguard Worker void push_front(T&& value) { emplace_front(std::move(value)); }
833*635a8641SAndroid Build Coastguard Worker
push_back(const T & value)834*635a8641SAndroid Build Coastguard Worker void push_back(const T& value) { emplace_back(value); }
push_back(T && value)835*635a8641SAndroid Build Coastguard Worker void push_back(T&& value) { emplace_back(std::move(value)); }
836*635a8641SAndroid Build Coastguard Worker
837*635a8641SAndroid Build Coastguard Worker template <class... Args>
emplace_front(Args &&...args)838*635a8641SAndroid Build Coastguard Worker reference emplace_front(Args&&... args) {
839*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(1);
840*635a8641SAndroid Build Coastguard Worker if (begin_ == 0)
841*635a8641SAndroid Build Coastguard Worker begin_ = buffer_.capacity() - 1;
842*635a8641SAndroid Build Coastguard Worker else
843*635a8641SAndroid Build Coastguard Worker begin_--;
844*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
845*635a8641SAndroid Build Coastguard Worker new (&buffer_[begin_]) T(std::forward<Args>(args)...);
846*635a8641SAndroid Build Coastguard Worker return front();
847*635a8641SAndroid Build Coastguard Worker }
848*635a8641SAndroid Build Coastguard Worker
849*635a8641SAndroid Build Coastguard Worker template <class... Args>
emplace_back(Args &&...args)850*635a8641SAndroid Build Coastguard Worker reference emplace_back(Args&&... args) {
851*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(1);
852*635a8641SAndroid Build Coastguard Worker new (&buffer_[end_]) T(std::forward<Args>(args)...);
853*635a8641SAndroid Build Coastguard Worker if (end_ == buffer_.capacity() - 1)
854*635a8641SAndroid Build Coastguard Worker end_ = 0;
855*635a8641SAndroid Build Coastguard Worker else
856*635a8641SAndroid Build Coastguard Worker end_++;
857*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
858*635a8641SAndroid Build Coastguard Worker return back();
859*635a8641SAndroid Build Coastguard Worker }
860*635a8641SAndroid Build Coastguard Worker
pop_front()861*635a8641SAndroid Build Coastguard Worker void pop_front() {
862*635a8641SAndroid Build Coastguard Worker DCHECK(size());
863*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[begin_], &buffer_[begin_ + 1]);
864*635a8641SAndroid Build Coastguard Worker begin_++;
865*635a8641SAndroid Build Coastguard Worker if (begin_ == buffer_.capacity())
866*635a8641SAndroid Build Coastguard Worker begin_ = 0;
867*635a8641SAndroid Build Coastguard Worker
868*635a8641SAndroid Build Coastguard Worker ShrinkCapacityIfNecessary();
869*635a8641SAndroid Build Coastguard Worker
870*635a8641SAndroid Build Coastguard Worker // Technically popping will not invalidate any iterators since the
871*635a8641SAndroid Build Coastguard Worker // underlying buffer will be stable. But in the future we may want to add a
872*635a8641SAndroid Build Coastguard Worker // feature that resizes the buffer smaller if there is too much wasted
873*635a8641SAndroid Build Coastguard Worker // space. This ensures we can make such a change safely.
874*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
875*635a8641SAndroid Build Coastguard Worker }
pop_back()876*635a8641SAndroid Build Coastguard Worker void pop_back() {
877*635a8641SAndroid Build Coastguard Worker DCHECK(size());
878*635a8641SAndroid Build Coastguard Worker if (end_ == 0)
879*635a8641SAndroid Build Coastguard Worker end_ = buffer_.capacity() - 1;
880*635a8641SAndroid Build Coastguard Worker else
881*635a8641SAndroid Build Coastguard Worker end_--;
882*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[end_], &buffer_[end_ + 1]);
883*635a8641SAndroid Build Coastguard Worker
884*635a8641SAndroid Build Coastguard Worker ShrinkCapacityIfNecessary();
885*635a8641SAndroid Build Coastguard Worker
886*635a8641SAndroid Build Coastguard Worker // See pop_front comment about why this is here.
887*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
888*635a8641SAndroid Build Coastguard Worker }
889*635a8641SAndroid Build Coastguard Worker
890*635a8641SAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
891*635a8641SAndroid Build Coastguard Worker // General operations.
892*635a8641SAndroid Build Coastguard Worker
swap(circular_deque & other)893*635a8641SAndroid Build Coastguard Worker void swap(circular_deque& other) {
894*635a8641SAndroid Build Coastguard Worker std::swap(buffer_, other.buffer_);
895*635a8641SAndroid Build Coastguard Worker std::swap(begin_, other.begin_);
896*635a8641SAndroid Build Coastguard Worker std::swap(end_, other.end_);
897*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
898*635a8641SAndroid Build Coastguard Worker }
899*635a8641SAndroid Build Coastguard Worker
swap(circular_deque & lhs,circular_deque & rhs)900*635a8641SAndroid Build Coastguard Worker friend void swap(circular_deque& lhs, circular_deque& rhs) { lhs.swap(rhs); }
901*635a8641SAndroid Build Coastguard Worker
902*635a8641SAndroid Build Coastguard Worker private:
903*635a8641SAndroid Build Coastguard Worker friend internal::circular_deque_iterator<T>;
904*635a8641SAndroid Build Coastguard Worker friend internal::circular_deque_const_iterator<T>;
905*635a8641SAndroid Build Coastguard Worker
906*635a8641SAndroid Build Coastguard Worker // Moves the items in the given circular buffer to the current one. The
907*635a8641SAndroid Build Coastguard Worker // source is moved from so will become invalid. The destination buffer must
908*635a8641SAndroid Build Coastguard Worker // have already been allocated with enough size.
MoveBuffer(VectorBuffer & from_buf,size_t from_begin,size_t from_end,VectorBuffer * to_buf,size_t * to_begin,size_t * to_end)909*635a8641SAndroid Build Coastguard Worker static void MoveBuffer(VectorBuffer& from_buf,
910*635a8641SAndroid Build Coastguard Worker size_t from_begin,
911*635a8641SAndroid Build Coastguard Worker size_t from_end,
912*635a8641SAndroid Build Coastguard Worker VectorBuffer* to_buf,
913*635a8641SAndroid Build Coastguard Worker size_t* to_begin,
914*635a8641SAndroid Build Coastguard Worker size_t* to_end) {
915*635a8641SAndroid Build Coastguard Worker size_t from_capacity = from_buf.capacity();
916*635a8641SAndroid Build Coastguard Worker
917*635a8641SAndroid Build Coastguard Worker *to_begin = 0;
918*635a8641SAndroid Build Coastguard Worker if (from_begin < from_end) {
919*635a8641SAndroid Build Coastguard Worker // Contiguous.
920*635a8641SAndroid Build Coastguard Worker from_buf.MoveRange(&from_buf[from_begin], &from_buf[from_end],
921*635a8641SAndroid Build Coastguard Worker to_buf->begin());
922*635a8641SAndroid Build Coastguard Worker *to_end = from_end - from_begin;
923*635a8641SAndroid Build Coastguard Worker } else if (from_begin > from_end) {
924*635a8641SAndroid Build Coastguard Worker // Discontiguous, copy the right side to the beginning of the new buffer.
925*635a8641SAndroid Build Coastguard Worker from_buf.MoveRange(&from_buf[from_begin], &from_buf[from_capacity],
926*635a8641SAndroid Build Coastguard Worker to_buf->begin());
927*635a8641SAndroid Build Coastguard Worker size_t right_size = from_capacity - from_begin;
928*635a8641SAndroid Build Coastguard Worker // Append the left side.
929*635a8641SAndroid Build Coastguard Worker from_buf.MoveRange(&from_buf[0], &from_buf[from_end],
930*635a8641SAndroid Build Coastguard Worker &(*to_buf)[right_size]);
931*635a8641SAndroid Build Coastguard Worker *to_end = right_size + from_end;
932*635a8641SAndroid Build Coastguard Worker } else {
933*635a8641SAndroid Build Coastguard Worker // No items.
934*635a8641SAndroid Build Coastguard Worker *to_end = 0;
935*635a8641SAndroid Build Coastguard Worker }
936*635a8641SAndroid Build Coastguard Worker }
937*635a8641SAndroid Build Coastguard Worker
938*635a8641SAndroid Build Coastguard Worker // Expands the buffer size. This assumes the size is larger than the
939*635a8641SAndroid Build Coastguard Worker // number of elements in the vector (it won't call delete on anything).
SetCapacityTo(size_t new_capacity)940*635a8641SAndroid Build Coastguard Worker void SetCapacityTo(size_t new_capacity) {
941*635a8641SAndroid Build Coastguard Worker // Use the capacity + 1 as the internal buffer size to differentiate
942*635a8641SAndroid Build Coastguard Worker // empty and full (see definition of buffer_ below).
943*635a8641SAndroid Build Coastguard Worker VectorBuffer new_buffer(new_capacity + 1);
944*635a8641SAndroid Build Coastguard Worker MoveBuffer(buffer_, begin_, end_, &new_buffer, &begin_, &end_);
945*635a8641SAndroid Build Coastguard Worker buffer_ = std::move(new_buffer);
946*635a8641SAndroid Build Coastguard Worker }
ExpandCapacityIfNecessary(size_t additional_elts)947*635a8641SAndroid Build Coastguard Worker void ExpandCapacityIfNecessary(size_t additional_elts) {
948*635a8641SAndroid Build Coastguard Worker size_t min_new_capacity = size() + additional_elts;
949*635a8641SAndroid Build Coastguard Worker if (capacity() >= min_new_capacity)
950*635a8641SAndroid Build Coastguard Worker return; // Already enough room.
951*635a8641SAndroid Build Coastguard Worker
952*635a8641SAndroid Build Coastguard Worker min_new_capacity =
953*635a8641SAndroid Build Coastguard Worker std::max(min_new_capacity, internal::kCircularBufferInitialCapacity);
954*635a8641SAndroid Build Coastguard Worker
955*635a8641SAndroid Build Coastguard Worker // std::vector always grows by at least 50%. WTF::Deque grows by at least
956*635a8641SAndroid Build Coastguard Worker // 25%. We expect queue workloads to generally stay at a similar size and
957*635a8641SAndroid Build Coastguard Worker // grow less than a vector might, so use 25%.
958*635a8641SAndroid Build Coastguard Worker size_t new_capacity =
959*635a8641SAndroid Build Coastguard Worker std::max(min_new_capacity, capacity() + capacity() / 4);
960*635a8641SAndroid Build Coastguard Worker SetCapacityTo(new_capacity);
961*635a8641SAndroid Build Coastguard Worker }
962*635a8641SAndroid Build Coastguard Worker
ShrinkCapacityIfNecessary()963*635a8641SAndroid Build Coastguard Worker void ShrinkCapacityIfNecessary() {
964*635a8641SAndroid Build Coastguard Worker // Don't auto-shrink below this size.
965*635a8641SAndroid Build Coastguard Worker if (capacity() <= internal::kCircularBufferInitialCapacity)
966*635a8641SAndroid Build Coastguard Worker return;
967*635a8641SAndroid Build Coastguard Worker
968*635a8641SAndroid Build Coastguard Worker // Shrink when 100% of the size() is wasted.
969*635a8641SAndroid Build Coastguard Worker size_t sz = size();
970*635a8641SAndroid Build Coastguard Worker size_t empty_spaces = capacity() - sz;
971*635a8641SAndroid Build Coastguard Worker if (empty_spaces < sz)
972*635a8641SAndroid Build Coastguard Worker return;
973*635a8641SAndroid Build Coastguard Worker
974*635a8641SAndroid Build Coastguard Worker // Leave 1/4 the size as free capacity, not going below the initial
975*635a8641SAndroid Build Coastguard Worker // capacity.
976*635a8641SAndroid Build Coastguard Worker size_t new_capacity =
977*635a8641SAndroid Build Coastguard Worker std::max(internal::kCircularBufferInitialCapacity, sz + sz / 4);
978*635a8641SAndroid Build Coastguard Worker if (new_capacity < capacity()) {
979*635a8641SAndroid Build Coastguard Worker // Count extra item to convert to internal capacity.
980*635a8641SAndroid Build Coastguard Worker SetCapacityTo(new_capacity);
981*635a8641SAndroid Build Coastguard Worker }
982*635a8641SAndroid Build Coastguard Worker }
983*635a8641SAndroid Build Coastguard Worker
984*635a8641SAndroid Build Coastguard Worker // Backend for clear() but does not resize the internal buffer.
ClearRetainCapacity()985*635a8641SAndroid Build Coastguard Worker void ClearRetainCapacity() {
986*635a8641SAndroid Build Coastguard Worker // This can't resize(0) because that requires a default constructor to
987*635a8641SAndroid Build Coastguard Worker // compile, which not all contained classes may implement.
988*635a8641SAndroid Build Coastguard Worker DestructRange(begin_, end_);
989*635a8641SAndroid Build Coastguard Worker begin_ = 0;
990*635a8641SAndroid Build Coastguard Worker end_ = 0;
991*635a8641SAndroid Build Coastguard Worker IncrementGeneration();
992*635a8641SAndroid Build Coastguard Worker }
993*635a8641SAndroid Build Coastguard Worker
994*635a8641SAndroid Build Coastguard Worker // Calls destructors for the given begin->end indices. The indices may wrap
995*635a8641SAndroid Build Coastguard Worker // around. The buffer is not resized, and the begin_ and end_ members are
996*635a8641SAndroid Build Coastguard Worker // not changed.
DestructRange(size_t begin,size_t end)997*635a8641SAndroid Build Coastguard Worker void DestructRange(size_t begin, size_t end) {
998*635a8641SAndroid Build Coastguard Worker if (end == begin) {
999*635a8641SAndroid Build Coastguard Worker return;
1000*635a8641SAndroid Build Coastguard Worker } else if (end > begin) {
1001*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[begin], &buffer_[end]);
1002*635a8641SAndroid Build Coastguard Worker } else {
1003*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[begin], &buffer_[buffer_.capacity()]);
1004*635a8641SAndroid Build Coastguard Worker buffer_.DestructRange(&buffer_[0], &buffer_[end]);
1005*635a8641SAndroid Build Coastguard Worker }
1006*635a8641SAndroid Build Coastguard Worker }
1007*635a8641SAndroid Build Coastguard Worker
1008*635a8641SAndroid Build Coastguard Worker // Makes room for |count| items starting at |*insert_begin|. Since iterators
1009*635a8641SAndroid Build Coastguard Worker // are not stable across buffer resizes, |*insert_begin| will be updated to
1010*635a8641SAndroid Build Coastguard Worker // point to the beginning of the newly opened position in the new array (it's
1011*635a8641SAndroid Build Coastguard Worker // in/out), and the end of the newly opened position (it's out-only).
MakeRoomFor(size_t count,iterator * insert_begin,iterator * insert_end)1012*635a8641SAndroid Build Coastguard Worker void MakeRoomFor(size_t count, iterator* insert_begin, iterator* insert_end) {
1013*635a8641SAndroid Build Coastguard Worker if (count == 0) {
1014*635a8641SAndroid Build Coastguard Worker *insert_end = *insert_begin;
1015*635a8641SAndroid Build Coastguard Worker return;
1016*635a8641SAndroid Build Coastguard Worker }
1017*635a8641SAndroid Build Coastguard Worker
1018*635a8641SAndroid Build Coastguard Worker // The offset from the beginning will be stable across reallocations.
1019*635a8641SAndroid Build Coastguard Worker size_t begin_offset = insert_begin->OffsetFromBegin();
1020*635a8641SAndroid Build Coastguard Worker ExpandCapacityIfNecessary(count);
1021*635a8641SAndroid Build Coastguard Worker
1022*635a8641SAndroid Build Coastguard Worker insert_begin->index_ = (begin_ + begin_offset) % buffer_.capacity();
1023*635a8641SAndroid Build Coastguard Worker *insert_end =
1024*635a8641SAndroid Build Coastguard Worker iterator(this, (insert_begin->index_ + count) % buffer_.capacity());
1025*635a8641SAndroid Build Coastguard Worker
1026*635a8641SAndroid Build Coastguard Worker // Update the new end and prepare the iterators for copying.
1027*635a8641SAndroid Build Coastguard Worker iterator src = end();
1028*635a8641SAndroid Build Coastguard Worker end_ = (end_ + count) % buffer_.capacity();
1029*635a8641SAndroid Build Coastguard Worker iterator dest = end();
1030*635a8641SAndroid Build Coastguard Worker
1031*635a8641SAndroid Build Coastguard Worker // Move the elements. This will always involve shifting logically to the
1032*635a8641SAndroid Build Coastguard Worker // right, so move in a right-to-left order.
1033*635a8641SAndroid Build Coastguard Worker while (true) {
1034*635a8641SAndroid Build Coastguard Worker if (src == *insert_begin)
1035*635a8641SAndroid Build Coastguard Worker break;
1036*635a8641SAndroid Build Coastguard Worker --src;
1037*635a8641SAndroid Build Coastguard Worker --dest;
1038*635a8641SAndroid Build Coastguard Worker buffer_.MoveRange(&buffer_[src.index_], &buffer_[src.index_ + 1],
1039*635a8641SAndroid Build Coastguard Worker &buffer_[dest.index_]);
1040*635a8641SAndroid Build Coastguard Worker }
1041*635a8641SAndroid Build Coastguard Worker }
1042*635a8641SAndroid Build Coastguard Worker
1043*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
1044*635a8641SAndroid Build Coastguard Worker // Asserts the given index is dereferencable. The index is an index into the
1045*635a8641SAndroid Build Coastguard Worker // buffer, not an index used by operator[] or at() which will be offsets from
1046*635a8641SAndroid Build Coastguard Worker // begin.
CheckValidIndex(size_t i)1047*635a8641SAndroid Build Coastguard Worker void CheckValidIndex(size_t i) const {
1048*635a8641SAndroid Build Coastguard Worker if (begin_ <= end_)
1049*635a8641SAndroid Build Coastguard Worker DCHECK(i >= begin_ && i < end_);
1050*635a8641SAndroid Build Coastguard Worker else
1051*635a8641SAndroid Build Coastguard Worker DCHECK((i >= begin_ && i < buffer_.capacity()) || i < end_);
1052*635a8641SAndroid Build Coastguard Worker }
1053*635a8641SAndroid Build Coastguard Worker
1054*635a8641SAndroid Build Coastguard Worker // Asserts the given index is either dereferencable or points to end().
CheckValidIndexOrEnd(size_t i)1055*635a8641SAndroid Build Coastguard Worker void CheckValidIndexOrEnd(size_t i) const {
1056*635a8641SAndroid Build Coastguard Worker if (i != end_)
1057*635a8641SAndroid Build Coastguard Worker CheckValidIndex(i);
1058*635a8641SAndroid Build Coastguard Worker }
1059*635a8641SAndroid Build Coastguard Worker
ValidateIterator(const const_iterator & i)1060*635a8641SAndroid Build Coastguard Worker void ValidateIterator(const const_iterator& i) const {
1061*635a8641SAndroid Build Coastguard Worker DCHECK(i.parent_deque_ == this);
1062*635a8641SAndroid Build Coastguard Worker i.CheckUnstableUsage();
1063*635a8641SAndroid Build Coastguard Worker }
1064*635a8641SAndroid Build Coastguard Worker
1065*635a8641SAndroid Build Coastguard Worker // See generation_ below.
IncrementGeneration()1066*635a8641SAndroid Build Coastguard Worker void IncrementGeneration() { generation_++; }
1067*635a8641SAndroid Build Coastguard Worker #else
1068*635a8641SAndroid Build Coastguard Worker // No-op versions of these functions for release builds.
CheckValidIndex(size_t)1069*635a8641SAndroid Build Coastguard Worker void CheckValidIndex(size_t) const {}
CheckValidIndexOrEnd(size_t)1070*635a8641SAndroid Build Coastguard Worker void CheckValidIndexOrEnd(size_t) const {}
ValidateIterator(const const_iterator & i)1071*635a8641SAndroid Build Coastguard Worker void ValidateIterator(const const_iterator& i) const {}
IncrementGeneration()1072*635a8641SAndroid Build Coastguard Worker void IncrementGeneration() {}
1073*635a8641SAndroid Build Coastguard Worker #endif
1074*635a8641SAndroid Build Coastguard Worker
1075*635a8641SAndroid Build Coastguard Worker // Danger, the buffer_.capacity() is the "internal capacity" which is
1076*635a8641SAndroid Build Coastguard Worker // capacity() + 1 since there is an extra item to indicate the end. Otherwise
1077*635a8641SAndroid Build Coastguard Worker // being completely empty and completely full are indistinguishable (begin ==
1078*635a8641SAndroid Build Coastguard Worker // end). We could add a separate flag to avoid it, but that adds significant
1079*635a8641SAndroid Build Coastguard Worker // extra complexity since every computation will have to check for it. Always
1080*635a8641SAndroid Build Coastguard Worker // keeping one extra unused element in the buffer makes iterator computations
1081*635a8641SAndroid Build Coastguard Worker // much simpler.
1082*635a8641SAndroid Build Coastguard Worker //
1083*635a8641SAndroid Build Coastguard Worker // Container internal code will want to use buffer_.capacity() for offset
1084*635a8641SAndroid Build Coastguard Worker // computations rather than capacity().
1085*635a8641SAndroid Build Coastguard Worker VectorBuffer buffer_;
1086*635a8641SAndroid Build Coastguard Worker size_type begin_ = 0;
1087*635a8641SAndroid Build Coastguard Worker size_type end_ = 0;
1088*635a8641SAndroid Build Coastguard Worker
1089*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
1090*635a8641SAndroid Build Coastguard Worker // Incremented every time a modification is made that could affect iterator
1091*635a8641SAndroid Build Coastguard Worker // invalidations.
1092*635a8641SAndroid Build Coastguard Worker uint64_t generation_ = 0;
1093*635a8641SAndroid Build Coastguard Worker #endif
1094*635a8641SAndroid Build Coastguard Worker };
1095*635a8641SAndroid Build Coastguard Worker
1096*635a8641SAndroid Build Coastguard Worker // Implementations of base::Erase[If] (see base/stl_util.h).
1097*635a8641SAndroid Build Coastguard Worker template <class T, class Value>
Erase(circular_deque<T> & container,const Value & value)1098*635a8641SAndroid Build Coastguard Worker void Erase(circular_deque<T>& container, const Value& value) {
1099*635a8641SAndroid Build Coastguard Worker container.erase(std::remove(container.begin(), container.end(), value),
1100*635a8641SAndroid Build Coastguard Worker container.end());
1101*635a8641SAndroid Build Coastguard Worker }
1102*635a8641SAndroid Build Coastguard Worker
1103*635a8641SAndroid Build Coastguard Worker template <class T, class Predicate>
EraseIf(circular_deque<T> & container,Predicate pred)1104*635a8641SAndroid Build Coastguard Worker void EraseIf(circular_deque<T>& container, Predicate pred) {
1105*635a8641SAndroid Build Coastguard Worker container.erase(std::remove_if(container.begin(), container.end(), pred),
1106*635a8641SAndroid Build Coastguard Worker container.end());
1107*635a8641SAndroid Build Coastguard Worker }
1108*635a8641SAndroid Build Coastguard Worker
1109*635a8641SAndroid Build Coastguard Worker } // namespace base
1110*635a8641SAndroid Build Coastguard Worker
1111*635a8641SAndroid Build Coastguard Worker #endif // BASE_CONTAINERS_CIRCULAR_DEQUE_H_
1112