#ifndef WTF_HashSet_h
#define WTF_HashSet_h
#include "HashTable.h"
namespace WTF {
template<typename Value, typename HashFunctions, typename Traits> class HashSet;
template<typename Value, typename HashFunctions, typename Traits>
void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&);
template<typename T> struct IdentityExtractor;
template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash,
typename TraitsArg = HashTraits<ValueArg> > class HashSet {
private:
typedef HashArg HashFunctions;
typedef TraitsArg ValueTraits;
public:
typedef typename ValueTraits::TraitType ValueType;
private:
typedef HashTable<ValueType, ValueType, IdentityExtractor<ValueType>,
HashFunctions, ValueTraits, ValueTraits> HashTableType;
public:
typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
void swap(HashSet&);
int size() const;
int capacity() const;
bool isEmpty() const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
iterator find(const ValueType&);
const_iterator find(const ValueType&) const;
bool contains(const ValueType&) const;
template<typename T, typename HashTranslator> iterator find(const T&);
template<typename T, typename HashTranslator> const_iterator find(const T&) const;
template<typename T, typename HashTranslator> bool contains(const T&) const;
pair<iterator, bool> add(const ValueType&);
template<typename T, typename HashTranslator> pair<iterator, bool> add(const T&);
void remove(const ValueType&);
void remove(iterator);
void clear();
private:
friend void deleteAllValues<>(const HashSet&);
HashTableType m_impl;
};
template<typename T> struct IdentityExtractor {
static const T& extract(const T& t) { return t; }
};
template<typename ValueType, typename ValueTraits, typename T, typename Translator>
struct HashSetTranslatorAdapter {
static unsigned hash(const T& key) { return Translator::hash(key); }
static bool equal(const ValueType& a, const T& b) { return Translator::equal(a, b); }
static void translate(ValueType& location, const T& key, const T&, unsigned hashCode)
{
Translator::translate(location, key, hashCode);
}
};
template<typename T, typename U, typename V>
inline void HashSet<T, U, V>::swap(HashSet& other)
{
m_impl.swap(other.m_impl);
}
template<typename T, typename U, typename V>
inline int HashSet<T, U, V>::size() const
{
return m_impl.size();
}
template<typename T, typename U, typename V>
inline int HashSet<T, U, V>::capacity() const
{
return m_impl.capacity();
}
template<typename T, typename U, typename V>
inline bool HashSet<T, U, V>::isEmpty() const
{
return m_impl.isEmpty();
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::begin()
{
return m_impl.begin();
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::end()
{
return m_impl.end();
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::begin() const
{
return m_impl.begin();
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::end() const
{
return m_impl.end();
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const ValueType& value)
{
return m_impl.find(value);
}
template<typename T, typename U, typename V>
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::find(const ValueType& value) const
{
return m_impl.find(value);
}
template<typename T, typename U, typename V>
inline bool HashSet<T, U, V>::contains(const ValueType& value) const
{
return m_impl.contains(value);
}
template<typename Value, typename HashFunctions, typename Traits>
template<typename T, typename Translator>
typename HashSet<Value, HashFunctions, Traits>::iterator
inline HashSet<Value, HashFunctions, Traits>::find(const T& value)
{
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter;
return m_impl.template find<T, Adapter>(value);
}
template<typename Value, typename HashFunctions, typename Traits>
template<typename T, typename Translator>
typename HashSet<Value, HashFunctions, Traits>::const_iterator
inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const
{
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter;
return m_impl.template find<T, Adapter>(value);
}
template<typename Value, typename HashFunctions, typename Traits>
template<typename T, typename Translator>
inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value) const
{
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter;
return m_impl.template contains<T, Adapter>(value);
}
template<typename T, typename U, typename V>
pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType& value)
{
return m_impl.add(value);
}
template<typename Value, typename HashFunctions, typename Traits>
template<typename T, typename Translator>
pair<typename HashSet<Value, HashFunctions, Traits>::iterator, bool>
HashSet<Value, HashFunctions, Traits>::add(const T& value)
{
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter;
return m_impl.template addPassingHashCode<T, T, Adapter>(value, value);
}
template<typename T, typename U, typename V>
inline void HashSet<T, U, V>::remove(iterator it)
{
if (it.m_impl == m_impl.end())
return;
m_impl.checkTableConsistency();
m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
}
template<typename T, typename U, typename V>
inline void HashSet<T, U, V>::remove(const ValueType& value)
{
remove(find(value));
}
template<typename T, typename U, typename V>
inline void HashSet<T, U, V>::clear()
{
m_impl.clear();
}
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>
inline void deleteAllValues(const HashSet<T, U, V>& collection)
{
deleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
}
template<typename T, typename U, typename V, typename W>
inline void copyToVector(const HashSet<T, U, V>& collection, W& vector)
{
typedef typename HashSet<T, U, V>::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