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