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