2
* Copyright (C) 2005, 2006, 2007, 2008 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 "FastAllocBase.h"
25
#include "HashTable.h"
29
template<typename Value, typename HashFunctions, typename Traits> class HashSet;
30
template<typename Value, typename HashFunctions, typename Traits>
31
void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&);
32
template<typename Value, typename HashFunctions, typename Traits>
33
void fastDeleteAllValues(const HashSet<Value, HashFunctions, Traits>&);
35
template<typename T> struct IdentityExtractor;
37
template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash,
38
typename TraitsArg = HashTraits<ValueArg> > class HashSet : public FastAllocBase {
40
typedef HashArg HashFunctions;
41
typedef TraitsArg ValueTraits;
44
typedef typename ValueTraits::TraitType ValueType;
47
typedef HashTable<ValueType, ValueType, IdentityExtractor<ValueType>,
48
HashFunctions, ValueTraits, ValueTraits> HashTableType;
51
typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
52
typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
62
const_iterator begin() const;
63
const_iterator end() const;
65
iterator find(const ValueType&);
66
const_iterator find(const ValueType&) const;
67
bool contains(const ValueType&) const;
69
// An alternate version of find() that finds the object by hashing and comparing
70
// with some other type, to avoid the cost of type conversion. HashTranslator
71
// must have the following function members:
72
// static unsigned hash(const T&);
73
// static bool equal(const ValueType&, const T&);
74
template<typename T, typename HashTranslator> iterator find(const T&);
75
template<typename T, typename HashTranslator> const_iterator find(const T&) const;
76
template<typename T, typename HashTranslator> bool contains(const T&) const;
78
// The return value is a pair of an interator to the new value's location,
79
// and a bool that is true if an new entry was added.
80
pair<iterator, bool> add(const ValueType&);
82
// An alternate version of add() that finds the object by hashing and comparing
83
// with some other type, to avoid the cost of type conversion if the object is already
84
// in the table. HashTranslator must have the following methods:
85
// static unsigned hash(const T&);
86
// static bool equal(const ValueType&, const T&);
87
// static translate(ValueType&, const T&, unsigned hashCode);
88
template<typename T, typename HashTranslator> pair<iterator, bool> add(const T&);
90
void remove(const ValueType&);
91
void remove(iterator);
95
friend void deleteAllValues<>(const HashSet&);
96
friend void fastDeleteAllValues<>(const HashSet&);
101
template<typename T> struct IdentityExtractor {
102
static const T& extract(const T& t) { return t; }
105
template<typename ValueType, typename ValueTraits, typename T, typename Translator>
106
struct HashSetTranslatorAdapter {
107
static unsigned hash(const T& key) { return Translator::hash(key); }
108
static bool equal(const ValueType& a, const T& b) { return Translator::equal(a, b); }
109
static void translate(ValueType& location, const T& key, const T&, unsigned hashCode)
111
Translator::translate(location, key, hashCode);
115
template<typename T, typename U, typename V>
116
inline void HashSet<T, U, V>::swap(HashSet& other)
118
m_impl.swap(other.m_impl);
121
template<typename T, typename U, typename V>
122
inline int HashSet<T, U, V>::size() const
124
return m_impl.size();
127
template<typename T, typename U, typename V>
128
inline int HashSet<T, U, V>::capacity() const
130
return m_impl.capacity();
133
template<typename T, typename U, typename V>
134
inline bool HashSet<T, U, V>::isEmpty() const
136
return m_impl.isEmpty();
139
template<typename T, typename U, typename V>
140
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::begin()
142
return m_impl.begin();
145
template<typename T, typename U, typename V>
146
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::end()
151
template<typename T, typename U, typename V>
152
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::begin() const
154
return m_impl.begin();
157
template<typename T, typename U, typename V>
158
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::end() const
163
template<typename T, typename U, typename V>
164
inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const ValueType& value)
166
return m_impl.find(value);
169
template<typename T, typename U, typename V>
170
inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::find(const ValueType& value) const
172
return m_impl.find(value);
175
template<typename T, typename U, typename V>
176
inline bool HashSet<T, U, V>::contains(const ValueType& value) const
178
return m_impl.contains(value);
181
template<typename Value, typename HashFunctions, typename Traits>
182
template<typename T, typename HashTranslator>
183
typename HashSet<Value, HashFunctions, Traits>::iterator
184
inline HashSet<Value, HashFunctions, Traits>::find(const T& value)
186
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, HashTranslator> Adapter;
187
return m_impl.template find<T, Adapter>(value);
190
template<typename Value, typename HashFunctions, typename Traits>
191
template<typename T, typename HashTranslator>
192
typename HashSet<Value, HashFunctions, Traits>::const_iterator
193
inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const
195
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, HashTranslator> Adapter;
196
return m_impl.template find<T, Adapter>(value);
199
template<typename Value, typename HashFunctions, typename Traits>
200
template<typename T, typename HashTranslator>
201
inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value) const
203
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, HashTranslator> Adapter;
204
return m_impl.template contains<T, Adapter>(value);
207
template<typename T, typename U, typename V>
208
pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType& value)
210
pair<typename HashTable<T, T, IdentityExtractor<T>, U, V, V>::iterator, bool> p = m_impl.add(value);
211
typename HashSet<T, U, V>::iterator temp = p.first;
212
pair<typename HashSet<T, U, V>::iterator, bool> p2 = make_pair<typename HashSet<T, U, V>::iterator, bool>(temp, p.second);
213
// p2.first = p.first;
214
// p2.second = p.second;
218
template<typename Value, typename HashFunctions, typename Traits>
219
template<typename T, typename HashTranslator>
220
pair<typename HashSet<Value, HashFunctions, Traits>::iterator, bool>
221
HashSet<Value, HashFunctions, Traits>::add(const T& value)
223
typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, HashTranslator> Adapter;
224
pair<typename HashTableType::iterator, bool> p = m_impl.template addPassingHashCode<T, T, Adapter>(value, value);
225
return make_pair<iterator, bool>(p.first, p.second);
228
template<typename T, typename U, typename V>
229
inline void HashSet<T, U, V>::remove(iterator it)
231
if (it.m_impl == m_impl.end())
233
m_impl.checkTableConsistency();
234
m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
237
template<typename T, typename U, typename V>
238
inline void HashSet<T, U, V>::remove(const ValueType& value)
243
template<typename T, typename U, typename V>
244
inline void HashSet<T, U, V>::clear()
249
template<typename ValueType, typename HashTableType>
250
void deleteAllValues(HashTableType& collection)
252
typedef typename HashTableType::const_iterator iterator;
253
iterator end = collection.end();
254
for (iterator it = collection.begin(); it != end; ++it)
258
template<typename T, typename U, typename V>
259
inline void deleteAllValues(const HashSet<T, U, V>& collection)
261
deleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
264
template<typename ValueType, typename HashTableType>
265
void fastDeleteAllValues(HashTableType& collection)
267
typedef typename HashTableType::const_iterator iterator;
268
iterator end = collection.end();
269
for (iterator it = collection.begin(); it != end; ++it)
273
template<typename T, typename U, typename V>
274
inline void fastDeleteAllValues(const HashSet<T, U, V>& collection)
276
fastDeleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl);
279
template<typename T, typename U, typename V, typename W>
280
inline void copyToVector(const HashSet<T, U, V>& collection, W& vector)
282
typedef typename HashSet<T, U, V>::const_iterator iterator;
284
vector.resize(collection.size());
286
iterator it = collection.begin();
287
iterator end = collection.end();
288
for (unsigned i = 0; it != end; ++it, ++i)
296
#endif /* WTF_HashSet_h */