ListHashSet.h 33 KB

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