From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/ListHashSet.h | 404 +++++++++++++++++-------------------------- 1 file changed, 158 insertions(+), 246 deletions(-) (limited to 'Source/WTF/wtf/ListHashSet.h') diff --git a/Source/WTF/wtf/ListHashSet.h b/Source/WTF/wtf/ListHashSet.h index 912f0f033..278605215 100644 --- a/Source/WTF/wtf/ListHashSet.h +++ b/Source/WTF/wtf/ListHashSet.h @@ -19,12 +19,9 @@ * */ -#ifndef WTF_ListHashSet_h -#define WTF_ListHashSet_h +#pragma once #include -#include -#include namespace WTF { @@ -38,22 +35,20 @@ namespace WTF { // guaranteed safe against mutation of the ListHashSet, except for // removal of the item currently pointed to by a given iterator. -template class ListHashSet; +template class ListHashSet; -template class ListHashSetIterator; -template class ListHashSetConstIterator; +template class ListHashSetIterator; +template class ListHashSetConstIterator; -template struct ListHashSetNode; -template struct ListHashSetNodeAllocator; +template struct ListHashSetNode; template struct ListHashSetNodeHashFunctions; template struct ListHashSetTranslator; -template::Hash> class ListHashSet { +template::Hash> class ListHashSet { WTF_MAKE_FAST_ALLOCATED; private: - typedef ListHashSetNode Node; - typedef ListHashSetNodeAllocator NodeAllocator; + typedef ListHashSetNode Node; typedef HashTraits NodeTraits; typedef ListHashSetNodeHashFunctions NodeHash; @@ -64,24 +59,26 @@ private: public: typedef ValueArg ValueType; - typedef ListHashSetIterator iterator; - typedef ListHashSetConstIterator const_iterator; - friend class ListHashSetConstIterator; + typedef ListHashSetIterator iterator; + typedef ListHashSetConstIterator const_iterator; + friend class ListHashSetConstIterator; typedef std::reverse_iterator reverse_iterator; typedef std::reverse_iterator const_reverse_iterator; typedef HashTableAddResult AddResult; - ListHashSet(); + ListHashSet() = default; ListHashSet(const ListHashSet&); + ListHashSet(ListHashSet&&); ListHashSet& operator=(const ListHashSet&); + ListHashSet& operator=(ListHashSet&&); ~ListHashSet(); void swap(ListHashSet&); - int size() const; - int capacity() const; + unsigned size() const; + unsigned capacity() const; bool isEmpty() const; iterator begin() { return makeIterator(m_head); } @@ -153,110 +150,22 @@ private: const_iterator makeConstIterator(Node*) const; HashTable m_impl; - Node* m_head; - Node* m_tail; - std::unique_ptr m_allocator; + Node* m_head { nullptr }; + Node* m_tail { nullptr }; }; -template struct ListHashSetNodeAllocator { - typedef ListHashSetNode Node; - typedef ListHashSetNodeAllocator NodeAllocator; - - ListHashSetNodeAllocator() - : m_freeList(pool()) - , m_isDoneWithInitialFreeList(false) - { - memset(m_pool.pool, 0, sizeof(m_pool.pool)); - } - - Node* allocate() - { - Node* result = m_freeList; - - if (!result) - return static_cast(fastMalloc(sizeof(Node))); - - ASSERT(!result->m_isAllocated); - - Node* next = result->m_next; - ASSERT(!next || !next->m_isAllocated); - if (!next && !m_isDoneWithInitialFreeList) { - next = result + 1; - if (next == pastPool()) { - m_isDoneWithInitialFreeList = true; - next = 0; - } else { - ASSERT(inPool(next)); - ASSERT(!next->m_isAllocated); - } - } - m_freeList = next; - - return result; - } - - void deallocate(Node* node) - { - if (inPool(node)) { -#ifndef NDEBUG - node->m_isAllocated = false; -#endif - node->m_next = m_freeList; - m_freeList = node; - return; - } - - fastFree(node); - } - -private: - Node* pool() { return reinterpret_cast_ptr(m_pool.pool); } - Node* pastPool() { return pool() + m_poolSize; } - bool inPool(Node* node) - { - return node >= pool() && node < pastPool(); - } - - Node* m_freeList; - bool m_isDoneWithInitialFreeList; - static const size_t m_poolSize = inlineCapacity; - union { - char pool[sizeof(Node) * m_poolSize]; - double forAlignment; - } m_pool; -}; - -template struct ListHashSetNode { - typedef ListHashSetNodeAllocator NodeAllocator; - +template struct ListHashSetNode { + WTF_MAKE_FAST_ALLOCATED; +public: template ListHashSetNode(T&& value) : m_value(std::forward(value)) - , m_prev(0) - , m_next(0) -#ifndef NDEBUG - , m_isAllocated(true) -#endif { } - void* operator new(size_t, NodeAllocator* allocator) - { - return allocator->allocate(); - } - void destroy(NodeAllocator* allocator) - { - this->~ListHashSetNode(); - allocator->deallocate(this); - } - ValueArg m_value; - ListHashSetNode* m_prev; - ListHashSetNode* m_next; - -#ifndef NDEBUG - bool m_isAllocated; -#endif + ListHashSetNode* m_prev { nullptr }; + ListHashSetNode* m_next { nullptr }; }; template struct ListHashSetNodeHashFunctions { @@ -265,15 +174,15 @@ template struct ListHashSetNodeHashFunctions { static const bool safeToCompareToEmptyOrDeleted = false; }; -template class ListHashSetIterator { +template class ListHashSetIterator { private: - typedef ListHashSet ListHashSetType; - typedef ListHashSetIterator iterator; - typedef ListHashSetConstIterator const_iterator; - typedef ListHashSetNode Node; + typedef ListHashSet ListHashSetType; + typedef ListHashSetIterator iterator; + typedef ListHashSetConstIterator const_iterator; + typedef ListHashSetNode Node; typedef ValueArg ValueType; - friend class ListHashSet; + friend class ListHashSet; ListHashSetIterator(const ListHashSetType* set, Node* position) : m_iterator(set, position) { } @@ -312,16 +221,16 @@ private: const_iterator m_iterator; }; -template class ListHashSetConstIterator { +template class ListHashSetConstIterator { private: - typedef ListHashSet ListHashSetType; - typedef ListHashSetIterator iterator; - typedef ListHashSetConstIterator const_iterator; - typedef ListHashSetNode Node; + typedef ListHashSet ListHashSetType; + typedef ListHashSetIterator iterator; + typedef ListHashSetConstIterator const_iterator; + typedef ListHashSetNode Node; typedef ValueArg ValueType; - friend class ListHashSet; - friend class ListHashSetIterator; + friend class ListHashSet; + friend class ListHashSetIterator; ListHashSetConstIterator(const ListHashSetType* set, Node* position) : m_set(set) @@ -342,7 +251,7 @@ public: const ValueType* get() const { - return &m_position->m_value; + return std::addressof(m_position->m_value); } const ValueType& operator*() const { return *get(); } @@ -350,7 +259,7 @@ public: const_iterator& operator++() { - ASSERT(m_position != 0); + ASSERT(m_position); m_position = m_position->m_next; return *this; } @@ -390,139 +299,144 @@ template struct ListHashSetTranslator { template static unsigned hash(const T& key) { return HashFunctions::hash(key); } template static bool equal(const T& a, const U& b) { return HashFunctions::equal(a->m_value, b); } - template static void translate(T*& location, U&& key, const V& allocator) + template static void translate(T*& location, U&& key, V&&) { - location = new (allocator) T(std::forward(key)); + location = new T(std::forward(key)); } }; -template -inline ListHashSet::ListHashSet() - : m_head(0) - , m_tail(0) - , m_allocator(std::make_unique()) -{ -} - -template -inline ListHashSet::ListHashSet(const ListHashSet& other) - : m_head(0) - , m_tail(0) - , m_allocator(std::make_unique()) +template +inline ListHashSet::ListHashSet(const ListHashSet& other) { for (auto it = other.begin(), end = other.end(); it != end; ++it) add(*it); } -template -inline ListHashSet& ListHashSet::operator=(const ListHashSet& other) +template +inline ListHashSet& ListHashSet::operator=(const ListHashSet& other) { ListHashSet tmp(other); swap(tmp); return *this; } -template -inline void ListHashSet::swap(ListHashSet& other) +template +inline ListHashSet::ListHashSet(ListHashSet&& other) + : m_impl(WTFMove(other.m_impl)) + , m_head(std::exchange(other.m_head, nullptr)) + , m_tail(std::exchange(other.m_tail, nullptr)) +{ +} + +template +inline ListHashSet& ListHashSet::operator=(ListHashSet&& other) +{ + m_impl = WTFMove(other.m_impl); + m_head = std::exchange(other.m_head, nullptr); + m_tail = std::exchange(other.m_tail, nullptr); + return *this; +} + +template +inline void ListHashSet::swap(ListHashSet& other) { m_impl.swap(other.m_impl); std::swap(m_head, other.m_head); std::swap(m_tail, other.m_tail); - m_allocator.swap(other.m_allocator); } -template -inline ListHashSet::~ListHashSet() +template +inline ListHashSet::~ListHashSet() { deleteAllNodes(); } -template -inline int ListHashSet::size() const +template +inline unsigned ListHashSet::size() const { return m_impl.size(); } -template -inline int ListHashSet::capacity() const +template +inline unsigned ListHashSet::capacity() const { return m_impl.capacity(); } -template -inline bool ListHashSet::isEmpty() const +template +inline bool ListHashSet::isEmpty() const { return m_impl.isEmpty(); } -template -inline T& ListHashSet::first() +template +inline T& ListHashSet::first() { ASSERT(!isEmpty()); return m_head->m_value; } -template -inline void ListHashSet::removeFirst() +template +inline void ListHashSet::removeFirst() { takeFirst(); } -template -inline T ListHashSet::takeFirst() +template +inline T ListHashSet::takeFirst() { ASSERT(!isEmpty()); auto it = m_impl.find(m_head); - T result = std::move((*it)->m_value); + T result = WTFMove((*it)->m_value); m_impl.remove(it); unlinkAndDelete(m_head); return result; } -template -inline const T& ListHashSet::first() const +template +inline const T& ListHashSet::first() const { ASSERT(!isEmpty()); return m_head->m_value; } -template -inline T& ListHashSet::last() +template +inline T& ListHashSet::last() { ASSERT(!isEmpty()); return m_tail->m_value; } -template -inline const T& ListHashSet::last() const +template +inline const T& ListHashSet::last() const { ASSERT(!isEmpty()); return m_tail->m_value; } -template -inline void ListHashSet::removeLast() +template +inline void ListHashSet::removeLast() { takeLast(); } -template -inline T ListHashSet::takeLast() +template +inline T ListHashSet::takeLast() { ASSERT(!isEmpty()); auto it = m_impl.find(m_tail); - T result = std::move((*it)->m_value); + T result = WTFMove((*it)->m_value); m_impl.remove(it); unlinkAndDelete(m_tail); return result; } -template -inline auto ListHashSet::find(const ValueType& value) -> iterator +template +inline auto ListHashSet::find(const ValueType& value) -> iterator { auto it = m_impl.template find(value); if (it == m_impl.end()) @@ -530,8 +444,8 @@ inline auto ListHashSet::find(const ValueType& value) -> i return makeIterator(*it); } -template -inline auto ListHashSet::find(const ValueType& value) const -> const_iterator +template +inline auto ListHashSet::find(const ValueType& value) const -> const_iterator { auto it = m_impl.template find(value); if (it == m_impl.end()) @@ -545,9 +459,9 @@ struct ListHashSetTranslatorAdapter { template static bool equal(const T& a, const U& b) { return Translator::equal(a->m_value, b); } }; -template +template template -inline auto ListHashSet::find(const T& value) -> iterator +inline auto ListHashSet::find(const T& value) -> iterator { auto it = m_impl.template find>(value); if (it == m_impl.end()) @@ -555,9 +469,9 @@ inline auto ListHashSet::find(const T& value) -> i return makeIterator(*it); } -template +template template -inline auto ListHashSet::find(const T& value) const -> const_iterator +inline auto ListHashSet::find(const T& value) const -> const_iterator { auto it = m_impl.template find>(value); if (it == m_impl.end()) @@ -565,41 +479,41 @@ inline auto ListHashSet::find(const T& value) cons return makeConstIterator(*it); } -template +template template -inline bool ListHashSet::contains(const T& value) const +inline bool ListHashSet::contains(const T& value) const { return m_impl.template contains>(value); } -template -inline bool ListHashSet::contains(const ValueType& value) const +template +inline bool ListHashSet::contains(const ValueType& value) const { return m_impl.template contains(value); } -template -auto ListHashSet::add(const ValueType& value) -> AddResult +template +auto ListHashSet::add(const ValueType& value) -> AddResult { - auto result = m_impl.template add(value, m_allocator.get()); + auto result = m_impl.template add(value, nullptr); if (result.isNewEntry) appendNode(*result.iterator); return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::add(ValueType&& value) -> AddResult +template +auto ListHashSet::add(ValueType&& value) -> AddResult { - auto result = m_impl.template add(std::move(value), m_allocator.get()); + auto result = m_impl.template add(WTFMove(value), nullptr); if (result.isNewEntry) appendNode(*result.iterator); return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::appendOrMoveToLast(const ValueType& value) -> AddResult +template +auto ListHashSet::appendOrMoveToLast(const ValueType& value) -> AddResult { - auto result = m_impl.template add(value, m_allocator.get()); + auto result = m_impl.template add(value, nullptr); Node* node = *result.iterator; if (!result.isNewEntry) unlink(node); @@ -608,10 +522,10 @@ auto ListHashSet::appendOrMoveToLast(const ValueType& valu return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::appendOrMoveToLast(ValueType&& value) -> AddResult +template +auto ListHashSet::appendOrMoveToLast(ValueType&& value) -> AddResult { - auto result = m_impl.template add(std::move(value), m_allocator.get()); + auto result = m_impl.template add(WTFMove(value), nullptr); Node* node = *result.iterator; if (!result.isNewEntry) unlink(node); @@ -620,10 +534,10 @@ auto ListHashSet::appendOrMoveToLast(ValueType&& value) -> return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::prependOrMoveToFirst(const ValueType& value) -> AddResult +template +auto ListHashSet::prependOrMoveToFirst(const ValueType& value) -> AddResult { - auto result = m_impl.template add(value, m_allocator.get()); + auto result = m_impl.template add(value, nullptr); Node* node = *result.iterator; if (!result.isNewEntry) unlink(node); @@ -632,10 +546,10 @@ auto ListHashSet::prependOrMoveToFirst(const ValueType& va return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::prependOrMoveToFirst(ValueType&& value) -> AddResult +template +auto ListHashSet::prependOrMoveToFirst(ValueType&& value) -> AddResult { - auto result = m_impl.template add(std::move(value), m_allocator.get()); + auto result = m_impl.template add(WTFMove(value), nullptr); Node* node = *result.iterator; if (!result.isNewEntry) unlink(node); @@ -644,38 +558,38 @@ auto ListHashSet::prependOrMoveToFirst(ValueType&& value) return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::insertBefore(const ValueType& beforeValue, const ValueType& newValue) -> AddResult +template +auto ListHashSet::insertBefore(const ValueType& beforeValue, const ValueType& newValue) -> AddResult { return insertBefore(find(beforeValue), newValue); } -template -auto ListHashSet::insertBefore(const ValueType& beforeValue, ValueType&& newValue) -> AddResult +template +auto ListHashSet::insertBefore(const ValueType& beforeValue, ValueType&& newValue) -> AddResult { - return insertBefore(find(beforeValue), std::move(newValue)); + return insertBefore(find(beforeValue), WTFMove(newValue)); } -template -auto ListHashSet::insertBefore(iterator it, const ValueType& newValue) -> AddResult +template +auto ListHashSet::insertBefore(iterator it, const ValueType& newValue) -> AddResult { - auto result = m_impl.template add(newValue, m_allocator.get()); + auto result = m_impl.template add(newValue, nullptr); if (result.isNewEntry) insertNodeBefore(it.node(), *result.iterator); return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -auto ListHashSet::insertBefore(iterator it, ValueType&& newValue) -> AddResult +template +auto ListHashSet::insertBefore(iterator it, ValueType&& newValue) -> AddResult { - auto result = m_impl.template add(std::move(newValue), m_allocator.get()); + auto result = m_impl.template add(WTFMove(newValue), nullptr); if (result.isNewEntry) insertNodeBefore(it.node(), *result.iterator); return AddResult(makeIterator(*result.iterator), result.isNewEntry); } -template -inline bool ListHashSet::remove(iterator it) +template +inline bool ListHashSet::remove(iterator it) { if (it == end()) return false; @@ -684,23 +598,23 @@ inline bool ListHashSet::remove(iterator it) return true; } -template -inline bool ListHashSet::remove(const ValueType& value) +template +inline bool ListHashSet::remove(const ValueType& value) { return remove(find(value)); } -template -inline void ListHashSet::clear() +template +inline void ListHashSet::clear() { deleteAllNodes(); m_impl.clear(); - m_head = 0; - m_tail = 0; + m_head = nullptr; + m_tail = nullptr; } -template -void ListHashSet::unlink(Node* node) +template +void ListHashSet::unlink(Node* node) { if (!node->m_prev) { ASSERT(node == m_head); @@ -719,18 +633,18 @@ void ListHashSet::unlink(Node* node) } } -template -void ListHashSet::unlinkAndDelete(Node* node) +template +void ListHashSet::unlinkAndDelete(Node* node) { unlink(node); - node->destroy(m_allocator.get()); + delete node; } -template -void ListHashSet::appendNode(Node* node) +template +void ListHashSet::appendNode(Node* node) { node->m_prev = m_tail; - node->m_next = 0; + node->m_next = nullptr; if (m_tail) { ASSERT(m_head); @@ -743,10 +657,10 @@ void ListHashSet::appendNode(Node* node) m_tail = node; } -template -void ListHashSet::prependNode(Node* node) +template +void ListHashSet::prependNode(Node* node) { - node->m_prev = 0; + node->m_prev = nullptr; node->m_next = m_head; if (m_head) @@ -757,8 +671,8 @@ void ListHashSet::prependNode(Node* node) m_head = node; } -template -void ListHashSet::insertNodeBefore(Node* beforeNode, Node* newNode) +template +void ListHashSet::insertNodeBefore(Node* beforeNode, Node* newNode) { if (!beforeNode) return appendNode(newNode); @@ -773,24 +687,24 @@ void ListHashSet::insertNodeBefore(Node* beforeNode, Node* m_head = newNode; } -template -void ListHashSet::deleteAllNodes() +template +void ListHashSet::deleteAllNodes() { if (!m_head) return; - for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : 0) - node->destroy(m_allocator.get()); + for (Node* node = m_head, *next = m_head->m_next; node; node = next, next = node ? node->m_next : nullptr) + delete node; } -template -inline auto ListHashSet::makeIterator(Node* position) -> iterator +template +inline auto ListHashSet::makeIterator(Node* position) -> iterator { return iterator(this, position); } -template -inline auto ListHashSet::makeConstIterator(Node* position) const -> const_iterator +template +inline auto ListHashSet::makeConstIterator(Node* position) const -> const_iterator { return const_iterator(this, position); } @@ -798,5 +712,3 @@ inline auto ListHashSet::makeConstIterator(Node* position) } // namespace WTF using WTF::ListHashSet; - -#endif /* WTF_ListHashSet_h */ -- cgit v1.2.1