1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/*
* Copyright (C) 2007-2017 Apple Inc. All rights reserved.
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
// FIXME: Could move what Vector and Deque share into a separate file.
// Deque doesn't actually use Vector.
#include <algorithm>
#include <iterator>
#include <wtf/Vector.h>
namespace WTF {
template<typename T, size_t inlineCapacity> class DequeIteratorBase;
template<typename T, size_t inlineCapacity> class DequeIterator;
template<typename T, size_t inlineCapacity> class DequeConstIterator;
template<typename T, size_t inlineCapacity> class Deque final {
WTF_MAKE_FAST_ALLOCATED;
public:
typedef T ValueType;
typedef DequeIterator<T, inlineCapacity> iterator;
typedef DequeConstIterator<T, inlineCapacity> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Deque();
Deque(std::initializer_list<T>);
Deque(const Deque&);
Deque(Deque&&);
~Deque();
Deque& operator=(const Deque&);
Deque& operator=(Deque&&);
void swap(Deque&);
size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; }
bool isEmpty() const { return m_start == m_end; }
iterator begin() { return iterator(this, m_start); }
iterator end() { return iterator(this, m_end); }
const_iterator begin() const { return const_iterator(this, m_start); }
const_iterator end() const { return const_iterator(this, m_end); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
template<typename U> bool contains(const U&) const;
T& first() { return m_buffer.capacitySpan()[m_start]; }
const T& first() const { return m_buffer.capacitySpan()[m_start]; }
T takeFirst();
T& last() { return m_end ? m_buffer.capacitySpan()[m_end - 1] : m_buffer.capacitySpan().back(); }
const T& last() const { return m_end ? m_buffer.capacitySpan()[m_end - 1] : m_buffer.capacitySpan().back(); }
T takeLast();
void append(T&& value) { append<T>(std::forward<T>(value)); }
template<typename U> void append(U&&);
template<typename U> void prepend(U&&);
void removeFirst();
void removeLast();
void remove(iterator&);
void remove(const_iterator&);
template<typename Func> size_t removeAllMatching(const Func&);
template<typename Func> bool removeFirstMatching(const Func&);
// This is a priority enqueue. The callback is given a value, and if it returns true, then this
// will put the appended value before that value. It will keep bubbling until the callback returns
// false or the value ends up at the head of the queue.
template<typename U, typename Func>
void appendAndBubble(U&&, const Func&);
// Remove and return the first element for which the callback returns true. Returns a null version of
// T if it the callback always returns false.
template<typename Func>
T takeFirst(const Func&);
// Remove and return the last element for which the callback returns true. Returns a null version of
// T if it the callback always returns false.
template<typename Func>
T takeLast(const Func&);
void clear();
template<typename Predicate> iterator findIf(NOESCAPE const Predicate&);
template<typename Predicate> const_iterator findIf(NOESCAPE const Predicate&) const;
template<typename Predicate> bool containsIf(NOESCAPE const Predicate& predicate) const
{
return findIf(predicate) != end();
}
private:
friend class DequeIteratorBase<T, inlineCapacity>;
typedef VectorBuffer<T, inlineCapacity> Buffer;
typedef VectorTypeOperations<T> TypeOperations;
typedef DequeIteratorBase<T, inlineCapacity> IteratorBase;
void remove(size_t position);
void invalidateIterators();
void destroyAll();
void checkValidity() const;
void checkIndexValidity(size_t) const;
void expandCapacityIfNeeded();
void expandCapacity();
size_t m_start;
size_t m_end;
Buffer m_buffer;
#ifndef NDEBUG
mutable IteratorBase* m_iterators;
#endif
};
template<typename T, size_t inlineCapacity = 0>
class DequeIteratorBase {
WTF_MAKE_FAST_ALLOCATED;
protected:
DequeIteratorBase();
DequeIteratorBase(const Deque<T, inlineCapacity>*, size_t);
DequeIteratorBase(const DequeIteratorBase&);
DequeIteratorBase& operator=(const DequeIteratorBase&);
~DequeIteratorBase();
void assign(const DequeIteratorBase& other) { *this = other; }
void increment(std::ptrdiff_t count = 1);
void decrement();
T* before() const;
T* after() const;
bool isEqual(const DequeIteratorBase&) const;
private:
void addToIteratorsList();
void removeFromIteratorsList();
void checkValidity() const;
void checkValidity(const DequeIteratorBase&) const;
Deque<T, inlineCapacity>* m_deque;
size_t m_index;
friend class Deque<T, inlineCapacity>;
#ifndef NDEBUG
mutable DequeIteratorBase* m_next;
mutable DequeIteratorBase* m_previous;
#endif
};
template<typename T, size_t inlineCapacity = 0>
class DequeIterator : public DequeIteratorBase<T, inlineCapacity> {
WTF_MAKE_FAST_ALLOCATED;
private:
typedef DequeIteratorBase<T, inlineCapacity> Base;
typedef DequeIterator<T, inlineCapacity> Iterator;
public:
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef std::bidirectional_iterator_tag iterator_category;
DequeIterator(Deque<T, inlineCapacity>* deque, size_t index)
: Base(deque, index) { }
DequeIterator(const Iterator& other) : Base(other) { }
DequeIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
T& operator*() const { return *Base::after(); }
T* operator->() const { return Base::after(); }
bool operator==(const Iterator& other) const { return Base::isEqual(other); }
Iterator& operator++() { Base::increment(); return *this; }
// postfix ++ intentionally omitted
Iterator& operator--() { Base::decrement(); return *this; }
// postfix -- intentionally omitted
// Only forwarding + unsigned is supported.
Iterator& operator+=(size_t count) { Base::increment(count); return *this; }
Iterator operator+(size_t count) const { Iterator result(*this); result += count; return result; }
};
template<typename T, size_t inlineCapacity = 0>
class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity> {
WTF_MAKE_FAST_ALLOCATED;
private:
typedef DequeIteratorBase<T, inlineCapacity> Base;
typedef DequeConstIterator<T, inlineCapacity> Iterator;
typedef DequeIterator<T, inlineCapacity> NonConstIterator;
public:
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef std::bidirectional_iterator_tag iterator_category;
DequeConstIterator(const Deque<T, inlineCapacity>* deque, size_t index)
: Base(deque, index) { }
DequeConstIterator(const Iterator& other) : Base(other) { }
DequeConstIterator(const NonConstIterator& other) : Base(other) { }
DequeConstIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
DequeConstIterator& operator=(const NonConstIterator& other) { Base::assign(other); return *this; }
const T& operator*() const { return *Base::after(); }
const T* operator->() const { return Base::after(); }
bool operator==(const Iterator& other) const { return Base::isEqual(other); }
Iterator& operator++() { Base::increment(); return *this; }
// postfix ++ intentionally omitted
Iterator& operator--() { Base::decrement(); return *this; }
// postfix -- intentionally omitted
// Only forwarding + unsigned is supported.
Iterator& operator+=(size_t count) { Base::increment(count); return *this; }
Iterator operator+(size_t count) const { Iterator result(*this); result += count; return result; }
};
#ifdef NDEBUG
template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::checkValidity() const { }
template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::checkIndexValidity(size_t) const { }
template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::invalidateIterators() { }
#else
template<typename T, size_t inlineCapacity>
void Deque<T, inlineCapacity>::checkValidity() const
{
// In this implementation a capacity of 1 would confuse append() and
// other places that assume the index after capacity - 1 is 0.
ASSERT(m_buffer.capacity() != 1);
if (!m_buffer.capacity()) {
ASSERT(!m_start);
ASSERT(!m_end);
} else {
ASSERT(m_start < m_buffer.capacity());
ASSERT(m_end < m_buffer.capacity());
}
}
template<typename T, size_t inlineCapacity>
void Deque<T, inlineCapacity>::checkIndexValidity(size_t index) const
{
ASSERT_UNUSED(index, index <= m_buffer.capacity());
if (m_start <= m_end) {
ASSERT(index >= m_start);
ASSERT(index <= m_end);
} else {
ASSERT(index >= m_start || index <= m_end);
}
}
template<typename T, size_t inlineCapacity>
void Deque<T, inlineCapacity>::invalidateIterators()
{
IteratorBase* next;
for (IteratorBase* p = m_iterators; p; p = next) {
next = p->m_next;
p->m_deque = 0;
p->m_next = 0;
p->m_previous = 0;
}
m_iterators = 0;
}
#endif
template<typename T, size_t inlineCapacity>
inline Deque<T, inlineCapacity>::Deque()
: m_start(0)
, m_end(0)
#ifndef NDEBUG
, m_iterators(0)
#endif
{
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline Deque<T, inlineCapacity>::Deque(std::initializer_list<T> initializerList)
: Deque()
{
for (auto& element : initializerList)
append(element);
}
template<typename T, size_t inlineCapacity>
inline Deque<T, inlineCapacity>::Deque(const Deque& other)
: m_start(other.m_start)
, m_end(other.m_end)
, m_buffer(other.m_buffer.capacity())
#ifndef NDEBUG
, m_iterators(0)
#endif
{
auto otherBuffer = other.m_buffer.capacitySpan();
if (m_start <= m_end)
TypeOperations::uninitializedCopy(otherBuffer.subspan(m_start, m_end - m_start), m_buffer.capacitySpan().subspan(m_start));
else {
TypeOperations::uninitializedCopy(otherBuffer.first(m_end), m_buffer.capacitySpan());
TypeOperations::uninitializedCopy(otherBuffer.subspan(m_start, m_buffer.capacity() - m_start), m_buffer.capacitySpan().subspan(m_start));
}
}
template<typename T, size_t inlineCapacity>
inline Deque<T, inlineCapacity>::Deque(Deque&& other)
: Deque()
{
swap(other);
}
template<typename T, size_t inlineCapacity>
inline auto Deque<T, inlineCapacity>::operator=(const Deque& other) -> Deque&
{
// FIXME: This is inefficient if we're using an inline buffer and T is
// expensive to copy since it will copy the buffer twice instead of once.
Deque<T, inlineCapacity> copy(other);
swap(copy);
return *this;
}
template<typename T, size_t inlineCapacity>
inline auto Deque<T, inlineCapacity>::operator=(Deque&& other) -> Deque&
{
swap(other);
return *this;
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::destroyAll()
{
auto span = m_buffer.capacitySpan();
if (m_start <= m_end)
TypeOperations::destruct(span.subspan(m_start, m_end - m_start));
else {
TypeOperations::destruct(span.first(m_end));
TypeOperations::destruct(span.subspan(m_start));
}
}
template<typename T, size_t inlineCapacity>
inline Deque<T, inlineCapacity>::~Deque()
{
checkValidity();
invalidateIterators();
destroyAll();
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::swap(Deque<T, inlineCapacity>& other)
{
checkValidity();
other.checkValidity();
invalidateIterators();
std::swap(m_start, other.m_start);
std::swap(m_end, other.m_end);
m_buffer.swap(other.m_buffer, 0, 0);
checkValidity();
other.checkValidity();
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::clear()
{
checkValidity();
invalidateIterators();
destroyAll();
m_start = 0;
m_end = 0;
m_buffer.deallocateBuffer(m_buffer.buffer());
checkValidity();
}
template<typename T, size_t inlineCapacity>
template<typename Predicate>
inline auto Deque<T, inlineCapacity>::findIf(NOESCAPE const Predicate& predicate) -> iterator
{
return std::find_if(begin(), end(), predicate);
}
template<typename T, size_t inlineCapacity>
template<typename Predicate>
inline auto Deque<T, inlineCapacity>::findIf(NOESCAPE const Predicate& predicate) const -> const_iterator
{
return std::find_if(begin(), end(), predicate);
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::expandCapacityIfNeeded()
{
if (m_start) {
if (m_end + 1 != m_start)
return;
} else if (m_end) {
if (m_end != m_buffer.capacity() - 1)
return;
} else if (m_buffer.capacity())
return;
expandCapacity();
}
template<typename T, size_t inlineCapacity>
void Deque<T, inlineCapacity>::expandCapacity()
{
checkValidity();
size_t oldCapacity = m_buffer.capacity();
auto oldBuffer = m_buffer.capacitySpan();
m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1));
if (m_start <= m_end)
TypeOperations::move(oldBuffer.subspan(m_start, m_end - m_start), m_buffer.capacitySpan().subspan(m_start));
else {
TypeOperations::move(oldBuffer.first(m_end), m_buffer.capacitySpan());
size_t newStart = m_buffer.capacity() - (oldCapacity - m_start);
TypeOperations::move(oldBuffer.subspan(m_start, oldCapacity - m_start), m_buffer.capacitySpan().subspan(newStart));
m_start = newStart;
}
m_buffer.deallocateBuffer(oldBuffer.data());
checkValidity();
}
template<typename T, size_t inlineCapacity>
template<typename U>
bool Deque<T, inlineCapacity>::contains(const U& searchValue) const
{
auto endIterator = end();
return std::find(begin(), endIterator, searchValue) != endIterator;
}
template<typename T, size_t inlineCapacity>
inline auto Deque<T, inlineCapacity>::takeFirst() -> T
{
T oldFirst = WTFMove(first());
removeFirst();
return oldFirst;
}
template<typename T, size_t inlineCapacity>
inline auto Deque<T, inlineCapacity>::takeLast() -> T
{
T oldLast = WTFMove(last());
removeLast();
return oldLast;
}
template<typename T, size_t inlineCapacity> template<typename U>
inline void Deque<T, inlineCapacity>::append(U&& value)
{
checkValidity();
expandCapacityIfNeeded();
new (NotNull, std::addressof(m_buffer.capacitySpan()[m_end])) T(std::forward<U>(value));
if (m_end == m_buffer.capacity() - 1)
m_end = 0;
else
++m_end;
checkValidity();
}
template<typename T, size_t inlineCapacity> template<typename U>
inline void Deque<T, inlineCapacity>::prepend(U&& value)
{
checkValidity();
expandCapacityIfNeeded();
if (!m_start)
m_start = m_buffer.capacity() - 1;
else
--m_start;
new (NotNull, std::addressof(m_buffer.capacitySpan()[m_start])) T(std::forward<U>(value));
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::removeFirst()
{
checkValidity();
invalidateIterators();
RELEASE_ASSERT(!isEmpty());
TypeOperations::destruct(m_buffer.capacitySpan().subspan(m_start, 1));
if (m_start == m_buffer.capacity() - 1)
m_start = 0;
else
++m_start;
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::removeLast()
{
checkValidity();
invalidateIterators();
RELEASE_ASSERT(!isEmpty());
if (!m_end)
m_end = m_buffer.capacity() - 1;
else
--m_end;
TypeOperations::destruct(m_buffer.capacitySpan().subspan(m_end, 1));
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::remove(iterator& it)
{
it.checkValidity();
remove(it.m_index);
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::remove(const_iterator& it)
{
it.checkValidity();
remove(it.m_index);
}
template<typename T, size_t inlineCapacity>
inline void Deque<T, inlineCapacity>::remove(size_t position)
{
if (position == m_end)
return;
checkValidity();
invalidateIterators();
auto buffer = m_buffer.capacitySpan();
TypeOperations::destruct(buffer.subspan(position, 1));
// Find which segment of the circular buffer contained the remove element, and only move elements in that part.
if (position >= m_start) {
TypeOperations::moveOverlapping(buffer.subspan(m_start, position - m_start), buffer.subspan(m_start + 1));
m_start = (m_start + 1) % m_buffer.capacity();
} else {
TypeOperations::moveOverlapping(buffer.subspan(position + 1, m_end - (position + 1)), buffer.subspan(position));
m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity();
}
checkValidity();
}
template<typename T, size_t inlineCapacity>
template<typename Func>
inline size_t Deque<T, inlineCapacity>::removeAllMatching(const Func& func)
{
auto oldSize = size();
for (size_t i = 0; i < oldSize; ++i) {
auto value = takeFirst();
if (!func(value))
append(WTFMove(value));
}
return size() - oldSize;
}
template<typename T, size_t inlineCapacity>
template<typename Func>
inline bool Deque<T, inlineCapacity>::removeFirstMatching(const Func& func)
{
for (auto iter = begin(); iter != end(); ++iter) {
if (func(*iter)) {
remove(iter);
return true;
}
}
return false;
}
template<typename T, size_t inlineCapacity>
template<typename U, typename Func>
inline void Deque<T, inlineCapacity>::appendAndBubble(U&& value, const Func& func)
{
append(std::forward<U>(value));
iterator begin = this->begin();
iterator iter = end();
--iter;
while (iter != begin) {
iterator prev = iter;
--prev;
if (!func(*prev))
return;
std::swap(*prev, *iter);
iter = prev;
}
}
template<typename T, size_t inlineCapacity>
template<typename Func>
inline T Deque<T, inlineCapacity>::takeFirst(const Func& func)
{
unsigned count = 0;
unsigned size = this->size();
while (count < size) {
T candidate = takeFirst();
if (func(candidate)) {
while (count--)
prepend(takeLast());
return candidate;
}
count++;
append(WTFMove(candidate));
}
return T();
}
template<typename T, size_t inlineCapacity>
template<typename Func>
inline T Deque<T, inlineCapacity>::takeLast(const Func& func)
{
unsigned count = 0;
unsigned size = this->size();
while (count < size) {
T candidate = takeLast();
if (func(candidate)) {
while (count--)
append(takeFirst());
return candidate;
}
count++;
prepend(WTFMove(candidate));
}
return T();
}
#ifdef NDEBUG
template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::checkValidity() const { }
template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::checkValidity(const DequeIteratorBase<T, inlineCapacity>&) const { }
template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::addToIteratorsList() { }
template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::removeFromIteratorsList() { }
#else
template<typename T, size_t inlineCapacity>
void DequeIteratorBase<T, inlineCapacity>::checkValidity() const
{
ASSERT(m_deque);
m_deque->checkIndexValidity(m_index);
}
template<typename T, size_t inlineCapacity>
void DequeIteratorBase<T, inlineCapacity>::checkValidity(const DequeIteratorBase& other) const
{
checkValidity();
other.checkValidity();
ASSERT(m_deque == other.m_deque);
}
template<typename T, size_t inlineCapacity>
void DequeIteratorBase<T, inlineCapacity>::addToIteratorsList()
{
if (!m_deque)
m_next = 0;
else {
m_next = m_deque->m_iterators;
m_deque->m_iterators = this;
if (m_next)
m_next->m_previous = this;
}
m_previous = 0;
}
template<typename T, size_t inlineCapacity>
void DequeIteratorBase<T, inlineCapacity>::removeFromIteratorsList()
{
if (!m_deque) {
ASSERT(!m_next);
ASSERT(!m_previous);
} else {
if (m_next) {
ASSERT(m_next->m_previous == this);
m_next->m_previous = m_previous;
}
if (m_previous) {
ASSERT(m_deque->m_iterators != this);
ASSERT(m_previous->m_next == this);
m_previous->m_next = m_next;
} else {
ASSERT(m_deque->m_iterators == this);
m_deque->m_iterators = m_next;
}
}
m_next = 0;
m_previous = 0;
}
#endif
template<typename T, size_t inlineCapacity>
inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase()
: m_deque(0)
{
}
template<typename T, size_t inlineCapacity>
inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const Deque<T, inlineCapacity>* deque, size_t index)
: m_deque(const_cast<Deque<T, inlineCapacity>*>(deque))
, m_index(index)
{
addToIteratorsList();
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const DequeIteratorBase& other)
: m_deque(other.m_deque)
, m_index(other.m_index)
{
addToIteratorsList();
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline DequeIteratorBase<T, inlineCapacity>& DequeIteratorBase<T, inlineCapacity>::operator=(const DequeIteratorBase& other)
{
other.checkValidity();
removeFromIteratorsList();
m_deque = other.m_deque;
m_index = other.m_index;
addToIteratorsList();
checkValidity();
return *this;
}
template<typename T, size_t inlineCapacity>
inline DequeIteratorBase<T, inlineCapacity>::~DequeIteratorBase()
{
#ifndef NDEBUG
removeFromIteratorsList();
m_deque = 0;
#endif
}
template<typename T, size_t inlineCapacity>
inline bool DequeIteratorBase<T, inlineCapacity>::isEqual(const DequeIteratorBase& other) const
{
checkValidity(other);
return m_index == other.m_index;
}
template<typename T, size_t inlineCapacity>
inline void DequeIteratorBase<T, inlineCapacity>::increment(std::ptrdiff_t count)
{
checkValidity();
if (!count)
return;
ASSERT(m_index != m_deque->m_end);
size_t capacity = m_deque->m_buffer.capacity();
ASSERT(capacity);
m_index += count;
do {
if (m_index < capacity)
break;
m_index -= capacity;
} while (true);
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline void DequeIteratorBase<T, inlineCapacity>::decrement()
{
checkValidity();
ASSERT(m_index != m_deque->m_start);
ASSERT(m_deque->m_buffer.capacity());
if (!m_index)
m_index = m_deque->m_buffer.capacity() - 1;
else
--m_index;
checkValidity();
}
template<typename T, size_t inlineCapacity>
inline T* DequeIteratorBase<T, inlineCapacity>::after() const
{
checkValidity();
ASSERT(m_index != m_deque->m_end);
return std::addressof(m_deque->m_buffer.capacitySpan()[m_index]);
}
template<typename T, size_t inlineCapacity>
inline T* DequeIteratorBase<T, inlineCapacity>::before() const
{
checkValidity();
ASSERT(m_index != m_deque->m_start);
if (!m_index)
return std::addressof(m_deque->m_buffer.capacitySpan()[m_deque->m_buffer.capacity() - 1]);
return std::addressof(m_deque->m_buffer.capacitySpan()[m_index - 1]);
}
} // namespace WTF
|