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