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 function members:
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 return m_impl
.add(value
);
213 template<typename Value
, typename HashFunctions
, typename Traits
>
214 template<typename T
, typename HashTranslator
>
215 pair
<typename HashSet
<Value
, HashFunctions
, Traits
>::iterator
, bool>
216 HashSet
<Value
, HashFunctions
, Traits
>::add(const T
& value
)
218 typedef HashSetTranslatorAdapter
<ValueType
, ValueTraits
, T
, HashTranslator
> Adapter
;
219 return m_impl
.template addPassingHashCode
<T
, T
, Adapter
>(value
, value
);
222 template<typename T
, typename U
, typename V
>
223 inline void HashSet
<T
, U
, V
>::remove(iterator it
)
225 if (it
.m_impl
== m_impl
.end())
227 m_impl
.internalCheckTableConsistency();
228 m_impl
.removeWithoutEntryConsistencyCheck(it
.m_impl
);
231 template<typename T
, typename U
, typename V
>
232 inline void HashSet
<T
, U
, V
>::remove(const ValueType
& value
)
237 template<typename T
, typename U
, typename V
>
238 inline void HashSet
<T
, U
, V
>::clear()
243 template<typename ValueType
, typename HashTableType
>
244 void deleteAllValues(HashTableType
& collection
)
246 typedef typename
HashTableType::const_iterator iterator
;
247 iterator end
= collection
.end();
248 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
252 template<typename T
, typename U
, typename V
>
253 inline void deleteAllValues(const HashSet
<T
, U
, V
>& collection
)
255 deleteAllValues
<typename HashSet
<T
, U
, V
>::ValueType
>(collection
.m_impl
);
258 template<typename ValueType
, typename HashTableType
>
259 void fastDeleteAllValues(HashTableType
& collection
)
261 typedef typename
HashTableType::const_iterator iterator
;
262 iterator end
= collection
.end();
263 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
267 template<typename T
, typename U
, typename V
>
268 inline void fastDeleteAllValues(const HashSet
<T
, U
, V
>& collection
)
270 fastDeleteAllValues
<typename HashSet
<T
, U
, V
>::ValueType
>(collection
.m_impl
);
273 template<typename T
, typename U
, typename V
, typename W
>
274 inline void copyToVector(const HashSet
<T
, U
, V
>& collection
, W
& vector
)
276 typedef typename HashSet
<T
, U
, V
>::const_iterator iterator
;
278 vector
.resize(collection
.size());
280 iterator it
= collection
.begin();
281 iterator end
= collection
.end();
282 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
290 #endif /* WTF_HashSet_h */