OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. | |
3 * | |
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. | |
8 * | |
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. | |
13 * | |
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. | |
18 * | |
19 */ | |
20 | |
21 #ifndef WTF_HashSet_h | |
22 #define WTF_HashSet_h | |
23 | |
24 #include <wtf/FastAllocBase.h> | |
25 #include <wtf/HashTable.h> | |
26 | |
27 namespace WTF { | |
28 | |
29 struct IdentityExtractor; | |
30 | |
31 template<typename Value, typename HashFunctions, typename Traits> class Hash
Set; | |
32 template<typename Value, typename HashFunctions, typename Traits> | |
33 void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&); | |
34 | |
35 template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg
>::Hash, | |
36 typename TraitsArg = HashTraits<ValueArg> > class HashSet { | |
37 WTF_MAKE_FAST_ALLOCATED; | |
38 private: | |
39 typedef HashArg HashFunctions; | |
40 typedef TraitsArg ValueTraits; | |
41 | |
42 public: | |
43 typedef typename ValueTraits::TraitType ValueType; | |
44 | |
45 private: | |
46 typedef HashTable<ValueType, ValueType, IdentityExtractor, | |
47 HashFunctions, ValueTraits, ValueTraits> HashTableType; | |
48 | |
49 public: | |
50 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> iterator
; | |
51 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_it
erator; | |
52 typedef typename HashTableType::AddResult AddResult; | |
53 | |
54 void swap(HashSet&); | |
55 | |
56 int size() const; | |
57 int capacity() const; | |
58 bool isEmpty() const; | |
59 | |
60 iterator begin() const; | |
61 iterator end() const; | |
62 | |
63 iterator find(const ValueType&) const; | |
64 bool contains(const ValueType&) const; | |
65 | |
66 // An alternate version of find() that finds the object by hashing and c
omparing | |
67 // with some other type, to avoid the cost of type conversion. HashTrans
lator | |
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&) co
nst; | |
74 template<typename T, typename HashTranslator> bool contains(const T&) co
nst; | |
75 | |
76 // The return value is a pair of an interator to the new value's locatio
n, | |
77 // and a bool that is true if an new entry was added. | |
78 AddResult add(const ValueType&); | |
79 | |
80 // An alternate version of add() that finds the object by hashing and co
mparing | |
81 // with some other type, to avoid the cost of type conversion if the obj
ect 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&); | |
89 | |
90 void remove(const ValueType&); | |
91 void remove(iterator); | |
92 void clear(); | |
93 | |
94 static bool isValidValue(const ValueType&); | |
95 | |
96 private: | |
97 friend void deleteAllValues<>(const HashSet&); | |
98 | |
99 HashTableType m_impl; | |
100 }; | |
101 | |
102 struct IdentityExtractor { | |
103 template<typename T> static const T& extract(const T& t) { return t; } | |
104 }; | |
105 | |
106 template<typename Translator> | |
107 struct HashSetTranslatorAdapter { | |
108 template<typename T> static unsigned hash(const T& key) { return Transla
tor::hash(key); } | |
109 template<typename T, typename U> static bool equal(const T& a, const U&
b) { return Translator::equal(a, b); } | |
110 template<typename T, typename U> static void translate(T& location, cons
t U& key, const U&, unsigned hashCode) | |
111 { | |
112 Translator::translate(location, key, hashCode); | |
113 } | |
114 }; | |
115 | |
116 template<typename T, typename U, typename V> | |
117 inline void HashSet<T, U, V>::swap(HashSet& other) | |
118 { | |
119 m_impl.swap(other.m_impl); | |
120 } | |
121 | |
122 template<typename T, typename U, typename V> | |
123 inline int HashSet<T, U, V>::size() const | |
124 { | |
125 return m_impl.size(); | |
126 } | |
127 | |
128 template<typename T, typename U, typename V> | |
129 inline int HashSet<T, U, V>::capacity() const | |
130 { | |
131 return m_impl.capacity(); | |
132 } | |
133 | |
134 template<typename T, typename U, typename V> | |
135 inline bool HashSet<T, U, V>::isEmpty() const | |
136 { | |
137 return m_impl.isEmpty(); | |
138 } | |
139 | |
140 template<typename T, typename U, typename V> | |
141 inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::begin() const | |
142 { | |
143 return m_impl.begin(); | |
144 } | |
145 | |
146 template<typename T, typename U, typename V> | |
147 inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::end() const | |
148 { | |
149 return m_impl.end(); | |
150 } | |
151 | |
152 template<typename T, typename U, typename V> | |
153 inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const Valu
eType& value) const | |
154 { | |
155 return m_impl.find(value); | |
156 } | |
157 | |
158 template<typename T, typename U, typename V> | |
159 inline bool HashSet<T, U, V>::contains(const ValueType& value) const | |
160 { | |
161 return m_impl.contains(value); | |
162 } | |
163 | |
164 template<typename Value, typename HashFunctions, typename Traits> | |
165 template<typename T, typename HashTranslator> | |
166 typename HashSet<Value, HashFunctions, Traits>::iterator | |
167 inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const | |
168 { | |
169 return m_impl.template find<HashSetTranslatorAdapter<HashTranslator> >(v
alue); | |
170 } | |
171 | |
172 template<typename Value, typename HashFunctions, typename Traits> | |
173 template<typename T, typename HashTranslator> | |
174 inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value)
const | |
175 { | |
176 return m_impl.template contains<HashSetTranslatorAdapter<HashTranslator>
>(value); | |
177 } | |
178 | |
179 template<typename T, typename U, typename V> | |
180 inline typename HashSet<T, U, V>::AddResult HashSet<T, U, V>::add(const Valu
eType& value) | |
181 { | |
182 return m_impl.add(value); | |
183 } | |
184 | |
185 template<typename Value, typename HashFunctions, typename Traits> | |
186 template<typename T, typename HashTranslator> | |
187 inline typename HashSet<Value, HashFunctions, Traits>::AddResult | |
188 HashSet<Value, HashFunctions, Traits>::add(const T& value) | |
189 { | |
190 return m_impl.template addPassingHashCode<HashSetTranslatorAdapter<HashT
ranslator> >(value, value); | |
191 } | |
192 | |
193 template<typename T, typename U, typename V> | |
194 inline void HashSet<T, U, V>::remove(iterator it) | |
195 { | |
196 if (it.m_impl == m_impl.end()) | |
197 return; | |
198 m_impl.internalCheckTableConsistency(); | |
199 m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); | |
200 } | |
201 | |
202 template<typename T, typename U, typename V> | |
203 inline void HashSet<T, U, V>::remove(const ValueType& value) | |
204 { | |
205 remove(find(value)); | |
206 } | |
207 | |
208 template<typename T, typename U, typename V> | |
209 inline void HashSet<T, U, V>::clear() | |
210 { | |
211 m_impl.clear(); | |
212 } | |
213 | |
214 template<typename T, typename U, typename V> | |
215 inline bool HashSet<T, U, V>::isValidValue(const ValueType& value) | |
216 { | |
217 if (ValueTraits::isDeletedValue(value)) | |
218 return false; | |
219 | |
220 if (HashFunctions::safeToCompareToEmptyOrDeleted) { | |
221 if (value == ValueTraits::emptyValue()) | |
222 return false; | |
223 } else { | |
224 if (isHashTraitsEmptyValue<ValueTraits>(value)) | |
225 return false; | |
226 } | |
227 | |
228 return true; | |
229 } | |
230 | |
231 template<typename ValueType, typename HashTableType> | |
232 void deleteAllValues(HashTableType& collection) | |
233 { | |
234 typedef typename HashTableType::const_iterator iterator; | |
235 iterator end = collection.end(); | |
236 for (iterator it = collection.begin(); it != end; ++it) | |
237 delete *it; | |
238 } | |
239 | |
240 template<typename T, typename U, typename V> | |
241 inline void deleteAllValues(const HashSet<T, U, V>& collection) | |
242 { | |
243 deleteAllValues<typename HashSet<T, U, V>::ValueType>(collection.m_impl)
; | |
244 } | |
245 | |
246 template<typename C, typename W> | |
247 inline void copyToVector(const C& collection, W& vector) | |
248 { | |
249 typedef typename C::const_iterator iterator; | |
250 | |
251 vector.resize(collection.size()); | |
252 | |
253 iterator it = collection.begin(); | |
254 iterator end = collection.end(); | |
255 for (unsigned i = 0; it != end; ++it, ++i) | |
256 vector[i] = *it; | |
257 } | |
258 | |
259 } // namespace WTF | |
260 | |
261 using WTF::HashSet; | |
262 | |
263 #endif /* WTF_HashSet_h */ | |
OLD | NEW |