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