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