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