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