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