source: webkit/trunk/JavaScriptCore/wtf/HashMap.h@ 35419

Last change on this file since 35419 was 32650, checked in by Darin Adler, 17 years ago

2008-04-28 Darin Adler <Darin Adler>

Reviewed by Adam.

  • make sure RefPtr's default hash doesn't ref/deref when computing the hash
  • remove remnants of the hash table storage type optimization
  • wtf/HashFunctions.h: Used "using" to get the hash and equal functions from PtrHash<P*> into PtrHash<RefPtr<P>>.
  • wtf/HashMap.h: Replaced uses of PairBaseHashTraits with PairHashTraits. Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Took out unnnecessary typecasts. Removed use of RefCounter.
  • wtf/HashSet.h: Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Removed unneeded template arguents from HashSetTranslatorAdapter. Eliminated unneeded HashSetTranslator template.
  • wtf/HashTable.h: Tweaked formatting. Removed NeedsRef, RefCounterBase, RefCounter, HashTableRefCounterBase, HashTableRefCounter, and Assigner class templates.
  • wtf/HashTraits.h: Removed StorageTraits, needsRef, PairBaseHashTraits, and HashKeyStorageTraits.
  • wtf/RefPtrHashMap.h: Made all the same fixes as in HashMap. Also made the corresponding changes to RefPtrHashMapRawKeyTranslator.
  • Property svn:eol-style set to native
