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 21 matching lines...) Expand all Loading... | |
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 | 41 |
42 template<typename T, size_t inlineCapacity> class DequeIteratorBase; | 42 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI teratorBase; |
43 template<typename T, size_t inlineCapacity> class DequeIterator; | 43 template<typename T, size_t inlineCapacity, typename Allocator> class DequeI terator; |
44 template<typename T, size_t inlineCapacity> class DequeConstIterator; | 44 template<typename T, size_t inlineCapacity, typename Allocator> class DequeC onstIterator; |
45 | 45 |
46 template<typename T, size_t inlineCapacity = 0> | 46 // All non-GC managed deques need a destructor. This destructor will |
47 class Deque { | 47 // simply call finalize on the actual deque type. |
48 WTF_MAKE_FAST_ALLOCATED; | 48 template<typename Derived, typename Elements, bool hasInlineCapacity, bool i sGarbageCollected> |
49 class DequeDestructorBase { | |
Mikhail
2014/04/08 11:03:01
This looks similar to the existing VectorDestructo
Erik Corry
2014/04/08 11:48:53
Good point, yes we can!
| |
49 public: | 50 public: |
50 typedef DequeIterator<T, inlineCapacity> iterator; | 51 ~DequeDestructorBase() { static_cast<Derived*>(this)->finalize(); } |
51 typedef DequeConstIterator<T, inlineCapacity> const_iterator; | 52 }; |
53 | |
54 // Heap-allocated deques with no inlineCapacity never need a destructor. | |
55 template<typename Derived, typename Elements> | |
56 class DequeDestructorBase<Derived, Elements, false, true> { }; | |
57 | |
58 // Heap-allocator deques with inlineCapacity need a destructor if the | |
59 // inline elements do. The use of VectorTraits<Elements>::needsDestruction | |
60 // is delayed until we know that inlineCapacity is non-zero to allow | |
61 // classes that recursively refer to themselves in deque members. If | |
62 // inlineCapacity is non-zero doing so would have undefined meaning, so in | |
63 // this case we can use HeapDequeWithInlineCapacityDestructorBase to | |
64 // define a destructor depending on the value of | |
65 // VectorTraits<Elements>::needsDestruction. | |
66 template<typename Derived, bool elementsNeedsDestruction> | |
67 class HeapDequeWithInlineCapacityDestructorBase; | |
68 | |
69 template<typename Derived> | |
70 class HeapDequeWithInlineCapacityDestructorBase<Derived, true> { | |
71 public: | |
72 ~HeapDequeWithInlineCapacityDestructorBase() { static_cast<Derived*>(thi s)->finalize(); } | |
73 }; | |
74 | |
75 template<typename Derived> | |
76 class HeapDequeWithInlineCapacityDestructorBase<Derived, false> { }; | |
77 | |
78 template<typename Derived, typename Elements> | |
79 class DequeDestructorBase<Derived, Elements, true, true> : public HeapDequeW ithInlineCapacityDestructorBase<Derived, VectorTraits<Elements>::needsDestructio n> { }; | |
80 | |
81 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator> | |
82 class Deque : public DequeDestructorBase<Deque<T, inlineCapacity, Allocator> , T, (inlineCapacity > 0), Allocator::isGarbageCollected> { | |
83 public: | |
84 typedef DequeIterator<T, inlineCapacity, Allocator> iterator; | |
85 typedef DequeConstIterator<T, inlineCapacity, Allocator> const_iterator; | |
52 typedef std::reverse_iterator<iterator> reverse_iterator; | 86 typedef std::reverse_iterator<iterator> reverse_iterator; |
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | 87 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
54 typedef PassTraits<T> Pass; | 88 typedef PassTraits<T> Pass; |
55 typedef typename PassTraits<T>::PassType PassType; | 89 typedef typename PassTraits<T>::PassType PassType; |
56 | 90 |
91 void* operator new(size_t size) | |
92 { | |
93 return Allocator::template malloc<void*, Deque>(size); | |
94 } | |
95 void operator delete(void* p) { Allocator::free(p); } | |
96 void* operator new[](size_t size) { return Allocator::template newArray< Vector>(size); } | |
97 void operator delete[](void* p) { Allocator::deleteArray(p); } | |
98 void* operator new(size_t, NotNullTag, void* location) | |
99 { | |
100 ASSERT(location); | |
101 return location; | |
102 } | |
103 | |
57 Deque(); | 104 Deque(); |
58 Deque(const Deque<T, inlineCapacity>&); | 105 Deque(const Deque<T, inlineCapacity, Allocator>&); |
59 Deque& operator=(const Deque<T, inlineCapacity>&); | 106 // FIXME: Doesn't work if there is an inline buffer, so for now we only |
60 ~Deque(); | 107 // define the version without. The issue is that the VectorBuffer with |
108 // inline capacity uses the m_size field to know how much of the array t o | |
109 // copy, but that field is unused and unitialized with deques, because t hey | |
110 // do not fill the buffer only from 0 to m_size. | |
111 Deque<T, 0, Allocator>& operator=(const Deque&); | |
61 | 112 |
62 void swap(Deque<T, inlineCapacity>&); | 113 void finalize(); |
114 void finalizeGarbageCollectedObject() { finalize(); } | |
115 | |
116 // We hard wire the inlineCapacity to zero here. See the comment on the | |
117 // assignment operator above. | |
Mikhail
2014/04/08 11:03:01
did not get how we wire the inlineCapacity to zero
Erik Corry
2014/04/08 11:48:53
It seems I didn't wire the inlineCapacity to zero
| |
118 void swap(Deque&); | |
63 | 119 |
64 size_t size() const { return m_start <= m_end ? m_end - m_start : m_end + m_buffer.capacity() - m_start; } | 120 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; } | 121 bool isEmpty() const { return m_start == m_end; } |
66 | 122 |
67 iterator begin() { return iterator(this, m_start); } | 123 iterator begin() { return iterator(this, m_start); } |
68 iterator end() { return iterator(this, m_end); } | 124 iterator end() { return iterator(this, m_end); } |
69 const_iterator begin() const { return const_iterator(this, m_start); } | 125 const_iterator begin() const { return const_iterator(this, m_start); } |
70 const_iterator end() const { return const_iterator(this, m_end); } | 126 const_iterator end() const { return const_iterator(this, m_end); } |
71 reverse_iterator rbegin() { return reverse_iterator(end()); } | 127 reverse_iterator rbegin() { return reverse_iterator(end()); } |
72 reverse_iterator rend() { return reverse_iterator(begin()); } | 128 reverse_iterator rend() { return reverse_iterator(begin()); } |
(...skipping 13 matching lines...) Expand all Loading... | |
86 void removeFirst(); | 142 void removeFirst(); |
87 void removeLast(); | 143 void removeLast(); |
88 void remove(iterator&); | 144 void remove(iterator&); |
89 void remove(const_iterator&); | 145 void remove(const_iterator&); |
90 | 146 |
91 void clear(); | 147 void clear(); |
92 | 148 |
93 template<typename Predicate> | 149 template<typename Predicate> |
94 iterator findIf(Predicate&); | 150 iterator findIf(Predicate&); |
95 | 151 |
152 void trace(typename Allocator::Visitor*); | |
153 | |
96 private: | 154 private: |
97 friend class DequeIteratorBase<T, inlineCapacity>; | 155 friend class DequeIteratorBase<T, inlineCapacity, Allocator>; |
98 | 156 |
99 typedef VectorBuffer<T, inlineCapacity> Buffer; | 157 typedef VectorBuffer<T, inlineCapacity, Allocator> Buffer; |
100 typedef VectorTypeOperations<T> TypeOperations; | 158 typedef VectorTypeOperations<T> TypeOperations; |
101 typedef DequeIteratorBase<T, inlineCapacity> IteratorBase; | 159 typedef DequeIteratorBase<T, inlineCapacity, Allocator> IteratorBase; |
102 | 160 |
103 void remove(size_t position); | 161 void remove(size_t position); |
104 void destroyAll(); | 162 void destroyAll(); |
105 void expandCapacityIfNeeded(); | 163 void expandCapacityIfNeeded(); |
106 void expandCapacity(); | 164 void expandCapacity(); |
107 | 165 |
108 Buffer m_buffer; | 166 Buffer m_buffer; |
109 unsigned m_start; | 167 unsigned m_start; |
110 unsigned m_end; | 168 unsigned m_end; |
111 }; | 169 }; |
112 | 170 |
113 template<typename T, size_t inlineCapacity = 0> | 171 template<typename T, size_t inlineCapacity, typename Allocator> |
114 class DequeIteratorBase { | 172 class DequeIteratorBase { |
115 protected: | 173 protected: |
116 DequeIteratorBase(); | 174 DequeIteratorBase(); |
117 DequeIteratorBase(const Deque<T, inlineCapacity>*, size_t); | 175 DequeIteratorBase(const Deque<T, inlineCapacity, Allocator>*, size_t); |
118 DequeIteratorBase(const DequeIteratorBase&); | 176 DequeIteratorBase(const DequeIteratorBase&); |
119 DequeIteratorBase& operator=(const DequeIteratorBase&); | 177 DequeIteratorBase<T, 0, Allocator>& operator=(const DequeIteratorBase<T, 0, Allocator>&); |
120 ~DequeIteratorBase(); | 178 ~DequeIteratorBase(); |
121 | 179 |
122 void assign(const DequeIteratorBase& other) { *this = other; } | 180 void assign(const DequeIteratorBase& other) { *this = other; } |
123 | 181 |
124 void increment(); | 182 void increment(); |
125 void decrement(); | 183 void decrement(); |
126 | 184 |
127 T* before() const; | 185 T* before() const; |
128 T* after() const; | 186 T* after() const; |
129 | 187 |
130 bool isEqual(const DequeIteratorBase&) const; | 188 bool isEqual(const DequeIteratorBase&) const; |
131 | 189 |
132 private: | 190 private: |
133 Deque<T, inlineCapacity>* m_deque; | 191 Deque<T, inlineCapacity, Allocator>* m_deque; |
134 unsigned m_index; | 192 unsigned m_index; |
135 | 193 |
136 friend class Deque<T, inlineCapacity>; | 194 friend class Deque<T, inlineCapacity, Allocator>; |
137 }; | 195 }; |
138 | 196 |
139 template<typename T, size_t inlineCapacity = 0> | 197 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator> |
140 class DequeIterator : public DequeIteratorBase<T, inlineCapacity> { | 198 class DequeIterator : public DequeIteratorBase<T, inlineCapacity, Allocator> { |
141 private: | 199 private: |
142 typedef DequeIteratorBase<T, inlineCapacity> Base; | 200 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base; |
143 typedef DequeIterator<T, inlineCapacity> Iterator; | 201 typedef DequeIterator<T, inlineCapacity, Allocator> Iterator; |
144 | 202 |
145 public: | 203 public: |
146 typedef ptrdiff_t difference_type; | 204 typedef ptrdiff_t difference_type; |
147 typedef T value_type; | 205 typedef T value_type; |
148 typedef T* pointer; | 206 typedef T* pointer; |
149 typedef T& reference; | 207 typedef T& reference; |
150 typedef std::bidirectional_iterator_tag iterator_category; | 208 typedef std::bidirectional_iterator_tag iterator_category; |
151 | 209 |
152 DequeIterator(Deque<T, inlineCapacity>* deque, size_t index) : Base(dequ e, index) { } | 210 DequeIterator(Deque<T, inlineCapacity, Allocator>* deque, size_t index) : Base(deque, index) { } |
153 | 211 |
154 DequeIterator(const Iterator& other) : Base(other) { } | 212 DequeIterator(const Iterator& other) : Base(other) { } |
155 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r eturn *this; } | 213 DequeIterator& operator=(const Iterator& other) { Base::assign(other); r eturn *this; } |
156 | 214 |
157 T& operator*() const { return *Base::after(); } | 215 T& operator*() const { return *Base::after(); } |
158 T* operator->() const { return Base::after(); } | 216 T* operator->() const { return Base::after(); } |
159 | 217 |
160 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } | 218 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } |
161 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } | 219 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } |
162 | 220 |
163 Iterator& operator++() { Base::increment(); return *this; } | 221 Iterator& operator++() { Base::increment(); return *this; } |
164 // postfix ++ intentionally omitted | 222 // postfix ++ intentionally omitted |
165 Iterator& operator--() { Base::decrement(); return *this; } | 223 Iterator& operator--() { Base::decrement(); return *this; } |
166 // postfix -- intentionally omitted | 224 // postfix -- intentionally omitted |
167 }; | 225 }; |
168 | 226 |
169 template<typename T, size_t inlineCapacity = 0> | 227 template<typename T, size_t inlineCapacity = 0, typename Allocator = Default Allocator> |
170 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity> { | 228 class DequeConstIterator : public DequeIteratorBase<T, inlineCapacity, Alloc ator> { |
171 private: | 229 private: |
172 typedef DequeIteratorBase<T, inlineCapacity> Base; | 230 typedef DequeIteratorBase<T, inlineCapacity, Allocator> Base; |
173 typedef DequeConstIterator<T, inlineCapacity> Iterator; | 231 typedef DequeConstIterator<T, inlineCapacity, Allocator> Iterator; |
174 typedef DequeIterator<T, inlineCapacity> NonConstIterator; | 232 typedef DequeIterator<T, inlineCapacity, Allocator> NonConstIterator; |
175 | 233 |
176 public: | 234 public: |
177 typedef ptrdiff_t difference_type; | 235 typedef ptrdiff_t difference_type; |
178 typedef T value_type; | 236 typedef T value_type; |
179 typedef const T* pointer; | 237 typedef const T* pointer; |
180 typedef const T& reference; | 238 typedef const T& reference; |
181 typedef std::bidirectional_iterator_tag iterator_category; | 239 typedef std::bidirectional_iterator_tag iterator_category; |
182 | 240 |
183 DequeConstIterator(const Deque<T, inlineCapacity>* deque, size_t index) : Base(deque, index) { } | 241 DequeConstIterator(const Deque<T, inlineCapacity, Allocator>* deque, siz e_t index) : Base(deque, index) { } |
184 | 242 |
185 DequeConstIterator(const Iterator& other) : Base(other) { } | 243 DequeConstIterator(const Iterator& other) : Base(other) { } |
186 DequeConstIterator(const NonConstIterator& other) : Base(other) { } | 244 DequeConstIterator(const NonConstIterator& other) : Base(other) { } |
187 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe r); return *this; } | 245 DequeConstIterator& operator=(const Iterator& other) { Base::assign(othe r); return *this; } |
188 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass ign(other); return *this; } | 246 DequeConstIterator& operator=(const NonConstIterator& other) { Base::ass ign(other); return *this; } |
189 | 247 |
190 const T& operator*() const { return *Base::after(); } | 248 const T& operator*() const { return *Base::after(); } |
191 const T* operator->() const { return Base::after(); } | 249 const T* operator->() const { return Base::after(); } |
192 | 250 |
193 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } | 251 bool operator==(const Iterator& other) const { return Base::isEqual(othe r); } |
194 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } | 252 bool operator!=(const Iterator& other) const { return !Base::isEqual(oth er); } |
195 | 253 |
196 Iterator& operator++() { Base::increment(); return *this; } | 254 Iterator& operator++() { Base::increment(); return *this; } |
197 // postfix ++ intentionally omitted | 255 // postfix ++ intentionally omitted |
198 Iterator& operator--() { Base::decrement(); return *this; } | 256 Iterator& operator--() { Base::decrement(); return *this; } |
199 // postfix -- intentionally omitted | 257 // postfix -- intentionally omitted |
200 }; | 258 }; |
201 | 259 |
202 template<typename T, size_t inlineCapacity> | 260 template<typename T, size_t inlineCapacity, typename Allocator> |
203 inline Deque<T, inlineCapacity>::Deque() | 261 inline Deque<T, inlineCapacity, Allocator>::Deque() |
204 : m_start(0) | 262 : m_start(0) |
205 , m_end(0) | 263 , m_end(0) |
206 { | 264 { |
207 } | 265 } |
208 | 266 |
209 template<typename T, size_t inlineCapacity> | 267 template<typename T, size_t inlineCapacity, typename Allocator> |
210 inline Deque<T, inlineCapacity>::Deque(const Deque<T, inlineCapacity>& other ) | 268 inline Deque<T, inlineCapacity, Allocator>::Deque(const Deque<T, inlineCapac ity, Allocator>& other) |
211 : m_buffer(other.m_buffer.capacity()) | 269 : m_buffer(other.m_buffer.capacity()) |
212 , m_start(other.m_start) | 270 , m_start(other.m_start) |
213 , m_end(other.m_end) | 271 , m_end(other.m_end) |
214 { | 272 { |
215 const T* otherBuffer = other.m_buffer.buffer(); | 273 const T* otherBuffer = other.m_buffer.buffer(); |
216 if (m_start <= m_end) | 274 if (m_start <= m_end) |
217 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start); | 275 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_end, m_buffer.buffer() + m_start); |
218 else { | 276 else { |
219 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer()); | 277 TypeOperations::uninitializedCopy(otherBuffer, otherBuffer + m_end, m_buffer.buffer()); |
220 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start); | 278 TypeOperations::uninitializedCopy(otherBuffer + m_start, otherBuffer + m_buffer.capacity(), m_buffer.buffer() + m_start); |
221 } | 279 } |
222 } | 280 } |
223 | 281 |
224 template<typename T, size_t inlineCapacity> | 282 template<typename T, size_t inlineCapacity, typename Allocator> |
225 void deleteAllValues(const Deque<T, inlineCapacity>& collection) | 283 void deleteAllValues(const Deque<T, inlineCapacity, Allocator>& collection) |
226 { | 284 { |
227 typedef typename Deque<T, inlineCapacity>::const_iterator iterator; | 285 typedef typename Deque<T, inlineCapacity, Allocator>::const_iterator ite rator; |
228 iterator end = collection.end(); | 286 iterator end = collection.end(); |
229 for (iterator it = collection.begin(); it != end; ++it) | 287 for (iterator it = collection.begin(); it != end; ++it) |
230 delete *it; | 288 delete *it; |
231 } | 289 } |
232 | 290 |
233 template<typename T, size_t inlineCapacity> | 291 template<typename T, size_t inlineCapacity, typename Allocator> |
234 inline Deque<T, inlineCapacity>& Deque<T, inlineCapacity>::operator=(const D eque<T, inlineCapacity>& other) | 292 inline Deque<T, 0, Allocator>& Deque<T, inlineCapacity, Allocator>::operator =(const Deque& other) |
235 { | 293 { |
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); | 294 Deque<T> copy(other); |
239 swap(copy); | 295 swap(copy); |
240 return *this; | 296 return *this; |
241 } | 297 } |
242 | 298 |
243 template<typename T, size_t inlineCapacity> | 299 template<typename T, size_t inlineCapacity, typename Allocator> |
244 inline void Deque<T, inlineCapacity>::destroyAll() | 300 inline void Deque<T, inlineCapacity, Allocator>::destroyAll() |
245 { | 301 { |
246 if (m_start <= m_end) | 302 if (m_start <= m_end) |
247 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_end); | 303 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_end); |
248 else { | 304 else { |
249 TypeOperations::destruct(m_buffer.buffer(), m_buffer.buffer() + m_en d); | 305 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()); | 306 TypeOperations::destruct(m_buffer.buffer() + m_start, m_buffer.buffe r() + m_buffer.capacity()); |
251 } | 307 } |
252 } | 308 } |
253 | 309 |
254 template<typename T, size_t inlineCapacity> | 310 // Off-GC-heap deques: Destructor should be called. |
255 inline Deque<T, inlineCapacity>::~Deque() | 311 // On-GC-heap deques: Destructor should be called for inline buffers |
312 // (if any) but destructor shouldn't be called for vector backing since | |
313 // it is managed by the traced GC heap. | |
314 template<typename T, size_t inlineCapacity, typename Allocator> | |
315 inline void Deque<T, inlineCapacity, Allocator>::finalize() | |
256 { | 316 { |
257 destroyAll(); | 317 if (!inlineCapacity) { |
318 if (LIKELY(!m_buffer.buffer())) | |
Mikhail
2014/04/08 11:03:01
so not having the buffer is most expected?
Erik Corry
2014/04/08 11:48:53
Removed the LIKELY, as I don't think it really add
| |
319 return; | |
320 } | |
321 if (LIKELY(!isEmpty()) && !(Allocator::isGarbageCollected && m_buffer.ha sOutOfLineBuffer())) { | |
322 destroyAll(); | |
323 } | |
324 | |
258 m_buffer.destruct(); | 325 m_buffer.destruct(); |
259 } | 326 } |
260 | 327 |
261 template<typename T, size_t inlineCapacity> | 328 // FIXME: Doesn't work if there is an inline buffer, so for now we only |
262 inline void Deque<T, inlineCapacity>::swap(Deque<T, inlineCapacity>& other) | 329 // define the version without. |
330 template<typename T, size_t inlineCapacity, typename Allocator> | |
331 inline void Deque<T, inlineCapacity, Allocator>::swap(Deque& other) | |
263 { | 332 { |
264 std::swap(m_start, other.m_start); | 333 std::swap(m_start, other.m_start); |
265 std::swap(m_end, other.m_end); | 334 std::swap(m_end, other.m_end); |
266 m_buffer.swapVectorBuffer(other.m_buffer); | 335 m_buffer.swapVectorBuffer(other.m_buffer); |
267 } | 336 } |
268 | 337 |
269 template<typename T, size_t inlineCapacity> | 338 template<typename T, size_t inlineCapacity, typename Allocator> |
270 inline void Deque<T, inlineCapacity>::clear() | 339 inline void Deque<T, inlineCapacity, Allocator>::clear() |
271 { | 340 { |
272 destroyAll(); | 341 destroyAll(); |
273 m_start = 0; | 342 m_start = 0; |
274 m_end = 0; | 343 m_end = 0; |
275 m_buffer.deallocateBuffer(m_buffer.buffer()); | 344 m_buffer.deallocateBuffer(m_buffer.buffer()); |
276 m_buffer.resetBufferPointer(); | 345 m_buffer.resetBufferPointer(); |
277 } | 346 } |
278 | 347 |
279 template<typename T, size_t inlineCapacity> | 348 template<typename T, size_t inlineCapacity, typename Allocator> |
280 template<typename Predicate> | 349 template<typename Predicate> |
281 inline DequeIterator<T, inlineCapacity> Deque<T, inlineCapacity>::findIf(Pre dicate& predicate) | 350 inline DequeIterator<T, inlineCapacity, Allocator> Deque<T, inlineCapacity, Allocator>::findIf(Predicate& predicate) |
282 { | 351 { |
283 iterator end_iterator = end(); | 352 iterator end_iterator = end(); |
284 for (iterator it = begin(); it != end_iterator; ++it) { | 353 for (iterator it = begin(); it != end_iterator; ++it) { |
285 if (predicate(*it)) | 354 if (predicate(*it)) |
286 return it; | 355 return it; |
287 } | 356 } |
288 return end_iterator; | 357 return end_iterator; |
289 } | 358 } |
290 | 359 |
291 template<typename T, size_t inlineCapacity> | 360 template<typename T, size_t inlineCapacity, typename Allocator> |
292 inline void Deque<T, inlineCapacity>::expandCapacityIfNeeded() | 361 inline void Deque<T, inlineCapacity, Allocator>::expandCapacityIfNeeded() |
293 { | 362 { |
294 if (m_start) { | 363 if (m_start) { |
295 if (m_end + 1 != m_start) | 364 if (m_end + 1 != m_start) |
296 return; | 365 return; |
297 } else if (m_end) { | 366 } else if (m_end) { |
298 if (m_end != m_buffer.capacity() - 1) | 367 if (m_end != m_buffer.capacity() - 1) |
299 return; | 368 return; |
300 } else if (m_buffer.capacity()) | 369 } else if (m_buffer.capacity()) |
301 return; | 370 return; |
302 | 371 |
303 expandCapacity(); | 372 expandCapacity(); |
304 } | 373 } |
305 | 374 |
306 template<typename T, size_t inlineCapacity> | 375 template<typename T, size_t inlineCapacity, typename Allocator> |
307 void Deque<T, inlineCapacity>::expandCapacity() | 376 void Deque<T, inlineCapacity, Allocator>::expandCapacity() |
308 { | 377 { |
309 size_t oldCapacity = m_buffer.capacity(); | 378 size_t oldCapacity = m_buffer.capacity(); |
310 T* oldBuffer = m_buffer.buffer(); | 379 T* oldBuffer = m_buffer.buffer(); |
311 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1)); | 380 m_buffer.allocateBuffer(std::max(static_cast<size_t>(16), oldCapacity + oldCapacity / 4 + 1)); |
312 if (m_start <= m_end) | 381 if (m_start <= m_end) |
313 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe r.buffer() + m_start); | 382 TypeOperations::move(oldBuffer + m_start, oldBuffer + m_end, m_buffe r.buffer() + m_start); |
314 else { | 383 else { |
315 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer() ); | 384 TypeOperations::move(oldBuffer, oldBuffer + m_end, m_buffer.buffer() ); |
316 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start); | 385 size_t newStart = m_buffer.capacity() - (oldCapacity - m_start); |
317 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m _buffer.buffer() + newStart); | 386 TypeOperations::move(oldBuffer + m_start, oldBuffer + oldCapacity, m _buffer.buffer() + newStart); |
318 m_start = newStart; | 387 m_start = newStart; |
319 } | 388 } |
320 m_buffer.deallocateBuffer(oldBuffer); | 389 m_buffer.deallocateBuffer(oldBuffer); |
321 } | 390 } |
322 | 391 |
323 template<typename T, size_t inlineCapacity> | 392 template<typename T, size_t inlineCapacity, typename Allocator> |
324 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>: :takeFirst() | 393 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin eCapacity, Allocator>::takeFirst() |
325 { | 394 { |
326 T oldFirst = Pass::transfer(first()); | 395 T oldFirst = Pass::transfer(first()); |
327 removeFirst(); | 396 removeFirst(); |
328 return Pass::transfer(oldFirst); | 397 return Pass::transfer(oldFirst); |
329 } | 398 } |
330 | 399 |
331 template<typename T, size_t inlineCapacity> | 400 template<typename T, size_t inlineCapacity, typename Allocator> |
332 inline typename Deque<T, inlineCapacity>::PassType Deque<T, inlineCapacity>: :takeLast() | 401 inline typename Deque<T, inlineCapacity, Allocator>::PassType Deque<T, inlin eCapacity, Allocator>::takeLast() |
333 { | 402 { |
334 T oldLast = Pass::transfer(last()); | 403 T oldLast = Pass::transfer(last()); |
335 removeLast(); | 404 removeLast(); |
336 return Pass::transfer(oldLast); | 405 return Pass::transfer(oldLast); |
337 } | 406 } |
338 | 407 |
339 template<typename T, size_t inlineCapacity> template<typename U> | 408 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U> |
340 inline void Deque<T, inlineCapacity>::append(const U& value) | 409 inline void Deque<T, inlineCapacity, Allocator>::append(const U& value) |
341 { | 410 { |
342 expandCapacityIfNeeded(); | 411 expandCapacityIfNeeded(); |
343 new (NotNull, &m_buffer.buffer()[m_end]) T(value); | 412 new (NotNull, &m_buffer.buffer()[m_end]) T(value); |
344 if (m_end == m_buffer.capacity() - 1) | 413 if (m_end == m_buffer.capacity() - 1) |
345 m_end = 0; | 414 m_end = 0; |
346 else | 415 else |
347 ++m_end; | 416 ++m_end; |
348 } | 417 } |
349 | 418 |
350 template<typename T, size_t inlineCapacity> template<typename U> | 419 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U> |
351 inline void Deque<T, inlineCapacity>::prepend(const U& value) | 420 inline void Deque<T, inlineCapacity, Allocator>::prepend(const U& value) |
352 { | 421 { |
353 expandCapacityIfNeeded(); | 422 expandCapacityIfNeeded(); |
354 if (!m_start) | 423 if (!m_start) |
355 m_start = m_buffer.capacity() - 1; | 424 m_start = m_buffer.capacity() - 1; |
356 else | 425 else |
357 --m_start; | 426 --m_start; |
358 new (NotNull, &m_buffer.buffer()[m_start]) T(value); | 427 new (NotNull, &m_buffer.buffer()[m_start]) T(value); |
359 } | 428 } |
360 | 429 |
361 template<typename T, size_t inlineCapacity> | 430 template<typename T, size_t inlineCapacity, typename Allocator> |
362 inline void Deque<T, inlineCapacity>::removeFirst() | 431 inline void Deque<T, inlineCapacity, Allocator>::removeFirst() |
363 { | 432 { |
364 ASSERT(!isEmpty()); | 433 ASSERT(!isEmpty()); |
365 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer() [m_start + 1]); | 434 TypeOperations::destruct(&m_buffer.buffer()[m_start], &m_buffer.buffer() [m_start + 1]); |
366 if (m_start == m_buffer.capacity() - 1) | 435 if (m_start == m_buffer.capacity() - 1) |
367 m_start = 0; | 436 m_start = 0; |
368 else | 437 else |
369 ++m_start; | 438 ++m_start; |
370 } | 439 } |
371 | 440 |
372 template<typename T, size_t inlineCapacity> | 441 template<typename T, size_t inlineCapacity, typename Allocator> |
373 inline void Deque<T, inlineCapacity>::removeLast() | 442 inline void Deque<T, inlineCapacity, Allocator>::removeLast() |
374 { | 443 { |
375 ASSERT(!isEmpty()); | 444 ASSERT(!isEmpty()); |
376 if (!m_end) | 445 if (!m_end) |
377 m_end = m_buffer.capacity() - 1; | 446 m_end = m_buffer.capacity() - 1; |
378 else | 447 else |
379 --m_end; | 448 --m_end; |
380 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m _end + 1]); | 449 TypeOperations::destruct(&m_buffer.buffer()[m_end], &m_buffer.buffer()[m _end + 1]); |
381 } | 450 } |
382 | 451 |
383 template<typename T, size_t inlineCapacity> | 452 template<typename T, size_t inlineCapacity, typename Allocator> |
384 inline void Deque<T, inlineCapacity>::remove(iterator& it) | 453 inline void Deque<T, inlineCapacity, Allocator>::remove(iterator& it) |
385 { | 454 { |
386 remove(it.m_index); | 455 remove(it.m_index); |
387 } | 456 } |
388 | 457 |
389 template<typename T, size_t inlineCapacity> | 458 template<typename T, size_t inlineCapacity, typename Allocator> |
390 inline void Deque<T, inlineCapacity>::remove(const_iterator& it) | 459 inline void Deque<T, inlineCapacity, Allocator>::remove(const_iterator& it) |
391 { | 460 { |
392 remove(it.m_index); | 461 remove(it.m_index); |
393 } | 462 } |
394 | 463 |
395 template<typename T, size_t inlineCapacity> | 464 template<typename T, size_t inlineCapacity, typename Allocator> |
396 inline void Deque<T, inlineCapacity>::remove(size_t position) | 465 inline void Deque<T, inlineCapacity, Allocator>::remove(size_t position) |
397 { | 466 { |
398 if (position == m_end) | 467 if (position == m_end) |
399 return; | 468 return; |
400 | 469 |
401 T* buffer = m_buffer.buffer(); | 470 T* buffer = m_buffer.buffer(); |
402 TypeOperations::destruct(&buffer[position], &buffer[position + 1]); | 471 TypeOperations::destruct(&buffer[position], &buffer[position + 1]); |
403 | 472 |
404 // Find which segment of the circular buffer contained the remove elemen t, and only move elements in that part. | 473 // Find which segment of the circular buffer contained the remove elemen t, and only move elements in that part. |
405 if (position >= m_start) { | 474 if (position >= m_start) { |
406 TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1); | 475 TypeOperations::moveOverlapping(buffer + m_start, buffer + position, buffer + m_start + 1); |
407 m_start = (m_start + 1) % m_buffer.capacity(); | 476 m_start = (m_start + 1) % m_buffer.capacity(); |
408 } else { | 477 } else { |
409 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en d, buffer + position); | 478 TypeOperations::moveOverlapping(buffer + position + 1, buffer + m_en d, buffer + position); |
410 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity(); | 479 m_end = (m_end - 1 + m_buffer.capacity()) % m_buffer.capacity(); |
411 } | 480 } |
412 } | 481 } |
413 | 482 |
414 template<typename T, size_t inlineCapacity> | 483 template<typename T, size_t inlineCapacity, typename Allocator> |
415 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase() | 484 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase() |
416 : m_deque(0) | 485 : m_deque(0) |
417 { | 486 { |
418 } | 487 } |
419 | 488 |
420 template<typename T, size_t inlineCapacity> | 489 template<typename T, size_t inlineCapacity, typename Allocator> |
421 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const Deque<T , inlineCapacity>* deque, size_t index) | 490 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)) | 491 : m_deque(const_cast<Deque<T, inlineCapacity, Allocator>*>(deque)) |
423 , m_index(index) | 492 , m_index(index) |
424 { | 493 { |
425 } | 494 } |
426 | 495 |
427 template<typename T, size_t inlineCapacity> | 496 template<typename T, size_t inlineCapacity, typename Allocator> |
428 inline DequeIteratorBase<T, inlineCapacity>::DequeIteratorBase(const DequeIt eratorBase& other) | 497 inline DequeIteratorBase<T, inlineCapacity, Allocator>::DequeIteratorBase(co nst DequeIteratorBase& other) |
429 : m_deque(other.m_deque) | 498 : m_deque(other.m_deque) |
430 , m_index(other.m_index) | 499 , m_index(other.m_index) |
431 { | 500 { |
432 } | 501 } |
433 | 502 |
434 template<typename T, size_t inlineCapacity> | 503 template<typename T, size_t inlineCapacity, typename Allocator> |
435 inline DequeIteratorBase<T, inlineCapacity>& DequeIteratorBase<T, inlineCapa city>::operator=(const DequeIteratorBase& other) | 504 inline DequeIteratorBase<T, 0, Allocator>& DequeIteratorBase<T, inlineCapaci ty, Allocator>::operator=(const DequeIteratorBase<T, 0, Allocator>& other) |
436 { | 505 { |
437 m_deque = other.m_deque; | 506 m_deque = other.m_deque; |
438 m_index = other.m_index; | 507 m_index = other.m_index; |
439 return *this; | 508 return *this; |
440 } | 509 } |
441 | 510 |
442 template<typename T, size_t inlineCapacity> | 511 template<typename T, size_t inlineCapacity, typename Allocator> |
443 inline DequeIteratorBase<T, inlineCapacity>::~DequeIteratorBase() | 512 inline DequeIteratorBase<T, inlineCapacity, Allocator>::~DequeIteratorBase() |
444 { | 513 { |
445 } | 514 } |
446 | 515 |
447 template<typename T, size_t inlineCapacity> | 516 template<typename T, size_t inlineCapacity, typename Allocator> |
448 inline bool DequeIteratorBase<T, inlineCapacity>::isEqual(const DequeIterato rBase& other) const | 517 inline bool DequeIteratorBase<T, inlineCapacity, Allocator>::isEqual(const D equeIteratorBase& other) const |
449 { | 518 { |
450 return m_index == other.m_index; | 519 return m_index == other.m_index; |
451 } | 520 } |
452 | 521 |
453 template<typename T, size_t inlineCapacity> | 522 template<typename T, size_t inlineCapacity, typename Allocator> |
454 inline void DequeIteratorBase<T, inlineCapacity>::increment() | 523 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::increment() |
455 { | 524 { |
456 ASSERT(m_index != m_deque->m_end); | 525 ASSERT(m_index != m_deque->m_end); |
457 ASSERT(m_deque->m_buffer.capacity()); | 526 ASSERT(m_deque->m_buffer.capacity()); |
458 if (m_index == m_deque->m_buffer.capacity() - 1) | 527 if (m_index == m_deque->m_buffer.capacity() - 1) |
459 m_index = 0; | 528 m_index = 0; |
460 else | 529 else |
461 ++m_index; | 530 ++m_index; |
462 } | 531 } |
463 | 532 |
464 template<typename T, size_t inlineCapacity> | 533 template<typename T, size_t inlineCapacity, typename Allocator> |
465 inline void DequeIteratorBase<T, inlineCapacity>::decrement() | 534 inline void DequeIteratorBase<T, inlineCapacity, Allocator>::decrement() |
466 { | 535 { |
467 ASSERT(m_index != m_deque->m_start); | 536 ASSERT(m_index != m_deque->m_start); |
468 ASSERT(m_deque->m_buffer.capacity()); | 537 ASSERT(m_deque->m_buffer.capacity()); |
469 if (!m_index) | 538 if (!m_index) |
470 m_index = m_deque->m_buffer.capacity() - 1; | 539 m_index = m_deque->m_buffer.capacity() - 1; |
471 else | 540 else |
472 --m_index; | 541 --m_index; |
473 } | 542 } |
474 | 543 |
475 template<typename T, size_t inlineCapacity> | 544 template<typename T, size_t inlineCapacity, typename Allocator> |
476 inline T* DequeIteratorBase<T, inlineCapacity>::after() const | 545 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::after() const |
477 { | 546 { |
478 ASSERT(m_index != m_deque->m_end); | 547 ASSERT(m_index != m_deque->m_end); |
479 return &m_deque->m_buffer.buffer()[m_index]; | 548 return &m_deque->m_buffer.buffer()[m_index]; |
480 } | 549 } |
481 | 550 |
482 template<typename T, size_t inlineCapacity> | 551 template<typename T, size_t inlineCapacity, typename Allocator> |
483 inline T* DequeIteratorBase<T, inlineCapacity>::before() const | 552 inline T* DequeIteratorBase<T, inlineCapacity, Allocator>::before() const |
484 { | 553 { |
485 ASSERT(m_index != m_deque->m_start); | 554 ASSERT(m_index != m_deque->m_start); |
486 if (!m_index) | 555 if (!m_index) |
487 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1] ; | 556 return &m_deque->m_buffer.buffer()[m_deque->m_buffer.capacity() - 1] ; |
488 return &m_deque->m_buffer.buffer()[m_index - 1]; | 557 return &m_deque->m_buffer.buffer()[m_index - 1]; |
489 } | 558 } |
490 | 559 |
560 // This is only called if the allocator is a HeapAllocator. It is used when | |
561 // visiting during a tracing GC. | |
562 template<typename T, size_t inlineCapacity, typename Allocator> | |
563 void Deque<T, inlineCapacity, Allocator>::trace(typename Allocator::Visitor* visitor) | |
564 { | |
565 COMPILE_ASSERT(Allocator::isGarbageCollected, Garbage_collector_must_be_ enabled); | |
566 const T* bufferBegin = m_buffer.buffer(); | |
567 const T* end = bufferBegin + m_end; | |
568 if (ShouldBeTraced<VectorTraits<T> >::value) { | |
569 if (m_start <= m_end) { | |
570 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry ! = end; bufferEntry++) | |
Mikhail
2014/04/08 11:03:01
nit: ++bufferEntry
Erik Corry
2014/04/08 11:48:53
Style guide allows both.
| |
571 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry)); | |
572 } else { | |
573 for (const T* bufferEntry = bufferBegin; bufferEntry != end; buf ferEntry++) | |
574 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry)); | |
575 const T* bufferEnd = m_buffer.buffer() + m_buffer.capacity(); | |
576 for (const T* bufferEntry = bufferBegin + m_start; bufferEntry ! = bufferEnd; bufferEntry++) | |
577 Allocator::template trace<T, VectorTraits<T> >(visitor, *con st_cast<T*>(bufferEntry)); | |
578 } | |
579 } | |
580 if (m_buffer.hasOutOfLineBuffer()) | |
581 Allocator::markNoTracing(visitor, m_buffer.buffer()); | |
582 } | |
583 | |
491 } // namespace WTF | 584 } // namespace WTF |
492 | 585 |
493 using WTF::Deque; | 586 using WTF::Deque; |
494 | 587 |
495 #endif // WTF_Deque_h | 588 #endif // WTF_Deque_h |
OLD | NEW |