]>
Commit | Line | Data |
---|---|---|
0508ba2a | 1 | ///////////////////////////////////////////////////////////////////////////// |
df69528b | 2 | // Name: wx/hashmap.h |
0508ba2a MB |
3 | // Purpose: wxHashMap class |
4 | // Author: Mattia Barbon | |
376e1129 | 5 | // Modified by: |
0508ba2a MB |
6 | // Created: 29/01/2002 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Mattia Barbon | |
65571936 | 9 | // Licence: wxWindows licence |
0508ba2a MB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_HASHMAP_H_ | |
13 | #define _WX_HASHMAP_H_ | |
14 | ||
a4f24526 | 15 | #include "wx/string.h" |
092793cb | 16 | #include "wx/wxcrt.h" |
0508ba2a | 17 | |
01871bf6 VZ |
18 | // In wxUSE_STD_CONTAINERS build we prefer to use the standard hash map class |
19 | // but it can be either in non-standard hash_map header (old g++ and some other | |
20 | // STL implementations) or in C++0x standard unordered_map which can in turn be | |
f380544a VZ |
21 | // available either in std::tr1 or std namespace itself |
22 | // | |
23 | // To summarize: if std::unordered_map is available use it, otherwise use tr1 | |
24 | // and finally fall back to non-standard hash_map | |
25 | ||
bdcade0a MB |
26 | #if (defined(HAVE_EXT_HASH_MAP) || defined(HAVE_HASH_MAP)) \ |
27 | && (defined(HAVE_GNU_CXX_HASH_MAP) || defined(HAVE_STD_HASH_MAP)) | |
28 | #define HAVE_STL_HASH_MAP | |
29 | #endif | |
30 | ||
01871bf6 | 31 | #if wxUSE_STD_CONTAINERS && \ |
f380544a VZ |
32 | (defined(HAVE_STD_UNORDERED_MAP) || defined(HAVE_TR1_UNORDERED_MAP)) |
33 | ||
34 | #if defined(HAVE_STD_UNORDERED_MAP) | |
35 | #include <unordered_map> | |
36 | #define WX_HASH_MAP_NAMESPACE std | |
37 | #elif defined(HAVE_TR1_UNORDERED_MAP) | |
38 | #include <tr1/unordered_map> | |
39 | #define WX_HASH_MAP_NAMESPACE std::tr1 | |
40 | #endif | |
41 | ||
42 | #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ | |
43 | typedef WX_HASH_MAP_NAMESPACE::unordered_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME | |
44 | ||
01871bf6 | 45 | #elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP) |
bdcade0a MB |
46 | |
47 | #if defined(HAVE_EXT_HASH_MAP) | |
48 | #include <ext/hash_map> | |
49 | #elif defined(HAVE_HASH_MAP) | |
50 | #include <hash_map> | |
51 | #endif | |
52 | ||
53 | #if defined(HAVE_GNU_CXX_HASH_MAP) | |
54 | #define WX_HASH_MAP_NAMESPACE __gnu_cxx | |
55 | #elif defined(HAVE_STD_HASH_MAP) | |
56 | #define WX_HASH_MAP_NAMESPACE std | |
57 | #endif | |
58 | ||
59 | #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ | |
e786a78b | 60 | typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME |
bdcade0a | 61 | |
01871bf6 | 62 | #else // !wxUSE_STD_CONTAINERS || no std::{hash,unordered}_map class available |
f6a46bb0 VZ |
63 | |
64 | #define wxNEEDS_WX_HASH_MAP | |
65 | ||
7f0586ef JS |
66 | #ifdef __WXWINCE__ |
67 | typedef int ptrdiff_t; | |
68c30476 RR |
68 | #else |
69 | #include <stddef.h> // for ptrdiff_t | |
7f0586ef JS |
70 | #endif |
71 | ||
0508ba2a | 72 | // private |
bddd7a8d | 73 | struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase |
0508ba2a | 74 | { |
872051d8 | 75 | _wxHashTable_NodeBase() : m_next(NULL) {} |
0508ba2a | 76 | |
872051d8 | 77 | _wxHashTable_NodeBase* m_next; |
22f3361e VZ |
78 | |
79 | // Cannot do this: | |
c0c133e1 | 80 | // wxDECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase); |
22f3361e | 81 | // without rewriting the macros, which require a public copy constructor. |
0508ba2a MB |
82 | }; |
83 | ||
84 | // private | |
bddd7a8d | 85 | class WXDLLIMPEXP_BASE _wxHashTableBase2 |
0508ba2a MB |
86 | { |
87 | public: | |
88 | typedef void (*NodeDtor)(_wxHashTable_NodeBase*); | |
376e1129 | 89 | typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*); |
0508ba2a MB |
90 | typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*); |
91 | protected: | |
92 | static _wxHashTable_NodeBase* DummyProcessNode(_wxHashTable_NodeBase* node); | |
93 | static void DeleteNodes( size_t buckets, _wxHashTable_NodeBase** table, | |
94 | NodeDtor dtor ); | |
95 | static _wxHashTable_NodeBase* GetFirstNode( size_t buckets, | |
96 | _wxHashTable_NodeBase** table ) | |
97 | { | |
98 | for( size_t i = 0; i < buckets; ++i ) | |
99 | if( table[i] ) | |
100 | return table[i]; | |
872051d8 | 101 | return NULL; |
0508ba2a MB |
102 | } |
103 | ||
376e1129 | 104 | // as static const unsigned prime_count = 31 but works with all compilers |
0508ba2a | 105 | enum { prime_count = 31 }; |
376e1129 | 106 | static const unsigned long ms_primes[prime_count]; |
0508ba2a | 107 | |
376e1129 | 108 | // returns the first prime in ms_primes greater than n |
0508ba2a MB |
109 | static unsigned long GetNextPrime( unsigned long n ); |
110 | ||
376e1129 VZ |
111 | // returns the first prime in ms_primes smaller than n |
112 | // ( or ms_primes[0] if n is very small ) | |
0508ba2a MB |
113 | static unsigned long GetPreviousPrime( unsigned long n ); |
114 | ||
115 | static void CopyHashTable( _wxHashTable_NodeBase** srcTable, | |
116 | size_t srcBuckets, _wxHashTableBase2* dst, | |
117 | _wxHashTable_NodeBase** dstTable, | |
118 | BucketFromNode func, ProcessNode proc ); | |
119 | ||
2b5f62a0 | 120 | static void** AllocTable( size_t sz ) |
0508ba2a | 121 | { |
2b5f62a0 | 122 | return (void **)calloc(sz, sizeof(void*)); |
0508ba2a | 123 | } |
f5db5af9 MB |
124 | static void FreeTable(void *table) |
125 | { | |
126 | free(table); | |
127 | } | |
0508ba2a MB |
128 | }; |
129 | ||
e4c8592e VZ |
130 | #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T,\ |
131 | PTROPERATOR, CLASSNAME, CLASSEXP, \ | |
132 | SHOULD_GROW, SHOULD_SHRINK ) \ | |
376e1129 | 133 | CLASSEXP CLASSNAME : protected _wxHashTableBase2 \ |
0508ba2a MB |
134 | { \ |
135 | public: \ | |
136 | typedef KEY_T key_type; \ | |
137 | typedef VALUE_T value_type; \ | |
138 | typedef HASH_T hasher; \ | |
139 | typedef KEY_EQ_T key_equal; \ | |
140 | \ | |
141 | typedef size_t size_type; \ | |
142 | typedef ptrdiff_t difference_type; \ | |
143 | typedef value_type* pointer; \ | |
144 | typedef const value_type* const_pointer; \ | |
145 | typedef value_type& reference; \ | |
146 | typedef const value_type& const_reference; \ | |
147 | /* should these be protected? */ \ | |
148 | typedef const KEY_T const_key_type; \ | |
149 | typedef const VALUE_T const_mapped_type; \ | |
bc850f29 | 150 | public: \ |
0508ba2a MB |
151 | typedef KEY_EX_T key_extractor; \ |
152 | typedef CLASSNAME Self; \ | |
bc850f29 | 153 | protected: \ |
872051d8 | 154 | _wxHashTable_NodeBase** m_table; \ |
0508ba2a MB |
155 | size_t m_tableBuckets; \ |
156 | size_t m_items; \ | |
157 | hasher m_hasher; \ | |
158 | key_equal m_equals; \ | |
159 | key_extractor m_getKey; \ | |
bc850f29 | 160 | public: \ |
0508ba2a MB |
161 | struct Node:public _wxHashTable_NodeBase \ |
162 | { \ | |
163 | public: \ | |
164 | Node( const value_type& value ) \ | |
165 | : m_value( value ) {} \ | |
5c33522f | 166 | Node* next() { return static_cast<Node*>(m_next); } \ |
0508ba2a MB |
167 | \ |
168 | value_type m_value; \ | |
169 | }; \ | |
170 | \ | |
bc850f29 | 171 | protected: \ |
0508ba2a MB |
172 | static void DeleteNode( _wxHashTable_NodeBase* node ) \ |
173 | { \ | |
5c33522f | 174 | delete static_cast<Node*>(node); \ |
0508ba2a | 175 | } \ |
bc850f29 | 176 | public: \ |
0508ba2a MB |
177 | /* */ \ |
178 | /* forward iterator */ \ | |
179 | /* */ \ | |
e1688485 | 180 | CLASSEXP Iterator \ |
0508ba2a | 181 | { \ |
e1688485 | 182 | public: \ |
0508ba2a MB |
183 | Node* m_node; \ |
184 | Self* m_ht; \ | |
185 | \ | |
872051d8 | 186 | Iterator() : m_node(NULL), m_ht(NULL) {} \ |
0508ba2a | 187 | Iterator( Node* node, const Self* ht ) \ |
5c33522f | 188 | : m_node(node), m_ht(const_cast<Self*>(ht)) {} \ |
0508ba2a MB |
189 | bool operator ==( const Iterator& it ) const \ |
190 | { return m_node == it.m_node; } \ | |
191 | bool operator !=( const Iterator& it ) const \ | |
192 | { return m_node != it.m_node; } \ | |
193 | protected: \ | |
194 | Node* GetNextNode() \ | |
195 | { \ | |
376e1129 | 196 | size_type bucket = GetBucketForNode(m_ht,m_node); \ |
0508ba2a MB |
197 | for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \ |
198 | { \ | |
199 | if( m_ht->m_table[i] ) \ | |
5c33522f | 200 | return static_cast<Node*>(m_ht->m_table[i]); \ |
0508ba2a | 201 | } \ |
872051d8 | 202 | return NULL; \ |
0508ba2a MB |
203 | } \ |
204 | \ | |
205 | void PlusPlus() \ | |
206 | { \ | |
872051d8 | 207 | Node* next = m_node->next(); \ |
376e1129 | 208 | m_node = next ? next : GetNextNode(); \ |
0508ba2a MB |
209 | } \ |
210 | }; \ | |
b5dbe15d | 211 | friend class Iterator; \ |
0508ba2a MB |
212 | \ |
213 | public: \ | |
e1688485 | 214 | CLASSEXP iterator : public Iterator \ |
0508ba2a | 215 | { \ |
e1688485 | 216 | public: \ |
0508ba2a MB |
217 | iterator() : Iterator() {} \ |
218 | iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \ | |
219 | iterator& operator++() { PlusPlus(); return *this; } \ | |
220 | iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \ | |
221 | reference operator *() const { return m_node->m_value; } \ | |
e4c8592e | 222 | PTROPERATOR(pointer) \ |
0508ba2a MB |
223 | }; \ |
224 | \ | |
e1688485 | 225 | CLASSEXP const_iterator : public Iterator \ |
0508ba2a | 226 | { \ |
e1688485 | 227 | public: \ |
0508ba2a | 228 | const_iterator() : Iterator() {} \ |
cd4e32af | 229 | const_iterator(iterator i) : Iterator(i) {} \ |
0508ba2a | 230 | const_iterator( Node* node, const Self* ht ) \ |
5c33522f | 231 | : Iterator(node, const_cast<Self*>(ht)) {} \ |
0508ba2a MB |
232 | const_iterator& operator++() { PlusPlus();return *this; } \ |
233 | const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \ | |
234 | const_reference operator *() const { return m_node->m_value; } \ | |
e4c8592e | 235 | PTROPERATOR(const_pointer) \ |
0508ba2a MB |
236 | }; \ |
237 | \ | |
2b5f62a0 | 238 | CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \ |
0508ba2a MB |
239 | const key_equal& k_eq = key_equal(), \ |
240 | const key_extractor& k_ex = key_extractor() ) \ | |
083f7497 | 241 | : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \ |
0508ba2a MB |
242 | m_items( 0 ), \ |
243 | m_hasher( hfun ), \ | |
244 | m_equals( k_eq ), \ | |
245 | m_getKey( k_ex ) \ | |
246 | { \ | |
872051d8 | 247 | m_table = (_wxHashTable_NodeBase**)AllocTable(m_tableBuckets); \ |
0508ba2a MB |
248 | } \ |
249 | \ | |
250 | CLASSNAME( const Self& ht ) \ | |
872051d8 | 251 | : m_table(NULL), \ |
0508ba2a MB |
252 | m_tableBuckets( 0 ), \ |
253 | m_items( ht.m_items ), \ | |
254 | m_hasher( ht.m_hasher ), \ | |
255 | m_equals( ht.m_equals ), \ | |
256 | m_getKey( ht.m_getKey ) \ | |
257 | { \ | |
258 | HashCopy( ht ); \ | |
259 | } \ | |
260 | \ | |
261 | const Self& operator=( const Self& ht ) \ | |
262 | { \ | |
162e998c PC |
263 | if (&ht != this) \ |
264 | { \ | |
265 | clear(); \ | |
266 | m_hasher = ht.m_hasher; \ | |
267 | m_equals = ht.m_equals; \ | |
268 | m_getKey = ht.m_getKey; \ | |
269 | m_items = ht.m_items; \ | |
270 | HashCopy( ht ); \ | |
271 | } \ | |
376e1129 | 272 | return *this; \ |
0508ba2a MB |
273 | } \ |
274 | \ | |
275 | ~CLASSNAME() \ | |
276 | { \ | |
277 | clear(); \ | |
278 | \ | |
86baa52d | 279 | FreeTable(m_table); \ |
0508ba2a MB |
280 | } \ |
281 | \ | |
282 | hasher hash_funct() { return m_hasher; } \ | |
283 | key_equal key_eq() { return m_equals; } \ | |
284 | \ | |
285 | /* removes all elements from the hash table, but does not */ \ | |
286 | /* shrink it ( perhaps it should ) */ \ | |
2b5f62a0 VZ |
287 | void clear() \ |
288 | { \ | |
872051d8 | 289 | DeleteNodes(m_tableBuckets, m_table, DeleteNode); \ |
2b5f62a0 VZ |
290 | m_items = 0; \ |
291 | } \ | |
0508ba2a MB |
292 | \ |
293 | size_type size() const { return m_items; } \ | |
294 | size_type max_size() const { return size_type(-1); } \ | |
295 | bool empty() const { return size() == 0; } \ | |
296 | \ | |
872051d8 PC |
297 | const_iterator end() const { return const_iterator(NULL, this); } \ |
298 | iterator end() { return iterator(NULL, this); } \ | |
0508ba2a | 299 | const_iterator begin() const \ |
5c33522f | 300 | { return const_iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \ |
0508ba2a | 301 | iterator begin() \ |
5c33522f | 302 | { return iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \ |
0508ba2a MB |
303 | \ |
304 | size_type erase( const const_key_type& key ) \ | |
305 | { \ | |
872051d8 | 306 | _wxHashTable_NodeBase** node = GetNodePtr(key); \ |
0508ba2a MB |
307 | \ |
308 | if( !node ) \ | |
309 | return 0; \ | |
310 | \ | |
311 | --m_items; \ | |
872051d8 | 312 | _wxHashTable_NodeBase* temp = (*node)->m_next; \ |
5c33522f | 313 | delete static_cast<Node*>(*node); \ |
0508ba2a MB |
314 | (*node) = temp; \ |
315 | if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \ | |
083f7497 | 316 | ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \ |
0508ba2a MB |
317 | return 1; \ |
318 | } \ | |
319 | \ | |
320 | protected: \ | |
321 | static size_type GetBucketForNode( Self* ht, Node* node ) \ | |
322 | { \ | |
323 | return ht->m_hasher( ht->m_getKey( node->m_value ) ) \ | |
324 | % ht->m_tableBuckets; \ | |
325 | } \ | |
326 | static Node* CopyNode( Node* node ) { return new Node( *node ); } \ | |
327 | \ | |
fe8604ac | 328 | Node* GetOrCreateNode( const value_type& value, bool& created ) \ |
0508ba2a MB |
329 | { \ |
330 | const const_key_type& key = m_getKey( value ); \ | |
331 | size_t bucket = m_hasher( key ) % m_tableBuckets; \ | |
5c33522f | 332 | Node* node = static_cast<Node*>(m_table[bucket]); \ |
0508ba2a MB |
333 | \ |
334 | while( node ) \ | |
335 | { \ | |
336 | if( m_equals( m_getKey( node->m_value ), key ) ) \ | |
fe8604ac VZ |
337 | { \ |
338 | created = false; \ | |
0508ba2a | 339 | return node; \ |
fe8604ac | 340 | } \ |
872051d8 | 341 | node = node->next(); \ |
0508ba2a | 342 | } \ |
fe8604ac VZ |
343 | created = true; \ |
344 | return CreateNode( value, bucket); \ | |
ddf5de5b MB |
345 | }\ |
346 | Node * CreateNode( const value_type& value, size_t bucket ) \ | |
347 | {\ | |
348 | Node* node = new Node( value ); \ | |
872051d8 | 349 | node->m_next = m_table[bucket]; \ |
0508ba2a MB |
350 | m_table[bucket] = node; \ |
351 | \ | |
352 | /* must be after the node is inserted */ \ | |
353 | ++m_items; \ | |
354 | if( SHOULD_GROW( m_tableBuckets, m_items ) ) \ | |
355 | ResizeTable( m_tableBuckets ); \ | |
356 | \ | |
357 | return node; \ | |
358 | } \ | |
ddf5de5b MB |
359 | void CreateNode( const value_type& value ) \ |
360 | {\ | |
361 | CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \ | |
362 | }\ | |
0508ba2a MB |
363 | \ |
364 | /* returns NULL if not found */ \ | |
872051d8 | 365 | _wxHashTable_NodeBase** GetNodePtr(const const_key_type& key) const \ |
0508ba2a | 366 | { \ |
7c6c0acf | 367 | size_t bucket = m_hasher( key ) % m_tableBuckets; \ |
872051d8 | 368 | _wxHashTable_NodeBase** node = &m_table[bucket]; \ |
0508ba2a MB |
369 | \ |
370 | while( *node ) \ | |
371 | { \ | |
5c33522f | 372 | if (m_equals(m_getKey(static_cast<Node*>(*node)->m_value), key)) \ |
0508ba2a | 373 | return node; \ |
872051d8 | 374 | node = &(*node)->m_next; \ |
0508ba2a MB |
375 | } \ |
376 | \ | |
9a71e770 | 377 | return NULL; \ |
0508ba2a MB |
378 | } \ |
379 | \ | |
380 | /* returns NULL if not found */ \ | |
381 | /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \ | |
382 | Node* GetNode( const const_key_type& key ) const \ | |
383 | { \ | |
7c6c0acf | 384 | size_t bucket = m_hasher( key ) % m_tableBuckets; \ |
5c33522f | 385 | Node* node = static_cast<Node*>(m_table[bucket]); \ |
0508ba2a MB |
386 | \ |
387 | while( node ) \ | |
388 | { \ | |
389 | if( m_equals( m_getKey( node->m_value ), key ) ) \ | |
390 | return node; \ | |
872051d8 | 391 | node = node->next(); \ |
0508ba2a MB |
392 | } \ |
393 | \ | |
872051d8 | 394 | return NULL; \ |
0508ba2a MB |
395 | } \ |
396 | \ | |
397 | void ResizeTable( size_t newSize ) \ | |
398 | { \ | |
083f7497 | 399 | newSize = GetNextPrime( (unsigned long)newSize ); \ |
872051d8 | 400 | _wxHashTable_NodeBase** srcTable = m_table; \ |
0508ba2a | 401 | size_t srcBuckets = m_tableBuckets; \ |
872051d8 | 402 | m_table = (_wxHashTable_NodeBase**)AllocTable( newSize ); \ |
0508ba2a MB |
403 | m_tableBuckets = newSize; \ |
404 | \ | |
872051d8 PC |
405 | CopyHashTable( srcTable, srcBuckets, \ |
406 | this, m_table, \ | |
1c157205 | 407 | (BucketFromNode)GetBucketForNode,\ |
0508ba2a | 408 | (ProcessNode)&DummyProcessNode ); \ |
86baa52d | 409 | FreeTable(srcTable); \ |
0508ba2a MB |
410 | } \ |
411 | \ | |
412 | /* this must be called _after_ m_table has been cleaned */ \ | |
413 | void HashCopy( const Self& ht ) \ | |
414 | { \ | |
415 | ResizeTable( ht.size() ); \ | |
872051d8 | 416 | CopyHashTable( ht.m_table, ht.m_tableBuckets, \ |
0508ba2a | 417 | (_wxHashTableBase2*)this, \ |
872051d8 | 418 | m_table, \ |
1c157205 MB |
419 | (BucketFromNode)GetBucketForNode, \ |
420 | (ProcessNode)CopyNode ); \ | |
0508ba2a MB |
421 | } \ |
422 | }; | |
423 | ||
376e1129 VZ |
424 | // defines an STL-like pair class CLASSNAME storing two fields: first of type |
425 | // KEY_T and second of type VALUE_T | |
0508ba2a MB |
426 | #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \ |
427 | CLASSEXP CLASSNAME \ | |
428 | { \ | |
429 | public: \ | |
d7ad5a02 VZ |
430 | typedef KEY_T first_type; \ |
431 | typedef VALUE_T second_type; \ | |
0508ba2a MB |
432 | typedef KEY_T t1; \ |
433 | typedef VALUE_T t2; \ | |
434 | typedef const KEY_T const_t1; \ | |
435 | typedef const VALUE_T const_t2; \ | |
436 | \ | |
162e998c | 437 | CLASSNAME(const const_t1& f, const const_t2& s) \ |
5c33522f | 438 | : first(const_cast<t1&>(f)), second(const_cast<t2&>(s)) {} \ |
0508ba2a MB |
439 | \ |
440 | t1 first; \ | |
441 | t2 second; \ | |
442 | }; | |
443 | ||
376e1129 VZ |
444 | // defines the class CLASSNAME returning the key part (of type KEY_T) from a |
445 | // pair of type PAIR_T | |
0508ba2a MB |
446 | #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \ |
447 | CLASSEXP CLASSNAME \ | |
448 | { \ | |
95197553 MB |
449 | typedef KEY_T key_type; \ |
450 | typedef PAIR_T pair_type; \ | |
451 | typedef const key_type const_key_type; \ | |
452 | typedef const pair_type const_pair_type; \ | |
453 | typedef const_key_type& const_key_reference; \ | |
454 | typedef const_pair_type& const_pair_reference; \ | |
0508ba2a | 455 | public: \ |
376e1129 | 456 | CLASSNAME() { } \ |
95197553 | 457 | const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\ |
376e1129 VZ |
458 | \ |
459 | /* the dummy assignment operator is needed to suppress compiler */ \ | |
460 | /* warnings from hash table class' operator=(): gcc complains about */ \ | |
461 | /* "statement with no effect" without it */ \ | |
462 | CLASSNAME& operator=(const CLASSNAME&) { return *this; } \ | |
0508ba2a MB |
463 | }; |
464 | ||
465 | // grow/shrink predicates | |
5d3e7b52 WS |
466 | inline bool never_grow( size_t, size_t ) { return false; } |
467 | inline bool never_shrink( size_t, size_t ) { return false; } | |
0508ba2a MB |
468 | inline bool grow_lf70( size_t buckets, size_t items ) |
469 | { | |
4e116788 | 470 | return float(items)/float(buckets) >= 0.85f; |
0508ba2a MB |
471 | } |
472 | ||
f6a46bb0 | 473 | #endif // various hash map implementations |
bdcade0a | 474 | |
376e1129 VZ |
475 | // ---------------------------------------------------------------------------- |
476 | // hashing and comparison functors | |
477 | // ---------------------------------------------------------------------------- | |
478 | ||
479 | // NB: implementation detail: all of these classes must have dummy assignment | |
480 | // operators to suppress warnings about "statement with no effect" from gcc | |
481 | // in the hash table class assignment operator (where they're assigned) | |
482 | ||
f6a46bb0 | 483 | #ifndef wxNEEDS_WX_HASH_MAP |
bdcade0a MB |
484 | |
485 | // integer types | |
17473a77 | 486 | struct WXDLLIMPEXP_BASE wxIntegerHash |
bdcade0a | 487 | { |
17473a77 | 488 | private: |
bdcade0a MB |
489 | WX_HASH_MAP_NAMESPACE::hash<long> longHash; |
490 | WX_HASH_MAP_NAMESPACE::hash<unsigned long> ulongHash; | |
491 | WX_HASH_MAP_NAMESPACE::hash<int> intHash; | |
492 | WX_HASH_MAP_NAMESPACE::hash<unsigned int> uintHash; | |
493 | WX_HASH_MAP_NAMESPACE::hash<short> shortHash; | |
494 | WX_HASH_MAP_NAMESPACE::hash<unsigned short> ushortHash; | |
bfe1edbd | 495 | |
066e5e3f | 496 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
b158be6e MW |
497 | // hash<wxLongLong_t> ought to work but doesn't on some compilers |
498 | #if (!defined SIZEOF_LONG_LONG && SIZEOF_LONG == 4) \ | |
499 | || (defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG * 2) | |
bfe1edbd MW |
500 | size_t longlongHash( wxLongLong_t x ) const |
501 | { | |
ec2f62b1 MW |
502 | return longHash( wx_truncate_cast(long, x) ) ^ |
503 | longHash( wx_truncate_cast(long, x >> (sizeof(long) * 8)) ); | |
bfe1edbd | 504 | } |
b158be6e MW |
505 | #elif defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG |
506 | WX_HASH_MAP_NAMESPACE::hash<long> longlongHash; | |
ec2f62b1 MW |
507 | #else |
508 | WX_HASH_MAP_NAMESPACE::hash<wxLongLong_t> longlongHash; | |
509 | #endif | |
066e5e3f | 510 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
bfe1edbd | 511 | |
bdcade0a MB |
512 | public: |
513 | wxIntegerHash() { } | |
514 | size_t operator()( long x ) const { return longHash( x ); } | |
515 | size_t operator()( unsigned long x ) const { return ulongHash( x ); } | |
516 | size_t operator()( int x ) const { return intHash( x ); } | |
517 | size_t operator()( unsigned int x ) const { return uintHash( x ); } | |
518 | size_t operator()( short x ) const { return shortHash( x ); } | |
519 | size_t operator()( unsigned short x ) const { return ushortHash( x ); } | |
066e5e3f | 520 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
bfe1edbd MW |
521 | size_t operator()( wxLongLong_t x ) const { return longlongHash(x); } |
522 | size_t operator()( wxULongLong_t x ) const { return longlongHash(x); } | |
066e5e3f | 523 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
bdcade0a MB |
524 | |
525 | wxIntegerHash& operator=(const wxIntegerHash&) { return *this; } | |
526 | }; | |
527 | ||
f6a46bb0 | 528 | #else // wxNEEDS_WX_HASH_MAP |
bdcade0a | 529 | |
0508ba2a | 530 | // integer types |
17473a77 | 531 | struct WXDLLIMPEXP_BASE wxIntegerHash |
0508ba2a | 532 | { |
376e1129 | 533 | wxIntegerHash() { } |
0508ba2a MB |
534 | unsigned long operator()( long x ) const { return (unsigned long)x; } |
535 | unsigned long operator()( unsigned long x ) const { return x; } | |
536 | unsigned long operator()( int x ) const { return (unsigned long)x; } | |
537 | unsigned long operator()( unsigned int x ) const { return x; } | |
376e1129 VZ |
538 | unsigned long operator()( short x ) const { return (unsigned long)x; } |
539 | unsigned long operator()( unsigned short x ) const { return x; } | |
066e5e3f | 540 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
5c33522f | 541 | wxULongLong_t operator()( wxLongLong_t x ) const { return static_cast<wxULongLong_t>(x); } |
bfe1edbd | 542 | wxULongLong_t operator()( wxULongLong_t x ) const { return x; } |
066e5e3f | 543 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
376e1129 VZ |
544 | |
545 | wxIntegerHash& operator=(const wxIntegerHash&) { return *this; } | |
0508ba2a MB |
546 | }; |
547 | ||
f6a46bb0 | 548 | #endif // !wxNEEDS_WX_HASH_MAP/wxNEEDS_WX_HASH_MAP |
bdcade0a | 549 | |
17473a77 | 550 | struct WXDLLIMPEXP_BASE wxIntegerEqual |
0508ba2a | 551 | { |
376e1129 | 552 | wxIntegerEqual() { } |
0508ba2a | 553 | bool operator()( long a, long b ) const { return a == b; } |
376e1129 | 554 | bool operator()( unsigned long a, unsigned long b ) const { return a == b; } |
0508ba2a | 555 | bool operator()( int a, int b ) const { return a == b; } |
376e1129 VZ |
556 | bool operator()( unsigned int a, unsigned int b ) const { return a == b; } |
557 | bool operator()( short a, short b ) const { return a == b; } | |
558 | bool operator()( unsigned short a, unsigned short b ) const { return a == b; } | |
066e5e3f | 559 | #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
bfe1edbd MW |
560 | bool operator()( wxLongLong_t a, wxLongLong_t b ) const { return a == b; } |
561 | bool operator()( wxULongLong_t a, wxULongLong_t b ) const { return a == b; } | |
066e5e3f | 562 | #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG |
376e1129 VZ |
563 | |
564 | wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; } | |
0508ba2a MB |
565 | }; |
566 | ||
567 | // pointers | |
17473a77 | 568 | struct WXDLLIMPEXP_BASE wxPointerHash |
0508ba2a | 569 | { |
376e1129 VZ |
570 | wxPointerHash() { } |
571 | ||
f6a46bb0 | 572 | #ifdef wxNEEDS_WX_HASH_MAP |
f1288544 | 573 | wxUIntPtr operator()( const void* k ) const { return wxPtrToUInt(k); } |
f6a46bb0 VZ |
574 | #else |
575 | size_t operator()( const void* k ) const { return (size_t)k; } | |
bdcade0a | 576 | #endif |
376e1129 VZ |
577 | |
578 | wxPointerHash& operator=(const wxPointerHash&) { return *this; } | |
0508ba2a MB |
579 | }; |
580 | ||
17473a77 | 581 | struct WXDLLIMPEXP_BASE wxPointerEqual |
0508ba2a | 582 | { |
376e1129 VZ |
583 | wxPointerEqual() { } |
584 | bool operator()( const void* a, const void* b ) const { return a == b; } | |
585 | ||
586 | wxPointerEqual& operator=(const wxPointerEqual&) { return *this; } | |
0508ba2a MB |
587 | }; |
588 | ||
ad78ab8c | 589 | // wxString, char*, wchar_t* |
17473a77 | 590 | struct WXDLLIMPEXP_BASE wxStringHash |
0508ba2a | 591 | { |
376e1129 | 592 | wxStringHash() {} |
0508ba2a | 593 | unsigned long operator()( const wxString& x ) const |
ad78ab8c VS |
594 | { return stringHash( x.wx_str() ); } |
595 | unsigned long operator()( const wchar_t* x ) const | |
596 | { return stringHash( x ); } | |
0508ba2a | 597 | unsigned long operator()( const char* x ) const |
ad78ab8c VS |
598 | { return stringHash( x ); } |
599 | ||
600 | #if WXWIN_COMPATIBILITY_2_8 | |
601 | static unsigned long wxCharStringHash( const wxChar* x ) | |
602 | { return stringHash(x); } | |
603 | #if wxUSE_UNICODE | |
604 | static unsigned long charStringHash( const char* x ) | |
605 | { return stringHash(x); } | |
606 | #endif | |
607 | #endif // WXWIN_COMPATIBILITY_2_8 | |
608 | ||
609 | static unsigned long stringHash( const wchar_t* ); | |
610 | static unsigned long stringHash( const char* ); | |
376e1129 VZ |
611 | |
612 | wxStringHash& operator=(const wxStringHash&) { return *this; } | |
0508ba2a MB |
613 | }; |
614 | ||
17473a77 | 615 | struct WXDLLIMPEXP_BASE wxStringEqual |
0508ba2a | 616 | { |
376e1129 | 617 | wxStringEqual() {} |
0508ba2a MB |
618 | bool operator()( const wxString& a, const wxString& b ) const |
619 | { return a == b; } | |
620 | bool operator()( const wxChar* a, const wxChar* b ) const | |
621 | { return wxStrcmp( a, b ) == 0; } | |
622 | #if wxUSE_UNICODE | |
623 | bool operator()( const char* a, const char* b ) const | |
624 | { return strcmp( a, b ) == 0; } | |
376e1129 VZ |
625 | #endif // wxUSE_UNICODE |
626 | ||
627 | wxStringEqual& operator=(const wxStringEqual&) { return *this; } | |
0508ba2a MB |
628 | }; |
629 | ||
f6a46bb0 | 630 | #ifdef wxNEEDS_WX_HASH_MAP |
bdcade0a | 631 | |
e4c8592e VZ |
632 | #define wxPTROP_NORMAL(pointer) \ |
633 | pointer operator ->() const { return &(m_node->m_value); } | |
634 | #define wxPTROP_NOP(pointer) | |
635 | ||
0508ba2a MB |
636 | #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ |
637 | _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \ | |
638 | _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \ | |
e4c8592e VZ |
639 | _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, \ |
640 | CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, wxPTROP_NORMAL, \ | |
641 | CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \ | |
0508ba2a MB |
642 | CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \ |
643 | { \ | |
644 | public: \ | |
645 | typedef VALUE_T mapped_type; \ | |
fe8604ac | 646 | _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \ |
0508ba2a | 647 | \ |
d8771ac7 MB |
648 | wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \ |
649 | key_equal eq = key_equal() ) \ | |
650 | : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \ | |
651 | CLASSNAME##_wxImplementation_KeyEx() ) {} \ | |
0508ba2a MB |
652 | \ |
653 | mapped_type& operator[]( const const_key_type& key ) \ | |
654 | { \ | |
fe8604ac VZ |
655 | bool created; \ |
656 | return GetOrCreateNode( \ | |
657 | CLASSNAME##_wxImplementation_Pair( key, mapped_type() ), \ | |
658 | created)->m_value.second; \ | |
0508ba2a MB |
659 | } \ |
660 | \ | |
661 | const_iterator find( const const_key_type& key ) const \ | |
662 | { \ | |
663 | return const_iterator( GetNode( key ), this ); \ | |
664 | } \ | |
665 | \ | |
666 | iterator find( const const_key_type& key ) \ | |
667 | { \ | |
668 | return iterator( GetNode( key ), this ); \ | |
669 | } \ | |
670 | \ | |
fe8604ac VZ |
671 | Insert_Result insert( const value_type& v ) \ |
672 | { \ | |
673 | bool created; \ | |
674 | Node *node = GetOrCreateNode( \ | |
675 | CLASSNAME##_wxImplementation_Pair( v.first, v.second ), \ | |
676 | created); \ | |
fe8604ac VZ |
677 | return Insert_Result(iterator(node, this), created); \ |
678 | } \ | |
0508ba2a MB |
679 | \ |
680 | size_type erase( const key_type& k ) \ | |
681 | { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \ | |
e4c8592e | 682 | void erase( const iterator& it ) { erase( (*it).first ); } \ |
0508ba2a MB |
683 | \ |
684 | /* count() == 0 | 1 */ \ | |
685 | size_type count( const const_key_type& key ) \ | |
3952889a | 686 | { \ |
2415cf67 | 687 | return GetNode( key ) ? 1u : 0u; \ |
3952889a | 688 | } \ |
3f5c62f9 | 689 | } |
0508ba2a | 690 | |
f6a46bb0 | 691 | #endif // wxNEEDS_WX_HASH_MAP |
bdcade0a | 692 | |
0508ba2a MB |
693 | // these macros are to be used in the user code |
694 | #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \ | |
695 | _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class ) | |
696 | ||
697 | #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \ | |
698 | _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \ | |
d86939c4 | 699 | CLASSNAME, class ) |
0508ba2a MB |
700 | |
701 | #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \ | |
702 | _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \ | |
d86939c4 | 703 | CLASSNAME, class ) |
0508ba2a MB |
704 | |
705 | // and these do exactly the same thing but should be used inside the | |
706 | // library | |
c0421c99 VS |
707 | #define WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \ |
708 | _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL ) | |
709 | ||
0508ba2a | 710 | #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \ |
c0421c99 | 711 | WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, \ |
53a2db12 | 712 | CLASSNAME, class WXDLLIMPEXP_CORE ) |
0508ba2a | 713 | |
c0421c99 | 714 | #define WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \ |
0508ba2a | 715 | _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \ |
c0421c99 | 716 | CLASSNAME, DECL ) |
0508ba2a | 717 | |
c0421c99 VS |
718 | #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \ |
719 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \ | |
53a2db12 | 720 | class WXDLLIMPEXP_CORE ) |
c0421c99 VS |
721 | |
722 | #define WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \ | |
0508ba2a | 723 | _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \ |
c0421c99 VS |
724 | CLASSNAME, DECL ) |
725 | ||
726 | #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \ | |
727 | WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \ | |
53a2db12 | 728 | class WXDLLIMPEXP_CORE ) |
0508ba2a | 729 | |
df5168c4 MB |
730 | // delete all hash elements |
731 | // | |
732 | // NB: the class declaration of the hash elements must be visible from the | |
733 | // place where you use this macro, otherwise the proper destructor may not | |
734 | // be called (a decent compiler should give a warning about it, but don't | |
735 | // count on it)! | |
736 | #define WX_CLEAR_HASH_MAP(type, hashmap) \ | |
737 | { \ | |
486f0e76 | 738 | type::iterator it, en; \ |
df5168c4 MB |
739 | for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \ |
740 | delete it->second; \ | |
741 | (hashmap).clear(); \ | |
742 | } | |
743 | ||
0fab3a5a RD |
744 | //--------------------------------------------------------------------------- |
745 | // Declarations of common hashmap classes | |
746 | ||
747 | WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual, | |
3f5c62f9 | 748 | wxLongToLongHashMap, class WXDLLIMPEXP_BASE ); |
0fab3a5a | 749 | |
730b772b FM |
750 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxString, wxStringToStringHashMap, |
751 | class WXDLLIMPEXP_BASE ); | |
752 | ||
af588446 VZ |
753 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxUIntPtr, wxStringToNumHashMap, |
754 | class WXDLLIMPEXP_BASE ); | |
755 | ||
0fab3a5a | 756 | |
376e1129 | 757 | #endif // _WX_HASHMAP_H_ |