| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #define WTF_Deque_h | 31 #define WTF_Deque_h |
| 32 | 32 |
| 33 // FIXME: Could move what Vector and Deque share into a separate file. | 33 // FIXME: Could move what Vector and Deque share into a separate file. |
| 34 // Deque doesn't actually use Vector. | 34 // Deque doesn't actually use Vector. |
| 35 | 35 |
| 36 #include "wtf/PassTraits.h" | 36 #include "wtf/PassTraits.h" |
| 37 #include "wtf/Vector.h" | 37 #include "wtf/Vector.h" |
| 38 #include <iterator> | 38 #include <iterator> |
| 39 | 39 |
| 40 namespace WTF { | 40 namespace WTF { |
| 41 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI
teratorBase; |
| 42 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI
terator; |
| 43 template<typename T, size_t inlineCapacity, typename Allocator> class DequeC
onstIterator; |
| 41 | 44 |
| 42 template<typename T, size_t inlineCapacity> class DequeIteratorBase; | 45 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default
Allocator> |
| 43 template<typename T, size_t inlineCapacity> class DequeIterator; | 46 class Deque : public VectorDestructorBase<Deque<T, inlineCapacity, Allocator
>, T, (inlineCapacity > 0), Allocator::isGarbageCollected> { |
| 44 template<typename T, size_t inlineCapacity> class DequeConstIterator; | |
| 45 | |
| 46 template<typename T, size_t inlineCapacity = 0> | |
| 47 class Deque { | |
| 48 WTF_MAKE_FAST_ALLOCATED; | |
| 49 public: | 47 public: |
| 50 typedef DequeIterator<T, inlineCapacity> iterator; | 48 typedef DequeIterator<T, inlineCapacity, Allocator> iterator; |
| 51 typedef DequeConstIterator<T, inlineCapacity> const_iterator; | 49 typedef DequeConstIterator<T, inlineCapacity, Allocator> const_iterator; |
| 52 typedef std::reverse_iterator<iterator> reverse_iterator; | 50 typedef std::reverse_iterator<iterator> reverse_iterator; |
| 53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | 51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 54 typedef PassTraits<T> Pass; | 52 typedef PassTraits<T> Pass; |
| 55 typedef typename PassTraits<T>::PassType PassType; | 53 typedef typename PassTraits<T>::PassType PassType; |
| 56 | 54 |
| 55 void* operator new(size_t size) |
| 56 { |
| 57 return Allocator::template malloc<void*, Deque>(size); |
| 58 } |
| 59 void operator delete(void* p) { Allocator::free(p); } |
| 60 void* operator new[](size_t size) { return Allocator::template newArray<
Vector>(size); } |
| 61 void operator delete[](void* p) { Allocator::deleteArray(p); } |
| 62 void* operator new(size_t, NotNullTag, void* location) |
| 63 { |
| 64 ASSERT(location); |
| 65 return location; |
| 66 } |
| 67 |
| 57 Deque(); | 68 Deque(); |
| 58 Deque(const Deque<T, inlineCapacity>&); | 69 Deque(const Deque<T, inlineCapacity, Allocator>&); |
| 59 Deque& operator=(const Deque<T, inlineCapacity>&); | 70 // FIXME: Doesn't work if there is an inline buffer, due to crbug.com/36
0572 |
| 60 ~Deque(); | 71 Deque<T, 0, Allocator>& operator=(const Deque&); |
| 61 | 72 |
| 62 void swap(Deque<T, inlineCapacity>&); | 73 void finalize(); |
| 74 void finalizeGarbageCollectedObject() { finalize(); } |
| 75 |
| 76 // We hard wire the inlineCapacity to zero here, due to crbug.com/360572 |
| 77 void swap(Deque<T, 0, Allocator>&); |
| 63 | 78 |
| 64 size_t size() const { return m_start <= m_end ? m_end - m_start : m_end
+ m_buffer.capacity() - m_start; } | 79 size_t size() const { return m_start <= m_end ? m_end - m_start : m_end
+ m_buffer.capacity() - m_start; } |
| 65 bool isEmpty() const { return m_start == m_end; } | 80 bool isEmpty() const { return m_start == m_end; } |
| 66 | 81 |
| 67 iterator begin() { return iterator(this, m_start); } | 82 iterator begin() { return iterator(this, m_start); } |
| 68 iterator end() { return iterator(this, m_end); } | 83 iterator end() { return iterator(this, m_end); } |
| 69 const_iterator begin() const { return const_iterator(this, m_start); } | 84 const_iterator begin() const { return const_iterator(this, m_start); } |
| 70 const_iterator end() const { return const_iterator(this, m_end); } | 85 const_iterator end() const { return const_iterator(this, m_end); } |
| 71 reverse_iterator rbegin() { return reverse_iterator(end()); } | 86 reverse_iterator rbegin() { return reverse_iterator(end()); } |
| 72 reverse_iterator rend() { return reverse_iterator(begin()); } | 87 reverse_iterator rend() { return reverse_iterator(begin()); } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 86 void removeFirst(); | 101 void removeFirst(); |
| 87 void removeLast(); | 102 void removeLast(); |
| 88 void remove(iterator&); | 103 void remove(iterator&); |
| 89 void remove(const_iterator&); | 104 void remove(const_iterator&); |
| 90 | 105 |
| 91 void clear(); | 106 void clear(); |
| 92 | 107 |
| 93 template<typename Predicate> | 108 template<typename Predicate> |
| 94 iterator findIf(Predicate&); | 109 iterator findIf(Predicate&); |
| 95 | 110 |
| 111 void trace(typename Allocator::Visitor*); |
| 112 |
| 96 private: | 113 private: |
| 97 friend class DequeIteratorBase<T, inlineCapacity>; | 114 friend class DequeIteratorBase<T, inlineCapacity, Allocator>; |
| 98 | 115 |
| 99 typedef VectorBuffer<T, inlineCapacity> Buffer; | 116 typedef VectorBuffer<T, inlineCapacity, Allocator> Buffer; |
| 100 typedef VectorTypeOperations<T> TypeOperations; | 117 typedef VectorTypeOperations<T> TypeOperations; |
| 101 typedef DequeIteratorBase<T, inlineCapacity> IteratorBase; | 118 typedef DequeIteratorBase<T, inlineCapacity, Allocator> IteratorBase; |
| 102 | 119 |
| 103 void remove(size_t position); | 120 void remove(size_t position); |
| 104 void destroyAll(); | 121 void destroyAll(); |
| 105 void expandCapacityIfNeeded(); | 122 void expandCapacityIfNeeded(); |
| 106 void expandCapacity(); | 123 void expandCapacity(); |
| 107 | 124 |
| 108 Buffer m_buffer; | 125 Buffer m_buffer; |
| 109 unsigned m_start; | 126 unsigned m_start; |
| 110 unsigned m_end; | 127 unsigned m_end; |
| 111 }; | 128 }; |
| 112 | 129 |
| 113 template<typename T, size_t inlineCapacity = 0> | 130 template<typename T, size_t inlineCapacity, typename Allocator> |
| 114 class DequeIteratorBase { | 131 class DequeIteratorBase { |
| 115 protected: | 132 protected: |
| 116 DequeIteratorBase(); | 133 DequeIteratorBase(); |
| 117 DequeIteratorBase(const Deque<T, inlineCapacity>*, size_t); | 134 DequeIteratorBase(const Deque<T, inlineCapacity, Allocator>*, size_t); |
| 118 DequeIteratorBase(const DequeIteratorBase&); | 135 DequeIteratorBase(const DequeIteratorBase&); |
| 119 DequeIteratorBase& operator=(const DequeIteratorBase&); | 136 DequeIteratorBase<T, 0, Allocator>& operator=(const DequeIteratorBase<T,
0, Allocator>&); |
| 120 ~DequeIteratorBase(); | 137 ~DequeIteratorBase(); |
| 121 | 138 |
| 122 void assign(const DequeIteratorBase& other) { *this = other; } | 139 void assign(const DequeIteratorBase& other) { *this = other; } |
| 123 | 140 |
| 124 void increment(); | 141 void increment(); |
| 125 void decrement(); | 142 void decrement(); |
| 126 | 143 |
| 127 T* before() const; | 144 T* before() const; |
| 128 T* after() const; | 145 T* after() const; |
| 129 | 146 |
| 130 bool isEqual(const DequeIteratorBase&) const; | 147 bool isEqual(const DequeIteratorBase&) const; |
| 131 | 148 |
| 132 private: | 149 private: |
| 133 Deque<T, inlineCapacity>* m_deque; | 150 Deque<T, inlineCapacity, Allocator>* m_deque; |
| 134 unsigned m_index; | 151 unsigned m_index; |
| 135 | 152 |
| 136 friend class Deque<T, inlineCapacity>; | 153 friend class Deque<T, inlineCapacity, Allocator>; |
| 137 }; | 154 }; |
| 138 | 155 |
| 139 template<typename T, size_t inlineCapacity = 0> | 156 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default
Allocator> |
| 140 class DequeIterator : public DequeIteratorBase<T, inlineCapacity> { | 157 class DequeIterator : public DequeIteratorBase<T, inlineCapacity, Allocator>
{ |
| 141 private: | 158 private: |
| 142 typedef DequeIteratorBase<T, inlineCapacity> Base; | 159 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base; |
| 143 typedef DequeIterator<T, inlineCapacity> Iterator; | 160 typedef DequeIterator<T, inlineCapacity, Allocator> Iterator; |
| 144 | 161 |
| 145 public: | 162 public: |
| 146 typedef ptrdiff_t difference_type; | 163 typedef ptrdiff_t difference_type; |
| 147 typedef T value_type; | 164 typedef T value_type; |
| 148 typedef T* pointer; | 165 typedef T* pointer; |
| 149 typedef T& reference; | 166 typedef T& reference; |
| 150 typedef std::bidirectional_iterator_tag iterator_category; | 167 typedef std::bidirectional_iterator_tag iterator_category; |
| 151 | 168 |
| 152 DequeIterator(Deque<T, inlineCapacity>* deque, size_t index) : Base(dequ
e, index) { } | 169 DequeIterator(Deque<T, inlineCapacity, Allocator>* deque, size_t index)
: Base(deque, index) { } |
| 153 | 170 |
| 154 DequeIterator(const Iterator& other) : Base(other) { } | 171 DequeIterator(const Iterator& other) : Base(other) { } |
| 155 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r
eturn *this; } | 172 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r
eturn *this; } |
| 156 | 173 |
| 157 T& operator*() const { return *Base::after(); } | 174 T& operator*() const { return *Base::after(); } |
| 158 T* operator->() const { return Base::after(); } | 175 T* operator->() const { return Base::after(); } |
| 159 | 176 |
| 160 bool operator==(const Iterator& other) const { return Base::isEqual(othe
r); } | 177 bool operator==(const Iterator& other) const { return Base::isEqual(othe
r); } |
| 161 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth
er); } | 178 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth
er); } |
| 162 | 179 |
| 163 Iterator& operator++() { Base::increment(); return *this; } | 180 Iterator& operator++() { Base::increment(); return *this; } |
| 164 // postfix ++ intentionally omitted | 181 // postfix ++ intentionally omitted |
| 165 Iterator& operator--() { Base::decrement(); return *this; } | 182 Iterator& operator--() { Base::decrement(); return *this; } |
| 166 // postfix -- intentionally omitted | 183 // postfix -- intentionally omitted |
| 167 }; | 184 }; |
| 168 | 185 |
| 169 template<typename T, size_t inlineCapacity = 0> | 186 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default
Allocator> |
| 170 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity> { | 187 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity, Alloc
ator> { |
| 171 private: | 188 private: |
| 172 typedef DequeIteratorBase<T, inlineCapacity> Base; | 189 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base; |
| 173 typedef DequeConstIterator<T, inlineCapacity> Iterator; | 190 typedef DequeConstIterator<T, inlineCapacity, Allocator> Iterator; |
| 174 typedef DequeIterator<T, inlineCapacity> NonConstIterator; | 191 typedef DequeIterator<T, inlineCapacity, Allocator> NonConstIterator; |
| 175 | 192 |
| 176 public: | 193 public: |
| 177 typedef ptrdiff_t difference_type; | 194 typedef ptrdiff_t difference_type; |
| 178 typedef T value_type; | 195 typedef T value_type; |
| 179 typedef const T* pointer; | 196 typedef const T* pointer; |
| 180 typedef const T& reference; | 197 typedef const T& reference; |
| 181 typedef std::bidirectional_iterator_tag iterator_category; | 198 typedef std::bidirectional_iterator_tag iterator_category; |
| 182 | 199 |
| 183 DequeConstIterator(const Deque<T, inlineCapacity>* deque, size_t index)
: Base(deque, index) { } | 200 DequeConstIterator(const Deque<T, inlineCapacity, Allocator>* deque, siz
e_t index) : Base(deque, index) { } |
| 184 | 201 |
| 185 DequeConstIterator(const Iterator& other) : Base(other) { } | 202 DequeConstIterator(const Iterator& other) : Base(other) { } |
| 186 DequeConstIterator(const NonConstIterator& other) : Base(other) { } | 203 DequeConstIterator(const NonConstIterator& other) : Base(other) { } |
| 187 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe
r); return *this; } | 204 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe
r); return *this; } |
| 188 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass
ign(other); return *this; } | 205 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass
ign(other); return *this; } |
| 189 | 206 |
| 190 const T& operator*() const { return *Base::after(); } | 207 const T& operator*() const { return *Base::after(); } |
| 191 const T* operator->() const { return Base::after(); } | 208 const T* operator->() const { return Base::after(); } |
| 192 | 209 |
| 193 bool operator==(const Iterator& other) const { return Base::isEqual(othe
r); } | 210 bool operator==(const Iterator& other) const { return Base::isEqual(othe
r); } |
| 194 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth
er); } | 211 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth
er); } |
| 195 | 212 |
| 196 Iterator& operator++() { Base::increment(); return *this; } | 213 Iterator& operator++() { Base::increment(); return *this; } |
| 197 // postfix ++ intentionally omitted | 214 // postfix ++ intentionally omitted |
| 198 Iterator& operator--() { Base::decrement(); return *this; } | 215 Iterator& operator--() { Base::decrement(); return *this; } |
| 199 // postfix -- intentionally omitted | 216 // postfix -- intentionally omitted |
| 200 }; | 217 }; |
| 201 | 218 |
| 202 template<typename T, size_t inlineCapacity> | 219 template<typename T, size_t inlineCapacity, typename Allocator> |
| 203 inline Deque<T, inlineCapacity>::Deque() | 220 inline Deque<T, inlineCapacity, Allocator>::Deque() |
| 204 : m_start(0) | 221 : m_start(0) |
| 205 , m_end(0) | 222 , m_end(0) |
| 206 { | 223 { |
| 207 } | 224 } |
| 208 | 225 |
| 209 template<typename T, size_t inlineCapacity> | 226 template<typename T, size_t inlineCapacity, typename Allocator> |
| 210 inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other
) | 227 inline Deque<T, inlineCapacity, Allocator>::Deque(const Deque<T, inlineCapac
ity, Allocator>& other) |
| 211 : m_buffer(other.m_buffer.capacity()) | 228 : m_buffer(other.m_buffer.capacity()) |
| 212 , m_start(other.m_start) | 229 , m_start(other.m_start) |
| 213 , m_end(other.m_end) | 230 , m_end(other.m_end) |
| 214 { | 231 { |
| 215 const T* otherBuffer = other.m_buffer.buffer(); | 232 const T* otherBuffer = other.m_buffer.buffer(); |
| 216 if (m_start <= m_end) | 233 if (m_start <= m_end) |
| 217 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer
+ m_end, m_buffer.buffer() + m_start); | 234 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer
+ m_end, m_buffer.buffer() + m_start); |
| 218 else { | 235 else { |
| 219 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end,
m_buffer.buffer()); | 236 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end,
m_buffer.buffer()); |
| 220 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer
+ m_buffer.capacity(), m_buffer.buffer() + m_start); | 237 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer
+ m_buffer.capacity(), m_buffer.buffer() + m_start); |
| 221 } | 238 } |
| 222 } | 239 } |
| 223 | 240 |
| 224 template<typename T, size_t inlineCapacity> | 241 template<typename T, size_t inlineCapacity, typename Allocator> |
| 225 void deleteAllValues(const Deque<T, inlineCapacity>& collection) | 242 void deleteAllValues(const Deque<T, inlineCapacity, Allocator>& collection) |
| 226 { | 243 { |
| 227 typedef typename Deque<T, inlineCapacity>::const_iterator iterator; | 244 typedef typename Deque<T, inlineCapacity, Allocator>::const_iterator ite
rator; |
| 228 iterator end = collection.end(); | 245 iterator end = collection.end(); |
| 229 for (iterator it = collection.begin(); it != end; ++it) | 246 for (iterator it = collection.begin(); it != end; ++it) |
| 230 delete *it; | 247 delete *it; |
| 231 } | 248 } |
| 232 | 249 |
| 233 template<typename T, size_t inlineCapacity> | 250 template<typename T, size_t inlineCapacity, typename Allocator> |
| 234 inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const D
eque<T, inlineCapacity>& other) | 251 inline Deque<T, 0, Allocator>& Deque<T, inlineCapacity, Allocator>::operator
=(const Deque& other) |
| 235 { | 252 { |
| 236 // FIXME: This is inefficient if we're using an inline buffer and T is | |
| 237 // expensive to copy since it will copy the buffer twice instead of once
. | |
| 238 Deque<T> copy(other); | 253 Deque<T> copy(other); |
| 239 swap(copy); | 254 swap(copy); |
| 240 return *this; | 255 return *this; |
| 241 } | 256 } |
| 242 | 257 |
| 243 template<typename T, size_t inlineCapacity> | 258 template<typename T, size_t inlineCapacity, typename Allocator> |
| 244 inline void Deque<T, inlineCapacity>::destroyAll() | 259 inline void Deque<T, inlineCapacity, Allocator>::destroyAll() |
| 245 { | 260 { |
| 246 if (m_start <= m_end) | 261 if (m_start <= m_end) |
| 247 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe
r() + m_end); | 262 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe
r() + m_end); |
| 248 else { | 263 else { |
| 249 TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_en
d); | 264 TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_en
d); |
| 250 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe
r() + m_buffer.capacity()); | 265 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe
r() + m_buffer.capacity()); |
| 251 } | 266 } |
| 252 } | 267 } |
| 253 | 268 |
| 254 template<typename T, size_t inlineCapacity> | 269 // Off-GC-heap deques: Destructor should be called. |
| 255 inline Deque<T, inlineCapacity>::~Deque() | 270 // On-GC-heap deques: Destructor should be called for inline buffers |
| 271 // (if any) but destructor shouldn't be called for vector backing since |
| 272 // it is managed by the traced GC heap. |
| 273 template<typename T, size_t inlineCapacity, typename Allocator> |
| 274 inline void Deque<T, inlineCapacity, Allocator>::finalize() |
| 256 { | 275 { |
| 257 destroyAll(); | 276 if (!inlineCapacity && !m_buffer.buffer()) |
| 277 return; |
| 278 if (!isEmpty() && !(Allocator::isGarbageCollected && m_buffer.hasOutOfLi
neBuffer())) |
| 279 destroyAll(); |
| 280 |
| 258 m_buffer.destruct(); | 281 m_buffer.destruct(); |
| 259 } | 282 } |
| 260 | 283 |
| 261 template<typename T, size_t inlineCapacity> | 284 // FIXME: Doesn't work if there is an inline buffer, due to crbug.com/360572 |
| 262 inline void Deque<T, inlineCapacity>::swap(Deque<T, inlineCapacity>& other) | 285 template<typename T, size_t inlineCapacity, typename Allocator> |
| 286 inline void Deque<T, inlineCapacity, Allocator>::swap(Deque<T, 0, Allocator>
& other) |
| 263 { | 287 { |
| 264 std::swap(m_start, other.m_start); | 288 std::swap(m_start, other.m_start); |
| 265 std::swap(m_end, other.m_end); | 289 std::swap(m_end, other.m_end); |
| 266 m_buffer.swapVectorBuffer(other.m_buffer); | 290 m_buffer.swapVectorBuffer(other.m_buffer); |
| 267 } | 291 } |
| 268 | 292 |
| 269 template<typename T, size_t inlineCapacity> | 293 template<typename T, size_t inlineCapacity, typename Allocator> |
| 270 inline void Deque<T, inlineCapacity>::clear() | 294 inline void Deque<T, inlineCapacity, Allocator>::clear() |
| 271 { | 295 { |
| 272 destroyAll(); | 296 destroyAll(); |
| 273 m_start = 0; | 297 m_start = 0; |
| 274 m_end = 0; | 298 m_end = 0; |
| 275 m_buffer.deallocateBuffer(m_buffer.buffer()); | 299 m_buffer.deallocateBuffer(m_buffer.buffer()); |
| 276 m_buffer.resetBufferPointer(); | 300 m_buffer.resetBufferPointer(); |
| 277 } | 301 } |
| 278 | 302 |
| 279 template<typename T, size_t inlineCapacity> | 303 template<typename T, size_t inlineCapacity, typename Allocator> |
| 280 template<typename Predicate> | 304 template<typename Predicate> |
| 281 inline DequeIterator<T, inlineCapacity> Deque<T, inlineCapacity>::findIf(Pre
dicate& predicate) | 305 inline DequeIterator<T, inlineCapacity, Allocator> Deque<T, inlineCapacity,
Allocator>::findIf(Predicate& predicate) |
| 282 { | 306 { |
| 283 iterator end_iterator = end(); | 307 iterator end_iterator = end(); |
| 284 for (iterator it = begin(); it != end_iterator; ++it) { | 308 for (iterator it = begin(); it != end_iterator; ++it) { |
| 285 if (predicate(*it)) | 309 if (predicate(*it)) |
| 286 return it; | 310 return it; |
| 287 } | 311 } |
| 288 return end_iterator; | 312 return end_iterator; |
| 289 } | 313 } |
| 290 | 314 |
| 291 template<typename T, size_t inlineCapacity> | 315 template<typename T, size_t inlineCapacity, typename Allocator> |
| 292 inline void Deque<T, inlineCapacity>::expandCapacityIfNeeded() | 316 inline void Deque<T, inlineCapacity, Allocator>::expandCapacityIfNeeded() |
| 293 { | 317 { |
| 294 if (m_start) { | 318 if (m_start) { |
| 295 if (m_end + 1 != m_start) | 319 if (m_end + 1 != m_start) |
| 296 return; | 320 return; |
| 297 } else if (m_end) { | 321 } else if (m_end) { |
| 298 if (m_end != m_buffer.capacity() - 1) | 322 if (m_end != m_buffer.capacity() - 1) |
| 299 return; | 323 return; |
| 300 } else if (m_buffer.capacity()) | 324 } else if (m_buffer.capacity()) |
| 301 return; | 325 return; |
| 302 | 326 |
| 303 expandCapacity(); | 327 expandCapacity(); |
| 304 } | 328 } |
| 305 | 329 |
| 306 template<typename T, size_t inlineCapacity> | 330 template<typename T, size_t inlineCapacity, typename Allocator> |
| 307 void Deque<T, inlineCapacity>::expandCapacity() | 331 void Deque<T, inlineCapacity, Allocator>::expandCapacity() |
| 308 { | 332 { |
| 309 size_t oldCapacity = m_buffer.capacity(); | 333 size_t oldCapacity = m_buffer.capacity(); |
| 310 T* oldBuffer = m_buffer.buffer(); | 334 T* oldBuffer = m_buffer.buffer(); |
| 311 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity +
oldCapacity / 4 + 1)); | 335 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity +
oldCapacity / 4 + 1)); |
| 312 if (m_start <= m_end) | 336 if (m_start <= m_end) |
| 313 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe
r.buffer() + m_start); | 337 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe
r.buffer() + m_start); |
| 314 else { | 338 else { |
| 315 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer()
); | 339 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer()
); |
| 316 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start); | 340 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start); |
| 317 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m
_buffer.buffer() + newStart); | 341 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m
_buffer.buffer() + newStart); |
| 318 m_start = newStart; | 342 m_start = newStart; |
| 319 } | 343 } |
| 320 m_buffer.deallocateBuffer(oldBuffer); | 344 m_buffer.deallocateBuffer(oldBuffer); |
| 321 } | 345 } |
| 322 | 346 |
| 323 template<typename T, size_t inlineCapacity> | 347 template<typename T, size_t inlineCapacity, typename Allocator> |
| 324 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>:
:takeFirst() | 348 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin
eCapacity, Allocator>::takeFirst() |
| 325 { | 349 { |
| 326 T oldFirst = Pass::transfer(first()); | 350 T oldFirst = Pass::transfer(first()); |
| 327 removeFirst(); | 351 removeFirst(); |
| 328 return Pass::transfer(oldFirst); | 352 return Pass::transfer(oldFirst); |
| 329 } | 353 } |
| 330 | 354 |
| 331 template<typename T, size_t inlineCapacity> | 355 template<typename T, size_t inlineCapacity, typename Allocator> |
| 332 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>:
:takeLast() | 356 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin
eCapacity, Allocator>::takeLast() |
| 333 { | 357 { |
| 334 T oldLast = Pass::transfer(last()); | 358 T oldLast = Pass::transfer(last()); |
| 335 removeLast(); | 359 removeLast(); |
| 336 return Pass::transfer(oldLast); | 360 return Pass::transfer(oldLast); |
| 337 } | 361 } |
| 338 | 362 |
| 339 template<typename T, size_t inlineCapacity> template<typename U> | 363 template<typename T, size_t inlineCapacity, typename Allocator> template<typ
ename U> |
| 340 inline void Deque<T, inlineCapacity>::append(const U& value) | 364 inline void Deque<T, inlineCapacity, Allocator>::append(const U& value) |
| 341 { | 365 { |
| 342 expandCapacityIfNeeded(); | 366 expandCapacityIfNeeded(); |
| 343 new (NotNull, &m_buffer.buffer()[m_end]) T(value); | 367 new (NotNull, &m_buffer.buffer()[m_end]) T(value); |
| 344 if (m_end == m_buffer.capacity() - 1) | 368 if (m_end == m_buffer.capacity() - 1) |
| 345 m_end = 0; | 369 m_end = 0; |
| 346 else | 370 else |
| 347 ++m_end; | 371 ++m_end; |
| 348 } | 372 } |
| 349 | 373 |
| 350 template<typename T, size_t inlineCapacity> template<typename U> | 374 template<typename T, size_t inlineCapacity, typename Allocator> template<typ
ename U> |
| 351 inline void Deque<T, inlineCapacity>::prepend(const U& value) | 375 inline void Deque<T, inlineCapacity, Allocator>::prepend(const U& value) |
| 352 { | 376 { |
| 353 expandCapacityIfNeeded(); | 377 expandCapacityIfNeeded(); |
| 354 if (!m_start) | 378 if (!m_start) |
| 355 m_start = m_buffer.capacity() - 1; | 379 m_start = m_buffer.capacity() - 1; |
| 356 else | 380 else |
| 357 --m_start; | 381 --m_start; |
| 358 new (NotNull, &m_buffer.buffer()[m_start]) T(value); | 382 new (NotNull, &m_buffer.buffer()[m_start]) T(value); |
| 359 } | 383 } |
| 360 | 384 |
| 361 template<typename T, size_t inlineCapacity> | 385 template<typename T, size_t inlineCapacity, typename Allocator> |
| 362 inline void Deque<T, inlineCapacity>::removeFirst() | 386 inline void Deque<T, inlineCapacity, Allocator>::removeFirst() |
| 363 { | 387 { |
| 364 ASSERT(!isEmpty()); | 388 ASSERT(!isEmpty()); |
| 365 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer()
[m_start + 1]); | 389 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer()
[m_start + 1]); |
| 366 if (m_start == m_buffer.capacity() - 1) | 390 if (m_start == m_buffer.capacity() - 1) |
| 367 m_start = 0; | 391 m_start = 0; |
| 368 else | 392 else |
| 369 ++m_start; | 393 ++m_start; |
| 370 } | 394 } |
| 371 | 395 |
| 372 template<typename T, size_t inlineCapacity> | 396 template<typename T, size_t inlineCapacity, typename Allocator> |
| 373 inline void Deque<T, inlineCapacity>::removeLast() | 397 inline void Deque<T, inlineCapacity, Allocator>::removeLast() |
| 374 { | 398 { |
| 375 ASSERT(!isEmpty()); | 399 ASSERT(!isEmpty()); |
| 376 if (!m_end) | 400 if (!m_end) |
| 377 m_end = m_buffer.capacity() - 1; | 401 m_end = m_buffer.capacity() - 1; |
| 378 else | 402 else |
| 379 --m_end; | 403 --m_end; |
| 380 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m
_end + 1]); | 404 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m
_end + 1]); |
| 381 } | 405 } |
| 382 | 406 |
| 383 template<typename T, size_t inlineCapacity> | 407 template<typename T, size_t inlineCapacity, typename Allocator> |
| 384 inline void Deque<T, inlineCapacity>::remove(iterator& it) | 408 inline void Deque<T, inlineCapacity, Allocator>::remove(iterator& it) |
| 385 { | 409 { |
| 386 remove(it.m_index); | 410 remove(it.m_index); |
| 387 } | 411 } |
| 388 | 412 |
| 389 template<typename T, size_t inlineCapacity> | 413 template<typename T, size_t inlineCapacity, typename Allocator> |
| 390 inline void Deque<T, inlineCapacity>::remove(const_iterator& it) | 414 inline void Deque<T, inlineCapacity, Allocator>::remove(const_iterator& it) |
| 391 { | 415 { |
| 392 remove(it.m_index); | 416 remove(it.m_index); |
| 393 } | 417 } |
| 394 | 418 |
| 395 template<typename T, size_t inlineCapacity> | 419 template<typename T, size_t inlineCapacity, typename Allocator> |
| 396 inline void Deque<T, inlineCapacity>::remove(size_t position) | 420 inline void Deque<T, inlineCapacity, Allocator>::remove(size_t position) |
| 397 { | 421 { |
| 398 if (position == m_end) | 422 if (position == m_end) |
| 399 return; | 423 return; |
| 400 | 424 |
| 401 T* buffer = m_buffer.buffer(); | 425 T* buffer = m_buffer.buffer(); |
| 402 TypeOperations::destruct(&buffer[position], &buffer[position + 1]); | 426 TypeOperations::destruct(&buffer[position], &buffer[position + 1]); |
| 403 | 427 |
| 404 // Find which segment of the circular buffer contained the remove elemen
t, and only move elements in that part. | 428 // Find which segment of the circular buffer contained the remove elemen
t, and only move elements in that part. |
| 405 if (position >= m_start) { | 429 if (position >= m_start) { |
| 406 TypeOperations::moveOverlapping(buffer + m_start, buffer + position,
buffer + m_start + 1); | 430 TypeOperations::moveOverlapping(buffer + m_start, buffer + position,
buffer + m_start + 1); |
| 407 m_start = (m_start + 1) % m_buffer.capacity(); | 431 m_start = (m_start + 1) % m_buffer.capacity(); |
| 408 } else { | 432 } else { |
| 409 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en
d, buffer + position); | 433 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en
d, buffer + position); |
| 410 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity(); | 434 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity(); |
| 411 } | 435 } |
| 412 } | 436 } |
| 413 | 437 |
| 414 template<typename T, size_t inlineCapacity> | 438 template<typename T, size_t inlineCapacity, typename Allocator> |
| 415 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase() | 439 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase() |
| 416 : m_deque(0) | 440 : m_deque(0) |
| 417 { | 441 { |
| 418 } | 442 } |
| 419 | 443 |
| 420 template<typename T, size_t inlineCapacity> | 444 template<typename T, size_t inlineCapacity, typename Allocator> |
| 421 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const Deque<T
, inlineCapacity>* deque, size_t index) | 445 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase(co
nst Deque<T, inlineCapacity, Allocator>* deque, size_t index) |
| 422 : m_deque(const_cast<Deque<T, inlineCapacity>*>(deque)) | 446 : m_deque(const_cast<Deque<T, inlineCapacity, Allocator>*>(deque)) |
| 423 , m_index(index) | 447 , m_index(index) |
| 424 { | 448 { |
| 425 } | 449 } |
| 426 | 450 |
| 427 template<typename T, size_t inlineCapacity> | 451 template<typename T, size_t inlineCapacity, typename Allocator> |
| 428 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const DequeIt
eratorBase& other) | 452 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase(co
nst DequeIteratorBase& other) |
| 429 : m_deque(other.m_deque) | 453 : m_deque(other.m_deque) |
| 430 , m_index(other.m_index) | 454 , m_index(other.m_index) |
| 431 { | 455 { |
| 432 } | 456 } |
| 433 | 457 |
| 434 template<typename T, size_t inlineCapacity> | 458 template<typename T, size_t inlineCapacity, typename Allocator> |
| 435 inline DequeIteratorBase<T, inlineCapacity>& DequeIteratorBase<T, inlineCapa
city>::operator=(const DequeIteratorBase& other) | 459 inline DequeIteratorBase<T, 0, Allocator>& DequeIteratorBase<T, inlineCapaci
ty, Allocator>::operator=(const DequeIteratorBase<T, 0, Allocator>& other) |
| 436 { | 460 { |
| 437 m_deque = other.m_deque; | 461 m_deque = other.m_deque; |
| 438 m_index = other.m_index; | 462 m_index = other.m_index; |
| 439 return *this; | 463 return *this; |
| 440 } | 464 } |
| 441 | 465 |
| 442 template<typename T, size_t inlineCapacity> | 466 template<typename T, size_t inlineCapacity, typename Allocator> |
| 443 inline DequeIteratorBase<T, inlineCapacity>::~DequeIteratorBase() | 467 inline DequeIteratorBase<T, inlineCapacity, Allocator>::~DequeIteratorBase() |
| 444 { | 468 { |
| 445 } | 469 } |
| 446 | 470 |
| 447 template<typename T, size_t inlineCapacity> | 471 template<typename T, size_t inlineCapacity, typename Allocator> |
| 448 inline bool DequeIteratorBase<T, inlineCapacity>::isEqual(const DequeIterato
rBase& other) const | 472 inline bool DequeIteratorBase<T, inlineCapacity, Allocator>::isEqual(const D
equeIteratorBase& other) const |
| 449 { | 473 { |
| 450 return m_index == other.m_index; | 474 return m_index == other.m_index; |
| 451 } | 475 } |
| 452 | 476 |
| 453 template<typename T, size_t inlineCapacity> | 477 template<typename T, size_t inlineCapacity, typename Allocator> |
| 454 inline void DequeIteratorBase<T, inlineCapacity>::increment() | 478 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::increment() |
| 455 { | 479 { |
| 456 ASSERT(m_index != m_deque->m_end); | 480 ASSERT(m_index != m_deque->m_end); |
| 457 ASSERT(m_deque->m_buffer.capacity()); | 481 ASSERT(m_deque->m_buffer.capacity()); |
| 458 if (m_index == m_deque->m_buffer.capacity() - 1) | 482 if (m_index == m_deque->m_buffer.capacity() - 1) |
| 459 m_index = 0; | 483 m_index = 0; |
| 460 else | 484 else |
| 461 ++m_index; | 485 ++m_index; |
| 462 } | 486 } |
| 463 | 487 |
| 464 template<typename T, size_t inlineCapacity> | 488 template<typename T, size_t inlineCapacity, typename Allocator> |
| 465 inline void DequeIteratorBase<T, inlineCapacity>::decrement() | 489 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::decrement() |
| 466 { | 490 { |
| 467 ASSERT(m_index != m_deque->m_start); | 491 ASSERT(m_index != m_deque->m_start); |
| 468 ASSERT(m_deque->m_buffer.capacity()); | 492 ASSERT(m_deque->m_buffer.capacity()); |
| 469 if (!m_index) | 493 if (!m_index) |
| 470 m_index = m_deque->m_buffer.capacity() - 1; | 494 m_index = m_deque->m_buffer.capacity() - 1; |
| 471 else | 495 else |
| 472 --m_index; | 496 --m_index; |
| 473 } | 497 } |
| 474 | 498 |
| 475 template<typename T, size_t inlineCapacity> | 499 template<typename T, size_t inlineCapacity, typename Allocator> |
| 476 inline T* DequeIteratorBase<T, inlineCapacity>::after() const | 500 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::after() const |
| 477 { | 501 { |
| 478 ASSERT(m_index != m_deque->m_end); | 502 ASSERT(m_index != m_deque->m_end); |
| 479 return &m_deque->m_buffer.buffer()[m_index]; | 503 return &m_deque->m_buffer.buffer()[m_index]; |
| 480 } | 504 } |
| 481 | 505 |
| 482 template<typename T, size_t inlineCapacity> | 506 template<typename T, size_t inlineCapacity, typename Allocator> |
| 483 inline T* DequeIteratorBase<T, inlineCapacity>::before() const | 507 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::before() const |
| 484 { | 508 { |
| 485 ASSERT(m_index != m_deque->m_start); | 509 ASSERT(m_index != m_deque->m_start); |
| 486 if (!m_index) | 510 if (!m_index) |
| 487 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1]
; | 511 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1]
; |
| 488 return &m_deque->m_buffer.buffer()[m_index - 1]; | 512 return &m_deque->m_buffer.buffer()[m_index - 1]; |
| 489 } | 513 } |
| 490 | 514 |
| 515 // This is only called if the allocator is a HeapAllocator. It is used when |
| 516 // visiting during a tracing GC. |
| 517 template<typename T, size_t inlineCapacity, typename Allocator> |
| 518 void Deque<T, inlineCapacity, Allocator>::trace(typename Allocator::Visitor*
visitor) |
| 519 { |
| 520 COMPILE_ASSERT(Allocator::isGarbageCollected, Garbage_collector_must_be_
enabled); |
| 521 const T* bufferBegin = m_buffer.buffer(); |
| 522 const T* end = bufferBegin + m_end; |
| 523 if (ShouldBeTraced<VectorTraits<T> >::value) { |
| 524 if (m_start <= m_end) { |
| 525 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry !
= end; bufferEntry++) |
| 526 Allocator::template trace<T, VectorTraits<T> >(visitor, *con
st_cast<T*>(bufferEntry)); |
| 527 } else { |
| 528 for (const T* bufferEntry = bufferBegin; bufferEntry != end; buf
ferEntry++) |
| 529 Allocator::template trace<T, VectorTraits<T> >(visitor, *con
st_cast<T*>(bufferEntry)); |
| 530 const T* bufferEnd = m_buffer.buffer() + m_buffer.capacity(); |
| 531 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry !
= bufferEnd; bufferEntry++) |
| 532 Allocator::template trace<T, VectorTraits<T> >(visitor, *con
st_cast<T*>(bufferEntry)); |
| 533 } |
| 534 } |
| 535 if (m_buffer.hasOutOfLineBuffer()) |
| 536 Allocator::markNoTracing(visitor, m_buffer.buffer()); |
| 537 } |
| 538 |
| 491 } // namespace WTF | 539 } // namespace WTF |
| 492 | 540 |
| 493 using WTF::Deque; | 541 using WTF::Deque; |
| 494 | 542 |
| 495 #endif // WTF_Deque_h | 543 #endif // WTF_Deque_h |
| OLD | NEW |