Deque.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef WTF_Deque_h
  30. #define WTF_Deque_h
  31. // FIXME: Could move what Vector and Deque share into a separate file.
  32. // Deque doesn't actually use Vector.
  33. #include <iterator>
  34. #include <wtf/PassTraits.h>
  35. #include <wtf/Vector.h>
  36. namespace WTF {
  37. template<typename T, size_t inlineCapacity> class DequeIteratorBase;
  38. template<typename T, size_t inlineCapacity> class DequeIterator;
  39. template<typename T, size_t inlineCapacity> class DequeConstIterator;
  40. template<typename T, size_t inlineCapacity = 0>
  41. class Deque {
  42. WTF_MAKE_FAST_ALLOCATED;
  43. public:
  44. typedef DequeIterator<T, inlineCapacity> iterator;
  45. typedef DequeConstIterator<T, inlineCapacity> const_iterator;
  46. typedef std::reverse_iterator<iterator> reverse_iterator;
  47. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  48. typedef PassTraits<T> Pass;
  49. typedef typename PassTraits<T>::PassType PassType;
  50. Deque();
  51. Deque(const Deque<T, inlineCapacity>&);
  52. Deque& operator=(const Deque<T, inlineCapacity>&);
  53. ~Deque();
  54. void swap(Deque<T, inlineCapacity>&);
  55. size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; }
  56. bool isEmpty() const { return m_start == m_end; }
  57. iterator begin() { return iterator(this, m_start); }
  58. iterator end() { return iterator(this, m_end); }
  59. const_iterator begin() const { return const_iterator(this, m_start); }
  60. const_iterator end() const { return const_iterator(this, m_end); }
  61. reverse_iterator rbegin() { return reverse_iterator(end()); }
  62. reverse_iterator rend() { return reverse_iterator(begin()); }
  63. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  64. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  65. T& first() { ASSERT(m_start != m_end); return m_buffer.buffer()[m_start]; }
  66. const T& first() const { ASSERT(m_start != m_end); return m_buffer.buffer()[m_start]; }
  67. PassType takeFirst();
  68. T& last() { ASSERT(m_start != m_end); return *(--end()); }
  69. const T& last() const { ASSERT(m_start != m_end); return *(--end()); }
  70. template<typename U> void append(const U&);
  71. template<typename U> void prepend(const U&);
  72. void removeFirst();
  73. void remove(iterator&);
  74. void remove(const_iterator&);
  75. void clear();
  76. template<typename Predicate>
  77. iterator findIf(Predicate&);
  78. private:
  79. friend class DequeIteratorBase<T, inlineCapacity>;
  80. typedef VectorBuffer<T, inlineCapacity> Buffer;
  81. typedef VectorTypeOperations<T> TypeOperations;
  82. typedef DequeIteratorBase<T, inlineCapacity> IteratorBase;
  83. void remove(size_t position);
  84. void invalidateIterators();
  85. void destroyAll();
  86. void checkValidity() const;
  87. void checkIndexValidity(size_t) const;
  88. void expandCapacityIfNeeded();
  89. void expandCapacity();
  90. size_t m_start;
  91. size_t m_end;
  92. Buffer m_buffer;
  93. #ifndef NDEBUG
  94. mutable IteratorBase* m_iterators;
  95. #endif
  96. };
  97. template<typename T, size_t inlineCapacity = 0>
  98. class DequeIteratorBase {
  99. protected:
  100. DequeIteratorBase();
  101. DequeIteratorBase(const Deque<T, inlineCapacity>*, size_t);
  102. DequeIteratorBase(const DequeIteratorBase&);
  103. DequeIteratorBase& operator=(const DequeIteratorBase&);
  104. ~DequeIteratorBase();
  105. void assign(const DequeIteratorBase& other) { *this = other; }
  106. void increment();
  107. void decrement();
  108. T* before() const;
  109. T* after() const;
  110. bool isEqual(const DequeIteratorBase&) const;
  111. private:
  112. void addToIteratorsList();
  113. void removeFromIteratorsList();
  114. void checkValidity() const;
  115. void checkValidity(const DequeIteratorBase&) const;
  116. Deque<T, inlineCapacity>* m_deque;
  117. size_t m_index;
  118. friend class Deque<T, inlineCapacity>;
  119. #ifndef NDEBUG
  120. mutable DequeIteratorBase* m_next;
  121. mutable DequeIteratorBase* m_previous;
  122. #endif
  123. };
  124. template<typename T, size_t inlineCapacity = 0>
  125. class DequeIterator : public DequeIteratorBase<T, inlineCapacity> {
  126. private:
  127. typedef DequeIteratorBase<T, inlineCapacity> Base;
  128. typedef DequeIterator<T, inlineCapacity> Iterator;
  129. public:
  130. typedef ptrdiff_t difference_type;
  131. typedef T value_type;
  132. typedef T* pointer;
  133. typedef T& reference;
  134. typedef std::bidirectional_iterator_tag iterator_category;
  135. DequeIterator(Deque<T, inlineCapacity>* deque, size_t index) : Base(deque, index) { }
  136. DequeIterator(const Iterator& other) : Base(other) { }
  137. DequeIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
  138. T& operator*() const { return *Base::after(); }
  139. T* operator->() const { return Base::after(); }
  140. bool operator==(const Iterator& other) const { return Base::isEqual(other); }
  141. bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
  142. Iterator& operator++() { Base::increment(); return *this; }
  143. // postfix ++ intentionally omitted
  144. Iterator& operator--() { Base::decrement(); return *this; }
  145. // postfix -- intentionally omitted
  146. };
  147. template<typename T, size_t inlineCapacity = 0>
  148. class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity> {
  149. private:
  150. typedef DequeIteratorBase<T, inlineCapacity> Base;
  151. typedef DequeConstIterator<T, inlineCapacity> Iterator;
  152. typedef DequeIterator<T, inlineCapacity> NonConstIterator;
  153. public:
  154. typedef ptrdiff_t difference_type;
  155. typedef T value_type;
  156. typedef const T* pointer;
  157. typedef const T& reference;
  158. typedef std::bidirectional_iterator_tag iterator_category;
  159. DequeConstIterator(const Deque<T, inlineCapacity>* deque, size_t index) : Base(deque, index) { }
  160. DequeConstIterator(const Iterator& other) : Base(other) { }
  161. DequeConstIterator(const NonConstIterator& other) : Base(other) { }
  162. DequeConstIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
  163. DequeConstIterator& operator=(const NonConstIterator& other) { Base::assign(other); return *this; }
  164. const T& operator*() const { return *Base::after(); }
  165. const T* operator->() const { return Base::after(); }
  166. bool operator==(const Iterator& other) const { return Base::isEqual(other); }
  167. bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
  168. Iterator& operator++() { Base::increment(); return *this; }
  169. // postfix ++ intentionally omitted
  170. Iterator& operator--() { Base::decrement(); return *this; }
  171. // postfix -- intentionally omitted
  172. };
  173. #ifdef NDEBUG
  174. template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::checkValidity() const { }
  175. template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::checkIndexValidity(size_t) const { }
  176. template<typename T, size_t inlineCapacity> inline void Deque<T, inlineCapacity>::invalidateIterators() { }
  177. #else
  178. template<typename T, size_t inlineCapacity>
  179. void Deque<T, inlineCapacity>::checkValidity() const
  180. {
  181. // In this implementation a capacity of 1 would confuse append() and
  182. // other places that assume the index after capacity - 1 is 0.
  183. ASSERT(m_buffer.capacity() != 1);
  184. if (!m_buffer.capacity()) {
  185. ASSERT(!m_start);
  186. ASSERT(!m_end);
  187. } else {
  188. ASSERT(m_start < m_buffer.capacity());
  189. ASSERT(m_end < m_buffer.capacity());
  190. }
  191. }
  192. template<typename T, size_t inlineCapacity>
  193. void Deque<T, inlineCapacity>::checkIndexValidity(size_t index) const
  194. {
  195. ASSERT_UNUSED(index, index <= m_buffer.capacity());
  196. if (m_start <= m_end) {
  197. ASSERT(index >= m_start);
  198. ASSERT(index <= m_end);
  199. } else {
  200. ASSERT(index >= m_start || index <= m_end);
  201. }
  202. }
  203. template<typename T, size_t inlineCapacity>
  204. void Deque<T, inlineCapacity>::invalidateIterators()
  205. {
  206. IteratorBase* next;
  207. for (IteratorBase* p = m_iterators; p; p = next) {
  208. next = p->m_next;
  209. p->m_deque = 0;
  210. p->m_next = 0;
  211. p->m_previous = 0;
  212. }
  213. m_iterators = 0;
  214. }
  215. #endif
  216. template<typename T, size_t inlineCapacity>
  217. inline Deque<T, inlineCapacity>::Deque()
  218. : m_start(0)
  219. , m_end(0)
  220. #ifndef NDEBUG
  221. , m_iterators(0)
  222. #endif
  223. {
  224. checkValidity();
  225. }
  226. template<typename T, size_t inlineCapacity>
  227. inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other)
  228. : m_start(other.m_start)
  229. , m_end(other.m_end)
  230. , m_buffer(other.m_buffer.capacity())
  231. #ifndef NDEBUG
  232. , m_iterators(0)
  233. #endif
  234. {
  235. const T* otherBuffer = other.m_buffer.buffer();
  236. if (m_start <= m_end)
  237. TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start);
  238. else {
  239. TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer());
  240. TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start);
  241. }
  242. }
  243. template<typename T, size_t inlineCapacity>
  244. void deleteAllValues(const Deque<T, inlineCapacity>& collection)
  245. {
  246. typedef typename Deque<T, inlineCapacity>::const_iterator iterator;
  247. iterator end = collection.end();
  248. for (iterator it = collection.begin(); it != end; ++it)
  249. delete *it;
  250. }
  251. template<typename T, size_t inlineCapacity>
  252. inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const Deque<T, inlineCapacity>& other)
  253. {
  254. // FIXME: This is inefficient if we're using an inline buffer and T is
  255. // expensive to copy since it will copy the buffer twice instead of once.
  256. Deque<T> copy(other);
  257. swap(copy);
  258. return *this;
  259. }
  260. template<typename T, size_t inlineCapacity>
  261. inline void Deque<T, inlineCapacity>::destroyAll()
  262. {
  263. if (m_start <= m_end)
  264. TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffer() + m_end);
  265. else {
  266. TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_end);
  267. TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffer() + m_buffer.capacity());
  268. }
  269. }
  270. template<typename T, size_t inlineCapacity>
  271. inline Deque<T, inlineCapacity>::~Deque()
  272. {
  273. checkValidity();
  274. invalidateIterators();
  275. destroyAll();
  276. }
  277. template<typename T, size_t inlineCapacity>
  278. inline void Deque<T, inlineCapacity>::swap(Deque<T, inlineCapacity>& other)
  279. {
  280. checkValidity();
  281. other.checkValidity();
  282. invalidateIterators();
  283. std::swap(m_start, other.m_start);
  284. std::swap(m_end, other.m_end);
  285. m_buffer.swap(other.m_buffer);
  286. checkValidity();
  287. other.checkValidity();
  288. }
  289. template<typename T, size_t inlineCapacity>
  290. inline void Deque<T, inlineCapacity>::clear()
  291. {
  292. checkValidity();
  293. invalidateIterators();
  294. destroyAll();
  295. m_start = 0;
  296. m_end = 0;
  297. m_buffer.deallocateBuffer(m_buffer.buffer());
  298. checkValidity();
  299. }
  300. template<typename T, size_t inlineCapacity>
  301. template<typename Predicate>
  302. inline DequeIterator<T, inlineCapacity> Deque<T, inlineCapacity>::findIf(Predicate& predicate)
  303. {
  304. iterator end_iterator = end();
  305. for (iterator it = begin(); it != end_iterator; ++it) {
  306. if (predicate(*it))
  307. return it;
  308. }
  309. return end_iterator;
  310. }
  311. template<typename T, size_t inlineCapacity>
  312. inline void Deque<T, inlineCapacity>::expandCapacityIfNeeded()
  313. {
  314. if (m_start) {
  315. if (m_end + 1 != m_start)
  316. return;
  317. } else if (m_end) {
  318. if (m_end != m_buffer.capacity() - 1)
  319. return;
  320. } else if (m_buffer.capacity())
  321. return;
  322. expandCapacity();
  323. }
  324. template<typename T, size_t inlineCapacity>
  325. void Deque<T, inlineCapacity>::expandCapacity()
  326. {
  327. checkValidity();
  328. size_t oldCapacity = m_buffer.capacity();
  329. T* oldBuffer = m_buffer.buffer();
  330. m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1));
  331. if (m_start <= m_end)
  332. TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffer.buffer() + m_start);
  333. else {
  334. TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer());
  335. size_t newStart = m_buffer.capacity() - (oldCapacity - m_start);
  336. TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m_buffer.buffer() + newStart);
  337. m_start = newStart;
  338. }
  339. m_buffer.deallocateBuffer(oldBuffer);
  340. checkValidity();
  341. }
  342. template<typename T, size_t inlineCapacity>
  343. inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>::takeFirst()
  344. {
  345. T oldFirst = Pass::transfer(first());
  346. removeFirst();
  347. return Pass::transfer(oldFirst);
  348. }
  349. template<typename T, size_t inlineCapacity> template<typename U>
  350. inline void Deque<T, inlineCapacity>::append(const U& value)
  351. {
  352. checkValidity();
  353. expandCapacityIfNeeded();
  354. new (NotNull, &m_buffer.buffer()[m_end]) T(value);
  355. if (m_end == m_buffer.capacity() - 1)
  356. m_end = 0;
  357. else
  358. ++m_end;
  359. checkValidity();
  360. }
  361. template<typename T, size_t inlineCapacity> template<typename U>
  362. inline void Deque<T, inlineCapacity>::prepend(const U& value)
  363. {
  364. checkValidity();
  365. expandCapacityIfNeeded();
  366. if (!m_start)
  367. m_start = m_buffer.capacity() - 1;
  368. else
  369. --m_start;
  370. new (NotNull, &m_buffer.buffer()[m_start]) T(value);
  371. checkValidity();
  372. }
  373. template<typename T, size_t inlineCapacity>
  374. inline void Deque<T, inlineCapacity>::removeFirst()
  375. {
  376. checkValidity();
  377. invalidateIterators();
  378. ASSERT(!isEmpty());
  379. TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer()[m_start + 1]);
  380. if (m_start == m_buffer.capacity() - 1)
  381. m_start = 0;
  382. else
  383. ++m_start;
  384. checkValidity();
  385. }
  386. template<typename T, size_t inlineCapacity>
  387. inline void Deque<T, inlineCapacity>::remove(iterator& it)
  388. {
  389. it.checkValidity();
  390. remove(it.m_index);
  391. }
  392. template<typename T, size_t inlineCapacity>
  393. inline void Deque<T, inlineCapacity>::remove(const_iterator& it)
  394. {
  395. it.checkValidity();
  396. remove(it.m_index);
  397. }
  398. template<typename T, size_t inlineCapacity>
  399. inline void Deque<T, inlineCapacity>::remove(size_t position)
  400. {
  401. if (position == m_end)
  402. return;
  403. checkValidity();
  404. invalidateIterators();
  405. T* buffer = m_buffer.buffer();
  406. TypeOperations::destruct(&buffer[position], &buffer[position + 1]);
  407. // Find which segment of the circular buffer contained the remove element, and only move elements in that part.
  408. if (position >= m_start) {
  409. TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1);
  410. m_start = (m_start + 1) % m_buffer.capacity();
  411. } else {
  412. TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_end, buffer + position);
  413. m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity();
  414. }
  415. checkValidity();
  416. }
  417. #ifdef NDEBUG
  418. template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::checkValidity() const { }
  419. template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::checkValidity(const DequeIteratorBase<T, inlineCapacity>&) const { }
  420. template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::addToIteratorsList() { }
  421. template<typename T, size_t inlineCapacity> inline void DequeIteratorBase<T, inlineCapacity>::removeFromIteratorsList() { }
  422. #else
  423. template<typename T, size_t inlineCapacity>
  424. void DequeIteratorBase<T, inlineCapacity>::checkValidity() const
  425. {
  426. ASSERT(m_deque);
  427. m_deque->checkIndexValidity(m_index);
  428. }
  429. template<typename T, size_t inlineCapacity>
  430. void DequeIteratorBase<T, inlineCapacity>::checkValidity(const DequeIteratorBase& other) const
  431. {
  432. checkValidity();
  433. other.checkValidity();
  434. ASSERT(m_deque == other.m_deque);
  435. }
  436. template<typename T, size_t inlineCapacity>
  437. void DequeIteratorBase<T, inlineCapacity>::addToIteratorsList()
  438. {
  439. if (!m_deque)
  440. m_next = 0;
  441. else {
  442. m_next = m_deque->m_iterators;
  443. m_deque->m_iterators = this;
  444. if (m_next)
  445. m_next->m_previous = this;
  446. }
  447. m_previous = 0;
  448. }
  449. template<typename T, size_t inlineCapacity>
  450. void DequeIteratorBase<T, inlineCapacity>::removeFromIteratorsList()
  451. {
  452. if (!m_deque) {
  453. ASSERT(!m_next);
  454. ASSERT(!m_previous);
  455. } else {
  456. if (m_next) {
  457. ASSERT(m_next->m_previous == this);
  458. m_next->m_previous = m_previous;
  459. }
  460. if (m_previous) {
  461. ASSERT(m_deque->m_iterators != this);
  462. ASSERT(m_previous->m_next == this);
  463. m_previous->m_next = m_next;
  464. } else {
  465. ASSERT(m_deque->m_iterators == this);
  466. m_deque->m_iterators = m_next;
  467. }
  468. }
  469. m_next = 0;
  470. m_previous = 0;
  471. }
  472. #endif
  473. template<typename T, size_t inlineCapacity>
  474. inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase()
  475. : m_deque(0)
  476. {
  477. }
  478. template<typename T, size_t inlineCapacity>
  479. inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const Deque<T, inlineCapacity>* deque, size_t index)
  480. : m_deque(const_cast<Deque<T, inlineCapacity>*>(deque))
  481. , m_index(index)
  482. {
  483. addToIteratorsList();
  484. checkValidity();
  485. }
  486. template<typename T, size_t inlineCapacity>
  487. inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const DequeIteratorBase& other)
  488. : m_deque(other.m_deque)
  489. , m_index(other.m_index)
  490. {
  491. addToIteratorsList();
  492. checkValidity();
  493. }
  494. template<typename T, size_t inlineCapacity>
  495. inline DequeIteratorBase<T, inlineCapacity>& DequeIteratorBase<T, inlineCapacity>::operator=(const DequeIteratorBase& other)
  496. {
  497. other.checkValidity();
  498. removeFromIteratorsList();
  499. m_deque = other.m_deque;
  500. m_index = other.m_index;
  501. addToIteratorsList();
  502. checkValidity();
  503. return *this;
  504. }
  505. template<typename T, size_t inlineCapacity>
  506. inline DequeIteratorBase<T, inlineCapacity>::~DequeIteratorBase()
  507. {
  508. #ifndef NDEBUG
  509. removeFromIteratorsList();
  510. m_deque = 0;
  511. #endif
  512. }
  513. template<typename T, size_t inlineCapacity>
  514. inline bool DequeIteratorBase<T, inlineCapacity>::isEqual(const DequeIteratorBase& other) const
  515. {
  516. checkValidity(other);
  517. return m_index == other.m_index;
  518. }
  519. template<typename T, size_t inlineCapacity>
  520. inline void DequeIteratorBase<T, inlineCapacity>::increment()
  521. {
  522. checkValidity();
  523. ASSERT(m_index != m_deque->m_end);
  524. ASSERT(m_deque->m_buffer.capacity());
  525. if (m_index == m_deque->m_buffer.capacity() - 1)
  526. m_index = 0;
  527. else
  528. ++m_index;
  529. checkValidity();
  530. }
  531. template<typename T, size_t inlineCapacity>
  532. inline void DequeIteratorBase<T, inlineCapacity>::decrement()
  533. {
  534. checkValidity();
  535. ASSERT(m_index != m_deque->m_start);
  536. ASSERT(m_deque->m_buffer.capacity());
  537. if (!m_index)
  538. m_index = m_deque->m_buffer.capacity() - 1;
  539. else
  540. --m_index;
  541. checkValidity();
  542. }
  543. template<typename T, size_t inlineCapacity>
  544. inline T* DequeIteratorBase<T, inlineCapacity>::after() const
  545. {
  546. checkValidity();
  547. ASSERT(m_index != m_deque->m_end);
  548. return &m_deque->m_buffer.buffer()[m_index];
  549. }
  550. template<typename T, size_t inlineCapacity>
  551. inline T* DequeIteratorBase<T, inlineCapacity>::before() const
  552. {
  553. checkValidity();
  554. ASSERT(m_index != m_deque->m_start);
  555. if (!m_index)
  556. return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1];
  557. return &m_deque->m_buffer.buffer()[m_index - 1];
  558. }
  559. } // namespace WTF
  560. using WTF::Deque;
  561. #endif // WTF_Deque_h