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 "HashTable.h"
28 template<typename PairType
> struct PairFirstExtractor
;
30 template<typename KeyArg
, typename MappedArg
, typename HashArg
= typename DefaultHash
<KeyArg
>::Hash
,
31 typename KeyTraitsArg
= HashTraits
<KeyArg
>, typename MappedTraitsArg
= HashTraits
<MappedArg
> >
32 class HashMap
: public FastAllocBase
{
34 typedef KeyTraitsArg KeyTraits
;
35 typedef MappedTraitsArg MappedTraits
;
36 typedef PairHashTraits
<KeyTraits
, MappedTraits
> ValueTraits
;
39 typedef typename
KeyTraits::TraitType KeyType
;
40 typedef typename
MappedTraits::TraitType MappedType
;
41 typedef typename
ValueTraits::TraitType ValueType
;
44 typedef HashArg HashFunctions
;
46 typedef HashTable
<KeyType
, ValueType
, PairFirstExtractor
<ValueType
>,
47 HashFunctions
, ValueTraits
, KeyTraits
> HashTableType
;
50 typedef HashTableIteratorAdapter
<HashTableType
, ValueType
> iterator
;
51 typedef HashTableConstIteratorAdapter
<HashTableType
, ValueType
> const_iterator
;
59 // iterators iterate over pairs of keys and values
62 const_iterator
begin() const;
63 const_iterator
end() const;
65 iterator
find(const KeyType
&);
66 const_iterator
find(const KeyType
&) const;
67 bool contains(const KeyType
&) const;
68 MappedType
get(const KeyType
&) const;
70 // replaces value but not key if key is already present
71 // return value is a pair of the iterator to the key location,
72 // and a boolean that's true if a new value was actually added
73 pair
<iterator
, bool> set(const KeyType
&, const MappedType
&);
75 // does nothing if key is already present
76 // return value is a pair of the iterator to the key location,
77 // and a boolean that's true if a new value was actually added
78 pair
<iterator
, bool> add(const KeyType
&, const MappedType
&);
80 void remove(const KeyType
&);
81 void remove(iterator
);
84 MappedType
take(const KeyType
&); // efficient combination of get with remove
86 // An alternate version of find() that finds the object by hashing and comparing
87 // with some other type, to avoid the cost of type conversion. HashTranslator
88 // must have the following function members:
89 // static unsigned hash(const T&);
90 // static bool equal(const ValueType&, const T&);
91 template<typename T
, typename HashTranslator
> iterator
find(const T
&);
92 template<typename T
, typename HashTranslator
> const_iterator
find(const T
&) const;
93 template<typename T
, typename HashTranslator
> bool contains(const T
&) const;
95 // An alternate version of add() that finds the object by hashing and comparing
96 // with some other type, to avoid the cost of type conversion if the object is already
97 // in the table. HashTranslator must have the following function members:
98 // static unsigned hash(const T&);
99 // static bool equal(const ValueType&, const T&);
100 // static translate(ValueType&, const T&, unsigned hashCode);
101 template<typename T
, typename HashTranslator
> pair
<iterator
, bool> add(const T
&, const MappedType
&);
103 void checkConsistency() const;
106 pair
<iterator
, bool> inlineAdd(const KeyType
&, const MappedType
&);
108 HashTableType m_impl
;
111 template<typename PairType
> struct PairFirstExtractor
{
112 static const typename
PairType::first_type
& extract(const PairType
& p
) { return p
.first
; }
115 template<typename ValueType
, typename ValueTraits
, typename HashFunctions
>
116 struct HashMapTranslator
{
117 typedef typename
ValueType::first_type KeyType
;
118 typedef typename
ValueType::second_type MappedType
;
120 static unsigned hash(const KeyType
& key
) { return HashFunctions::hash(key
); }
121 static bool equal(const KeyType
& a
, const KeyType
& b
) { return HashFunctions::equal(a
, b
); }
122 static void translate(ValueType
& location
, const KeyType
& key
, const MappedType
& mapped
)
124 location
.first
= key
;
125 location
.second
= mapped
;
129 template<typename ValueType
, typename ValueTraits
, typename T
, typename Translator
>
130 struct HashMapTranslatorAdapter
{
131 typedef typename
ValueType::first_type KeyType
;
132 typedef typename
ValueType::second_type MappedType
;
134 static unsigned hash(const T
& key
) { return Translator::hash(key
); }
135 static bool equal(const KeyType
& a
, const T
& b
) { return Translator::equal(a
, b
); }
136 static void translate(ValueType
& location
, const T
& key
, const MappedType
&, unsigned hashCode
)
138 Translator::translate(location
.first
, key
, hashCode
);
142 template<typename T
, typename U
, typename V
, typename W
, typename X
>
143 inline void HashMap
<T
, U
, V
, W
, X
>::swap(HashMap
& other
)
145 m_impl
.swap(other
.m_impl
);
148 template<typename T
, typename U
, typename V
, typename W
, typename X
>
149 inline int HashMap
<T
, U
, V
, W
, X
>::size() const
151 return m_impl
.size();
154 template<typename T
, typename U
, typename V
, typename W
, typename X
>
155 inline int HashMap
<T
, U
, V
, W
, X
>::capacity() const
157 return m_impl
.capacity();
160 template<typename T
, typename U
, typename V
, typename W
, typename X
>
161 inline bool HashMap
<T
, U
, V
, W
, X
>::isEmpty() const
163 return m_impl
.isEmpty();
166 template<typename T
, typename U
, typename V
, typename W
, typename X
>
167 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::begin()
169 return m_impl
.begin();
172 template<typename T
, typename U
, typename V
, typename W
, typename X
>
173 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::end()
178 template<typename T
, typename U
, typename V
, typename W
, typename X
>
179 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::begin() const
181 return m_impl
.begin();
184 template<typename T
, typename U
, typename V
, typename W
, typename X
>
185 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::end() const
190 template<typename T
, typename U
, typename V
, typename W
, typename X
>
191 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::find(const KeyType
& key
)
193 return m_impl
.find(key
);
196 template<typename T
, typename U
, typename V
, typename W
, typename X
>
197 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::find(const KeyType
& key
) const
199 return m_impl
.find(key
);
202 template<typename T
, typename U
, typename V
, typename W
, typename X
>
203 inline bool HashMap
<T
, U
, V
, W
, X
>::contains(const KeyType
& key
) const
205 return m_impl
.contains(key
);
208 template<typename T
, typename U
, typename V
, typename W
, typename X
>
209 template<typename TYPE
, typename HashTranslator
>
210 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator
211 HashMap
<T
, U
, V
, W
, X
>::find(const TYPE
& value
)
213 typedef HashMapTranslatorAdapter
<ValueType
, ValueTraits
, TYPE
, HashTranslator
> Adapter
;
214 return m_impl
.template find
<TYPE
, Adapter
>(value
);
217 template<typename T
, typename U
, typename V
, typename W
, typename X
>
218 template<typename TYPE
, typename HashTranslator
>
219 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator
220 HashMap
<T
, U
, V
, W
, X
>::find(const TYPE
& value
) const
222 typedef HashMapTranslatorAdapter
<ValueType
, ValueTraits
, TYPE
, HashTranslator
> Adapter
;
223 return m_impl
.template find
<TYPE
, Adapter
>(value
);
226 template<typename T
, typename U
, typename V
, typename W
, typename X
>
227 template<typename TYPE
, typename HashTranslator
>
229 HashMap
<T
, U
, V
, W
, X
>::contains(const TYPE
& value
) const
231 typedef HashMapTranslatorAdapter
<ValueType
, ValueTraits
, TYPE
, HashTranslator
> Adapter
;
232 return m_impl
.template contains
<TYPE
, Adapter
>(value
);
235 template<typename T
, typename U
, typename V
, typename W
, typename X
>
236 inline pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
237 HashMap
<T
, U
, V
, W
, X
>::inlineAdd(const KeyType
& key
, const MappedType
& mapped
)
239 typedef HashMapTranslator
<ValueType
, ValueTraits
, HashFunctions
> TranslatorType
;
240 return m_impl
.template add
<KeyType
, MappedType
, TranslatorType
>(key
, mapped
);
243 template<typename T
, typename U
, typename V
, typename W
, typename X
>
244 pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
245 HashMap
<T
, U
, V
, W
, X
>::set(const KeyType
& key
, const MappedType
& mapped
)
247 pair
<iterator
, bool> result
= inlineAdd(key
, mapped
);
248 if (!result
.second
) {
249 // add call above didn't change anything, so set the mapped value
250 result
.first
->second
= mapped
;
255 template<typename T
, typename U
, typename V
, typename W
, typename X
>
256 template<typename TYPE
, typename HashTranslator
>
257 pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
258 HashMap
<T
, U
, V
, W
, X
>::add(const TYPE
& key
, const MappedType
& value
)
260 typedef HashMapTranslatorAdapter
<ValueType
, ValueTraits
, TYPE
, HashTranslator
> Adapter
;
261 return m_impl
.template addPassingHashCode
<TYPE
, MappedType
, Adapter
>(key
, value
);
264 template<typename T
, typename U
, typename V
, typename W
, typename X
>
265 pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
266 HashMap
<T
, U
, V
, W
, X
>::add(const KeyType
& key
, const MappedType
& mapped
)
268 return inlineAdd(key
, mapped
);
271 template<typename T
, typename U
, typename V
, typename W
, typename MappedTraits
>
272 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType
273 HashMap
<T
, U
, V
, W
, MappedTraits
>::get(const KeyType
& key
) const
275 ValueType
* entry
= const_cast<HashTableType
&>(m_impl
).lookup(key
);
277 return MappedTraits::emptyValue();
278 return entry
->second
;
281 template<typename T
, typename U
, typename V
, typename W
, typename X
>
282 inline void HashMap
<T
, U
, V
, W
, X
>::remove(iterator it
)
284 if (it
.m_impl
== m_impl
.end())
286 m_impl
.internalCheckTableConsistency();
287 m_impl
.removeWithoutEntryConsistencyCheck(it
.m_impl
);
290 template<typename T
, typename U
, typename V
, typename W
, typename X
>
291 inline void HashMap
<T
, U
, V
, W
, X
>::remove(const KeyType
& key
)
296 template<typename T
, typename U
, typename V
, typename W
, typename X
>
297 inline void HashMap
<T
, U
, V
, W
, X
>::clear()
302 template<typename T
, typename U
, typename V
, typename W
, typename MappedTraits
>
303 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType
304 HashMap
<T
, U
, V
, W
, MappedTraits
>::take(const KeyType
& key
)
306 // This can probably be made more efficient to avoid ref/deref churn.
307 iterator it
= find(key
);
309 return MappedTraits::emptyValue();
310 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType result
= it
->second
;
315 template<typename T
, typename U
, typename V
, typename W
, typename X
>
316 inline void HashMap
<T
, U
, V
, W
, X
>::checkConsistency() const
318 m_impl
.checkTableConsistency();
322 template<typename T
, typename U
, typename V
, typename W
, typename X
>
323 bool operator==(const HashMap
<T
, U
, V
, W
, X
>& a
, const HashMap
<T
, U
, V
, W
, X
>& b
)
325 if (a
.size() != b
.size())
328 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator const_iterator
;
330 const_iterator end
= a
.end();
331 const_iterator notFound
= b
.end();
332 for (const_iterator it
= a
.begin(); it
!= end
; ++it
) {
333 const_iterator bPos
= b
.find(it
->first
);
334 if (bPos
== notFound
|| it
->second
!= bPos
->second
)
341 template<typename T
, typename U
, typename V
, typename W
, typename X
>
342 inline bool operator!=(const HashMap
<T
, U
, V
, W
, X
>& a
, const HashMap
<T
, U
, V
, W
, X
>& b
)
347 template<typename MappedType
, typename HashTableType
>
348 void deleteAllPairSeconds(HashTableType
& collection
)
350 typedef typename
HashTableType::const_iterator iterator
;
351 iterator end
= collection
.end();
352 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
356 template<typename T
, typename U
, typename V
, typename W
, typename X
>
357 inline void deleteAllValues(const HashMap
<T
, U
, V
, W
, X
>& collection
)
359 deleteAllPairSeconds
<typename HashMap
<T
, U
, V
, W
, X
>::MappedType
>(collection
);
362 template<typename KeyType
, typename HashTableType
>
363 void deleteAllPairFirsts(HashTableType
& collection
)
365 typedef typename
HashTableType::const_iterator iterator
;
366 iterator end
= collection
.end();
367 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
371 template<typename T
, typename U
, typename V
, typename W
, typename X
>
372 inline void deleteAllKeys(const HashMap
<T
, U
, V
, W
, X
>& collection
)
374 deleteAllPairFirsts
<typename HashMap
<T
, U
, V
, W
, X
>::KeyType
>(collection
);
377 template<typename T
, typename U
, typename V
, typename W
, typename X
, typename Y
>
378 inline void copyKeysToVector(const HashMap
<T
, U
, V
, W
, X
>& collection
, Y
& vector
)
380 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator::Keys iterator
;
382 vector
.resize(collection
.size());
384 iterator it
= collection
.begin().keys();
385 iterator end
= collection
.end().keys();
386 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
390 template<typename T
, typename U
, typename V
, typename W
, typename X
, typename Y
>
391 inline void copyValuesToVector(const HashMap
<T
, U
, V
, W
, X
>& collection
, Y
& vector
)
393 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator::Values iterator
;
395 vector
.resize(collection
.size());
397 iterator it
= collection
.begin().values();
398 iterator end
= collection
.end().values();
399 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
407 #include "RefPtrHashMap.h"
409 #endif /* WTF_HashMap_h */