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