]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/hashmap.h
Split up the context menu functionality to make it easier to customise.
[wxWidgets.git] / include / wx / hashmap.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/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#include "wx/wxcrt.h"
17
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
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
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
31#if wxUSE_STD_CONTAINERS && \
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
45#elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP)
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 ) \
60 typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME
61
62#else // !wxUSE_STD_CONTAINERS || no std::{hash,unordered}_map class available
63
64#define wxNEEDS_WX_HASH_MAP
65
66#ifdef __WXWINCE__
67typedef int ptrdiff_t;
68#else
69#include <stddef.h> // for ptrdiff_t
70#endif
71
72// private
73struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase
74{
75 _wxHashTable_NodeBase() : m_next(NULL) {}
76
77 _wxHashTable_NodeBase* m_next;
78
79// Cannot do this:
80// wxDECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase);
81// without rewriting the macros, which require a public copy constructor.
82};
83
84// private
85class WXDLLIMPEXP_BASE _wxHashTableBase2
86{
87public:
88 typedef void (*NodeDtor)(_wxHashTable_NodeBase*);
89 typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*);
90 typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*);
91protected:
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];
101 return NULL;
102 }
103
104 // as static const unsigned prime_count = 31 but works with all compilers
105 enum { prime_count = 31 };
106 static const unsigned long ms_primes[prime_count];
107
108 // returns the first prime in ms_primes greater than n
109 static unsigned long GetNextPrime( unsigned long n );
110
111 // returns the first prime in ms_primes smaller than n
112 // ( or ms_primes[0] if n is very small )
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
120 static void** AllocTable( size_t sz )
121 {
122 return (void **)calloc(sz, sizeof(void*));
123 }
124 static void FreeTable(void *table)
125 {
126 free(table);
127 }
128};
129
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 ) \
133CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
134{ \
135public: \
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; \
150public: \
151 typedef KEY_EX_T key_extractor; \
152 typedef CLASSNAME Self; \
153protected: \
154 _wxHashTable_NodeBase** m_table; \
155 size_t m_tableBuckets; \
156 size_t m_items; \
157 hasher m_hasher; \
158 key_equal m_equals; \
159 key_extractor m_getKey; \
160public: \
161 struct Node:public _wxHashTable_NodeBase \
162 { \
163 public: \
164 Node( const value_type& value ) \
165 : m_value( value ) {} \
166 Node* next() { return static_cast<Node*>(m_next); } \
167 \
168 value_type m_value; \
169 }; \
170 \
171protected: \
172 static void DeleteNode( _wxHashTable_NodeBase* node ) \
173 { \
174 delete static_cast<Node*>(node); \
175 } \
176public: \
177 /* */ \
178 /* forward iterator */ \
179 /* */ \
180 CLASSEXP Iterator \
181 { \
182 public: \
183 Node* m_node; \
184 Self* m_ht; \
185 \
186 Iterator() : m_node(NULL), m_ht(NULL) {} \
187 Iterator( Node* node, const Self* ht ) \
188 : m_node(node), m_ht(const_cast<Self*>(ht)) {} \
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 { \
196 size_type bucket = GetBucketForNode(m_ht,m_node); \
197 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
198 { \
199 if( m_ht->m_table[i] ) \
200 return static_cast<Node*>(m_ht->m_table[i]); \
201 } \
202 return NULL; \
203 } \
204 \
205 void PlusPlus() \
206 { \
207 Node* next = m_node->next(); \
208 m_node = next ? next : GetNextNode(); \
209 } \
210 }; \
211 friend class Iterator; \
212 \
213public: \
214 CLASSEXP iterator : public Iterator \
215 { \
216 public: \
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; } \
222 PTROPERATOR(pointer) \
223 }; \
224 \
225 CLASSEXP const_iterator : public Iterator \
226 { \
227 public: \
228 const_iterator() : Iterator() {} \
229 const_iterator(iterator i) : Iterator(i) {} \
230 const_iterator( Node* node, const Self* ht ) \
231 : Iterator(node, const_cast<Self*>(ht)) {} \
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; } \
235 PTROPERATOR(const_pointer) \
236 }; \
237 \
238 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
239 const key_equal& k_eq = key_equal(), \
240 const key_extractor& k_ex = key_extractor() ) \
241 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
242 m_items( 0 ), \
243 m_hasher( hfun ), \
244 m_equals( k_eq ), \
245 m_getKey( k_ex ) \
246 { \
247 m_table = (_wxHashTable_NodeBase**)AllocTable(m_tableBuckets); \
248 } \
249 \
250 CLASSNAME( const Self& ht ) \
251 : m_table(NULL), \
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 { \
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 } \
272 return *this; \
273 } \
274 \
275 ~CLASSNAME() \
276 { \
277 clear(); \
278 \
279 FreeTable(m_table); \
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 ) */ \
287 void clear() \
288 { \
289 DeleteNodes(m_tableBuckets, m_table, DeleteNode); \
290 m_items = 0; \
291 } \
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 \
297 const_iterator end() const { return const_iterator(NULL, this); } \
298 iterator end() { return iterator(NULL, this); } \
299 const_iterator begin() const \
300 { return const_iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \
301 iterator begin() \
302 { return iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \
303 \
304 size_type erase( const const_key_type& key ) \
305 { \
306 _wxHashTable_NodeBase** node = GetNodePtr(key); \
307 \
308 if( !node ) \
309 return 0; \
310 \
311 --m_items; \
312 _wxHashTable_NodeBase* temp = (*node)->m_next; \
313 delete static_cast<Node*>(*node); \
314 (*node) = temp; \
315 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
316 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
317 return 1; \
318 } \
319 \
320protected: \
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 \
328 Node* GetOrCreateNode( const value_type& value, bool& created ) \
329 { \
330 const const_key_type& key = m_getKey( value ); \
331 size_t bucket = m_hasher( key ) % m_tableBuckets; \
332 Node* node = static_cast<Node*>(m_table[bucket]); \
333 \
334 while( node ) \
335 { \
336 if( m_equals( m_getKey( node->m_value ), key ) ) \
337 { \
338 created = false; \
339 return node; \
340 } \
341 node = node->next(); \
342 } \
343 created = true; \
344 return CreateNode( value, bucket); \
345 }\
346 Node * CreateNode( const value_type& value, size_t bucket ) \
347 {\
348 Node* node = new Node( value ); \
349 node->m_next = m_table[bucket]; \
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 } \
359 void CreateNode( const value_type& value ) \
360 {\
361 CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \
362 }\
363 \
364 /* returns NULL if not found */ \
365 _wxHashTable_NodeBase** GetNodePtr(const const_key_type& key) const \
366 { \
367 size_t bucket = m_hasher( key ) % m_tableBuckets; \
368 _wxHashTable_NodeBase** node = &m_table[bucket]; \
369 \
370 while( *node ) \
371 { \
372 if (m_equals(m_getKey(static_cast<Node*>(*node)->m_value), key)) \
373 return node; \
374 node = &(*node)->m_next; \
375 } \
376 \
377 return NULL; \
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 { \
384 size_t bucket = m_hasher( key ) % m_tableBuckets; \
385 Node* node = static_cast<Node*>(m_table[bucket]); \
386 \
387 while( node ) \
388 { \
389 if( m_equals( m_getKey( node->m_value ), key ) ) \
390 return node; \
391 node = node->next(); \
392 } \
393 \
394 return NULL; \
395 } \
396 \
397 void ResizeTable( size_t newSize ) \
398 { \
399 newSize = GetNextPrime( (unsigned long)newSize ); \
400 _wxHashTable_NodeBase** srcTable = m_table; \
401 size_t srcBuckets = m_tableBuckets; \
402 m_table = (_wxHashTable_NodeBase**)AllocTable( newSize ); \
403 m_tableBuckets = newSize; \
404 \
405 CopyHashTable( srcTable, srcBuckets, \
406 this, m_table, \
407 (BucketFromNode)GetBucketForNode,\
408 (ProcessNode)&DummyProcessNode ); \
409 FreeTable(srcTable); \
410 } \
411 \
412 /* this must be called _after_ m_table has been cleaned */ \
413 void HashCopy( const Self& ht ) \
414 { \
415 ResizeTable( ht.size() ); \
416 CopyHashTable( ht.m_table, ht.m_tableBuckets, \
417 (_wxHashTableBase2*)this, \
418 m_table, \
419 (BucketFromNode)GetBucketForNode, \
420 (ProcessNode)CopyNode ); \
421 } \
422};
423
424// defines an STL-like pair class CLASSNAME storing two fields: first of type
425// KEY_T and second of type VALUE_T
426#define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
427CLASSEXP CLASSNAME \
428{ \
429public: \
430 typedef KEY_T first_type; \
431 typedef VALUE_T second_type; \
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 \
437 CLASSNAME(const const_t1& f, const const_t2& s) \
438 : first(const_cast<t1&>(f)), second(const_cast<t2&>(s)) {} \
439 \
440 t1 first; \
441 t2 second; \
442};
443
444// defines the class CLASSNAME returning the key part (of type KEY_T) from a
445// pair of type PAIR_T
446#define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
447CLASSEXP CLASSNAME \
448{ \
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; \
455public: \
456 CLASSNAME() { } \
457 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
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; } \
463};
464
465// grow/shrink predicates
466inline bool never_grow( size_t, size_t ) { return false; }
467inline bool never_shrink( size_t, size_t ) { return false; }
468inline bool grow_lf70( size_t buckets, size_t items )
469{
470 return float(items)/float(buckets) >= 0.85;
471}
472
473#endif // various hash map implementations
474
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
483#ifndef wxNEEDS_WX_HASH_MAP
484
485// integer types
486class WXDLLIMPEXP_BASE wxIntegerHash
487{
488 WX_HASH_MAP_NAMESPACE::hash<long> longHash;
489 WX_HASH_MAP_NAMESPACE::hash<unsigned long> ulongHash;
490 WX_HASH_MAP_NAMESPACE::hash<int> intHash;
491 WX_HASH_MAP_NAMESPACE::hash<unsigned int> uintHash;
492 WX_HASH_MAP_NAMESPACE::hash<short> shortHash;
493 WX_HASH_MAP_NAMESPACE::hash<unsigned short> ushortHash;
494
495#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
496 // hash<wxLongLong_t> ought to work but doesn't on some compilers
497 #if (!defined SIZEOF_LONG_LONG && SIZEOF_LONG == 4) \
498 || (defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG * 2)
499 size_t longlongHash( wxLongLong_t x ) const
500 {
501 return longHash( wx_truncate_cast(long, x) ) ^
502 longHash( wx_truncate_cast(long, x >> (sizeof(long) * 8)) );
503 }
504 #elif defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG
505 WX_HASH_MAP_NAMESPACE::hash<long> longlongHash;
506 #else
507 WX_HASH_MAP_NAMESPACE::hash<wxLongLong_t> longlongHash;
508 #endif
509#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
510
511public:
512 wxIntegerHash() { }
513 size_t operator()( long x ) const { return longHash( x ); }
514 size_t operator()( unsigned long x ) const { return ulongHash( x ); }
515 size_t operator()( int x ) const { return intHash( x ); }
516 size_t operator()( unsigned int x ) const { return uintHash( x ); }
517 size_t operator()( short x ) const { return shortHash( x ); }
518 size_t operator()( unsigned short x ) const { return ushortHash( x ); }
519#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
520 size_t operator()( wxLongLong_t x ) const { return longlongHash(x); }
521 size_t operator()( wxULongLong_t x ) const { return longlongHash(x); }
522#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
523
524 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
525};
526
527#else // wxNEEDS_WX_HASH_MAP
528
529// integer types
530class WXDLLIMPEXP_BASE wxIntegerHash
531{
532public:
533 wxIntegerHash() { }
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; }
538 unsigned long operator()( short x ) const { return (unsigned long)x; }
539 unsigned long operator()( unsigned short x ) const { return x; }
540#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
541 wxULongLong_t operator()( wxLongLong_t x ) const { return static_cast<wxULongLong_t>(x); }
542 wxULongLong_t operator()( wxULongLong_t x ) const { return x; }
543#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
544
545 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
546};
547
548#endif // !wxNEEDS_WX_HASH_MAP/wxNEEDS_WX_HASH_MAP
549
550class WXDLLIMPEXP_BASE wxIntegerEqual
551{
552public:
553 wxIntegerEqual() { }
554 bool operator()( long a, long b ) const { return a == b; }
555 bool operator()( unsigned long a, unsigned long b ) const { return a == b; }
556 bool operator()( int a, int b ) const { return a == b; }
557 bool operator()( unsigned int a, unsigned int b ) const { return a == b; }
558 bool operator()( short a, short b ) const { return a == b; }
559 bool operator()( unsigned short a, unsigned short b ) const { return a == b; }
560#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
561 bool operator()( wxLongLong_t a, wxLongLong_t b ) const { return a == b; }
562 bool operator()( wxULongLong_t a, wxULongLong_t b ) const { return a == b; }
563#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
564
565 wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; }
566};
567
568// pointers
569class WXDLLIMPEXP_BASE wxPointerHash
570{
571public:
572 wxPointerHash() { }
573
574#ifdef wxNEEDS_WX_HASH_MAP
575 wxUIntPtr operator()( const void* k ) const { return wxPtrToUInt(k); }
576#else
577 size_t operator()( const void* k ) const { return (size_t)k; }
578#endif
579
580 wxPointerHash& operator=(const wxPointerHash&) { return *this; }
581};
582
583class WXDLLIMPEXP_BASE wxPointerEqual
584{
585public:
586 wxPointerEqual() { }
587 bool operator()( const void* a, const void* b ) const { return a == b; }
588
589 wxPointerEqual& operator=(const wxPointerEqual&) { return *this; }
590};
591
592// wxString, char*, wchar_t*
593class WXDLLIMPEXP_BASE wxStringHash
594{
595public:
596 wxStringHash() {}
597 unsigned long operator()( const wxString& x ) const
598 { return stringHash( x.wx_str() ); }
599 unsigned long operator()( const wchar_t* x ) const
600 { return stringHash( x ); }
601 unsigned long operator()( const char* x ) const
602 { return stringHash( x ); }
603
604#if WXWIN_COMPATIBILITY_2_8
605 static unsigned long wxCharStringHash( const wxChar* x )
606 { return stringHash(x); }
607 #if wxUSE_UNICODE
608 static unsigned long charStringHash( const char* x )
609 { return stringHash(x); }
610 #endif
611#endif // WXWIN_COMPATIBILITY_2_8
612
613 static unsigned long stringHash( const wchar_t* );
614 static unsigned long stringHash( const char* );
615
616 wxStringHash& operator=(const wxStringHash&) { return *this; }
617};
618
619class WXDLLIMPEXP_BASE wxStringEqual
620{
621public:
622 wxStringEqual() {}
623 bool operator()( const wxString& a, const wxString& b ) const
624 { return a == b; }
625 bool operator()( const wxChar* a, const wxChar* b ) const
626 { return wxStrcmp( a, b ) == 0; }
627#if wxUSE_UNICODE
628 bool operator()( const char* a, const char* b ) const
629 { return strcmp( a, b ) == 0; }
630#endif // wxUSE_UNICODE
631
632 wxStringEqual& operator=(const wxStringEqual&) { return *this; }
633};
634
635#ifdef wxNEEDS_WX_HASH_MAP
636
637#define wxPTROP_NORMAL(pointer) \
638 pointer operator ->() const { return &(m_node->m_value); }
639#define wxPTROP_NOP(pointer)
640
641#define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
642_WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
643_WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
644_WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, \
645 CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, wxPTROP_NORMAL, \
646 CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
647CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
648{ \
649public: \
650 typedef VALUE_T mapped_type; \
651 _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \
652 \
653 wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \
654 key_equal eq = key_equal() ) \
655 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \
656 CLASSNAME##_wxImplementation_KeyEx() ) {} \
657 \
658 mapped_type& operator[]( const const_key_type& key ) \
659 { \
660 bool created; \
661 return GetOrCreateNode( \
662 CLASSNAME##_wxImplementation_Pair( key, mapped_type() ), \
663 created)->m_value.second; \
664 } \
665 \
666 const_iterator find( const const_key_type& key ) const \
667 { \
668 return const_iterator( GetNode( key ), this ); \
669 } \
670 \
671 iterator find( const const_key_type& key ) \
672 { \
673 return iterator( GetNode( key ), this ); \
674 } \
675 \
676 Insert_Result insert( const value_type& v ) \
677 { \
678 bool created; \
679 Node *node = GetOrCreateNode( \
680 CLASSNAME##_wxImplementation_Pair( v.first, v.second ), \
681 created); \
682 return Insert_Result(iterator(node, this), created); \
683 } \
684 \
685 size_type erase( const key_type& k ) \
686 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
687 void erase( const iterator& it ) { erase( (*it).first ); } \
688 \
689 /* count() == 0 | 1 */ \
690 size_type count( const const_key_type& key ) \
691 { \
692 /* explicit cast needed to suppress CodeWarrior warnings */ \
693 return (size_type)(GetNode( key ) ? 1 : 0); \
694 } \
695}
696
697#endif // wxNEEDS_WX_HASH_MAP
698
699// these macros are to be used in the user code
700#define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
701 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
702
703#define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
704 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
705 CLASSNAME, class )
706
707#define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
708 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
709 CLASSNAME, class )
710
711// and these do exactly the same thing but should be used inside the
712// library
713#define WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \
714 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL )
715
716#define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
717 WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, \
718 CLASSNAME, class WXDLLIMPEXP_CORE )
719
720#define WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
721 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
722 CLASSNAME, DECL )
723
724#define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
725 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \
726 class WXDLLIMPEXP_CORE )
727
728#define WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
729 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
730 CLASSNAME, DECL )
731
732#define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
733 WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \
734 class WXDLLIMPEXP_CORE )
735
736// delete all hash elements
737//
738// NB: the class declaration of the hash elements must be visible from the
739// place where you use this macro, otherwise the proper destructor may not
740// be called (a decent compiler should give a warning about it, but don't
741// count on it)!
742#define WX_CLEAR_HASH_MAP(type, hashmap) \
743 { \
744 type::iterator it, en; \
745 for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \
746 delete it->second; \
747 (hashmap).clear(); \
748 }
749
750//---------------------------------------------------------------------------
751// Declarations of common hashmap classes
752
753WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual,
754 wxLongToLongHashMap, class WXDLLIMPEXP_BASE );
755
756WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxString, wxStringToStringHashMap,
757 class WXDLLIMPEXP_BASE );
758
759WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxUIntPtr, wxStringToNumHashMap,
760 class WXDLLIMPEXP_BASE );
761
762
763#endif // _WX_HASHMAP_H_