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 |
|
---|
30 | #ifndef WTF_Deque_h
|
---|
31 | #define WTF_Deque_h
|
---|
32 |
|
---|
33 | // FIXME: Could move what Vector and Deque share into a separate file.
|
---|
34 | // Deque doesn't actually use Vector.
|
---|
35 |
|
---|
36 | #include "Vector.h"
|
---|
37 |
|
---|
38 | namespace WTF {
|
---|
39 |
|
---|
40 | template<typename T> class DequeIteratorBase;
|
---|
41 | template<typename T> class DequeIterator;
|
---|
42 | template<typename T> class DequeConstIterator;
|
---|
43 | template<typename T> class DequeReverseIterator;
|
---|
44 | template<typename T> class DequeConstReverseIterator;
|
---|
45 |
|
---|
46 | template<typename T>
|
---|
47 | class Deque : public FastAllocBase {
|
---|
48 | public:
|
---|
49 | typedef DequeIterator<T> iterator;
|
---|
50 | typedef DequeConstIterator<T> const_iterator;
|
---|
51 | typedef DequeReverseIterator<T> reverse_iterator;
|
---|
52 | typedef DequeConstReverseIterator<T> const_reverse_iterator;
|
---|
53 |
|
---|
54 | Deque();
|
---|
55 | Deque(const Deque<T>&);
|
---|
56 | Deque& operator=(const Deque<T>&);
|
---|
57 | ~Deque();
|
---|
58 |
|
---|
59 | void swap(Deque<T>&);
|
---|
60 |
|
---|
61 | size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; }
|
---|
62 | bool isEmpty() const { return m_start == m_end; }
|
---|
63 |
|
---|
64 | iterator begin() { return iterator(this, m_start); }
|
---|
65 | iterator end() { return iterator(this, m_end); }
|
---|
66 | const_iterator begin() const { return const_iterator(this, m_start); }
|
---|
67 | const_iterator end() const { return const_iterator(this, m_end); }
|
---|
68 | reverse_iterator rbegin() { return reverse_iterator(this, m_end); }
|
---|
69 | reverse_iterator rend() { return reverse_iterator(this, m_start); }
|
---|
70 | const_reverse_iterator rbegin() const { return const_reverse_iterator(this, m_end); }
|
---|
71 | const_reverse_iterator rend() const { return const_reverse_iterator(this, m_start); }
|
---|
72 |
|
---|
73 | T& first() { ASSERT(m_start != m_end); return m_buffer.buffer()[m_start]; }
|
---|
74 | const T& first() const { ASSERT(m_start != m_end); return m_buffer.buffer()[m_start]; }
|
---|
75 | T takeFirst();
|
---|
76 |
|
---|
77 | template<typename U> void append(const U&);
|
---|
78 | template<typename U> void prepend(const U&);
|
---|
79 | void removeFirst();
|
---|
80 | void remove(iterator&);
|
---|
81 | void remove(const_iterator&);
|
---|
82 |
|
---|
83 | void clear();
|
---|
84 |
|
---|
85 | template<typename Predicate>
|
---|
86 | iterator findIf(Predicate&);
|
---|
87 |
|
---|
88 | private:
|
---|
89 | friend class DequeIteratorBase<T>;
|
---|
90 |
|
---|
91 | typedef VectorBuffer<T, 0> Buffer;
|
---|
92 | typedef VectorTypeOperations<T> TypeOperations;
|
---|
93 | typedef DequeIteratorBase<T> IteratorBase;
|
---|
94 |
|
---|
95 | void remove(size_t position);
|
---|
96 | void invalidateIterators();
|
---|
97 | void destroyAll();
|
---|
98 | void checkValidity() const;
|
---|
99 | void checkIndexValidity(size_t) const;
|
---|
100 | void expandCapacityIfNeeded();
|
---|
101 | void expandCapacity();
|
---|
102 |
|
---|
103 | size_t m_start;
|
---|
104 | size_t m_end;
|
---|
105 | Buffer m_buffer;
|
---|
106 | #ifndef NDEBUG
|
---|
107 | mutable IteratorBase* m_iterators;
|
---|
108 | #endif
|
---|
109 | };
|
---|
110 |
|
---|
111 | template<typename T>
|
---|
112 | class DequeIteratorBase {
|
---|
113 | private:
|
---|
114 | typedef DequeIteratorBase<T> Base;
|
---|
115 |
|
---|
116 | protected:
|
---|
117 | DequeIteratorBase();
|
---|
118 | DequeIteratorBase(const Deque<T>*, size_t);
|
---|
119 | DequeIteratorBase(const Base&);
|
---|
120 | Base& operator=(const Base&);
|
---|
121 | ~DequeIteratorBase();
|
---|
122 |
|
---|
123 | void assign(const Base& other) { *this = other; }
|
---|
124 |
|
---|
125 | void increment();
|
---|
126 | void decrement();
|
---|
127 |
|
---|
128 | T* before() const;
|
---|
129 | T* after() const;
|
---|
130 |
|
---|
131 | bool isEqual(const Base&) const;
|
---|
132 |
|
---|
133 | private:
|
---|
134 | void addToIteratorsList();
|
---|
135 | void removeFromIteratorsList();
|
---|
136 | void checkValidity() const;
|
---|
137 | void checkValidity(const Base&) const;
|
---|
138 |
|
---|
139 | Deque<T>* m_deque;
|
---|
140 | size_t m_index;
|
---|
141 |
|
---|
142 | friend class Deque<T>;
|
---|
143 |
|
---|
144 | #ifndef NDEBUG
|
---|
145 | mutable DequeIteratorBase* m_next;
|
---|
146 | mutable DequeIteratorBase* m_previous;
|
---|
147 | #endif
|
---|
148 | };
|
---|
149 |
|
---|
150 | template<typename T>
|
---|
151 | class DequeIterator : public DequeIteratorBase<T> {
|
---|
152 | private:
|
---|
153 | typedef DequeIteratorBase<T> Base;
|
---|
154 | typedef DequeIterator<T> Iterator;
|
---|
155 |
|
---|
156 | public:
|
---|
157 | DequeIterator(Deque<T>* deque, size_t index) : Base(deque, index) { }
|
---|
158 |
|
---|
159 | DequeIterator(const Iterator& other) : Base(other) { }
|
---|
160 | DequeIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
|
---|
161 |
|
---|
162 | T& operator*() const { return *Base::after(); }
|
---|
163 | T* operator->() const { return Base::after(); }
|
---|
164 |
|
---|
165 | bool operator==(const Iterator& other) const { return Base::isEqual(other); }
|
---|
166 | bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
|
---|
167 |
|
---|
168 | Iterator& operator++() { Base::increment(); return *this; }
|
---|
169 | // postfix ++ intentionally omitted
|
---|
170 | Iterator& operator--() { Base::decrement(); return *this; }
|
---|
171 | // postfix -- intentionally omitted
|
---|
172 | };
|
---|
173 |
|
---|
174 | template<typename T>
|
---|
175 | class DequeConstIterator : public DequeIteratorBase<T> {
|
---|
176 | private:
|
---|
177 | typedef DequeIteratorBase<T> Base;
|
---|
178 | typedef DequeConstIterator<T> Iterator;
|
---|
179 | typedef DequeIterator<T> NonConstIterator;
|
---|
180 |
|
---|
181 | public:
|
---|
182 | DequeConstIterator(const Deque<T>* deque, size_t index) : Base(deque, index) { }
|
---|
183 |
|
---|
184 | DequeConstIterator(const Iterator& other) : Base(other) { }
|
---|
185 | DequeConstIterator(const NonConstIterator& other) : Base(other) { }
|
---|
186 | DequeConstIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
|
---|
187 | DequeConstIterator& operator=(const NonConstIterator& other) { Base::assign(other); return *this; }
|
---|
188 |
|
---|
189 | const T& operator*() const { return *Base::after(); }
|
---|
190 | const T* operator->() const { return Base::after(); }
|
---|
191 |
|
---|
192 | bool operator==(const Iterator& other) const { return Base::isEqual(other); }
|
---|
193 | bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
|
---|
194 |
|
---|
195 | Iterator& operator++() { Base::increment(); return *this; }
|
---|
196 | // postfix ++ intentionally omitted
|
---|
197 | Iterator& operator--() { Base::decrement(); return *this; }
|
---|
198 | // postfix -- intentionally omitted
|
---|
199 | };
|
---|
200 |
|
---|
201 | template<typename T>
|
---|
202 | class DequeReverseIterator : public DequeIteratorBase<T> {
|
---|
203 | private:
|
---|
204 | typedef DequeIteratorBase<T> Base;
|
---|
205 | typedef DequeReverseIterator<T> Iterator;
|
---|
206 |
|
---|
207 | public:
|
---|
208 | DequeReverseIterator(const Deque<T>* deque, size_t index) : Base(deque, index) { }
|
---|
209 |
|
---|
210 | DequeReverseIterator(const Iterator& other) : Base(other) { }
|
---|
211 | DequeReverseIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
|
---|
212 |
|
---|
213 | T& operator*() const { return *Base::before(); }
|
---|
214 | T* operator->() const { return Base::before(); }
|
---|
215 |
|
---|
216 | bool operator==(const Iterator& other) const { return Base::isEqual(other); }
|
---|
217 | bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
|
---|
218 |
|
---|
219 | Iterator& operator++() { Base::decrement(); return *this; }
|
---|
220 | // postfix ++ intentionally omitted
|
---|
221 | Iterator& operator--() { Base::increment(); return *this; }
|
---|
222 | // postfix -- intentionally omitted
|
---|
223 | };
|
---|
224 |
|
---|
225 | template<typename T>
|
---|
226 | class DequeConstReverseIterator : public DequeIteratorBase<T> {
|
---|
227 | private:
|
---|
228 | typedef DequeIteratorBase<T> Base;
|
---|
229 | typedef DequeConstReverseIterator<T> Iterator;
|
---|
230 | typedef DequeReverseIterator<T> NonConstIterator;
|
---|
231 |
|
---|
232 | public:
|
---|
233 | DequeConstReverseIterator(const Deque<T>* deque, size_t index) : Base(deque, index) { }
|
---|
234 |
|
---|
235 | DequeConstReverseIterator(const Iterator& other) : Base(other) { }
|
---|
236 | DequeConstReverseIterator(const NonConstIterator& other) : Base(other) { }
|
---|
237 | DequeConstReverseIterator& operator=(const Iterator& other) { Base::assign(other); return *this; }
|
---|
238 | DequeConstReverseIterator& operator=(const NonConstIterator& other) { Base::assign(other); return *this; }
|
---|
239 |
|
---|
240 | const T& operator*() const { return *Base::before(); }
|
---|
241 | const T* operator->() const { return Base::before(); }
|
---|
242 |
|
---|
243 | bool operator==(const Iterator& other) const { return Base::isEqual(other); }
|
---|
244 | bool operator!=(const Iterator& other) const { return !Base::isEqual(other); }
|
---|
245 |
|
---|
246 | Iterator& operator++() { Base::decrement(); return *this; }
|
---|
247 | // postfix ++ intentionally omitted
|
---|
248 | Iterator& operator--() { Base::increment(); return *this; }
|
---|
249 | // postfix -- intentionally omitted
|
---|
250 | };
|
---|
251 |
|
---|
252 | #ifdef NDEBUG
|
---|
253 | template<typename T> inline void Deque<T>::checkValidity() const { }
|
---|
254 | template<typename T> inline void Deque<T>::checkIndexValidity(size_t) const { }
|
---|
255 | template<typename T> inline void Deque<T>::invalidateIterators() { }
|
---|
256 | #else
|
---|
257 | template<typename T>
|
---|
258 | void Deque<T>::checkValidity() const
|
---|
259 | {
|
---|
260 | if (!m_buffer.capacity()) {
|
---|
261 | ASSERT(!m_start);
|
---|
262 | ASSERT(!m_end);
|
---|
263 | } else {
|
---|
264 | ASSERT(m_start < m_buffer.capacity());
|
---|
265 | ASSERT(m_end < m_buffer.capacity());
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | template<typename T>
|
---|
270 | void Deque<T>::checkIndexValidity(size_t index) const
|
---|
271 | {
|
---|
272 | ASSERT(index <= m_buffer.capacity());
|
---|
273 | if (m_start <= m_end) {
|
---|
274 | ASSERT(index >= m_start);
|
---|
275 | ASSERT(index <= m_end);
|
---|
276 | } else {
|
---|
277 | ASSERT(index >= m_start || index <= m_end);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | template<typename T>
|
---|
282 | void Deque<T>::invalidateIterators()
|
---|
283 | {
|
---|
284 | IteratorBase* next;
|
---|
285 | for (IteratorBase* p = m_iterators; p; p = next) {
|
---|
286 | next = p->m_next;
|
---|
287 | p->m_deque = 0;
|
---|
288 | p->m_next = 0;
|
---|
289 | p->m_previous = 0;
|
---|
290 | }
|
---|
291 | m_iterators = 0;
|
---|
292 | }
|
---|
293 | #endif
|
---|
294 |
|
---|
295 | template<typename T>
|
---|
296 | inline Deque<T>::Deque()
|
---|
297 | : m_start(0)
|
---|
298 | , m_end(0)
|
---|
299 | #ifndef NDEBUG
|
---|
300 | , m_iterators(0)
|
---|
301 | #endif
|
---|
302 | {
|
---|
303 | checkValidity();
|
---|
304 | }
|
---|
305 |
|
---|
306 | template<typename T>
|
---|
307 | inline Deque<T>::Deque(const Deque<T>& other)
|
---|
308 | : m_start(other.m_start)
|
---|
309 | , m_end(other.m_end)
|
---|
310 | , m_buffer(other.m_buffer.capacity())
|
---|
311 | #ifndef NDEBUG
|
---|
312 | , m_iterators(0)
|
---|
313 | #endif
|
---|
314 | {
|
---|
315 | const T* otherBuffer = other.m_buffer.buffer();
|
---|
316 | if (m_start <= m_end)
|
---|
317 | TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start);
|
---|
318 | else {
|
---|
319 | TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer());
|
---|
320 | TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | template<typename T>
|
---|
325 | void deleteAllValues(const Deque<T>& collection)
|
---|
326 | {
|
---|
327 | typedef typename Deque<T>::const_iterator iterator;
|
---|
328 | iterator end = collection.end();
|
---|
329 | for (iterator it = collection.begin(); it != end; ++it)
|
---|
330 | delete *it;
|
---|
331 | }
|
---|
332 |
|
---|
333 | template<typename T>
|
---|
334 | inline Deque<T>& Deque<T>::operator=(const Deque<T>& other)
|
---|
335 | {
|
---|
336 | Deque<T> copy(other);
|
---|
337 | swap(copy);
|
---|
338 | return *this;
|
---|
339 | }
|
---|
340 |
|
---|
341 | template<typename T>
|
---|
342 | inline void Deque<T>::destroyAll()
|
---|
343 | {
|
---|
344 | if (m_start <= m_end)
|
---|
345 | TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffer() + m_end);
|
---|
346 | else {
|
---|
347 | TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_end);
|
---|
348 | TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffer() + m_buffer.capacity());
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | template<typename T>
|
---|
353 | inline Deque<T>::~Deque()
|
---|
354 | {
|
---|
355 | checkValidity();
|
---|
356 | invalidateIterators();
|
---|
357 | destroyAll();
|
---|
358 | }
|
---|
359 |
|
---|
360 | template<typename T>
|
---|
361 | inline void Deque<T>::swap(Deque<T>& other)
|
---|
362 | {
|
---|
363 | checkValidity();
|
---|
364 | other.checkValidity();
|
---|
365 | invalidateIterators();
|
---|
366 | std::swap(m_start, other.m_start);
|
---|
367 | std::swap(m_end, other.m_end);
|
---|
368 | m_buffer.swap(other.m_buffer);
|
---|
369 | checkValidity();
|
---|
370 | other.checkValidity();
|
---|
371 | }
|
---|
372 |
|
---|
373 | template<typename T>
|
---|
374 | inline void Deque<T>::clear()
|
---|
375 | {
|
---|
376 | checkValidity();
|
---|
377 | invalidateIterators();
|
---|
378 | destroyAll();
|
---|
379 | m_start = 0;
|
---|
380 | m_end = 0;
|
---|
381 | checkValidity();
|
---|
382 | }
|
---|
383 |
|
---|
384 | template<typename T>
|
---|
385 | template<typename Predicate>
|
---|
386 | inline DequeIterator<T> Deque<T>::findIf(Predicate& predicate)
|
---|
387 | {
|
---|
388 | iterator end_iterator = end();
|
---|
389 | for (iterator it = begin(); it != end_iterator; ++it) {
|
---|
390 | if (predicate(*it))
|
---|
391 | return it;
|
---|
392 | }
|
---|
393 | return end_iterator;
|
---|
394 | }
|
---|
395 |
|
---|
396 | template<typename T>
|
---|
397 | inline void Deque<T>::expandCapacityIfNeeded()
|
---|
398 | {
|
---|
399 | if (m_start) {
|
---|
400 | if (m_end + 1 != m_start)
|
---|
401 | return;
|
---|
402 | } else if (m_end) {
|
---|
403 | if (m_end != m_buffer.capacity() - 1)
|
---|
404 | return;
|
---|
405 | } else if (m_buffer.capacity())
|
---|
406 | return;
|
---|
407 |
|
---|
408 | expandCapacity();
|
---|
409 | }
|
---|
410 |
|
---|
411 | template<typename T>
|
---|
412 | void Deque<T>::expandCapacity()
|
---|
413 | {
|
---|
414 | checkValidity();
|
---|
415 | size_t oldCapacity = m_buffer.capacity();
|
---|
416 | size_t newCapacity = max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1);
|
---|
417 | T* oldBuffer = m_buffer.buffer();
|
---|
418 | m_buffer.allocateBuffer(newCapacity);
|
---|
419 | if (m_start <= m_end)
|
---|
420 | TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffer.buffer() + m_start);
|
---|
421 | else {
|
---|
422 | TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer());
|
---|
423 | size_t newStart = newCapacity - (oldCapacity - m_start);
|
---|
424 | TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m_buffer.buffer() + newStart);
|
---|
425 | m_start = newStart;
|
---|
426 | }
|
---|
427 | m_buffer.deallocateBuffer(oldBuffer);
|
---|
428 | checkValidity();
|
---|
429 | }
|
---|
430 |
|
---|
431 | template<typename T>
|
---|
432 | inline T Deque<T>::takeFirst()
|
---|
433 | {
|
---|
434 | T oldFirst = first();
|
---|
435 | removeFirst();
|
---|
436 | return oldFirst;
|
---|
437 | }
|
---|
438 |
|
---|
439 | template<typename T> template<typename U>
|
---|
440 | inline void Deque<T>::append(const U& value)
|
---|
441 | {
|
---|
442 | checkValidity();
|
---|
443 | expandCapacityIfNeeded();
|
---|
444 | new (&m_buffer.buffer()[m_end]) T(value);
|
---|
445 | if (m_end == m_buffer.capacity() - 1)
|
---|
446 | m_end = 0;
|
---|
447 | else
|
---|
448 | ++m_end;
|
---|
449 | checkValidity();
|
---|
450 | }
|
---|
451 |
|
---|
452 | template<typename T> template<typename U>
|
---|
453 | inline void Deque<T>::prepend(const U& value)
|
---|
454 | {
|
---|
455 | checkValidity();
|
---|
456 | expandCapacityIfNeeded();
|
---|
457 | if (!m_start)
|
---|
458 | m_start = m_buffer.capacity() - 1;
|
---|
459 | else
|
---|
460 | --m_start;
|
---|
461 | new (&m_buffer.buffer()[m_start]) T(value);
|
---|
462 | checkValidity();
|
---|
463 | }
|
---|
464 |
|
---|
465 | template<typename T>
|
---|
466 | inline void Deque<T>::removeFirst()
|
---|
467 | {
|
---|
468 | checkValidity();
|
---|
469 | invalidateIterators();
|
---|
470 | ASSERT(!isEmpty());
|
---|
471 | TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer()[m_start + 1]);
|
---|
472 | if (m_start == m_buffer.capacity() - 1)
|
---|
473 | m_start = 0;
|
---|
474 | else
|
---|
475 | ++m_start;
|
---|
476 | checkValidity();
|
---|
477 | }
|
---|
478 |
|
---|
479 | template<typename T>
|
---|
480 | inline void Deque<T>::remove(iterator& it)
|
---|
481 | {
|
---|
482 | it.checkValidity();
|
---|
483 | remove(it.m_index);
|
---|
484 | }
|
---|
485 |
|
---|
486 | template<typename T>
|
---|
487 | inline void Deque<T>::remove(const_iterator& it)
|
---|
488 | {
|
---|
489 | it.checkValidity();
|
---|
490 | remove(it.m_index);
|
---|
491 | }
|
---|
492 |
|
---|
493 | template<typename T>
|
---|
494 | inline void Deque<T>::remove(size_t position)
|
---|
495 | {
|
---|
496 | if (position == m_end)
|
---|
497 | return;
|
---|
498 |
|
---|
499 | checkValidity();
|
---|
500 | invalidateIterators();
|
---|
501 |
|
---|
502 | T* buffer = m_buffer.buffer();
|
---|
503 | TypeOperations::destruct(&buffer[position], &buffer[position + 1]);
|
---|
504 |
|
---|
505 | // Find which segment of the circular buffer contained the remove element, and only move elements in that part.
|
---|
506 | if (position >= m_start) {
|
---|
507 | TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1);
|
---|
508 | m_start = (m_start + 1) % m_buffer.capacity();
|
---|
509 | } else {
|
---|
510 | TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_end, buffer + position);
|
---|
511 | m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity();
|
---|
512 | }
|
---|
513 | checkValidity();
|
---|
514 | }
|
---|
515 |
|
---|
516 | #ifdef NDEBUG
|
---|
517 | template<typename T> inline void DequeIteratorBase<T>::checkValidity() const { }
|
---|
518 | template<typename T> inline void DequeIteratorBase<T>::checkValidity(const DequeIteratorBase<T>&) const { }
|
---|
519 | template<typename T> inline void DequeIteratorBase<T>::addToIteratorsList() { }
|
---|
520 | template<typename T> inline void DequeIteratorBase<T>::removeFromIteratorsList() { }
|
---|
521 | #else
|
---|
522 | template<typename T>
|
---|
523 | void DequeIteratorBase<T>::checkValidity() const
|
---|
524 | {
|
---|
525 | ASSERT(m_deque);
|
---|
526 | m_deque->checkIndexValidity(m_index);
|
---|
527 | }
|
---|
528 |
|
---|
529 | template<typename T>
|
---|
530 | void DequeIteratorBase<T>::checkValidity(const Base& other) const
|
---|
531 | {
|
---|
532 | checkValidity();
|
---|
533 | other.checkValidity();
|
---|
534 | ASSERT(m_deque == other.m_deque);
|
---|
535 | }
|
---|
536 |
|
---|
537 | template<typename T>
|
---|
538 | void DequeIteratorBase<T>::addToIteratorsList()
|
---|
539 | {
|
---|
540 | if (!m_deque)
|
---|
541 | m_next = 0;
|
---|
542 | else {
|
---|
543 | m_next = m_deque->m_iterators;
|
---|
544 | m_deque->m_iterators = this;
|
---|
545 | if (m_next)
|
---|
546 | m_next->m_previous = this;
|
---|
547 | }
|
---|
548 | m_previous = 0;
|
---|
549 | }
|
---|
550 |
|
---|
551 | template<typename T>
|
---|
552 | void DequeIteratorBase<T>::removeFromIteratorsList()
|
---|
553 | {
|
---|
554 | if (!m_deque) {
|
---|
555 | ASSERT(!m_next);
|
---|
556 | ASSERT(!m_previous);
|
---|
557 | } else {
|
---|
558 | if (m_next) {
|
---|
559 | ASSERT(m_next->m_previous == this);
|
---|
560 | m_next->m_previous = m_previous;
|
---|
561 | }
|
---|
562 | if (m_previous) {
|
---|
563 | ASSERT(m_deque->m_iterators != this);
|
---|
564 | ASSERT(m_previous->m_next == this);
|
---|
565 | m_previous->m_next = m_next;
|
---|
566 | } else {
|
---|
567 | ASSERT(m_deque->m_iterators == this);
|
---|
568 | m_deque->m_iterators = m_next;
|
---|
569 | }
|
---|
570 | }
|
---|
571 | m_next = 0;
|
---|
572 | m_previous = 0;
|
---|
573 | }
|
---|
574 | #endif
|
---|
575 |
|
---|
576 | template<typename T>
|
---|
577 | inline DequeIteratorBase<T>::DequeIteratorBase()
|
---|
578 | : m_deque(0)
|
---|
579 | {
|
---|
580 | }
|
---|
581 |
|
---|
582 | template<typename T>
|
---|
583 | inline DequeIteratorBase<T>::DequeIteratorBase(const Deque<T>* deque, size_t index)
|
---|
584 | : m_deque(const_cast<Deque<T>*>(deque))
|
---|
585 | , m_index(index)
|
---|
586 | {
|
---|
587 | addToIteratorsList();
|
---|
588 | checkValidity();
|
---|
589 | }
|
---|
590 |
|
---|
591 | template<typename T>
|
---|
592 | inline DequeIteratorBase<T>::DequeIteratorBase(const Base& other)
|
---|
593 | : m_deque(other.m_deque)
|
---|
594 | , m_index(other.m_index)
|
---|
595 | {
|
---|
596 | addToIteratorsList();
|
---|
597 | checkValidity();
|
---|
598 | }
|
---|
599 |
|
---|
600 | template<typename T>
|
---|
601 | inline DequeIteratorBase<T>& DequeIteratorBase<T>::operator=(const Base& other)
|
---|
602 | {
|
---|
603 | checkValidity();
|
---|
604 | other.checkValidity();
|
---|
605 | removeFromIteratorsList();
|
---|
606 |
|
---|
607 | m_deque = other.m_deque;
|
---|
608 | m_index = other.m_index;
|
---|
609 | addToIteratorsList();
|
---|
610 | checkValidity();
|
---|
611 | return *this;
|
---|
612 | }
|
---|
613 |
|
---|
614 | template<typename T>
|
---|
615 | inline DequeIteratorBase<T>::~DequeIteratorBase()
|
---|
616 | {
|
---|
617 | #ifndef NDEBUG
|
---|
618 | removeFromIteratorsList();
|
---|
619 | m_deque = 0;
|
---|
620 | #endif
|
---|
621 | }
|
---|
622 |
|
---|
623 | template<typename T>
|
---|
624 | inline bool DequeIteratorBase<T>::isEqual(const Base& other) const
|
---|
625 | {
|
---|
626 | checkValidity(other);
|
---|
627 | return m_index == other.m_index;
|
---|
628 | }
|
---|
629 |
|
---|
630 | template<typename T>
|
---|
631 | inline void DequeIteratorBase<T>::increment()
|
---|
632 | {
|
---|
633 | checkValidity();
|
---|
634 | ASSERT(m_index != m_deque->m_end);
|
---|
635 | ASSERT(m_deque->m_buffer.capacity());
|
---|
636 | if (m_index == m_deque->m_buffer.capacity() - 1)
|
---|
637 | m_index = 0;
|
---|
638 | else
|
---|
639 | ++m_index;
|
---|
640 | checkValidity();
|
---|
641 | }
|
---|
642 |
|
---|
643 | template<typename T>
|
---|
644 | inline void DequeIteratorBase<T>::decrement()
|
---|
645 | {
|
---|
646 | checkValidity();
|
---|
647 | ASSERT(m_index != m_deque->m_start);
|
---|
648 | ASSERT(m_deque->m_buffer.capacity());
|
---|
649 | if (!m_index)
|
---|
650 | m_index = m_deque->m_buffer.capacity() - 1;
|
---|
651 | else
|
---|
652 | --m_index;
|
---|
653 | checkValidity();
|
---|
654 | }
|
---|
655 |
|
---|
656 | template<typename T>
|
---|
657 | inline T* DequeIteratorBase<T>::after() const
|
---|
658 | {
|
---|
659 | checkValidity();
|
---|
660 | ASSERT(m_index != m_deque->m_end);
|
---|
661 | return &m_deque->m_buffer.buffer()[m_index];
|
---|
662 | }
|
---|
663 |
|
---|
664 | template<typename T>
|
---|
665 | inline T* DequeIteratorBase<T>::before() const
|
---|
666 | {
|
---|
667 | checkValidity();
|
---|
668 | ASSERT(m_index != m_deque->m_start);
|
---|
669 | if (!m_index)
|
---|
670 | return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1];
|
---|
671 | return &m_deque->m_buffer.buffer()[m_index - 1];
|
---|
672 | }
|
---|
673 |
|
---|
674 | } // namespace WTF
|
---|
675 |
|
---|
676 | using WTF::Deque;
|
---|
677 |
|
---|
678 | #endif // WTF_Deque_h
|
---|