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
> >
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
87 pair
<iterator
, bool> inlineAdd(const KeyType
&, const MappedType
&);
92 template<typename PairType
> struct PairFirstExtractor
{
93 static const typename
PairType::first_type
& extract(const PairType
& p
) { return p
.first
; }
96 template<typename ValueType
, typename ValueTraits
, typename HashFunctions
>
97 struct HashMapTranslator
{
98 typedef typename
ValueType::first_type KeyType
;
99 typedef typename
ValueType::second_type MappedType
;
101 static unsigned hash(const KeyType
& key
) { return HashFunctions::hash(key
); }
102 static bool equal(const KeyType
& a
, const KeyType
& b
) { return HashFunctions::equal(a
, b
); }
103 static void translate(ValueType
& location
, const KeyType
& key
, const MappedType
& mapped
)
105 location
.first
= key
;
106 location
.second
= mapped
;
110 template<typename T
, typename U
, typename V
, typename W
, typename X
>
111 inline void HashMap
<T
, U
, V
, W
, X
>::swap(HashMap
& other
)
113 m_impl
.swap(other
.m_impl
);
116 template<typename T
, typename U
, typename V
, typename W
, typename X
>
117 inline int HashMap
<T
, U
, V
, W
, X
>::size() const
119 return m_impl
.size();
122 template<typename T
, typename U
, typename V
, typename W
, typename X
>
123 inline int HashMap
<T
, U
, V
, W
, X
>::capacity() const
125 return m_impl
.capacity();
128 template<typename T
, typename U
, typename V
, typename W
, typename X
>
129 inline bool HashMap
<T
, U
, V
, W
, X
>::isEmpty() const
131 return m_impl
.isEmpty();
134 template<typename T
, typename U
, typename V
, typename W
, typename X
>
135 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::begin()
137 return m_impl
.begin();
140 template<typename T
, typename U
, typename V
, typename W
, typename X
>
141 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::end()
146 template<typename T
, typename U
, typename V
, typename W
, typename X
>
147 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::begin() const
149 return m_impl
.begin();
152 template<typename T
, typename U
, typename V
, typename W
, typename X
>
153 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::end() const
158 template<typename T
, typename U
, typename V
, typename W
, typename X
>
159 inline typename HashMap
<T
, U
, V
, W
, X
>::iterator HashMap
<T
, U
, V
, W
, X
>::find(const KeyType
& key
)
161 return m_impl
.find(key
);
164 template<typename T
, typename U
, typename V
, typename W
, typename X
>
165 inline typename HashMap
<T
, U
, V
, W
, X
>::const_iterator HashMap
<T
, U
, V
, W
, X
>::find(const KeyType
& key
) const
167 return m_impl
.find(key
);
170 template<typename T
, typename U
, typename V
, typename W
, typename X
>
171 inline bool HashMap
<T
, U
, V
, W
, X
>::contains(const KeyType
& key
) const
173 return m_impl
.contains(key
);
176 template<typename T
, typename U
, typename V
, typename W
, typename X
>
177 inline pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
178 HashMap
<T
, U
, V
, W
, X
>::inlineAdd(const KeyType
& key
, const MappedType
& mapped
)
180 typedef HashMapTranslator
<ValueType
, ValueTraits
, HashFunctions
> TranslatorType
;
181 return m_impl
.template add
<KeyType
, MappedType
, TranslatorType
>(key
, mapped
);
184 template<typename T
, typename U
, typename V
, typename W
, typename X
>
185 pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
186 HashMap
<T
, U
, V
, W
, X
>::set(const KeyType
& key
, const MappedType
& mapped
)
188 pair
<iterator
, bool> result
= inlineAdd(key
, mapped
);
189 if (!result
.second
) {
190 // add call above didn't change anything, so set the mapped value
191 result
.first
->second
= mapped
;
196 template<typename T
, typename U
, typename V
, typename W
, typename X
>
197 pair
<typename HashMap
<T
, U
, V
, W
, X
>::iterator
, bool>
198 HashMap
<T
, U
, V
, W
, X
>::add(const KeyType
& key
, const MappedType
& mapped
)
200 return inlineAdd(key
, mapped
);
203 template<typename T
, typename U
, typename V
, typename W
, typename MappedTraits
>
204 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType
205 HashMap
<T
, U
, V
, W
, MappedTraits
>::get(const KeyType
& key
) const
207 ValueType
* entry
= const_cast<HashTableType
&>(m_impl
).lookup(key
);
209 return MappedTraits::emptyValue();
210 return entry
->second
;
213 template<typename T
, typename U
, typename V
, typename W
, typename X
>
214 inline void HashMap
<T
, U
, V
, W
, X
>::remove(iterator it
)
216 if (it
.m_impl
== m_impl
.end())
218 m_impl
.checkTableConsistency();
219 m_impl
.removeWithoutEntryConsistencyCheck(it
.m_impl
);
222 template<typename T
, typename U
, typename V
, typename W
, typename X
>
223 inline void HashMap
<T
, U
, V
, W
, X
>::remove(const KeyType
& key
)
228 template<typename T
, typename U
, typename V
, typename W
, typename X
>
229 inline void HashMap
<T
, U
, V
, W
, X
>::clear()
234 template<typename T
, typename U
, typename V
, typename W
, typename MappedTraits
>
235 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType
236 HashMap
<T
, U
, V
, W
, MappedTraits
>::take(const KeyType
& key
)
238 // This can probably be made more efficient to avoid ref/deref churn.
239 iterator it
= find(key
);
241 return MappedTraits::emptyValue();
242 typename HashMap
<T
, U
, V
, W
, MappedTraits
>::MappedType result
= it
->second
;
247 template<typename T
, typename U
, typename V
, typename W
, typename X
>
248 bool operator==(const HashMap
<T
, U
, V
, W
, X
>& a
, const HashMap
<T
, U
, V
, W
, X
>& b
)
250 if (a
.size() != b
.size())
253 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator const_iterator
;
255 const_iterator end
= a
.end();
256 const_iterator notFound
= b
.end();
257 for (const_iterator it
= a
.begin(); it
!= end
; ++it
) {
258 const_iterator bPos
= b
.find(it
->first
);
259 if (bPos
== notFound
|| it
->second
!= bPos
->second
)
266 template<typename T
, typename U
, typename V
, typename W
, typename X
>
267 inline bool operator!=(const HashMap
<T
, U
, V
, W
, X
>& a
, const HashMap
<T
, U
, V
, W
, X
>& b
)
272 template<typename MappedType
, typename HashTableType
>
273 void deleteAllPairSeconds(HashTableType
& collection
)
275 typedef typename
HashTableType::const_iterator iterator
;
276 iterator end
= collection
.end();
277 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
281 template<typename T
, typename U
, typename V
, typename W
, typename X
>
282 inline void deleteAllValues(const HashMap
<T
, U
, V
, W
, X
>& collection
)
284 deleteAllPairSeconds
<typename HashMap
<T
, U
, V
, W
, X
>::MappedType
>(collection
);
287 template<typename KeyType
, typename HashTableType
>
288 void deleteAllPairFirsts(HashTableType
& collection
)
290 typedef typename
HashTableType::const_iterator iterator
;
291 iterator end
= collection
.end();
292 for (iterator it
= collection
.begin(); it
!= end
; ++it
)
296 template<typename T
, typename U
, typename V
, typename W
, typename X
>
297 inline void deleteAllKeys(const HashMap
<T
, U
, V
, W
, X
>& collection
)
299 deleteAllPairFirsts
<typename HashMap
<T
, U
, V
, W
, X
>::KeyType
>(collection
);
302 template<typename T
, typename U
, typename V
, typename W
, typename X
, typename Y
>
303 inline void copyKeysToVector(const HashMap
<T
, U
, V
, W
, X
>& collection
, Y
& vector
)
305 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator::Keys iterator
;
307 vector
.resize(collection
.size());
309 iterator it
= collection
.begin().keys();
310 iterator end
= collection
.end().keys();
311 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
315 template<typename T
, typename U
, typename V
, typename W
, typename X
, typename Y
>
316 inline void copyValuesToVector(const HashMap
<T
, U
, V
, W
, X
>& collection
, Y
& vector
)
318 typedef typename HashMap
<T
, U
, V
, W
, X
>::const_iterator::Values iterator
;
320 vector
.resize(collection
.size());
322 iterator it
= collection
.begin().values();
323 iterator end
= collection
.end().values();
324 for (unsigned i
= 0; it
!= end
; ++it
, ++i
)
332 #include "RefPtrHashMap.h"
334 #endif /* WTF_HashMap_h */