/* * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2013 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef WTF_HashSet_h #define WTF_HashSet_h #include #include #include #include namespace WTF { struct IdentityExtractor; template class HashSet; template::Hash, typename TraitsArg = HashTraits> class HashSet final { WTF_MAKE_FAST_ALLOCATED; private: typedef HashArg HashFunctions; typedef TraitsArg ValueTraits; public: typedef typename ValueTraits::TraitType ValueType; private: typedef HashTable HashTableType; public: typedef HashTableConstIteratorAdapter iterator; typedef HashTableConstIteratorAdapter const_iterator; typedef typename HashTableType::AddResult AddResult; HashSet() { } HashSet(std::initializer_list initializerList) { for (const auto& value : initializerList) add(value); } void swap(HashSet&); unsigned size() const; unsigned capacity() const; bool isEmpty() const; iterator begin() const; iterator end() const; iterator find(const ValueType&) const; bool contains(const ValueType&) const; // An alternate version of find() that finds the object by hashing and comparing // with some other type, to avoid the cost of type conversion. HashTranslator // must have the following function members: // static unsigned hash(const T&); // static bool equal(const ValueType&, const T&); template iterator find(const T&) const; template bool contains(const T&) const; // The return value includes both an iterator to the added value's location, // and an isNewEntry bool that indicates if it is a new or existing entry in the set. AddResult add(const ValueType&); AddResult add(ValueType&&); // An alternate version of add() that finds the object by hashing and comparing // with some other type, to avoid the cost of type conversion if the object is already // in the table. HashTranslator must have the following function members: // static unsigned hash(const T&); // static bool equal(const ValueType&, const T&); // static translate(ValueType&, const T&, unsigned hashCode); template AddResult add(const T&); // Attempts to add a list of things to the set. Returns true if any of // them are new to the set. Returns false if the set is unchanged. template bool add(IteratorType begin, IteratorType end); bool remove(const ValueType&); bool remove(iterator); template void removeIf(const Functor&); void clear(); ValueType take(const ValueType&); ValueType take(iterator); ValueType 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, ValueType>::type take(typename GetPtrHelper::PtrType); static bool isValidValue(const ValueType&); template bool operator==(const OtherCollection&) const; private: HashTableType m_impl; }; struct IdentityExtractor { 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) { location = std::forward(value); } }; template struct HashSetTranslatorAdapter { template static unsigned hash(const T& key) { return Translator::hash(key); } template static bool equal(const T& a, const U& b) { return Translator::equal(a, b); } template static void translate(T& location, const U& key, const U&, unsigned hashCode) { Translator::translate(location, key, hashCode); } }; template inline void HashSet::swap(HashSet& other) { m_impl.swap(other.m_impl); } template inline unsigned HashSet::size() const { return m_impl.size(); } template inline unsigned HashSet::capacity() const { return m_impl.capacity(); } template inline bool HashSet::isEmpty() const { return m_impl.isEmpty(); } template inline auto HashSet::begin() const -> iterator { return m_impl.begin(); } template inline auto HashSet::end() const -> iterator { return m_impl.end(); } template inline auto HashSet::find(const ValueType& value) const -> iterator { return m_impl.find(value); } template inline bool HashSet::contains(const ValueType& value) const { return m_impl.contains(value); } template template inline auto HashSet::find(const T& value) const -> iterator { return m_impl.template find>(value); } template template inline bool HashSet::contains(const T& value) const { return m_impl.template contains>(value); } template inline auto HashSet::add(const ValueType& value) -> AddResult { return m_impl.add(value); } template inline auto HashSet::add(ValueType&& value) -> AddResult { return m_impl.add(WTFMove(value)); } template template inline auto HashSet::add(const T& value) -> AddResult { return m_impl.template addPassingHashCode>(value, value); } template template inline bool HashSet::add(IteratorType begin, IteratorType end) { bool changed = false; for (IteratorType iter = begin; iter != end; ++iter) changed |= add(*iter).isNewEntry; return changed; } template inline bool HashSet::remove(iterator it) { if (it.m_impl == m_impl.end()) return false; m_impl.internalCheckTableConsistency(); m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); return true; } template inline bool HashSet::remove(const ValueType& value) { return remove(find(value)); } template template inline void HashSet::removeIf(const Functor& functor) { m_impl.removeIf(functor); } template inline void HashSet::clear() { m_impl.clear(); } template inline auto HashSet::take(iterator it) -> ValueType { if (it == end()) return ValueTraits::emptyValue(); ValueType result = WTFMove(const_cast(*it)); remove(it); return result; } template inline auto HashSet::take(const ValueType& value) -> ValueType { return take(find(value)); } template inline auto HashSet::takeAny() -> ValueType { 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, ValueType>::type { return take(find(value)); } template inline bool HashSet::isValidValue(const ValueType& value) { if (ValueTraits::isDeletedValue(value)) return false; if (HashFunctions::safeToCompareToEmptyOrDeleted) { if (value == ValueTraits::emptyValue()) return false; } else { if (isHashTraitsEmptyValue(value)) return false; } return true; } template inline void copyToVector(const C& collection, W& vector) { typedef typename C::const_iterator iterator; vector.resize(collection.size()); iterator it = collection.begin(); iterator end = collection.end(); for (unsigned i = 0; it != end; ++it, ++i) vector[i] = *it; } template template inline bool HashSet::operator==(const OtherCollection& otherCollection) const { if (size() != otherCollection.size()) return false; for (const auto& other : otherCollection) { if (!contains(other)) return false; } return true; } } // namespace WTF using WTF::HashSet; #endif /* WTF_HashSet_h */