]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/ListHashSet.h
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / wtf / ListHashSet.h
CommitLineData
b37bf2e1 1/*
9dae56ea 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
14957cd0 3 * Copyright (C) 2011, Benjamin Poulain <ikipou@gmail.com>
b37bf2e1
A
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_ListHashSet_h
23#define WTF_ListHashSet_h
24
25#include "Assertions.h"
26#include "HashSet.h"
27#include "OwnPtr.h"
14957cd0
A
28#include "PassOwnPtr.h"
29#include "StdLibExtras.h"
b37bf2e1
A
30
31namespace WTF {
32
33 // ListHashSet: Just like HashSet, this class provides a Set
34 // interface - a collection of unique objects with O(1) insertion,
35 // removal and test for containership. However, it also has an
36 // order - iterating it will always give back values in the order
37 // in which they are added.
38
9dae56ea 39 // In theory it would be possible to add prepend, insertAfter
b37bf2e1
A
40 // and an append that moves the element to the end even if already present,
41 // but unclear yet if these are needed.
42
4e4e5a6f 43 template<typename Value, size_t inlineCapacity, typename HashFunctions> class ListHashSet;
b37bf2e1
A
44
45 template<typename T> struct IdentityExtractor;
46
4e4e5a6f
A
47 template<typename Value, size_t inlineCapacity, typename HashFunctions>
48 void deleteAllValues(const ListHashSet<Value, inlineCapacity, HashFunctions>&);
b37bf2e1 49
4e4e5a6f
A
50 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator;
51 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator;
b37bf2e1 52
4e4e5a6f
A
53 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode;
54 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator;
55 template<typename ValueArg, size_t inlineCapacity, typename HashArg> struct ListHashSetNodeHashFunctions;
b37bf2e1 56
14957cd0
A
57 template<typename ValueArg, size_t inlineCapacity = 256, typename HashArg = typename DefaultHash<ValueArg>::Hash> class ListHashSet {
58 WTF_MAKE_FAST_ALLOCATED;
b37bf2e1 59 private:
4e4e5a6f
A
60 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
61 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
b37bf2e1
A
62
63 typedef HashTraits<Node*> NodeTraits;
4e4e5a6f 64 typedef ListHashSetNodeHashFunctions<ValueArg, inlineCapacity, HashArg> NodeHash;
b37bf2e1
A
65
66 typedef HashTable<Node*, Node*, IdentityExtractor<Node*>, NodeHash, NodeTraits, NodeTraits> ImplType;
9dae56ea
A
67 typedef HashTableIterator<Node*, Node*, IdentityExtractor<Node*>, NodeHash, NodeTraits, NodeTraits> ImplTypeIterator;
68 typedef HashTableConstIterator<Node*, Node*, IdentityExtractor<Node*>, NodeHash, NodeTraits, NodeTraits> ImplTypeConstIterator;
b37bf2e1
A
69
70 typedef HashArg HashFunctions;
71
72 public:
73 typedef ValueArg ValueType;
4e4e5a6f
A
74 typedef ListHashSetIterator<ValueType, inlineCapacity, HashArg> iterator;
75 typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> const_iterator;
b37bf2e1 76
4e4e5a6f 77 friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg>;
b37bf2e1
A
78
79 ListHashSet();
80 ListHashSet(const ListHashSet&);
81 ListHashSet& operator=(const ListHashSet&);
82 ~ListHashSet();
83
84 void swap(ListHashSet&);
85
86 int size() const;
87 int capacity() const;
88 bool isEmpty() const;
89
90 iterator begin();
91 iterator end();
92 const_iterator begin() const;
93 const_iterator end() const;
94
14957cd0
A
95 ValueType& first();
96 const ValueType& first() const;
97
98 ValueType& last();
99 const ValueType& last() const;
100 void removeLast();
101
b37bf2e1
A
102 iterator find(const ValueType&);
103 const_iterator find(const ValueType&) const;
104 bool contains(const ValueType&) const;
105
14957cd0
A
106 // An alternate version of find() that finds the object by hashing and comparing
107 // with some other type, to avoid the cost of type conversion.
108 // The HashTranslator interface is defined in HashSet.
109 template<typename T, typename HashTranslator> iterator find(const T&);
110 template<typename T, typename HashTranslator> const_iterator find(const T&) const;
111 template<typename T, typename HashTranslator> bool contains(const T&) const;
112
9dae56ea 113 // the return value is a pair of an iterator to the new value's location,
b37bf2e1
A
114 // and a bool that is true if an new entry was added
115 pair<iterator, bool> add(const ValueType&);
116
9dae56ea
A
117 pair<iterator, bool> insertBefore(const ValueType& beforeValue, const ValueType& newValue);
118 pair<iterator, bool> insertBefore(iterator it, const ValueType&);
119
b37bf2e1
A
120 void remove(const ValueType&);
121 void remove(iterator);
122 void clear();
123
124 private:
125 void unlinkAndDelete(Node*);
126 void appendNode(Node*);
9dae56ea 127 void insertNodeBefore(Node* beforeNode, Node* newNode);
b37bf2e1
A
128 void deleteAllNodes();
129 iterator makeIterator(Node*);
130 const_iterator makeConstIterator(Node*) const;
131
132 friend void deleteAllValues<>(const ListHashSet&);
133
134 ImplType m_impl;
135 Node* m_head;
136 Node* m_tail;
137 OwnPtr<NodeAllocator> m_allocator;
138 };
139
4e4e5a6f
A
140 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator {
141 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
142 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
b37bf2e1
A
143
144 ListHashSetNodeAllocator()
145 : m_freeList(pool())
146 , m_isDoneWithInitialFreeList(false)
147 {
148 memset(m_pool.pool, 0, sizeof(m_pool.pool));
149 }
150
151 Node* allocate()
152 {
153 Node* result = m_freeList;
154
155 if (!result)
156 return static_cast<Node*>(fastMalloc(sizeof(Node)));
157
158 ASSERT(!result->m_isAllocated);
159
160 Node* next = result->m_next;
161 ASSERT(!next || !next->m_isAllocated);
162 if (!next && !m_isDoneWithInitialFreeList) {
163 next = result + 1;
164 if (next == pastPool()) {
165 m_isDoneWithInitialFreeList = true;
166 next = 0;
167 } else {
168 ASSERT(inPool(next));
169 ASSERT(!next->m_isAllocated);
170 }
171 }
172 m_freeList = next;
173
174 return result;
175 }
176
177 void deallocate(Node* node)
178 {
179 if (inPool(node)) {
180#ifndef NDEBUG
181 node->m_isAllocated = false;
182#endif
183 node->m_next = m_freeList;
184 m_freeList = node;
185 return;
186 }
187
188 fastFree(node);
189 }
190
191 private:
14957cd0 192 Node* pool() { return reinterpret_cast_ptr<Node*>(m_pool.pool); }
b37bf2e1
A
193 Node* pastPool() { return pool() + m_poolSize; }
194
195 bool inPool(Node* node)
196 {
197 return node >= pool() && node < pastPool();
198 }
199
200 Node* m_freeList;
201 bool m_isDoneWithInitialFreeList;
4e4e5a6f 202 static const size_t m_poolSize = inlineCapacity;
b37bf2e1
A
203 union {
204 char pool[sizeof(Node) * m_poolSize];
205 double forAlignment;
206 } m_pool;
207 };
208
4e4e5a6f
A
209 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode {
210 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
b37bf2e1
A
211
212 ListHashSetNode(ValueArg value)
213 : m_value(value)
214 , m_prev(0)
215 , m_next(0)
216#ifndef NDEBUG
217 , m_isAllocated(true)
218#endif
219 {
220 }
221
222 void* operator new(size_t, NodeAllocator* allocator)
223 {
224 return allocator->allocate();
225 }
226 void destroy(NodeAllocator* allocator)
227 {
228 this->~ListHashSetNode();
229 allocator->deallocate(this);
230 }
231
232 ValueArg m_value;
233 ListHashSetNode* m_prev;
234 ListHashSetNode* m_next;
235
236#ifndef NDEBUG
237 bool m_isAllocated;
238#endif
239 };
240
4e4e5a6f
A
241 template<typename ValueArg, size_t inlineCapacity, typename HashArg> struct ListHashSetNodeHashFunctions {
242 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
b37bf2e1
A
243
244 static unsigned hash(Node* const& key) { return HashArg::hash(key->m_value); }
245 static bool equal(Node* const& a, Node* const& b) { return HashArg::equal(a->m_value, b->m_value); }
246 static const bool safeToCompareToEmptyOrDeleted = false;
247 };
248
4e4e5a6f 249 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator {
b37bf2e1 250 private:
4e4e5a6f
A
251 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
252 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
253 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
254 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
b37bf2e1
A
255 typedef ValueArg ValueType;
256 typedef ValueType& ReferenceType;
257 typedef ValueType* PointerType;
258
4e4e5a6f 259 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
b37bf2e1
A
260
261 ListHashSetIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
262
263 public:
264 ListHashSetIterator() { }
265
266 // default copy, assignment and destructor are OK
267
268 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
269 ReferenceType operator*() const { return *get(); }
270 PointerType operator->() const { return get(); }
271
272 iterator& operator++() { ++m_iterator; return *this; }
273
274 // postfix ++ intentionally omitted
275
276 iterator& operator--() { --m_iterator; return *this; }
277
278 // postfix -- intentionally omitted
279
280 // Comparison.
281 bool operator==(const iterator& other) const { return m_iterator == other.m_iterator; }
282 bool operator!=(const iterator& other) const { return m_iterator != other.m_iterator; }
283
284 operator const_iterator() const { return m_iterator; }
285
286 private:
287 Node* node() { return m_iterator.node(); }
288
289 const_iterator m_iterator;
290 };
291
4e4e5a6f 292 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator {
b37bf2e1 293 private:
4e4e5a6f
A
294 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
295 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
296 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
297 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
b37bf2e1
A
298 typedef ValueArg ValueType;
299 typedef const ValueType& ReferenceType;
300 typedef const ValueType* PointerType;
301
4e4e5a6f
A
302 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
303 friend class ListHashSetIterator<ValueArg, inlineCapacity, HashArg>;
b37bf2e1
A
304
305 ListHashSetConstIterator(const ListHashSetType* set, Node* position)
306 : m_set(set)
307 , m_position(position)
308 {
309 }
310
311 public:
312 ListHashSetConstIterator()
313 {
314 }
315
316 PointerType get() const
317 {
318 return &m_position->m_value;
319 }
320 ReferenceType operator*() const { return *get(); }
321 PointerType operator->() const { return get(); }
322
323 const_iterator& operator++()
324 {
325 ASSERT(m_position != 0);
326 m_position = m_position->m_next;
327 return *this;
328 }
329
330 // postfix ++ intentionally omitted
331
332 const_iterator& operator--()
333 {
334 ASSERT(m_position != m_set->m_head);
9dae56ea
A
335 if (!m_position)
336 m_position = m_set->m_tail;
337 else
338 m_position = m_position->m_prev;
b37bf2e1
A
339 return *this;
340 }
341
342 // postfix -- intentionally omitted
343
344 // Comparison.
345 bool operator==(const const_iterator& other) const
346 {
347 return m_position == other.m_position;
348 }
349 bool operator!=(const const_iterator& other) const
350 {
351 return m_position != other.m_position;
352 }
353
354 private:
355 Node* node() { return m_position; }
356
357 const ListHashSetType* m_set;
358 Node* m_position;
359 };
360
361
4e4e5a6f 362 template<typename ValueType, size_t inlineCapacity, typename HashFunctions>
b37bf2e1
A
363 struct ListHashSetTranslator {
364 private:
4e4e5a6f
A
365 typedef ListHashSetNode<ValueType, inlineCapacity> Node;
366 typedef ListHashSetNodeAllocator<ValueType, inlineCapacity> NodeAllocator;
b37bf2e1
A
367 public:
368 static unsigned hash(const ValueType& key) { return HashFunctions::hash(key); }
369 static bool equal(Node* const& a, const ValueType& b) { return HashFunctions::equal(a->m_value, b); }
370 static void translate(Node*& location, const ValueType& key, NodeAllocator* allocator)
371 {
372 location = new (allocator) Node(key);
373 }
374 };
375
4e4e5a6f
A
376 template<typename T, size_t inlineCapacity, typename U>
377 inline ListHashSet<T, inlineCapacity, U>::ListHashSet()
b37bf2e1
A
378 : m_head(0)
379 , m_tail(0)
14957cd0 380 , m_allocator(adoptPtr(new NodeAllocator))
b37bf2e1
A
381 {
382 }
383
4e4e5a6f
A
384 template<typename T, size_t inlineCapacity, typename U>
385 inline ListHashSet<T, inlineCapacity, U>::ListHashSet(const ListHashSet& other)
b37bf2e1
A
386 : m_head(0)
387 , m_tail(0)
14957cd0 388 , m_allocator(adoptPtr(new NodeAllocator))
b37bf2e1
A
389 {
390 const_iterator end = other.end();
391 for (const_iterator it = other.begin(); it != end; ++it)
392 add(*it);
393 }
394
4e4e5a6f
A
395 template<typename T, size_t inlineCapacity, typename U>
396 inline ListHashSet<T, inlineCapacity, U>& ListHashSet<T, inlineCapacity, U>::operator=(const ListHashSet& other)
b37bf2e1
A
397 {
398 ListHashSet tmp(other);
399 swap(tmp);
400 return *this;
401 }
402
4e4e5a6f
A
403 template<typename T, size_t inlineCapacity, typename U>
404 inline void ListHashSet<T, inlineCapacity, U>::swap(ListHashSet& other)
b37bf2e1
A
405 {
406 m_impl.swap(other.m_impl);
407 std::swap(m_head, other.m_head);
408 std::swap(m_tail, other.m_tail);
409 m_allocator.swap(other.m_allocator);
b37bf2e1
A
410 }
411
4e4e5a6f
A
412 template<typename T, size_t inlineCapacity, typename U>
413 inline ListHashSet<T, inlineCapacity, U>::~ListHashSet()
b37bf2e1
A
414 {
415 deleteAllNodes();
416 }
417
4e4e5a6f
A
418 template<typename T, size_t inlineCapacity, typename U>
419 inline int ListHashSet<T, inlineCapacity, U>::size() const
b37bf2e1
A
420 {
421 return m_impl.size();
422 }
423
4e4e5a6f
A
424 template<typename T, size_t inlineCapacity, typename U>
425 inline int ListHashSet<T, inlineCapacity, U>::capacity() const
b37bf2e1
A
426 {
427 return m_impl.capacity();
428 }
429
4e4e5a6f
A
430 template<typename T, size_t inlineCapacity, typename U>
431 inline bool ListHashSet<T, inlineCapacity, U>::isEmpty() const
b37bf2e1
A
432 {
433 return m_impl.isEmpty();
434 }
435
4e4e5a6f
A
436 template<typename T, size_t inlineCapacity, typename U>
437 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::begin()
b37bf2e1
A
438 {
439 return makeIterator(m_head);
440 }
441
4e4e5a6f
A
442 template<typename T, size_t inlineCapacity, typename U>
443 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::end()
b37bf2e1
A
444 {
445 return makeIterator(0);
446 }
447
4e4e5a6f
A
448 template<typename T, size_t inlineCapacity, typename U>
449 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::begin() const
b37bf2e1
A
450 {
451 return makeConstIterator(m_head);
452 }
453
4e4e5a6f
A
454 template<typename T, size_t inlineCapacity, typename U>
455 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::end() const
b37bf2e1
A
456 {
457 return makeConstIterator(0);
458 }
459
14957cd0
A
460 template<typename T, size_t inlineCapacity, typename U>
461 inline T& ListHashSet<T, inlineCapacity, U>::first()
462 {
463 ASSERT(!isEmpty());
464 return m_head->m_value;
465 }
466
467 template<typename T, size_t inlineCapacity, typename U>
468 inline const T& ListHashSet<T, inlineCapacity, U>::first() const
469 {
470 ASSERT(!isEmpty());
471 return m_head->m_value;
472 }
473
474 template<typename T, size_t inlineCapacity, typename U>
475 inline T& ListHashSet<T, inlineCapacity, U>::last()
476 {
477 ASSERT(!isEmpty());
478 return m_tail->m_value;
479 }
480
481 template<typename T, size_t inlineCapacity, typename U>
482 inline const T& ListHashSet<T, inlineCapacity, U>::last() const
483 {
484 ASSERT(!isEmpty());
485 return m_tail->m_value;
486 }
487
488 template<typename T, size_t inlineCapacity, typename U>
489 inline void ListHashSet<T, inlineCapacity, U>::removeLast()
490 {
491 ASSERT(!isEmpty());
492 m_impl.remove(m_tail);
493 unlinkAndDelete(m_tail);
494 }
495
4e4e5a6f
A
496 template<typename T, size_t inlineCapacity, typename U>
497 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value)
b37bf2e1 498 {
4e4e5a6f 499 typedef ListHashSetTranslator<ValueType, inlineCapacity, HashFunctions> Translator;
9dae56ea 500 ImplTypeIterator it = m_impl.template find<ValueType, Translator>(value);
b37bf2e1
A
501 if (it == m_impl.end())
502 return end();
503 return makeIterator(*it);
504 }
505
4e4e5a6f
A
506 template<typename T, size_t inlineCapacity, typename U>
507 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value) const
b37bf2e1 508 {
4e4e5a6f 509 typedef ListHashSetTranslator<ValueType, inlineCapacity, HashFunctions> Translator;
9dae56ea 510 ImplTypeConstIterator it = m_impl.template find<ValueType, Translator>(value);
b37bf2e1
A
511 if (it == m_impl.end())
512 return end();
513 return makeConstIterator(*it);
514 }
515
14957cd0
A
516 template<typename ValueType, size_t inlineCapacity, typename T, typename Translator>
517 struct ListHashSetTranslatorAdapter {
518 private:
519 typedef ListHashSetNode<ValueType, inlineCapacity> Node;
520 public:
521 static unsigned hash(const T& key) { return Translator::hash(key); }
522 static bool equal(Node* const& a, const T& b) { return Translator::equal(a->m_value, b); }
523 };
524
525 template<typename ValueType, size_t inlineCapacity, typename U>
526 template<typename T, typename HashTranslator>
527 inline typename ListHashSet<ValueType, inlineCapacity, U>::iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value)
528 {
529 typedef ListHashSetTranslatorAdapter<ValueType, inlineCapacity, T, HashTranslator> Adapter;
530 ImplTypeConstIterator it = m_impl.template find<T, Adapter>(value);
531 if (it == m_impl.end())
532 return end();
533 return makeIterator(*it);
534 }
535
536 template<typename ValueType, size_t inlineCapacity, typename U>
537 template<typename T, typename HashTranslator>
538 inline typename ListHashSet<ValueType, inlineCapacity, U>::const_iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value) const
539 {
540 typedef ListHashSetTranslatorAdapter<ValueType, inlineCapacity, T, HashTranslator> Adapter;
541 ImplTypeConstIterator it = m_impl.template find<T, Adapter>(value);
542 if (it == m_impl.end())
543 return end();
544 return makeConstIterator(*it);
545 }
546
547 template<typename ValueType, size_t inlineCapacity, typename U>
548 template<typename T, typename HashTranslator>
549 inline bool ListHashSet<ValueType, inlineCapacity, U>::contains(const T& value) const
550 {
551 typedef ListHashSetTranslatorAdapter<ValueType, inlineCapacity, T, HashTranslator> Adapter;
552 return m_impl.template contains<T, Adapter>(value);
553 }
554
4e4e5a6f
A
555 template<typename T, size_t inlineCapacity, typename U>
556 inline bool ListHashSet<T, inlineCapacity, U>::contains(const ValueType& value) const
b37bf2e1 557 {
4e4e5a6f 558 typedef ListHashSetTranslator<ValueType, inlineCapacity, HashFunctions> Translator;
b37bf2e1
A
559 return m_impl.template contains<ValueType, Translator>(value);
560 }
561
4e4e5a6f
A
562 template<typename T, size_t inlineCapacity, typename U>
563 pair<typename ListHashSet<T, inlineCapacity, U>::iterator, bool> ListHashSet<T, inlineCapacity, U>::add(const ValueType &value)
b37bf2e1 564 {
4e4e5a6f 565 typedef ListHashSetTranslator<ValueType, inlineCapacity, HashFunctions> Translator;
b37bf2e1
A
566 pair<typename ImplType::iterator, bool> result = m_impl.template add<ValueType, NodeAllocator*, Translator>(value, m_allocator.get());
567 if (result.second)
568 appendNode(*result.first);
569 return std::make_pair(makeIterator(*result.first), result.second);
570 }
571
4e4e5a6f
A
572 template<typename T, size_t inlineCapacity, typename U>
573 pair<typename ListHashSet<T, inlineCapacity, U>::iterator, bool> ListHashSet<T, inlineCapacity, U>::insertBefore(iterator it, const ValueType& newValue)
9dae56ea 574 {
4e4e5a6f 575 typedef ListHashSetTranslator<ValueType, inlineCapacity, HashFunctions> Translator;
9dae56ea
A
576 pair<typename ImplType::iterator, bool> result = m_impl.template add<ValueType, NodeAllocator*, Translator>(newValue, m_allocator.get());
577 if (result.second)
578 insertNodeBefore(it.node(), *result.first);
579 return std::make_pair(makeIterator(*result.first), result.second);
580
581 }
582
4e4e5a6f
A
583 template<typename T, size_t inlineCapacity, typename U>
584 pair<typename ListHashSet<T, inlineCapacity, U>::iterator, bool> ListHashSet<T, inlineCapacity, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValue)
9dae56ea
A
585 {
586 return insertBefore(find(beforeValue), newValue);
587 }
588
4e4e5a6f
A
589 template<typename T, size_t inlineCapacity, typename U>
590 inline void ListHashSet<T, inlineCapacity, U>::remove(iterator it)
b37bf2e1
A
591 {
592 if (it == end())
593 return;
594 m_impl.remove(it.node());
595 unlinkAndDelete(it.node());
596 }
597
4e4e5a6f
A
598 template<typename T, size_t inlineCapacity, typename U>
599 inline void ListHashSet<T, inlineCapacity, U>::remove(const ValueType& value)
b37bf2e1
A
600 {
601 remove(find(value));
602 }
603
4e4e5a6f
A
604 template<typename T, size_t inlineCapacity, typename U>
605 inline void ListHashSet<T, inlineCapacity, U>::clear()
b37bf2e1
A
606 {
607 deleteAllNodes();
608 m_impl.clear();
609 m_head = 0;
610 m_tail = 0;
611 }
612
4e4e5a6f
A
613 template<typename T, size_t inlineCapacity, typename U>
614 void ListHashSet<T, inlineCapacity, U>::unlinkAndDelete(Node* node)
b37bf2e1
A
615 {
616 if (!node->m_prev) {
617 ASSERT(node == m_head);
618 m_head = node->m_next;
619 } else {
620 ASSERT(node != m_head);
621 node->m_prev->m_next = node->m_next;
622 }
623
624 if (!node->m_next) {
625 ASSERT(node == m_tail);
626 m_tail = node->m_prev;
627 } else {
628 ASSERT(node != m_tail);
629 node->m_next->m_prev = node->m_prev;
630 }
631
632 node->destroy(m_allocator.get());
633 }
634
4e4e5a6f
A
635 template<typename T, size_t inlineCapacity, typename U>
636 void ListHashSet<T, inlineCapacity, U>::appendNode(Node* node)
b37bf2e1
A
637 {
638 node->m_prev = m_tail;
639 node->m_next = 0;
640
641 if (m_tail) {
642 ASSERT(m_head);
643 m_tail->m_next = node;
644 } else {
645 ASSERT(!m_head);
646 m_head = node;
647 }
648
649 m_tail = node;
650 }
651
4e4e5a6f
A
652 template<typename T, size_t inlineCapacity, typename U>
653 void ListHashSet<T, inlineCapacity, U>::insertNodeBefore(Node* beforeNode, Node* newNode)
9dae56ea
A
654 {
655 if (!beforeNode)
656 return appendNode(newNode);
657
658 newNode->m_next = beforeNode;
659 newNode->m_prev = beforeNode->m_prev;
660 if (beforeNode->m_prev)
661 beforeNode->m_prev->m_next = newNode;
662 beforeNode->m_prev = newNode;
663
664 if (!newNode->m_prev)
665 m_head = newNode;
666 }
667
4e4e5a6f
A
668 template<typename T, size_t inlineCapacity, typename U>
669 void ListHashSet<T, inlineCapacity, U>::deleteAllNodes()
b37bf2e1
A
670 {
671 if (!m_head)
672 return;
673
674 for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : 0)
675 node->destroy(m_allocator.get());
676 }
677
4e4e5a6f
A
678 template<typename T, size_t inlineCapacity, typename U>
679 inline ListHashSetIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeIterator(Node* position)
b37bf2e1 680 {
4e4e5a6f 681 return ListHashSetIterator<T, inlineCapacity, U>(this, position);
b37bf2e1
A
682 }
683
4e4e5a6f
A
684 template<typename T, size_t inlineCapacity, typename U>
685 inline ListHashSetConstIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstIterator(Node* position) const
b37bf2e1 686 {
4e4e5a6f 687 return ListHashSetConstIterator<T, inlineCapacity, U>(this, position);
b37bf2e1
A
688 }
689
690 template<bool, typename ValueType, typename HashTableType>
691 void deleteAllValues(HashTableType& collection)
692 {
693 typedef typename HashTableType::const_iterator iterator;
694 iterator end = collection.end();
695 for (iterator it = collection.begin(); it != end; ++it)
696 delete (*it)->m_value;
697 }
698
4e4e5a6f
A
699 template<typename T, size_t inlineCapacity, typename U>
700 inline void deleteAllValues(const ListHashSet<T, inlineCapacity, U>& collection)
b37bf2e1 701 {
4e4e5a6f 702 deleteAllValues<true, typename ListHashSet<T, inlineCapacity, U>::ValueType>(collection.m_impl);
b37bf2e1
A
703 }
704
705} // namespace WTF
706
707using WTF::ListHashSet;
708
709#endif /* WTF_ListHashSet_h */