2
* Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public License
15
* along with this library; see the file COPYING.LIB. If not, write to
16
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301, USA.
24
#include <wtf/FastAllocBase.h>
25
#include <wtf/HashTable.h>
29
struct IdentityExtractor;
31
template<typename Value, typename HashFunctions, typename Traits> class HashSet;
32
template<typename Value, typename HashFunctions, typename Traits>
33
void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&);
35
template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash,
36
typename TraitsArg = HashTraits<ValueArg> > class HashSet {
37
WTF_MAKE_FAST_ALLOCATED;
39
typedef HashArg HashFunctions;
40
typedef TraitsArg ValueTraits;
43
typedef typename ValueTraits::TraitType ValueType;
46
typedef HashTable<ValueType, ValueType, IdentityExtractor,
47
HashFunctions, ValueTraits, ValueTraits> HashTableType;
50
typedef HashTableConstIteratorAdapter<HashTableType, ValueType> iterator;
51
typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
52
typedef typename HashTableType::AddResult AddResult;
60
iterator begin() const;
63
iterator find(const ValueType&) const;
64
bool contains(const ValueType&) const;
66
// An alternate version of find() that finds the object by hashing and comparing
67
// with some other type, to avoid the cost of type conversion. HashTranslator
68
// must have the following function members:
69
// static unsigned hash(const T&);
70
// static bool equal(const ValueType&, const T&);
71
// FIXME: We should reverse the order of the template arguments so that callers
72
// can just pass the translator and let the compiler deduce T.
73
template<typename T, typename HashTranslator> iterator find(const T&) const;
74
template<typename T, typename HashTranslator> bool contains(const T&) const;
76
// The return value is a pair of an interator to the new value's location,
77
// and a bool that is true if an new entry was added.
78
AddResult add(const ValueType&);
80
// An alternate version of add() that finds the object by hashing and comparing
81
// with some other type, to avoid the cost of type conversion if the object is already
82
// in the table. HashTranslator must have the following function members:
83
// static unsigned hash(const T&);
84
// static bool equal(const ValueType&, const T&);
85
// static translate(ValueType&, const T&, unsigned hashCode);
86
// FIXME: We should reverse the order of the template arguments so that callers
87
// can just pass the translator and let the compiler deduce T.
88
template<typename T, typename HashTranslator> AddResult add(const T&);
90
void remove(const ValueType&);
91
void remove(iterator);
95
friend void deleteAllValues<>(const HashSet&);
100
struct IdentityExtractor {
101
template<typename T> static const T& extract(const T& t) { return t; }
104
template<typename Translator>
105
struct HashSetTranslatorAdapter {
106
template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
107
template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
108
template<typename T, typename U> static void translate(T& location, const U& key, const U&, unsigned hashCode)
110
Translator::translate(location, key, hashCode);
114
template<typename T, typename U, typename V>
115
inline void HashSet<T, U, V>::swap(HashSet& other)
117
m_impl.swap(other.m_impl);
120
template<typename T, typename U, typename V>
121
inline int HashSet<T, U, V>::size() const
123
return m_impl.size();
126
template<typename T, typename U, typename V>
127
inline int HashSet<T, U, V>::capacity() const
129
return m_impl.capacity();
132
template<typename T, typename U, typename V>
133
inline bool HashSet<T, U, V>::isEmpty() const
135
return m_impl.isEmpty();
138
template<typename T, typename U, typename V>
139
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::begin() const
141
return m_impl.begin();
144
template<typename T, typename U, typename V>
145
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::end() const
150
template<typename T, typename U, typename V>
151
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const ValueType& value) const
153
return m_impl.find(value);
156
template<typename T, typename U, typename V>
157
inline bool HashSet<T, U, V>::contains(const ValueType& value) const
159
return m_impl.contains(value);
162
template<typename Value, typename HashFunctions, typename Traits>
163
template<typename T, typename HashTranslator>
164
typename HashSet<Value, HashFunctions, Traits>::iterator
165
inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const
167
return m_impl.template find<HashSetTranslatorAdapter<HashTranslator> >(value);
170
template<typename Value, typename HashFunctions, typename Traits>
171
template<typename T, typename HashTranslator>
172
inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value) const
174
return m_impl.template contains<HashSetTranslatorAdapter<HashTranslator> >(value);
177
template<typename T, typename U, typename V>
178
inline typename HashSet<T, U, V>::AddResult HashSet<T, U, V>::add(const ValueType& value)
180
return m_impl.add(value);
183
template<typename Value, typename HashFunctions, typename Traits>
184
template<typename T, typename HashTranslator>
185
inline typename HashSet<Value, HashFunctions, Traits>::AddResult
186
HashSet<Value, HashFunctions, Traits>::add(const T& value)
188
return m_impl.template addPassingHashCode<HashSetTranslatorAdapter<HashTranslator> >(value, value);
191
template<typename T, typename U, typename V>
192
inline void HashSet<T, U, V>::remove(iterator it)
194
if (it.m_impl == m_impl.end())
196
m_impl.internalCheckTableConsistency();
197
m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
200
template<typename T, typename U, typename V>
201
inline void HashSet<T, U, V>::remove(const ValueType& value)
206
template<typename T, typename U, typename V>
207
inline void HashSet<T, U, V>::clear()
212
template<typename ValueType, typename HashTableType>
213
void deleteAllValues(HashTableType& collection)
215
typedef typename HashTableType::const_iterator iterator;
216
iterator end = collection.end();
217
for (iterator it = collection.begin(); it != end; ++it)
221
template<typename T, typename U, typename V>
222
inline void deleteAllValues(const HashSet<T, U, V>& collection)
224
deleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
227
template<typename T, typename U, typename V, typename W>
228
inline void copyToVector(const HashSet<T, U, V>& collection, W& vector)
230
typedef typename HashSet<T, U, V>::const_iterator iterator;
232
vector.resize(collection.size());
234
iterator it = collection.begin();
235
iterator end = collection.end();
236
for (unsigned i = 0; it != end; ++it, ++i)
244
#endif /* WTF_HashSet_h */