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/HashSet.h"
26 #include "wtf/OwnPtr.h"
27 #include "wtf/PassOwnPtr.h"
31 // ListHashSet: Just like HashSet, this class provides a Set
32 // interface - a collection of unique objects with O(1) insertion,
33 // removal and test for containership. However, it also has an
34 // order - iterating it will always give back values in the order
35 // in which they are added.
37 // Unlike iteration of most WTF Hash data structures, iteration is
38 // guaranteed safe against mutation of the ListHashSet, except for
39 // removal of the item currently pointed to by a given iterator.
41 template<typename Value, size_t inlineCapacity, typename HashFunctions> class ListHashSet;
43 template<typename Value, size_t inlineCapacity, typename HashFunctions>
44 void deleteAllValues(const ListHashSet<Value, inlineCapacity, HashFunctions>&);
46 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator;
47 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator;
48 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator;
49 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator;
51 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode;
52 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator;
54 template<typename HashArg> struct ListHashSetNodeHashFunctions;
55 template<typename HashArg> struct ListHashSetTranslator;
57 template<typename ValueArg, size_t inlineCapacity = 256, typename HashArg = typename DefaultHash<ValueArg>::Hash> class ListHashSet {
58 WTF_MAKE_FAST_ALLOCATED;
60 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
61 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
63 typedef HashTraits<Node*> NodeTraits;
64 typedef ListHashSetNodeHashFunctions<HashArg> NodeHash;
65 typedef ListHashSetTranslator<HashArg> BaseTranslator;
67 typedef HashTable<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits> ImplType;
68 typedef HashTableIterator<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits> ImplTypeIterator;
69 typedef HashTableConstIterator<Node*, Node*, IdentityExtractor, NodeHash, NodeTraits, NodeTraits> ImplTypeConstIterator;
71 typedef HashArg HashFunctions;
74 typedef ValueArg ValueType;
76 typedef ListHashSetIterator<ValueType, inlineCapacity, HashArg> iterator;
77 typedef ListHashSetConstIterator<ValueType, inlineCapacity, HashArg> const_iterator;
78 friend class ListHashSetConstIterator<ValueType, inlineCapacity, HashArg>;
80 typedef ListHashSetReverseIterator<ValueType, inlineCapacity, HashArg> reverse_iterator;
81 typedef ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg> const_reverse_iterator;
82 friend class ListHashSetConstReverseIterator<ValueType, inlineCapacity, HashArg>;
84 typedef HashTableAddResult<iterator> AddResult;
87 ListHashSet(const ListHashSet&);
88 ListHashSet& operator=(const ListHashSet&);
91 void swap(ListHashSet&);
97 size_t sizeInBytes() const;
101 const_iterator begin() const;
102 const_iterator end() const;
104 reverse_iterator rbegin();
105 reverse_iterator rend();
106 const_reverse_iterator rbegin() const;
107 const_reverse_iterator rend() const;
110 const ValueType& first() const;
114 const ValueType& last() const;
117 iterator find(const ValueType&);
118 const_iterator find(const ValueType&) const;
119 bool contains(const ValueType&) const;
121 // An alternate version of find() that finds the object by hashing and comparing
122 // with some other type, to avoid the cost of type conversion.
123 // The HashTranslator interface is defined in HashSet.
124 // FIXME: We should reverse the order of the template arguments so that callers
125 // can just pass the translator let the compiler deduce T.
126 template<typename T, typename HashTranslator> iterator find(const T&);
127 template<typename T, typename HashTranslator> const_iterator find(const T&) const;
128 template<typename T, typename HashTranslator> bool contains(const T&) const;
130 // The return value of add is a pair of an iterator to the new value's location,
131 // and a bool that is true if an new entry was added.
132 AddResult add(const ValueType&);
134 // Add the value to the end of the collection. If the value was already in
135 // the list, it is moved to the end.
136 AddResult appendOrMoveToLast(const ValueType&);
138 // Add the value to the beginning of the collection. If the value was already in
139 // the list, it is moved to the beginning.
140 AddResult prependOrMoveToFirst(const ValueType&);
142 AddResult insertBefore(const ValueType& beforeValue, const ValueType& newValue);
143 AddResult insertBefore(iterator, const ValueType&);
145 void remove(const ValueType&);
146 void remove(iterator);
151 void unlinkAndDelete(Node*);
152 void appendNode(Node*);
153 void prependNode(Node*);
154 void insertNodeBefore(Node* beforeNode, Node* newNode);
155 void deleteAllNodes();
157 iterator makeIterator(Node*);
158 const_iterator makeConstIterator(Node*) const;
159 reverse_iterator makeReverseIterator(Node*);
160 const_reverse_iterator makeConstReverseIterator(Node*) const;
162 friend void deleteAllValues<>(const ListHashSet&);
167 OwnPtr<NodeAllocator> m_allocator;
170 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNodeAllocator {
171 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
172 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
174 ListHashSetNodeAllocator()
176 , m_isDoneWithInitialFreeList(false)
178 memset(m_pool.pool, 0, sizeof(m_pool.pool));
183 Node* result = m_freeList;
186 return static_cast<Node*>(fastMalloc(sizeof(Node)));
188 ASSERT(!result->m_isAllocated);
190 Node* next = result->m_next;
191 ASSERT(!next || !next->m_isAllocated);
192 if (!next && !m_isDoneWithInitialFreeList) {
194 if (next == pastPool()) {
195 m_isDoneWithInitialFreeList = true;
198 ASSERT(inPool(next));
199 ASSERT(!next->m_isAllocated);
207 void deallocate(Node* node)
211 node->m_isAllocated = false;
213 node->m_next = m_freeList;
221 bool inPool(Node* node)
223 return node >= pool() && node < pastPool();
227 Node* pool() { return reinterpret_cast_ptr<Node*>(m_pool.pool); }
228 Node* pastPool() { return pool() + m_poolSize; }
231 bool m_isDoneWithInitialFreeList;
232 static const size_t m_poolSize = inlineCapacity;
234 char pool[sizeof(Node) * m_poolSize];
239 template<typename ValueArg, size_t inlineCapacity> struct ListHashSetNode {
240 typedef ListHashSetNodeAllocator<ValueArg, inlineCapacity> NodeAllocator;
242 ListHashSetNode(ValueArg value)
247 , m_isAllocated(true)
252 void* operator new(size_t, NodeAllocator* allocator)
254 return allocator->allocate();
256 void destroy(NodeAllocator* allocator)
258 this->~ListHashSetNode();
259 allocator->deallocate(this);
263 ListHashSetNode* m_prev;
264 ListHashSetNode* m_next;
271 template<typename HashArg> struct ListHashSetNodeHashFunctions {
272 template<typename T> static unsigned hash(const T& key) { return HashArg::hash(key->m_value); }
273 template<typename T> static bool equal(const T& a, const T& b) { return HashArg::equal(a->m_value, b->m_value); }
274 static const bool safeToCompareToEmptyOrDeleted = false;
277 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetIterator {
279 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
280 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
281 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
282 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
283 typedef ValueArg ValueType;
284 typedef ValueType& ReferenceType;
285 typedef ValueType* PointerType;
287 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
289 ListHashSetIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
292 ListHashSetIterator() { }
294 // default copy, assignment and destructor are OK
296 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
297 ReferenceType operator*() const { return *get(); }
298 PointerType operator->() const { return get(); }
300 iterator& operator++() { ++m_iterator; return *this; }
302 // postfix ++ intentionally omitted
304 iterator& operator--() { --m_iterator; return *this; }
306 // postfix -- intentionally omitted
309 bool operator==(const iterator& other) const { return m_iterator == other.m_iterator; }
310 bool operator!=(const iterator& other) const { return m_iterator != other.m_iterator; }
312 operator const_iterator() const { return m_iterator; }
315 Node* node() { return m_iterator.node(); }
317 const_iterator m_iterator;
320 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstIterator {
322 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
323 typedef ListHashSetIterator<ValueArg, inlineCapacity, HashArg> iterator;
324 typedef ListHashSetConstIterator<ValueArg, inlineCapacity, HashArg> const_iterator;
325 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
326 typedef ValueArg ValueType;
327 typedef const ValueType& ReferenceType;
328 typedef const ValueType* PointerType;
330 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
331 friend class ListHashSetIterator<ValueArg, inlineCapacity, HashArg>;
333 ListHashSetConstIterator(const ListHashSetType* set, Node* position)
335 , m_position(position)
340 ListHashSetConstIterator()
344 PointerType get() const
346 return &m_position->m_value;
348 ReferenceType operator*() const { return *get(); }
349 PointerType operator->() const { return get(); }
351 const_iterator& operator++()
353 ASSERT(m_position != 0);
354 m_position = m_position->m_next;
358 // postfix ++ intentionally omitted
360 const_iterator& operator--()
362 ASSERT(m_position != m_set->m_head);
364 m_position = m_set->m_tail;
366 m_position = m_position->m_prev;
370 // postfix -- intentionally omitted
373 bool operator==(const const_iterator& other) const
375 return m_position == other.m_position;
377 bool operator!=(const const_iterator& other) const
379 return m_position != other.m_position;
383 Node* node() { return m_position; }
385 const ListHashSetType* m_set;
389 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetReverseIterator {
391 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
392 typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
393 typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
394 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
395 typedef ValueArg ValueType;
396 typedef ValueType& ReferenceType;
397 typedef ValueType* PointerType;
399 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
401 ListHashSetReverseIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { }
404 ListHashSetReverseIterator() { }
406 // default copy, assignment and destructor are OK
408 PointerType get() const { return const_cast<PointerType>(m_iterator.get()); }
409 ReferenceType operator*() const { return *get(); }
410 PointerType operator->() const { return get(); }
412 reverse_iterator& operator++() { ++m_iterator; return *this; }
414 // postfix ++ intentionally omitted
416 reverse_iterator& operator--() { --m_iterator; return *this; }
418 // postfix -- intentionally omitted
421 bool operator==(const reverse_iterator& other) const { return m_iterator == other.m_iterator; }
422 bool operator!=(const reverse_iterator& other) const { return m_iterator != other.m_iterator; }
424 operator const_reverse_iterator() const { return m_iterator; }
427 Node* node() { return m_iterator.node(); }
429 const_reverse_iterator m_iterator;
432 template<typename ValueArg, size_t inlineCapacity, typename HashArg> class ListHashSetConstReverseIterator {
434 typedef ListHashSet<ValueArg, inlineCapacity, HashArg> ListHashSetType;
435 typedef ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg> reverse_iterator;
436 typedef ListHashSetConstReverseIterator<ValueArg, inlineCapacity, HashArg> const_reverse_iterator;
437 typedef ListHashSetNode<ValueArg, inlineCapacity> Node;
438 typedef ValueArg ValueType;
439 typedef const ValueType& ReferenceType;
440 typedef const ValueType* PointerType;
442 friend class ListHashSet<ValueArg, inlineCapacity, HashArg>;
443 friend class ListHashSetReverseIterator<ValueArg, inlineCapacity, HashArg>;
445 ListHashSetConstReverseIterator(const ListHashSetType* set, Node* position)
447 , m_position(position)
452 ListHashSetConstReverseIterator()
456 PointerType get() const
458 return &m_position->m_value;
460 ReferenceType operator*() const { return *get(); }
461 PointerType operator->() const { return get(); }
463 const_reverse_iterator& operator++()
465 ASSERT(m_position != 0);
466 m_position = m_position->m_prev;
470 // postfix ++ intentionally omitted
472 const_reverse_iterator& operator--()
474 ASSERT(m_position != m_set->m_tail);
476 m_position = m_set->m_head;
478 m_position = m_position->m_next;
482 // postfix -- intentionally omitted
485 bool operator==(const const_reverse_iterator& other) const
487 return m_position == other.m_position;
489 bool operator!=(const const_reverse_iterator& other) const
491 return m_position != other.m_position;
495 Node* node() { return m_position; }
497 const ListHashSetType* m_set;
501 template<typename HashFunctions>
502 struct ListHashSetTranslator {
503 template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
504 template<typename T, typename U> static bool equal(const T& a, const U& b) { return HashFunctions::equal(a->m_value, b); }
505 template<typename T, typename U, typename V> static void translate(T*& location, const U& key, const V& allocator)
507 location = new (allocator) T(key);
511 template<typename T, size_t inlineCapacity, typename U>
512 inline ListHashSet<T, inlineCapacity, U>::ListHashSet()
515 , m_allocator(adoptPtr(new NodeAllocator))
519 template<typename T, size_t inlineCapacity, typename U>
520 inline ListHashSet<T, inlineCapacity, U>::ListHashSet(const ListHashSet& other)
523 , m_allocator(adoptPtr(new NodeAllocator))
525 const_iterator end = other.end();
526 for (const_iterator it = other.begin(); it != end; ++it)
530 template<typename T, size_t inlineCapacity, typename U>
531 inline ListHashSet<T, inlineCapacity, U>& ListHashSet<T, inlineCapacity, U>::operator=(const ListHashSet& other)
533 ListHashSet tmp(other);
538 template<typename T, size_t inlineCapacity, typename U>
539 inline void ListHashSet<T, inlineCapacity, U>::swap(ListHashSet& other)
541 m_impl.swap(other.m_impl);
542 std::swap(m_head, other.m_head);
543 std::swap(m_tail, other.m_tail);
544 m_allocator.swap(other.m_allocator);
547 template<typename T, size_t inlineCapacity, typename U>
548 inline ListHashSet<T, inlineCapacity, U>::~ListHashSet()
553 template<typename T, size_t inlineCapacity, typename U>
554 inline int ListHashSet<T, inlineCapacity, U>::size() const
556 return m_impl.size();
559 template<typename T, size_t inlineCapacity, typename U>
560 inline int ListHashSet<T, inlineCapacity, U>::capacity() const
562 return m_impl.capacity();
565 template<typename T, size_t inlineCapacity, typename U>
566 inline bool ListHashSet<T, inlineCapacity, U>::isEmpty() const
568 return m_impl.isEmpty();
571 template<typename T, size_t inlineCapacity, typename U>
572 size_t ListHashSet<T, inlineCapacity, U>::sizeInBytes() const
574 size_t result = sizeof(*this) + sizeof(*m_allocator);
575 result += sizeof(typename ImplType::ValueType) * m_impl.capacity();
576 for (Node* node = m_head; node; node = node->m_next) {
577 if (!m_allocator->inPool(node))
578 result += sizeof(Node);
583 template<typename T, size_t inlineCapacity, typename U>
584 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::begin()
586 return makeIterator(m_head);
589 template<typename T, size_t inlineCapacity, typename U>
590 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::end()
592 return makeIterator(0);
595 template<typename T, size_t inlineCapacity, typename U>
596 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::begin() const
598 return makeConstIterator(m_head);
601 template<typename T, size_t inlineCapacity, typename U>
602 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::end() const
604 return makeConstIterator(0);
607 template<typename T, size_t inlineCapacity, typename U>
608 inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin()
610 return makeReverseIterator(m_tail);
613 template<typename T, size_t inlineCapacity, typename U>
614 inline typename ListHashSet<T, inlineCapacity, U>::reverse_iterator ListHashSet<T, inlineCapacity, U>::rend()
616 return makeReverseIterator(0);
619 template<typename T, size_t inlineCapacity, typename U>
620 inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rbegin() const
622 return makeConstReverseIterator(m_tail);
625 template<typename T, size_t inlineCapacity, typename U>
626 inline typename ListHashSet<T, inlineCapacity, U>::const_reverse_iterator ListHashSet<T, inlineCapacity, U>::rend() const
628 return makeConstReverseIterator(0);
631 template<typename T, size_t inlineCapacity, typename U>
632 inline T& ListHashSet<T, inlineCapacity, U>::first()
635 return m_head->m_value;
638 template<typename T, size_t inlineCapacity, typename U>
639 inline void ListHashSet<T, inlineCapacity, U>::removeFirst()
642 m_impl.remove(m_head);
643 unlinkAndDelete(m_head);
646 template<typename T, size_t inlineCapacity, typename U>
647 inline const T& ListHashSet<T, inlineCapacity, U>::first() const
650 return m_head->m_value;
653 template<typename T, size_t inlineCapacity, typename U>
654 inline T& ListHashSet<T, inlineCapacity, U>::last()
657 return m_tail->m_value;
660 template<typename T, size_t inlineCapacity, typename U>
661 inline const T& ListHashSet<T, inlineCapacity, U>::last() const
664 return m_tail->m_value;
667 template<typename T, size_t inlineCapacity, typename U>
668 inline void ListHashSet<T, inlineCapacity, U>::removeLast()
671 m_impl.remove(m_tail);
672 unlinkAndDelete(m_tail);
675 template<typename T, size_t inlineCapacity, typename U>
676 inline typename ListHashSet<T, inlineCapacity, U>::iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value)
678 ImplTypeIterator it = m_impl.template find<BaseTranslator>(value);
679 if (it == m_impl.end())
681 return makeIterator(*it);
684 template<typename T, size_t inlineCapacity, typename U>
685 inline typename ListHashSet<T, inlineCapacity, U>::const_iterator ListHashSet<T, inlineCapacity, U>::find(const ValueType& value) const
687 ImplTypeConstIterator it = m_impl.template find<BaseTranslator>(value);
688 if (it == m_impl.end())
690 return makeConstIterator(*it);
693 template<typename Translator>
694 struct ListHashSetTranslatorAdapter {
695 template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
696 template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a->m_value, b); }
699 template<typename ValueType, size_t inlineCapacity, typename U>
700 template<typename T, typename HashTranslator>
701 inline typename ListHashSet<ValueType, inlineCapacity, U>::iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value)
703 ImplTypeConstIterator it = m_impl.template find<ListHashSetTranslatorAdapter<HashTranslator> >(value);
704 if (it == m_impl.end())
706 return makeIterator(*it);
709 template<typename ValueType, size_t inlineCapacity, typename U>
710 template<typename T, typename HashTranslator>
711 inline typename ListHashSet<ValueType, inlineCapacity, U>::const_iterator ListHashSet<ValueType, inlineCapacity, U>::find(const T& value) const
713 ImplTypeConstIterator it = m_impl.template find<ListHashSetTranslatorAdapter<HashTranslator> >(value);
714 if (it == m_impl.end())
716 return makeConstIterator(*it);
719 template<typename ValueType, size_t inlineCapacity, typename U>
720 template<typename T, typename HashTranslator>
721 inline bool ListHashSet<ValueType, inlineCapacity, U>::contains(const T& value) const
723 return m_impl.template contains<ListHashSetTranslatorAdapter<HashTranslator> >(value);
726 template<typename T, size_t inlineCapacity, typename U>
727 inline bool ListHashSet<T, inlineCapacity, U>::contains(const ValueType& value) const
729 return m_impl.template contains<BaseTranslator>(value);
732 template<typename T, size_t inlineCapacity, typename U>
733 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::add(const ValueType &value)
735 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
736 if (result.isNewEntry)
737 appendNode(*result.iterator);
738 return AddResult(makeIterator(*result.iterator), result.isNewEntry);
741 template<typename T, size_t inlineCapacity, typename U>
742 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::appendOrMoveToLast(const ValueType &value)
744 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
745 Node* node = *result.iterator;
746 if (!result.isNewEntry)
749 return AddResult(makeIterator(*result.iterator), result.isNewEntry);
752 template<typename T, size_t inlineCapacity, typename U>
753 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::prependOrMoveToFirst(const ValueType &value)
755 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(value, m_allocator.get());
756 Node* node = *result.iterator;
757 if (!result.isNewEntry)
760 return AddResult(makeIterator(*result.iterator), result.isNewEntry);
763 template<typename T, size_t inlineCapacity, typename U>
764 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::insertBefore(iterator it, const ValueType& newValue)
766 typename ImplType::AddResult result = m_impl.template add<BaseTranslator>(newValue, m_allocator.get());
767 if (result.isNewEntry)
768 insertNodeBefore(it.node(), *result.iterator);
769 return AddResult(makeIterator(*result.iterator), result.isNewEntry);
772 template<typename T, size_t inlineCapacity, typename U>
773 typename ListHashSet<T, inlineCapacity, U>::AddResult ListHashSet<T, inlineCapacity, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValue)
775 return insertBefore(find(beforeValue), newValue);
778 template<typename T, size_t inlineCapacity, typename U>
779 inline void ListHashSet<T, inlineCapacity, U>::remove(iterator it)
783 m_impl.remove(it.node());
784 unlinkAndDelete(it.node());
787 template<typename T, size_t inlineCapacity, typename U>
788 inline void ListHashSet<T, inlineCapacity, U>::remove(const ValueType& value)
793 template<typename T, size_t inlineCapacity, typename U>
794 inline void ListHashSet<T, inlineCapacity, U>::clear()
802 template<typename T, size_t inlineCapacity, typename U>
803 void ListHashSet<T, inlineCapacity, U>::unlink(Node* node)
806 ASSERT(node == m_head);
807 m_head = node->m_next;
809 ASSERT(node != m_head);
810 node->m_prev->m_next = node->m_next;
814 ASSERT(node == m_tail);
815 m_tail = node->m_prev;
817 ASSERT(node != m_tail);
818 node->m_next->m_prev = node->m_prev;
822 template<typename T, size_t inlineCapacity, typename U>
823 void ListHashSet<T, inlineCapacity, U>::unlinkAndDelete(Node* node)
826 node->destroy(m_allocator.get());
829 template<typename T, size_t inlineCapacity, typename U>
830 void ListHashSet<T, inlineCapacity, U>::appendNode(Node* node)
832 node->m_prev = m_tail;
837 m_tail->m_next = node;
846 template<typename T, size_t inlineCapacity, typename U>
847 void ListHashSet<T, inlineCapacity, U>::prependNode(Node* node)
850 node->m_next = m_head;
853 m_head->m_prev = node;
860 template<typename T, size_t inlineCapacity, typename U>
861 void ListHashSet<T, inlineCapacity, U>::insertNodeBefore(Node* beforeNode, Node* newNode)
864 return appendNode(newNode);
866 newNode->m_next = beforeNode;
867 newNode->m_prev = beforeNode->m_prev;
868 if (beforeNode->m_prev)
869 beforeNode->m_prev->m_next = newNode;
870 beforeNode->m_prev = newNode;
872 if (!newNode->m_prev)
876 template<typename T, size_t inlineCapacity, typename U>
877 void ListHashSet<T, inlineCapacity, U>::deleteAllNodes()
882 for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : 0)
883 node->destroy(m_allocator.get());
886 template<typename T, size_t inlineCapacity, typename U>
887 inline ListHashSetReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeReverseIterator(Node* position)
889 return ListHashSetReverseIterator<T, inlineCapacity, U>(this, position);
892 template<typename T, size_t inlineCapacity, typename U>
893 inline ListHashSetConstReverseIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstReverseIterator(Node* position) const
895 return ListHashSetConstReverseIterator<T, inlineCapacity, U>(this, position);
898 template<typename T, size_t inlineCapacity, typename U>
899 inline ListHashSetIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeIterator(Node* position)
901 return ListHashSetIterator<T, inlineCapacity, U>(this, position);
904 template<typename T, size_t inlineCapacity, typename U>
905 inline ListHashSetConstIterator<T, inlineCapacity, U> ListHashSet<T, inlineCapacity, U>::makeConstIterator(Node* position) const
907 return ListHashSetConstIterator<T, inlineCapacity, U>(this, position);
909 template<bool, typename ValueType, typename HashTableType>
910 void deleteAllValues(HashTableType& collection)
912 typedef typename HashTableType::const_iterator iterator;
913 iterator end = collection.end();
914 for (iterator it = collection.begin(); it != end; ++it)
915 delete (*it)->m_value;
918 template<typename T, size_t inlineCapacity, typename U>
919 inline void deleteAllValues(const ListHashSet<T, inlineCapacity, U>& collection)
921 deleteAllValues<true, typename ListHashSet<T, inlineCapacity, U>::ValueType>(collection.m_impl);
926 using WTF::ListHashSet;
928 #endif /* WTF_ListHashSet_h */