#ifndef WTF_HashSet_h
#define WTF_HashSet_h
#include "wtf/DefaultAllocator.h"
#include "wtf/HashTable.h"
namespace WTF {
struct IdentityExtractor;
template<typename T, typename U, typename V, typename W> class HashSet;
template<typename T, typename U, typename V, typename W>
void deleteAllValues(const HashSet<T, U, V, W>&);
template<
typename ValueArg,
typename HashArg = typename DefaultHash<ValueArg>::Hash,
typename TraitsArg = HashTraits<ValueArg>,
typename Allocator = DefaultAllocator> class HashSet {
private:
typedef HashArg HashFunctions;
typedef TraitsArg ValueTraits;
typedef const typename ValueTraits::PeekInType& ValuePeekInType;
public:
void* operator new(size_t size)
{
return Allocator::template malloc<void*, HashSet>(size);
}
void operator delete(void* p) { Allocator::free(p); }
void* operator new[](size_t size) { return Allocator::template newArray<HashSet>(size); }
void operator delete[](void* p) { Allocator::deleteArray(p); }
void* operator new(size_t, NotNullTag, void* location)
{
COMPILE_ASSERT(!Allocator::isGarbageCollected, Garbage_collector_must_be_disabled);
ASSERT(location);
return location;
}
typedef typename ValueTraits::TraitType ValueType;
private:
typedef HashTable<ValueType, ValueType, IdentityExtractor,
HashFunctions, ValueTraits, ValueTraits, Allocator> HashTableType;
public:
typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> iterator;
typedef HashTableConstIteratorAdapter<HashTableType, ValueTraits> const_iterator;
typedef typename HashTableType::AddResult AddResult;
void swap(HashSet& ref)
{
m_impl.swap(ref.m_impl);
}
void swap(typename Allocator::template OtherType<HashSet>::Type other)
{
HashSet& ref = Allocator::getOther(other);
m_impl.swap(ref.m_impl);
}
unsigned size() const;
unsigned capacity() const;
bool isEmpty() const;
iterator begin() const;
iterator end() const;
iterator find(ValuePeekInType) const;
bool contains(ValuePeekInType) const;
template<typename HashTranslator, typename T> iterator find(const T&) const;
template<typename HashTranslator, typename T> bool contains(const T&) const;
AddResult add(ValuePeekInType);
template<typename HashTranslator, typename T> AddResult add(const T&);
void remove(ValuePeekInType);
void remove(iterator);
void clear();
static bool isValidValue(ValuePeekInType);
void trace(typename Allocator::Visitor* visitor)
{
m_impl.trace(visitor);
}
private:
friend void deleteAllValues<>(const HashSet&);
HashTableType m_impl;
};
struct IdentityExtractor {
template<typename T>
static const T& extract(const T& t) { return t; }
};
template<typename Translator>
struct HashSetTranslatorAdapter {
template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
template<typename T, typename U> static void translate(T& location, const U& key, const U&, unsigned hashCode)
{
Translator::translate(location, key, hashCode);
}
};
template<typename T, typename U, typename V, typename W>
inline unsigned HashSet<T, U, V, W>::size() const
{
return m_impl.size();
}
template<typename T, typename U, typename V, typename W>
inline unsigned HashSet<T, U, V, W>::capacity() const
{
return m_impl.capacity();
}
template<typename T, typename U, typename V, typename W>
inline bool HashSet<T, U, V, W>::isEmpty() const
{
return m_impl.isEmpty();
}
template<typename T, typename U, typename V, typename W>
inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::begin() const
{
return m_impl.begin();
}
template<typename T, typename U, typename V, typename W>
inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::end() const
{
return m_impl.end();
}
template<typename T, typename U, typename V, typename W>
inline typename HashSet<T, U, V, W>::iterator HashSet<T, U, V, W>::find(ValuePeekInType value) const
{
return m_impl.find(value);
}
template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
inline bool HashSet<Value, HashFunctions, Traits, Allocator>::contains(ValuePeekInType value) const
{
return m_impl.contains(value);
}
template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
template<typename HashTranslator, typename T>
typename HashSet<Value, HashFunctions, Traits, Allocator>::iterator
inline HashSet<Value, HashFunctions, Traits, Allocator>::find(const T& value) const
{
return m_impl.template find<HashSetTranslatorAdapter<HashTranslator> >(value);
}
template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
template<typename HashTranslator, typename T>
inline bool HashSet<Value, HashFunctions, Traits, Allocator>::contains(const T& value) const
{
return m_impl.template contains<HashSetTranslatorAdapter<HashTranslator> >(value);
}
template<typename T, typename U, typename V, typename W>
inline typename HashSet<T, U, V, W>::AddResult HashSet<T, U, V, W>::add(ValuePeekInType value)
{
return m_impl.add(value);
}
template<typename Value, typename HashFunctions, typename Traits, typename Allocator>
template<typename HashTranslator, typename T>
inline typename HashSet<Value, HashFunctions, Traits, Allocator>::AddResult
HashSet<Value, HashFunctions, Traits, Allocator>::add(const T& value)
{
return m_impl.template addPassingHashCode<HashSetTranslatorAdapter<HashTranslator> >(value, value);
}
template<typename T, typename U, typename V, typename W>
inline void HashSet<T, U, V, W>::remove(iterator it)
{
m_impl.remove(it.m_impl);
}
template<typename T, typename U, typename V, typename W>
inline void HashSet<T, U, V, W>::remove(ValuePeekInType value)
{
remove(find(value));
}
template<typename T, typename U, typename V, typename W>
inline void HashSet<T, U, V, W>::clear()
{
m_impl.clear();
}
template<typename T, typename U, typename V, typename W>
inline bool HashSet<T, U, V, W>::isValidValue(ValuePeekInType value)
{
if (ValueTraits::isDeletedValue(value))
return false;
if (HashFunctions::safeToCompareToEmptyOrDeleted) {
if (value == ValueTraits::emptyValue())
return false;
} else {
if (isHashTraitsEmptyValue<ValueTraits>(value))
return false;
}
return true;
}
template<typename ValueType, typename HashTableType>
void deleteAllValues(HashTableType& collection)
{
typedef typename HashTableType::const_iterator iterator;
iterator end = collection.end();
for (iterator it = collection.begin(); it != end; ++it)
delete *it;
}
template<typename T, typename U, typename V, typename W>
inline void deleteAllValues(const HashSet<T, U, V, W>& collection)
{
deleteAllValues<typename HashSet<T, U, V, W>::ValueType>(collection.m_impl);
}
template<typename C, typename W>
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;
}
}
using WTF::HashSet;
#endif