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/HashSet.h | 104 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 17 deletions(-) (limited to 'Source/WTF/wtf/HashSet.h') diff --git a/Source/WTF/wtf/HashSet.h b/Source/WTF/wtf/HashSet.h index 46c3fe5d1..cf1f94214 100644 --- a/Source/WTF/wtf/HashSet.h +++ b/Source/WTF/wtf/HashSet.h @@ -23,6 +23,7 @@ #include #include +#include #include namespace WTF { @@ -32,11 +33,12 @@ namespace WTF { template class HashSet; template::Hash, - typename TraitsArg = HashTraits> class HashSet { + typename TraitsArg = HashTraits> class HashSet final { WTF_MAKE_FAST_ALLOCATED; private: typedef HashArg HashFunctions; typedef TraitsArg ValueTraits; + typedef typename ValueTraits::TakeType TakeType; public: typedef typename ValueTraits::TraitType ValueType; @@ -62,8 +64,8 @@ namespace WTF { void swap(HashSet&); - int size() const; - int capacity() const; + unsigned size() const; + unsigned capacity() const; bool isEmpty() const; iterator begin() const; @@ -100,13 +102,24 @@ namespace WTF { bool remove(const ValueType&); bool remove(iterator); + template + void removeIf(const Functor&); void clear(); - ValueType take(const ValueType&); + TakeType take(const ValueType&); + TakeType take(iterator); + TakeType takeAny(); + + // Overloads for smart pointer values that take the raw pointer type as the parameter. + template typename std::enable_if::value, iterator>::type find(typename GetPtrHelper::PtrType) const; + template typename std::enable_if::value, bool>::type contains(typename GetPtrHelper::PtrType) const; + template typename std::enable_if::value, bool>::type remove(typename GetPtrHelper::PtrType); + template typename std::enable_if::value, TakeType>::type take(typename GetPtrHelper::PtrType); static bool isValidValue(const ValueType&); - - bool operator==(const HashSet&) const; + + template + bool operator==(const OtherCollection&) const; private: HashTableType m_impl; @@ -116,6 +129,16 @@ namespace WTF { template static const T& extract(const T& t) { return t; } }; + template + struct HashSetTranslator { + 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, b); } + template static void translate(T& location, U&&, V&& value) + { + ValueTraits::assignToEmpty(location, std::forward(value)); + } + }; + template struct HashSetTranslatorAdapter { template static unsigned hash(const T& key) { return Translator::hash(key); } @@ -133,13 +156,13 @@ namespace WTF { } template - inline int HashSet::size() const + inline unsigned HashSet::size() const { return m_impl.size(); } template - inline int HashSet::capacity() const + inline unsigned HashSet::capacity() const { return m_impl.capacity(); } @@ -197,7 +220,7 @@ namespace WTF { template inline auto HashSet::add(ValueType&& value) -> AddResult { - return m_impl.add(std::move(value)); + return m_impl.add(WTFMove(value)); } template @@ -233,6 +256,13 @@ namespace WTF { return remove(find(value)); } + template + template + inline void HashSet::removeIf(const Functor& functor) + { + m_impl.removeIf(functor); + } + template inline void HashSet::clear() { @@ -240,17 +270,56 @@ namespace WTF { } template - auto HashSet::take(const ValueType& value) -> ValueType + inline auto HashSet::take(iterator it) -> TakeType { - auto it = find(value); if (it == end()) - return ValueTraits::emptyValue(); + return ValueTraits::take(ValueTraits::emptyValue()); - ValueType result = std::move(const_cast(*it)); + auto result = ValueTraits::take(WTFMove(const_cast(*it))); remove(it); return result; } + template + inline auto HashSet::take(const ValueType& value) -> TakeType + { + return take(find(value)); + } + + template + inline auto HashSet::takeAny() -> TakeType + { + return take(begin()); + } + + template + template + inline auto HashSet::find(typename GetPtrHelper::PtrType value) const -> typename std::enable_if::value, iterator>::type + { + return m_impl.template find>(value); + } + + template + template + inline auto HashSet::contains(typename GetPtrHelper::PtrType value) const -> typename std::enable_if::value, bool>::type + { + return m_impl.template contains>(value); + } + + template + template + inline auto HashSet::remove(typename GetPtrHelper::PtrType value) -> typename std::enable_if::value, bool>::type + { + return remove(find(value)); + } + + template + template + inline auto HashSet::take(typename GetPtrHelper::PtrType value) -> typename std::enable_if::value, TakeType>::type + { + return take(find(value)); + } + template inline bool HashSet::isValidValue(const ValueType& value) { @@ -282,12 +351,13 @@ namespace WTF { } template - inline bool HashSet::operator==(const HashSet& other) const + template + inline bool HashSet::operator==(const OtherCollection& otherCollection) const { - if (size() != other.size()) + if (size() != otherCollection.size()) return false; - for (const_iterator iter = begin(); iter != end(); ++iter) { - if (!other.contains(*iter)) + for (const auto& other : otherCollection) { + if (!contains(other)) return false; } return true; -- cgit v1.2.1