File size: 11.8 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_HashMap_h
23#define WTF_HashMap_h
24
25#include "HashTable.h"
26
27namespace WTF {
28
29 template<typename PairType> struct PairFirstExtractor;
30
31 template<typename KeyArg, typename MappedArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
32 typename KeyTraitsArg = HashTraits<KeyArg>, typename MappedTraitsArg = HashTraits<MappedArg> >
33 class HashMap {
34 private:
35 typedef KeyTraitsArg KeyTraits;
36 typedef MappedTraitsArg MappedTraits;
37 typedef PairHashTraits<KeyTraits, MappedTraits> ValueTraits;
38
39 public:
40 typedef typename KeyTraits::TraitType KeyType;
41 typedef typename MappedTraits::TraitType MappedType;
42 typedef typename ValueTraits::TraitType ValueType;
43
44 private:
45 typedef HashArg HashFunctions;
46
47 typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>,
48 HashFunctions, ValueTraits, KeyTraits> HashTableType;
49
50 public:
51 typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
52 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
53
54 void swap(HashMap&);
55
56 int size() const;
57 int capacity() const;
58 bool isEmpty() const;
59
60 // iterators iterate over pairs of keys and values
61 iterator begin();
62 iterator end();
63 const_iterator begin() const;
64 const_iterator end() const;
65
66 iterator find(const KeyType&);
67 const_iterator find(const KeyType&) const;
68 bool contains(const KeyType&) const;
69 MappedType get(const KeyType&) const;
70
71 // replaces value but not key if key is already present
72 // return value is a pair of the iterator to the key location,
73 // and a boolean that's true if a new value was actually added
74 pair<iterator, bool> set(const KeyType&, const MappedType&);
75
76 // does nothing if key is already present
77 // return value is a pair of the iterator to the key location,
78 // and a boolean that's true if a new value was actually added
79 pair<iterator, bool> add(const KeyType&, const MappedType&);
80
81 void remove(const KeyType&);
82 void remove(iterator);
83 void clear();
84
85 MappedType take(const KeyType&); // efficient combination of get with remove
86
87 private:
88 pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&);
89
90 HashTableType m_impl;
91 };
92
93 template<typename PairType> struct PairFirstExtractor {
94 static const typename PairType::first_type& extract(const PairType& p) { return p.first; }
95 };
96
97 template<typename ValueType, typename ValueTraits, typename HashFunctions>
98 struct HashMapTranslator {
99 typedef typename ValueType::first_type KeyType;
100 typedef typename ValueType::second_type MappedType;
101
102 static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); }
103 static bool equal(const KeyType& a, const KeyType& b) { return HashFunctions::equal(a, b); }
104 static void translate(ValueType& location, const KeyType& key, const MappedType& mapped)
105 {
106 location.first = key;
107 location.second = mapped;
108 }
109 };
110
111 template<typename T, typename U, typename V, typename W, typename X>
112 inline void HashMap<T, U, V, W, X>::swap(HashMap& other)
113 {
114 m_impl.swap(other.m_impl);
115 }
116
117 template<typename T, typename U, typename V, typename W, typename X>
118 inline int HashMap<T, U, V, W, X>::size() const
119 {
120 return m_impl.size();
121 }
122
123 template<typename T, typename U, typename V, typename W, typename X>
124 inline int HashMap<T, U, V, W, X>::capacity() const
125 {
126 return m_impl.capacity();
127 }
128
129 template<typename T, typename U, typename V, typename W, typename X>
130 inline bool HashMap<T, U, V, W, X>::isEmpty() const
131 {
132 return m_impl.isEmpty();
133 }
134
135 template<typename T, typename U, typename V, typename W, typename X>
136 inline typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::begin()
137 {
138 return m_impl.begin();
139 }
140
141 template<typename T, typename U, typename V, typename W, typename X>
142 inline typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::end()
143 {
144 return m_impl.end();
145 }
146
147 template<typename T, typename U, typename V, typename W, typename X>
148 inline typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::begin() const
149 {
150 return m_impl.begin();
151 }
152
153 template<typename T, typename U, typename V, typename W, typename X>
154 inline typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::end() const
155 {
156 return m_impl.end();
157 }
158
159 template<typename T, typename U, typename V, typename W, typename X>
160 inline typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::find(const KeyType& key)
161 {
162 return m_impl.find(key);
163 }
164
165 template<typename T, typename U, typename V, typename W, typename X>
166 inline typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::find(const KeyType& key) const
167 {
168 return m_impl.find(key);
169 }
170
171 template<typename T, typename U, typename V, typename W, typename X>
172 inline bool HashMap<T, U, V, W, X>::contains(const KeyType& key) const
173 {
174 return m_impl.contains(key);
175 }
176
177 template<typename T, typename U, typename V, typename W, typename X>
178 inline pair<typename HashMap<T, U, V, W, X>::iterator, bool>
179 HashMap<T, U, V, W, X>::inlineAdd(const KeyType& key, const MappedType& mapped)
180 {
181 typedef HashMapTranslator<ValueType, ValueTraits, HashFunctions> TranslatorType;
182 return m_impl.template add<KeyType, MappedType, TranslatorType>(key, mapped);
183 }
184
185 template<typename T, typename U, typename V, typename W, typename X>
186 pair<typename HashMap<T, U, V, W, X>::iterator, bool>
187 HashMap<T, U, V, W, X>::set(const KeyType& key, const MappedType& mapped)
188 {
189 pair<iterator, bool> result = inlineAdd(key, mapped);
190 if (!result.second) {
191 // add call above didn't change anything, so set the mapped value
192 result.first->second = mapped;
193 }
194 return result;
195 }
196
197 template<typename T, typename U, typename V, typename W, typename X>
198 pair<typename HashMap<T, U, V, W, X>::iterator, bool>
199 HashMap<T, U, V, W, X>::add(const KeyType& key, const MappedType& mapped)
200 {
201 return inlineAdd(key, mapped);
202 }
203
204 template<typename T, typename U, typename V, typename W, typename MappedTraits>
205 typename HashMap<T, U, V, W, MappedTraits>::MappedType
206 HashMap<T, U, V, W, MappedTraits>::get(const KeyType& key) const
207 {
208 ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
209 if (!entry)
210 return MappedTraits::emptyValue();
211 return entry->second;
212 }
213
214 template<typename T, typename U, typename V, typename W, typename X>
215 inline void HashMap<T, U, V, W, X>::remove(iterator it)
216 {
217 if (it.m_impl == m_impl.end())
218 return;
219 m_impl.checkTableConsistency();
220 m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
221 }
222
223 template<typename T, typename U, typename V, typename W, typename X>
224 inline void HashMap<T, U, V, W, X>::remove(const KeyType& key)
225 {
226 remove(find(key));
227 }
228
229 template<typename T, typename U, typename V, typename W, typename X>
230 inline void HashMap<T, U, V, W, X>::clear()
231 {
232 m_impl.clear();
233 }
234
235 template<typename T, typename U, typename V, typename W, typename MappedTraits>
236 typename HashMap<T, U, V, W, MappedTraits>::MappedType
237 HashMap<T, U, V, W, MappedTraits>::take(const KeyType& key)
238 {
239 // This can probably be made more efficient to avoid ref/deref churn.
240 iterator it = find(key);
241 if (it == end())
242 return MappedTraits::emptyValue();
243 typename HashMap<T, U, V, W, MappedTraits>::MappedType result = it->second;
244 remove(it);
245 return result;
246 }
247
248 template<typename T, typename U, typename V, typename W, typename X>
249 bool operator==(const HashMap<T, U, V, W, X>& a, const HashMap<T, U, V, W, X>& b)
250 {
251 if (a.size() != b.size())
252 return false;
253
254 typedef typename HashMap<T, U, V, W, X>::const_iterator const_iterator;
255
256 const_iterator end = a.end();
257 const_iterator notFound = b.end();
258 for (const_iterator it = a.begin(); it != end; ++it) {
259 const_iterator bPos = b.find(it->first);
260 if (bPos == notFound || it->second != bPos->second)
261 return false;
262 }
263
264 return true;
265 }
266
267 template<typename T, typename U, typename V, typename W, typename X>
268 inline bool operator!=(const HashMap<T, U, V, W, X>& a, const HashMap<T, U, V, W, X>& b)
269 {
270 return !(a == b);
271 }
272
273 template<typename MappedType, typename HashTableType>
274 void deleteAllPairSeconds(HashTableType& collection)
275 {
276 typedef typename HashTableType::const_iterator iterator;
277 iterator end = collection.end();
278 for (iterator it = collection.begin(); it != end; ++it)
279 delete it->second;
280 }
281
282 template<typename T, typename U, typename V, typename W, typename X>
283 inline void deleteAllValues(const HashMap<T, U, V, W, X>& collection)
284 {
285 deleteAllPairSeconds<typename HashMap<T, U, V, W, X>::MappedType>(collection);
286 }
287
288 template<typename KeyType, typename HashTableType>
289 void deleteAllPairFirsts(HashTableType& collection)
290 {
291 typedef typename HashTableType::const_iterator iterator;
292 iterator end = collection.end();
293 for (iterator it = collection.begin(); it != end; ++it)
294 delete it->first;
295 }
296
297 template<typename T, typename U, typename V, typename W, typename X>
298 inline void deleteAllKeys(const HashMap<T, U, V, W, X>& collection)
299 {
300 deleteAllPairFirsts<typename HashMap<T, U, V, W, X>::KeyType>(collection);
301 }
302
303 template<typename T, typename U, typename V, typename W, typename X, typename Y>
304 inline void copyKeysToVector(const HashMap<T, U, V, W, X>& collection, Y& vector)
305 {
306 typedef typename HashMap<T, U, V, W, X>::const_iterator::Keys iterator;
307
308 vector.resize(collection.size());
309
310 iterator it = collection.begin().keys();
311 iterator end = collection.end().keys();
312 for (unsigned i = 0; it != end; ++it, ++i)
313 vector[i] = *it;
314 }
315
316 template<typename T, typename U, typename V, typename W, typename X, typename Y>
317 inline void copyValuesToVector(const HashMap<T, U, V, W, X>& collection, Y& vector)
318 {
319 typedef typename HashMap<T, U, V, W, X>::const_iterator::Values iterator;
320
321 vector.resize(collection.size());
322
323 iterator it = collection.begin().values();
324 iterator end = collection.end().values();
325 for (unsigned i = 0; it != end; ++it, ++i)
326 vector[i] = *it;
327 }
328
329} // namespace WTF
330
331using WTF::HashMap;
332
333#include "RefPtrHashMap.h"
334
335#endif /* WTF_HashMap_h */
Note: See TracBrowser for help on using the repository browser.