2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2011, Benjamin Poulain <ikipou@gmail.com>
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.
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.
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.
22 #ifndef WTF_ListHashSet_h
23 #define WTF_ListHashSet_h
25 #include "wtf/DefaultAllocator.h"
26 #include "wtf/HashSet.h"
27 #include "wtf/OwnPtr.h"
28 #include "wtf/PassOwnPtr.h"
32 // ListHashSet: Just like HashSet, this class provides a Set
33 // interface - a collection of unique objects with O(1) insertion,
34 // removal and test for containership. However, it also has an
35 // order - iterating it will always give back values in the order
36 // in which they are added.
38 // Unlike iteration of most WTF Hash data structures, iteration is
39 // guaranteed safe against mutation of the ListHashSet, except for
40 // removal of the item currently pointed to by a given iterator.
42 template<typename Value, size_t inlineCapacity, typename HashFunctions> class ListHashSet;
44 template<typename Value, size_t inlineCapacity, typename HashFunctions>
45 void deleteAllValues(const ListHashSet<Value, inlineCapacity, HashFunctions>&);
47 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator;
48 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator;
49 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator;
50 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator;
52 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode;
53 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator;
55 template<typename HashArg> struct ListHashSetNodeHashFunctions;
56 template<typename HashArg> struct ListHashSetTranslator;
58 template<typename ValueArg, size_t inlineCapacity = 256, typename HashArg = typename DefaultHash<ValueArg>::Hash> class ListHashSet {
59 WTF_MAKE_FAST_ALLOCATED;
61 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
62 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
64 typedef HashTraits<Node*> NodeTraits;
65 typedef ListHashSetNodeHashFunctions<HashArg> NodeHash;
66 typedef ListHashSetTranslator<HashArg> BaseTranslator;
68 typedef HashTable<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits, DefaultAllocator> ImplType;
69 typedef HashTableIterator<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits, DefaultAllocator> ImplTypeIterator;
70 typedef HashTableConstIterator<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits, DefaultAllocator> ImplTypeConstIterator;
72 typedef HashArg HashFunctions;
75 typedef ValueArg ValueType;
77 typedef ListHashSetIterator<ValueType, inlineCapacity, HashArg> iterator;
78 typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> const_iterator;
79 friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg>;
81 typedef ListHashSetReverseIterator<ValueType, inlineCapacity, HashArg> reverse_iterator;
82 typedef ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg> const_reverse_iterator;
83 friend class ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg>;
85 template<typename ValueType> struct HashTableAddResult {
86 HashTableAddResult(Node* storedValue, bool isNewEntry) : storedValue(storedValue), isNewEntry(isNewEntry) { }
90 typedef HashTableAddResult<ValueType> AddResult;
93 ListHashSet(const ListHashSet&);
94 ListHashSet& operator=(const ListHashSet&);
97 void swap(ListHashSet&);
99 unsigned size() const;
100 unsigned capacity() const;
101 bool isEmpty() const;
103 size_t sizeInBytes() const;
107 const_iterator begin() const;
108 const_iterator end() const;
110 reverse_iterator rbegin();
111 reverse_iterator rend();
112 const_reverse_iterator rbegin() const;
113 const_reverse_iterator rend() const;
116 const ValueType& first() const;
120 const ValueType& last() const;
123 iterator find(const ValueType&);
124 const_iterator find(const ValueType&) const;
125 bool contains(const ValueType&) const;
127 // An alternate version of find() that finds the object by hashing and comparing
128 // with some other type, to avoid the cost of type conversion.
129 // The HashTranslator interface is defined in HashSet.
130 template<typename HashTranslator, typename T> iterator find(const T&);
131 template<typename HashTranslator, typename T> const_iterator find(const T&) const;
132 template<typename HashTranslator, typename T> bool contains(const T&) const;
134 // The return value of add is a pair of a pointer to the stored value,
135 // and a bool that is true if an new entry was added.
136 AddResult add(const ValueType&);
138 // Same as add() except that the return value is an
139 // iterator. Useful in cases where it's needed to have the
140 // same return value as find() and where it's not possible to
141 // use a pointer to the storedValue.
142 iterator addReturnIterator(const ValueType&);
144 // Add the value to the end of the collection. If the value was already in
145 // the list, it is moved to the end.
146 AddResult appendOrMoveToLast(const ValueType&);
148 // Add the value to the beginning of the collection. If the value was already in
149 // the list, it is moved to the beginning.
150 AddResult prependOrMoveToFirst(const ValueType&);
152 AddResult insertBefore(const ValueType& beforeValue, const ValueType& newValue);
153 AddResult insertBefore(iterator, const ValueType&);
155 void remove(const ValueType&);
156 void remove(iterator);
161 void unlinkAndDelete(Node*);
162 void appendNode(Node*);
163 void prependNode(Node*);
164 void insertNodeBefore(Node* beforeNode, Node* newNode);
165 void deleteAllNodes();
166 void createAllocatorIfNeeded();
168 iterator makeIterator(Node*);
169 const_iterator makeConstIterator(Node*) const;
170 reverse_iterator makeReverseIterator(Node*);
171 const_reverse_iterator makeConstReverseIterator(Node*) const;
173 friend void deleteAllValues<>(const ListHashSet&);
178 OwnPtr<NodeAllocator> m_allocator;
181 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator {
182 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
183 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
185 ListHashSetNodeAllocator()
187 , m_isDoneWithInitialFreeList(false)
189 memset(m_pool.pool, 0, sizeof(m_pool.pool));
194 Node* result = m_freeList;
197 return static_cast<Node*>(fastMalloc(sizeof(Node)));
199 ASSERT(!result->m_isAllocated);
201 Node* next = result->m_next;
202 ASSERT(!next || !next->m_isAllocated);
203 if (!next && !m_isDoneWithInitialFreeList) {
205 if (next == pastPool()) {
206 m_isDoneWithInitialFreeList = true;
209 ASSERT(inPool(next));
210 ASSERT(!next->m_isAllocated);
218 void deallocate(Node* node)
222 node->m_isAllocated = false;
224 node->m_next = m_freeList;
232 bool inPool(Node* node)
234 return node >= pool() && node < pastPool();
238 Node* pool() { return reinterpret_cast_ptr<Node*>(m_pool.pool); }
239 Node* pastPool() { return pool() + m_poolSize; }
242 bool m_isDoneWithInitialFreeList;
243 static const size_t m_poolSize = inlineCapacity;
245 char pool[sizeof(Node) * m_poolSize];
250 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode {
251 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
253 ListHashSetNode(ValueArg value)
258 , m_isAllocated(true)
263 void* operator new(size_t, NodeAllocator* allocator)
265 return allocator->allocate();
267 void destroy(NodeAllocator* allocator)
269 this->~ListHashSetNode();
270 allocator->deallocate(this);
274 ListHashSetNode* m_prev;
275 ListHashSetNode* m_next;
282 template<typename HashArg> struct ListHashSetNodeHashFunctions {
283 template<typename T> static unsigned hash(const T& key) { return HashArg::hash(key->m_value); }
284 template<typename T> static bool equal(const T& a, const T& b) { return HashArg::equal(a->m_value, b->m_value); }
285 static const bool safeToCompareToEmptyOrDeleted = false;
288 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator {
290 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
291 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
292 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
293 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
294 typedef ValueArg ValueType;
295 typedef ValueType& ReferenceType;
296 typedef ValueType* PointerType;
298 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
300 ListHashSetIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
303 ListHashSetIterator() { }
305 // default copy, assignment and destructor are OK
307 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
308 ReferenceType operator*() const { return *get(); }
309 PointerType operator->() const { return get(); }
311 iterator& operator++() { ++m_iterator; return *this; }
313 // postfix ++ intentionally omitted
315 iterator& operator--() { --m_iterator; return *this; }
317 // postfix -- intentionally omitted
320 bool operator==(const iterator& other) const { return m_iterator == other.m_iterator; }
321 bool operator!=(const iterator& other) const { return m_iterator != other.m_iterator; }
323 operator const_iterator() const { return m_iterator; }
326 Node* node() { return m_iterator.node(); }
328 const_iterator m_iterator;
331 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator {
333 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
334 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
335 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
336 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
337 typedef ValueArg ValueType;
338 typedef const ValueType& ReferenceType;
339 typedef const ValueType* PointerType;
341 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
342 friend class ListHashSetIterator<ValueArg, inlineCapacity, HashArg>;
344 ListHashSetConstIterator(const ListHashSetType* set, Node* position)
346 , m_position(position)
351 ListHashSetConstIterator()
355 PointerType get() const
357 return &m_position->m_value;
359 ReferenceType operator*() const { return *get(); }
360 PointerType operator->() const { return get(); }
362 const_iterator& operator++()
364 ASSERT(m_position != 0);
365 m_position = m_position->m_next;
369 // postfix ++ intentionally omitted
371 const_iterator& operator--()
373 ASSERT(m_position != m_set->m_head);
375 m_position = m_set->m_tail;
377 m_position = m_position->m_prev;
381 // postfix -- intentionally omitted
384 bool operator==(const const_iterator& other) const
386 return m_position == other.m_position;
388 bool operator!=(const const_iterator& other) const
390 return m_position != other.m_position;
394 Node* node() { return m_position; }
396 const ListHashSetType* m_set;
400 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator {
402 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
403 typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
404 typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
405 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
406 typedef ValueArg ValueType;
407 typedef ValueType& ReferenceType;
408 typedef ValueType* PointerType;
410 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
412 ListHashSetReverseIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
415 ListHashSetReverseIterator() { }
417 // default copy, assignment and destructor are OK
419 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
420 ReferenceType operator*() const { return *get(); }
421 PointerType operator->() const { return get(); }
423 reverse_iterator& operator++() { ++m_iterator; return *this; }
425 // postfix ++ intentionally omitted
427 reverse_iterator& operator--() { --m_iterator; return *this; }
429 // postfix -- intentionally omitted
432 bool operator==(const reverse_iterator& other) const { return m_iterator == other.m_iterator; }
433 bool operator!=(const reverse_iterator& other) const { return m_iterator != other.m_iterator; }
435 operator const_reverse_iterator() const { return m_iterator; }
438 Node* node() { return m_iterator.node(); }
440 const_reverse_iterator m_iterator;
443 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator {
445 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
446 typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
447 typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
448 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
449 typedef ValueArg ValueType;
450 typedef const ValueType& ReferenceType;
451 typedef const ValueType* PointerType;
453 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
454 friend class ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg>;
456 ListHashSetConstReverseIterator(const ListHashSetType* set, Node* position)
458 , m_position(position)
463 ListHashSetConstReverseIterator()
467 PointerType get() const
469 return &m_position->m_value;
471 ReferenceType operator*() const { return *get(); }
472 PointerType operator->() const { return get(); }
474 const_reverse_iterator& operator++()
476 ASSERT(m_position != 0);
477 m_position = m_position->m_prev;
481 // postfix ++ intentionally omitted
483 const_reverse_iterator& operator--()
485 ASSERT(m_position != m_set->m_tail);
487 m_position = m_set->m_head;
489 m_position = m_position->m_next;
493 // postfix -- intentionally omitted
496 bool operator==(const const_reverse_iterator& other) const
498 return m_position == other.m_position;
500 bool operator!=(const const_reverse_iterator& other) const
502 return m_position != other.m_position;
506 Node* node() { return m_position; }
508 const ListHashSetType* m_set;
512 template<typename HashFunctions>
513 struct ListHashSetTranslator {
514 template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
515 template<typename T, typename U> static bool equal(const T& a, const U& b) { return HashFunctions::equal(a->m_value, b); }
516 template<typename T, typename U, typename V> static void translate(T*& location, const U& key, const V& allocator)
518 location = new (allocator) T(key);
522 template<typename T, size_t inlineCapacity, typename U>
523 inline ListHashSet<T, inlineCapacity, U>::ListHashSet()
529 template<typename T, size_t inlineCapacity, typename U>
530 inline ListHashSet<T, inlineCapacity, U>::ListHashSet(const ListHashSet& other)
534 const_iterator end = other.end();
535 for (const_iterator it = other.begin(); it != end; ++it)
539 template<typename T, size_t inlineCapacity, typename U>
540 inline ListHashSet<T, inlineCapacity, U>& ListHashSet<T, inlineCapacity, U>::operator=(const ListHashSet& other)
542 ListHashSet tmp(other);
547 template<typename T, size_t inlineCapacity, typename U>
548 inline void ListHashSet<T, inlineCapacity, U>::swap(ListHashSet& other)
550 m_impl.swap(other.m_impl);
551 std::swap(m_head, other.m_head);
552 std::swap(m_tail, other.m_tail);
553 m_allocator.swap(other.m_allocator);
556 template<typename T, size_t inlineCapacity, typename U>
557 inline ListHashSet<T, inlineCapacity, U>::~ListHashSet()
562 template<typename T, size_t inlineCapacity, typename U>
563 inline unsigned ListHashSet<T, inlineCapacity, U>::size() const
565 return m_impl.size();
568 template<typename T, size_t inlineCapacity, typename U>
569 inline unsigned ListHashSet<T, inlineCapacity, U>::capacity() const
571 return m_impl.capacity();
574 template<typename T, size_t inlineCapacity, typename U>
575 inline bool ListHashSet<T, inlineCapacity, U>::isEmpty() const
577 return m_impl.isEmpty();
580 template<typename T, size_t inlineCapacity, typename U>
581 size_t ListHashSet<T, inlineCapacity, U>::sizeInBytes() const
583 size_t result = sizeof(*this);
586 result += sizeof(*m_allocator) + (sizeof(typename ImplType::ValueType) * m_impl.capacity());
587 for (Node* node = m_head; node; node = node->m_next) {
588 if (!m_allocator->inPool(node))
589 result += sizeof(Node);
594 template<typename T, size_t inlineCapacity, typename U>
595 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::begin()
597 return makeIterator(m_head);
600 template<typename T, size_t inlineCapacity, typename U>
601 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::end()
603 return makeIterator(0);
606 template<typename T, size_t inlineCapacity, typename U>
607 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::begin() const
609 return makeConstIterator(m_head);
612 template<typename T, size_t inlineCapacity, typename U>
613 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::end() const
615 return makeConstIterator(0);
618 template<typename T, size_t inlineCapacity, typename U>
619 inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin()
621 return makeReverseIterator(m_tail);
624 template<typename T, size_t inlineCapacity, typename U>
625 inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rend()
627 return makeReverseIterator(0);
630 template<typename T, size_t inlineCapacity, typename U>
631 inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin() const
633 return makeConstReverseIterator(m_tail);
636 template<typename T, size_t inlineCapacity, typename U>
637 inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rend() const
639 return makeConstReverseIterator(0);
642 template<typename T, size_t inlineCapacity, typename U>
643 inline T& ListHashSet<T, inlineCapacity, U>::first()
646 return m_head->m_value;
649 template<typename T, size_t inlineCapacity, typename U>
650 inline void ListHashSet<T, inlineCapacity, U>::removeFirst()
653 m_impl.remove(m_head);
654 unlinkAndDelete(m_head);
657 template<typename T, size_t inlineCapacity, typename U>
658 inline const T& ListHashSet<T, inlineCapacity, U>::first() const
661 return m_head->m_value;
664 template<typename T, size_t inlineCapacity, typename U>
665 inline T& ListHashSet<T, inlineCapacity, U>::last()
668 return m_tail->m_value;
671 template<typename T, size_t inlineCapacity, typename U>
672 inline const T& ListHashSet<T, inlineCapacity, U>::last() const
675 return m_tail->m_value;
678 template<typename T, size_t inlineCapacity, typename U>
679 inline void ListHashSet<T, inlineCapacity, U>::removeLast()
682 m_impl.remove(m_tail);
683 unlinkAndDelete(m_tail);
686 template<typename T, size_t inlineCapacity, typename U>
687 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value)
689 ImplTypeIterator it = m_impl.template find<BaseTranslator>(value);
690 if (it == m_impl.end())
692 return makeIterator(*it);
695 template<typename T, size_t inlineCapacity, typename U>
696 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value) const
698 ImplTypeConstIterator it = m_impl.template find<BaseTranslator>(value);
699 if (it == m_impl.end())
701 return makeConstIterator(*it);
704 template<typename Translator>
705 struct ListHashSetTranslatorAdapter {
706 template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
707 template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a->m_value, b); }
710 template<typename ValueType, size_t inlineCapacity, typename U>
711 template<typename HashTranslator, typename T>
712 inline typename ListHashSet<ValueType, inlineCapacity, U>::iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value)
714 ImplTypeConstIterator it = m_impl.template find<ListHashSetTranslatorAdapter<HashTranslator> >(value);
715 if (it == m_impl.end())
717 return makeIterator(*it);
720 template<typename ValueType, size_t inlineCapacity, typename U>
721 template<typename HashTranslator, typename T>
722 inline typename ListHashSet<ValueType, inlineCapacity, U>::const_iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value) const
724 ImplTypeConstIterator it = m_impl.template find<ListHashSetTranslatorAdapter<HashTranslator> >(value);
725 if (it == m_impl.end())
727 return makeConstIterator(*it);
730 template<typename ValueType, size_t inlineCapacity, typename U>
731 template<typename HashTranslator, typename T>
732 inline bool ListHashSet<ValueType, inlineCapacity, U>::contains(const T& value) const
734 return m_impl.template contains<ListHashSetTranslatorAdapter<HashTranslator> >(value);
737 template<typename T, size_t inlineCapacity, typename U>
738 inline bool ListHashSet<T, inlineCapacity, U>::contains(const ValueType& value) const
740 return m_impl.template contains<BaseTranslator>(value);
743 template<typename T, size_t inlineCapacity, typename U>
744 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::add(const ValueType &value)
746 createAllocatorIfNeeded();
747 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
748 if (result.isNewEntry)
749 appendNode(*result.storedValue);
750 return AddResult(*result.storedValue, result.isNewEntry);
753 template<typename T, size_t inlineCapacity, typename U>
754 typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::addReturnIterator(const ValueType &value)
756 return makeIterator(add(value).storedValue);
758 template<typename T, size_t inlineCapacity, typename U>
759 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::appendOrMoveToLast(const ValueType &value)
761 createAllocatorIfNeeded();
762 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
763 Node* node = *result.storedValue;
764 if (!result.isNewEntry)
767 return AddResult(*result.storedValue, result.isNewEntry);
770 template<typename T, size_t inlineCapacity, typename U>
771 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::prependOrMoveToFirst(const ValueType &value)
773 createAllocatorIfNeeded();
774 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
775 Node* node = *result.storedValue;
776 if (!result.isNewEntry)
779 return AddResult(*result.storedValue, result.isNewEntry);
782 template<typename T, size_t inlineCapacity, typename U>
783 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::insertBefore(iterator it, const ValueType& newValue)
785 createAllocatorIfNeeded();
786 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(newValue, m_allocator.get());
787 if (result.isNewEntry)
788 insertNodeBefore(it.node(), *result.storedValue);
789 return AddResult(*result.storedValue, result.isNewEntry);
792 template<typename T, size_t inlineCapacity, typename U>
793 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValue)
795 createAllocatorIfNeeded();
796 return insertBefore(find(beforeValue), newValue);
799 template<typename T, size_t inlineCapacity, typename U>
800 inline void ListHashSet<T, inlineCapacity, U>::remove(iterator it)
804 m_impl.remove(it.node());
805 unlinkAndDelete(it.node());
808 template<typename T, size_t inlineCapacity, typename U>
809 inline void ListHashSet<T, inlineCapacity, U>::remove(const ValueType& value)
814 template<typename T, size_t inlineCapacity, typename U>
815 inline void ListHashSet<T, inlineCapacity, U>::clear()
823 template<typename T, size_t inlineCapacity, typename U>
824 void ListHashSet<T, inlineCapacity, U>::unlink(Node* node)
827 ASSERT(node == m_head);
828 m_head = node->m_next;
830 ASSERT(node != m_head);
831 node->m_prev->m_next = node->m_next;
835 ASSERT(node == m_tail);
836 m_tail = node->m_prev;
838 ASSERT(node != m_tail);
839 node->m_next->m_prev = node->m_prev;
843 template<typename T, size_t inlineCapacity, typename U>
844 void ListHashSet<T, inlineCapacity, U>::unlinkAndDelete(Node* node)
847 node->destroy(m_allocator.get());
850 template<typename T, size_t inlineCapacity, typename U>
851 void ListHashSet<T, inlineCapacity, U>::appendNode(Node* node)
853 node->m_prev = m_tail;
858 m_tail->m_next = node;
867 template<typename T, size_t inlineCapacity, typename U>
868 void ListHashSet<T, inlineCapacity, U>::prependNode(Node* node)
871 node->m_next = m_head;
874 m_head->m_prev = node;
881 template<typename T, size_t inlineCapacity, typename U>
882 void ListHashSet<T, inlineCapacity, U>::insertNodeBefore(Node* beforeNode, Node* newNode)
885 return appendNode(newNode);
887 newNode->m_next = beforeNode;
888 newNode->m_prev = beforeNode->m_prev;
889 if (beforeNode->m_prev)
890 beforeNode->m_prev->m_next = newNode;
891 beforeNode->m_prev = newNode;
893 if (!newNode->m_prev)
897 template<typename T, size_t inlineCapacity, typename U>
898 void ListHashSet<T, inlineCapacity, U>::deleteAllNodes()
903 for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : 0)
904 node->destroy(m_allocator.get());
907 template<typename T, size_t inlineCapacity, typename U>
908 void ListHashSet<T, inlineCapacity, U>::createAllocatorIfNeeded()
911 m_allocator = adoptPtr(new NodeAllocator);
914 template<typename T, size_t inlineCapacity, typename U>
915 inline ListHashSetReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeReverseIterator(Node* position)
917 return ListHashSetReverseIterator<T, inlineCapacity, U>(this, position);
920 template<typename T, size_t inlineCapacity, typename U>
921 inline ListHashSetConstReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstReverseIterator(Node* position) const
923 return ListHashSetConstReverseIterator<T, inlineCapacity, U>(this, position);
926 template<typename T, size_t inlineCapacity, typename U>
927 inline ListHashSetIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeIterator(Node* position)
929 return ListHashSetIterator<T, inlineCapacity, U>(this, position);
932 template<typename T, size_t inlineCapacity, typename U>
933 inline ListHashSetConstIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstIterator(Node* position) const
935 return ListHashSetConstIterator<T, inlineCapacity, U>(this, position);
937 template<bool, typename ValueType, typename HashTableType>
938 void deleteAllValues(HashTableType& collection)
940 typedef typename HashTableType::const_iterator iterator;
941 iterator end = collection.end();
942 for (iterator it = collection.begin(); it != end; ++it)
943 delete (*it)->m_value;
946 template<typename T, size_t inlineCapacity, typename U>
947 inline void deleteAllValues(const ListHashSet<T, inlineCapacity, U>& collection)
949 deleteAllValues<true, typename ListHashSet<T, inlineCapacity, U>::ValueType>(collection.m_impl);
954 using WTF::ListHashSet;
956 #endif /* WTF_ListHashSet_h */