]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hashmap.h
Fix assorted typos in comments and other non-code.
[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 6// Created: 29/01/2002
0508ba2a 7// Copyright: (c) Mattia Barbon
65571936 8// Licence: wxWindows licence
0508ba2a
MB
9/////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_HASHMAP_H_
12#define _WX_HASHMAP_H_
13
a4f24526 14#include "wx/string.h"
092793cb 15#include "wx/wxcrt.h"
0508ba2a 16
01871bf6
VZ
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
f380544a
VZ
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
bdcade0a
MB
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
01871bf6 30#if wxUSE_STD_CONTAINERS && \
f380544a
VZ
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
01871bf6 44#elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP)
bdcade0a
MB
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 ) \
e786a78b 59 typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME
bdcade0a 60
01871bf6 61#else // !wxUSE_STD_CONTAINERS || no std::{hash,unordered}_map class available
f6a46bb0
VZ
62
63#define wxNEEDS_WX_HASH_MAP
64
7f0586ef
JS
65#ifdef __WXWINCE__
66typedef int ptrdiff_t;
68c30476
RR
67#else
68#include <stddef.h> // for ptrdiff_t
7f0586ef
JS
69#endif
70
0508ba2a 71// private
bddd7a8d 72struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase
0508ba2a 73{
872051d8 74 _wxHashTable_NodeBase() : m_next(NULL) {}
0508ba2a 75
872051d8 76 _wxHashTable_NodeBase* m_next;
22f3361e
VZ
77
78// Cannot do this:
c0c133e1 79// wxDECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase);
22f3361e 80// without rewriting the macros, which require a public copy constructor.
0508ba2a
MB
81};
82
83// private
bddd7a8d 84class WXDLLIMPEXP_BASE _wxHashTableBase2
0508ba2a
MB
85{
86public:
87 typedef void (*NodeDtor)(_wxHashTable_NodeBase*);
376e1129 88 typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*);
0508ba2a
MB
89 typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*);
90protected:
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];
872051d8 100 return NULL;
0508ba2a
MB
101 }
102
376e1129 103 // as static const unsigned prime_count = 31 but works with all compilers
0508ba2a 104 enum { prime_count = 31 };
376e1129 105 static const unsigned long ms_primes[prime_count];
0508ba2a 106
376e1129 107 // returns the first prime in ms_primes greater than n
0508ba2a
MB
108 static unsigned long GetNextPrime( unsigned long n );
109
376e1129
VZ
110 // returns the first prime in ms_primes smaller than n
111 // ( or ms_primes[0] if n is very small )
0508ba2a
MB
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
2b5f62a0 119 static void** AllocTable( size_t sz )
0508ba2a 120 {
2b5f62a0 121 return (void **)calloc(sz, sizeof(void*));
0508ba2a 122 }
f5db5af9
MB
123 static void FreeTable(void *table)
124 {
125 free(table);
126 }
0508ba2a
MB
127};
128
e4c8592e
VZ
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 ) \
376e1129 132CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
0508ba2a
MB
133{ \
134public: \
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; \
bc850f29 149public: \
0508ba2a
MB
150 typedef KEY_EX_T key_extractor; \
151 typedef CLASSNAME Self; \
bc850f29 152protected: \
872051d8 153 _wxHashTable_NodeBase** m_table; \
0508ba2a
MB
154 size_t m_tableBuckets; \
155 size_t m_items; \
156 hasher m_hasher; \
157 key_equal m_equals; \
158 key_extractor m_getKey; \
bc850f29 159public: \
0508ba2a
MB
160 struct Node:public _wxHashTable_NodeBase \
161 { \
162 public: \
163 Node( const value_type& value ) \
164 : m_value( value ) {} \
5c33522f 165 Node* next() { return static_cast<Node*>(m_next); } \
0508ba2a
MB
166 \
167 value_type m_value; \
168 }; \
169 \
bc850f29 170protected: \
0508ba2a
MB
171 static void DeleteNode( _wxHashTable_NodeBase* node ) \
172 { \
5c33522f 173 delete static_cast<Node*>(node); \
0508ba2a 174 } \
bc850f29 175public: \
0508ba2a
MB
176 /* */ \
177 /* forward iterator */ \
178 /* */ \
e1688485 179 CLASSEXP Iterator \
0508ba2a 180 { \
e1688485 181 public: \
0508ba2a
MB
182 Node* m_node; \
183 Self* m_ht; \
184 \
872051d8 185 Iterator() : m_node(NULL), m_ht(NULL) {} \
0508ba2a 186 Iterator( Node* node, const Self* ht ) \
5c33522f 187 : m_node(node), m_ht(const_cast<Self*>(ht)) {} \
0508ba2a
MB
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 { \
376e1129 195 size_type bucket = GetBucketForNode(m_ht,m_node); \
0508ba2a
MB
196 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
197 { \
198 if( m_ht->m_table[i] ) \
5c33522f 199 return static_cast<Node*>(m_ht->m_table[i]); \
0508ba2a 200 } \
872051d8 201 return NULL; \
0508ba2a
MB
202 } \
203 \
204 void PlusPlus() \
205 { \
872051d8 206 Node* next = m_node->next(); \
376e1129 207 m_node = next ? next : GetNextNode(); \
0508ba2a
MB
208 } \
209 }; \
b5dbe15d 210 friend class Iterator; \
0508ba2a
MB
211 \
212public: \
e1688485 213 CLASSEXP iterator : public Iterator \
0508ba2a 214 { \
e1688485 215 public: \
0508ba2a
MB
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; } \
e4c8592e 221 PTROPERATOR(pointer) \
0508ba2a
MB
222 }; \
223 \
e1688485 224 CLASSEXP const_iterator : public Iterator \
0508ba2a 225 { \
e1688485 226 public: \
0508ba2a 227 const_iterator() : Iterator() {} \
cd4e32af 228 const_iterator(iterator i) : Iterator(i) {} \
0508ba2a 229 const_iterator( Node* node, const Self* ht ) \
5c33522f 230 : Iterator(node, const_cast<Self*>(ht)) {} \
0508ba2a
MB
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; } \
e4c8592e 234 PTROPERATOR(const_pointer) \
0508ba2a
MB
235 }; \
236 \
2b5f62a0 237 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
0508ba2a
MB
238 const key_equal& k_eq = key_equal(), \
239 const key_extractor& k_ex = key_extractor() ) \
083f7497 240 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
0508ba2a
MB
241 m_items( 0 ), \
242 m_hasher( hfun ), \
243 m_equals( k_eq ), \
244 m_getKey( k_ex ) \
245 { \
872051d8 246 m_table = (_wxHashTable_NodeBase**)AllocTable(m_tableBuckets); \
0508ba2a
MB
247 } \
248 \
249 CLASSNAME( const Self& ht ) \
872051d8 250 : m_table(NULL), \
0508ba2a
MB
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 { \
162e998c
PC
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 } \
376e1129 271 return *this; \
0508ba2a
MB
272 } \
273 \
274 ~CLASSNAME() \
275 { \
276 clear(); \
277 \
86baa52d 278 FreeTable(m_table); \
0508ba2a
MB
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 ) */ \
2b5f62a0
VZ
286 void clear() \
287 { \
872051d8 288 DeleteNodes(m_tableBuckets, m_table, DeleteNode); \
2b5f62a0
VZ
289 m_items = 0; \
290 } \
0508ba2a
MB
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 \
872051d8
PC
296 const_iterator end() const { return const_iterator(NULL, this); } \
297 iterator end() { return iterator(NULL, this); } \
0508ba2a 298 const_iterator begin() const \
5c33522f 299 { return const_iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \
0508ba2a 300 iterator begin() \
5c33522f 301 { return iterator(static_cast<Node*>(GetFirstNode(m_tableBuckets, m_table)), this); } \
0508ba2a
MB
302 \
303 size_type erase( const const_key_type& key ) \
304 { \
872051d8 305 _wxHashTable_NodeBase** node = GetNodePtr(key); \
0508ba2a
MB
306 \
307 if( !node ) \
308 return 0; \
309 \
310 --m_items; \
872051d8 311 _wxHashTable_NodeBase* temp = (*node)->m_next; \
5c33522f 312 delete static_cast<Node*>(*node); \
0508ba2a
MB
313 (*node) = temp; \
314 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
083f7497 315 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
0508ba2a
MB
316 return 1; \
317 } \
318 \
319protected: \
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 \
fe8604ac 327 Node* GetOrCreateNode( const value_type& value, bool& created ) \
0508ba2a
MB
328 { \
329 const const_key_type& key = m_getKey( value ); \
330 size_t bucket = m_hasher( key ) % m_tableBuckets; \
5c33522f 331 Node* node = static_cast<Node*>(m_table[bucket]); \
0508ba2a
MB
332 \
333 while( node ) \
334 { \
335 if( m_equals( m_getKey( node->m_value ), key ) ) \
fe8604ac
VZ
336 { \
337 created = false; \
0508ba2a 338 return node; \
fe8604ac 339 } \
872051d8 340 node = node->next(); \
0508ba2a 341 } \
fe8604ac
VZ
342 created = true; \
343 return CreateNode( value, bucket); \
ddf5de5b
MB
344 }\
345 Node * CreateNode( const value_type& value, size_t bucket ) \
346 {\
347 Node* node = new Node( value ); \
872051d8 348 node->m_next = m_table[bucket]; \
0508ba2a
MB
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 } \
ddf5de5b
MB
358 void CreateNode( const value_type& value ) \
359 {\
360 CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \
361 }\
0508ba2a
MB
362 \
363 /* returns NULL if not found */ \
872051d8 364 _wxHashTable_NodeBase** GetNodePtr(const const_key_type& key) const \
0508ba2a 365 { \
7c6c0acf 366 size_t bucket = m_hasher( key ) % m_tableBuckets; \
872051d8 367 _wxHashTable_NodeBase** node = &m_table[bucket]; \
0508ba2a
MB
368 \
369 while( *node ) \
370 { \
5c33522f 371 if (m_equals(m_getKey(static_cast<Node*>(*node)->m_value), key)) \
0508ba2a 372 return node; \
872051d8 373 node = &(*node)->m_next; \
0508ba2a
MB
374 } \
375 \
9a71e770 376 return NULL; \
0508ba2a
MB
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 { \
7c6c0acf 383 size_t bucket = m_hasher( key ) % m_tableBuckets; \
5c33522f 384 Node* node = static_cast<Node*>(m_table[bucket]); \
0508ba2a
MB
385 \
386 while( node ) \
387 { \
388 if( m_equals( m_getKey( node->m_value ), key ) ) \
389 return node; \
872051d8 390 node = node->next(); \
0508ba2a
MB
391 } \
392 \
872051d8 393 return NULL; \
0508ba2a
MB
394 } \
395 \
396 void ResizeTable( size_t newSize ) \
397 { \
083f7497 398 newSize = GetNextPrime( (unsigned long)newSize ); \
872051d8 399 _wxHashTable_NodeBase** srcTable = m_table; \
0508ba2a 400 size_t srcBuckets = m_tableBuckets; \
872051d8 401 m_table = (_wxHashTable_NodeBase**)AllocTable( newSize ); \
0508ba2a
MB
402 m_tableBuckets = newSize; \
403 \
872051d8
PC
404 CopyHashTable( srcTable, srcBuckets, \
405 this, m_table, \
1c157205 406 (BucketFromNode)GetBucketForNode,\
0508ba2a 407 (ProcessNode)&DummyProcessNode ); \
86baa52d 408 FreeTable(srcTable); \
0508ba2a
MB
409 } \
410 \
411 /* this must be called _after_ m_table has been cleaned */ \
412 void HashCopy( const Self& ht ) \
413 { \
414 ResizeTable( ht.size() ); \
872051d8 415 CopyHashTable( ht.m_table, ht.m_tableBuckets, \
0508ba2a 416 (_wxHashTableBase2*)this, \
872051d8 417 m_table, \
1c157205
MB
418 (BucketFromNode)GetBucketForNode, \
419 (ProcessNode)CopyNode ); \
0508ba2a
MB
420 } \
421};
422
376e1129
VZ
423// defines an STL-like pair class CLASSNAME storing two fields: first of type
424// KEY_T and second of type VALUE_T
0508ba2a
MB
425#define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
426CLASSEXP CLASSNAME \
427{ \
428public: \
d7ad5a02
VZ
429 typedef KEY_T first_type; \
430 typedef VALUE_T second_type; \
0508ba2a
MB
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 \
162e998c 436 CLASSNAME(const const_t1& f, const const_t2& s) \
5c33522f 437 : first(const_cast<t1&>(f)), second(const_cast<t2&>(s)) {} \
0508ba2a
MB
438 \
439 t1 first; \
440 t2 second; \
441};
442
376e1129
VZ
443// defines the class CLASSNAME returning the key part (of type KEY_T) from a
444// pair of type PAIR_T
0508ba2a
MB
445#define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
446CLASSEXP CLASSNAME \
447{ \
95197553
MB
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; \
0508ba2a 454public: \
376e1129 455 CLASSNAME() { } \
95197553 456 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
376e1129
VZ
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; } \
0508ba2a
MB
462};
463
464// grow/shrink predicates
5d3e7b52
WS
465inline bool never_grow( size_t, size_t ) { return false; }
466inline bool never_shrink( size_t, size_t ) { return false; }
0508ba2a
MB
467inline bool grow_lf70( size_t buckets, size_t items )
468{
4e116788 469 return float(items)/float(buckets) >= 0.85f;
0508ba2a
MB
470}
471
f6a46bb0 472#endif // various hash map implementations
bdcade0a 473
376e1129
VZ
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
f6a46bb0 482#ifndef wxNEEDS_WX_HASH_MAP
bdcade0a
MB
483
484// integer types
17473a77 485struct WXDLLIMPEXP_BASE wxIntegerHash
bdcade0a 486{
17473a77 487private:
bdcade0a
MB
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;
bfe1edbd 494
066e5e3f 495#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
b158be6e
MW
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)
bfe1edbd
MW
499 size_t longlongHash( wxLongLong_t x ) const
500 {
ec2f62b1
MW
501 return longHash( wx_truncate_cast(long, x) ) ^
502 longHash( wx_truncate_cast(long, x >> (sizeof(long) * 8)) );
bfe1edbd 503 }
b158be6e
MW
504 #elif defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG
505 WX_HASH_MAP_NAMESPACE::hash<long> longlongHash;
ec2f62b1
MW
506 #else
507 WX_HASH_MAP_NAMESPACE::hash<wxLongLong_t> longlongHash;
508 #endif
066e5e3f 509#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bfe1edbd 510
bdcade0a
MB
511public:
512 wxIntegerHash() { }
513 size_t operator()( long x ) const { return longHash( x ); }
514 size_t operator()( unsigned long x ) const { return ulongHash( x ); }
515 size_t operator()( int x ) const { return intHash( x ); }
516 size_t operator()( unsigned int x ) const { return uintHash( x ); }
517 size_t operator()( short x ) const { return shortHash( x ); }
518 size_t operator()( unsigned short x ) const { return ushortHash( x ); }
066e5e3f 519#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bfe1edbd
MW
520 size_t operator()( wxLongLong_t x ) const { return longlongHash(x); }
521 size_t operator()( wxULongLong_t x ) const { return longlongHash(x); }
066e5e3f 522#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bdcade0a
MB
523
524 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
525};
526
f6a46bb0 527#else // wxNEEDS_WX_HASH_MAP
bdcade0a 528
0508ba2a 529// integer types
17473a77 530struct WXDLLIMPEXP_BASE wxIntegerHash
0508ba2a 531{
376e1129 532 wxIntegerHash() { }
0508ba2a
MB
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; }
376e1129
VZ
537 unsigned long operator()( short x ) const { return (unsigned long)x; }
538 unsigned long operator()( unsigned short x ) const { return x; }
066e5e3f 539#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
5c33522f 540 wxULongLong_t operator()( wxLongLong_t x ) const { return static_cast<wxULongLong_t>(x); }
bfe1edbd 541 wxULongLong_t operator()( wxULongLong_t x ) const { return x; }
066e5e3f 542#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
376e1129
VZ
543
544 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
0508ba2a
MB
545};
546
f6a46bb0 547#endif // !wxNEEDS_WX_HASH_MAP/wxNEEDS_WX_HASH_MAP
bdcade0a 548
17473a77 549struct WXDLLIMPEXP_BASE wxIntegerEqual
0508ba2a 550{
376e1129 551 wxIntegerEqual() { }
0508ba2a 552 bool operator()( long a, long b ) const { return a == b; }
376e1129 553 bool operator()( unsigned long a, unsigned long b ) const { return a == b; }
0508ba2a 554 bool operator()( int a, int b ) const { return a == b; }
376e1129
VZ
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; }
066e5e3f 558#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
bfe1edbd
MW
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; }
066e5e3f 561#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
376e1129
VZ
562
563 wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; }
0508ba2a
MB
564};
565
566// pointers
17473a77 567struct WXDLLIMPEXP_BASE wxPointerHash
0508ba2a 568{
376e1129
VZ
569 wxPointerHash() { }
570
f6a46bb0 571#ifdef wxNEEDS_WX_HASH_MAP
f1288544 572 wxUIntPtr operator()( const void* k ) const { return wxPtrToUInt(k); }
f6a46bb0
VZ
573#else
574 size_t operator()( const void* k ) const { return (size_t)k; }
bdcade0a 575#endif
376e1129
VZ
576
577 wxPointerHash& operator=(const wxPointerHash&) { return *this; }
0508ba2a
MB
578};
579
17473a77 580struct WXDLLIMPEXP_BASE wxPointerEqual
0508ba2a 581{
376e1129
VZ
582 wxPointerEqual() { }
583 bool operator()( const void* a, const void* b ) const { return a == b; }
584
585 wxPointerEqual& operator=(const wxPointerEqual&) { return *this; }
0508ba2a
MB
586};
587
ad78ab8c 588// wxString, char*, wchar_t*
17473a77 589struct WXDLLIMPEXP_BASE wxStringHash
0508ba2a 590{
376e1129 591 wxStringHash() {}
0508ba2a 592 unsigned long operator()( const wxString& x ) const
ad78ab8c
VS
593 { return stringHash( x.wx_str() ); }
594 unsigned long operator()( const wchar_t* x ) const
595 { return stringHash( x ); }
0508ba2a 596 unsigned long operator()( const char* x ) const
ad78ab8c
VS
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* );
376e1129
VZ
610
611 wxStringHash& operator=(const wxStringHash&) { return *this; }
0508ba2a
MB
612};
613
17473a77 614struct WXDLLIMPEXP_BASE wxStringEqual
0508ba2a 615{
376e1129 616 wxStringEqual() {}
0508ba2a
MB
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; }
376e1129
VZ
624#endif // wxUSE_UNICODE
625
626 wxStringEqual& operator=(const wxStringEqual&) { return *this; }
0508ba2a
MB
627};
628
f6a46bb0 629#ifdef wxNEEDS_WX_HASH_MAP
bdcade0a 630
e4c8592e
VZ
631#define wxPTROP_NORMAL(pointer) \
632 pointer operator ->() const { return &(m_node->m_value); }
633#define wxPTROP_NOP(pointer)
634
0508ba2a
MB
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 ) \
e4c8592e
VZ
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 ) \
0508ba2a
MB
641CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
642{ \
643public: \
644 typedef VALUE_T mapped_type; \
fe8604ac 645 _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \
0508ba2a 646 \
d8771ac7
MB
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() ) {} \
0508ba2a
MB
651 \
652 mapped_type& operator[]( const const_key_type& key ) \
653 { \
fe8604ac
VZ
654 bool created; \
655 return GetOrCreateNode( \
656 CLASSNAME##_wxImplementation_Pair( key, mapped_type() ), \
657 created)->m_value.second; \
0508ba2a
MB
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 \
fe8604ac
VZ
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); \
fe8604ac
VZ
676 return Insert_Result(iterator(node, this), created); \
677 } \
0508ba2a
MB
678 \
679 size_type erase( const key_type& k ) \
680 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
e4c8592e 681 void erase( const iterator& it ) { erase( (*it).first ); } \
0508ba2a
MB
682 \
683 /* count() == 0 | 1 */ \
684 size_type count( const const_key_type& key ) \
3952889a 685 { \
2415cf67 686 return GetNode( key ) ? 1u : 0u; \
3952889a 687 } \
3f5c62f9 688}
0508ba2a 689
f6a46bb0 690#endif // wxNEEDS_WX_HASH_MAP
bdcade0a 691
0508ba2a
MB
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, \
d86939c4 698 CLASSNAME, class )
0508ba2a
MB
699
700#define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
701 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
d86939c4 702 CLASSNAME, class )
0508ba2a
MB
703
704// and these do exactly the same thing but should be used inside the
705// library
c0421c99
VS
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
0508ba2a 709#define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
c0421c99 710 WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, \
53a2db12 711 CLASSNAME, class WXDLLIMPEXP_CORE )
0508ba2a 712
c0421c99 713#define WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
0508ba2a 714 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
c0421c99 715 CLASSNAME, DECL )
0508ba2a 716
c0421c99
VS
717#define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
718 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \
53a2db12 719 class WXDLLIMPEXP_CORE )
c0421c99
VS
720
721#define WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
0508ba2a 722 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
c0421c99
VS
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, \
53a2db12 727 class WXDLLIMPEXP_CORE )
0508ba2a 728
df5168c4
MB
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 { \
486f0e76 737 type::iterator it, en; \
df5168c4
MB
738 for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \
739 delete it->second; \
740 (hashmap).clear(); \
741 }
742
0fab3a5a
RD
743//---------------------------------------------------------------------------
744// Declarations of common hashmap classes
745
746WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual,
3f5c62f9 747 wxLongToLongHashMap, class WXDLLIMPEXP_BASE );
0fab3a5a 748
730b772b
FM
749WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxString, wxStringToStringHashMap,
750 class WXDLLIMPEXP_BASE );
751
af588446
VZ
752WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxUIntPtr, wxStringToNumHashMap,
753 class WXDLLIMPEXP_BASE );
754
0fab3a5a 755
376e1129 756#endif // _WX_HASHMAP_H_