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