]> git.saurik.com Git - wxWidgets.git/blob - include/wx/hashmap.h
fixed wxHTML parsing to run in O(n) even in UTF8 build
[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 #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 ) \
38 typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME
39
40 #else // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
41
42
43 #ifdef __WXWINCE__
44 typedef int ptrdiff_t;
45 #else
46 #include <stddef.h> // for ptrdiff_t
47 #endif
48
49 // private
50 struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase
51 {
52 _wxHashTable_NodeBase() : m_nxt(0) {}
53
54 _wxHashTable_NodeBase* m_nxt;
55
56 // Cannot do this:
57 // DECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase)
58 // without rewriting the macros, which require a public copy constructor.
59 };
60
61 // private
62 class WXDLLIMPEXP_BASE _wxHashTableBase2
63 {
64 public:
65 typedef void (*NodeDtor)(_wxHashTable_NodeBase*);
66 typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*);
67 typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*);
68 protected:
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];
78 return 0;
79 }
80
81 // as static const unsigned prime_count = 31 but works with all compilers
82 enum { prime_count = 31 };
83 static const unsigned long ms_primes[prime_count];
84
85 // returns the first prime in ms_primes greater than n
86 static unsigned long GetNextPrime( unsigned long n );
87
88 // returns the first prime in ms_primes smaller than n
89 // ( or ms_primes[0] if n is very small )
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
97 static void** AllocTable( size_t sz )
98 {
99 return (void **)calloc(sz, sizeof(void*));
100 }
101 static void FreeTable(void *table)
102 {
103 free(table);
104 }
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 ) \
108 CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
109 { \
110 public: \
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; \
125 public: \
126 struct Node; \
127 typedef KEY_EX_T key_extractor; \
128 typedef CLASSNAME Self; \
129 protected: \
130 Node** m_table; \
131 size_t m_tableBuckets; \
132 size_t m_items; \
133 hasher m_hasher; \
134 key_equal m_equals; \
135 key_extractor m_getKey; \
136 public: \
137 struct Node:public _wxHashTable_NodeBase \
138 { \
139 public: \
140 Node( const value_type& value ) \
141 : m_value( value ) {} \
142 Node* m_next() { return (Node*)this->m_nxt; } \
143 \
144 value_type m_value; \
145 }; \
146 \
147 protected: \
148 static void DeleteNode( _wxHashTable_NodeBase* node ) \
149 { \
150 delete (Node*)node; \
151 } \
152 public: \
153 /* */ \
154 /* forward iterator */ \
155 /* */ \
156 CLASSEXP Iterator \
157 { \
158 public: \
159 Node* m_node; \
160 Self* m_ht; \
161 \
162 Iterator() : m_node(0), m_ht(0) {} \
163 Iterator( Node* node, const Self* ht ) \
164 : m_node(node), m_ht((Self*)ht) {} \
165 bool operator ==( const Iterator& it ) const \
166 { return m_node == it.m_node; } \
167 bool operator !=( const Iterator& it ) const \
168 { return m_node != it.m_node; } \
169 protected: \
170 Node* GetNextNode() \
171 { \
172 size_type bucket = GetBucketForNode(m_ht,m_node); \
173 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
174 { \
175 if( m_ht->m_table[i] ) \
176 return m_ht->m_table[i]; \
177 } \
178 return 0; \
179 } \
180 \
181 void PlusPlus() \
182 { \
183 Node* next = m_node->m_next(); \
184 m_node = next ? next : GetNextNode(); \
185 } \
186 }; \
187 friend class Iterator; \
188 \
189 public: \
190 CLASSEXP iterator : public Iterator \
191 { \
192 public: \
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 \
201 CLASSEXP const_iterator : public Iterator \
202 { \
203 public: \
204 const_iterator() : Iterator() {} \
205 const_iterator(iterator i) : Iterator(i) {} \
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 \
214 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
215 const key_equal& k_eq = key_equal(), \
216 const key_extractor& k_ex = key_extractor() ) \
217 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
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 ); \
245 return *this; \
246 } \
247 \
248 ~CLASSNAME() \
249 { \
250 clear(); \
251 \
252 FreeTable(m_table); \
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 ) */ \
260 void clear() \
261 { \
262 DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \
263 DeleteNode ); \
264 m_items = 0; \
265 } \
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 ) ) \
290 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
291 return 1; \
292 } \
293 \
294 protected: \
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 \
302 Node* GetOrCreateNode( const value_type& value, bool& created ) \
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 ) ) \
311 { \
312 created = false; \
313 return node; \
314 } \
315 node = node->m_next(); \
316 } \
317 created = true; \
318 return CreateNode( value, bucket); \
319 }\
320 Node * CreateNode( const value_type& value, size_t bucket ) \
321 {\
322 Node* node = new Node( value ); \
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 } \
333 void CreateNode( const value_type& value ) \
334 {\
335 CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \
336 }\
337 \
338 /* returns NULL if not found */ \
339 Node** GetNodePtr( const const_key_type& key ) const \
340 { \
341 size_t bucket = m_hasher( key ) % m_tableBuckets; \
342 Node** node = &m_table[bucket]; \
343 \
344 while( *node ) \
345 { \
346 if( m_equals( m_getKey( (*node)->m_value ), key ) ) \
347 return node; \
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; \
350 } \
351 \
352 return NULL; \
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 { \
359 size_t bucket = m_hasher( key ) % m_tableBuckets; \
360 Node* node = m_table[bucket]; \
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 { \
374 newSize = GetNextPrime( (unsigned long)newSize ); \
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, \
381 this, (_wxHashTable_NodeBase**)m_table, \
382 (BucketFromNode)GetBucketForNode,\
383 (ProcessNode)&DummyProcessNode ); \
384 FreeTable(srcTable); \
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, \
393 (_wxHashTable_NodeBase**)m_table, \
394 (BucketFromNode)GetBucketForNode, \
395 (ProcessNode)CopyNode ); \
396 } \
397 };
398
399 // defines an STL-like pair class CLASSNAME storing two fields: first of type
400 // KEY_T and second of type VALUE_T
401 #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
402 CLASSEXP CLASSNAME \
403 { \
404 public: \
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)) {} \
411 \
412 t1 first; \
413 t2 second; \
414 };
415
416 // defines the class CLASSNAME returning the key part (of type KEY_T) from a
417 // pair of type PAIR_T
418 #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
419 CLASSEXP CLASSNAME \
420 { \
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; \
427 public: \
428 CLASSNAME() { } \
429 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
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; } \
435 };
436
437 // grow/shrink predicates
438 inline bool never_grow( size_t, size_t ) { return false; }
439 inline bool never_shrink( size_t, size_t ) { return false; }
440 inline bool grow_lf70( size_t buckets, size_t items )
441 {
442 return float(items)/float(buckets) >= 0.85;
443 }
444
445 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
446
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
455 #if wxUSE_STL && defined(HAVE_STL_HASH_MAP)
456
457 // integer types
458 class 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;
466
467 #if defined wxLongLong_t && !defined wxLongLongIsLong
468 // hash<wxLongLong_t> ought to work but doesn't on some compilers
469 #if (!defined SIZEOF_LONG_LONG && SIZEOF_LONG == 4) \
470 || (defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG * 2)
471 size_t longlongHash( wxLongLong_t x ) const
472 {
473 return longHash( wx_truncate_cast(long, x) ) ^
474 longHash( wx_truncate_cast(long, x >> (sizeof(long) * 8)) );
475 }
476 #elif defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG
477 WX_HASH_MAP_NAMESPACE::hash<long> longlongHash;
478 #else
479 WX_HASH_MAP_NAMESPACE::hash<wxLongLong_t> longlongHash;
480 #endif
481 #endif
482
483 public:
484 wxIntegerHash() { }
485 size_t operator()( long x ) const { return longHash( x ); }
486 size_t operator()( unsigned long x ) const { return ulongHash( x ); }
487 size_t operator()( int x ) const { return intHash( x ); }
488 size_t operator()( unsigned int x ) const { return uintHash( x ); }
489 size_t operator()( short x ) const { return shortHash( x ); }
490 size_t operator()( unsigned short x ) const { return ushortHash( x ); }
491 #if defined wxLongLong_t && !defined wxLongLongIsLong
492 size_t operator()( wxLongLong_t x ) const { return longlongHash(x); }
493 size_t operator()( wxULongLong_t x ) const { return longlongHash(x); }
494 #endif
495
496 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
497 };
498
499 #else // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
500
501 // integer types
502 class WXDLLIMPEXP_BASE wxIntegerHash
503 {
504 public:
505 wxIntegerHash() { }
506 unsigned long operator()( long x ) const { return (unsigned long)x; }
507 unsigned long operator()( unsigned long x ) const { return x; }
508 unsigned long operator()( int x ) const { return (unsigned long)x; }
509 unsigned long operator()( unsigned int x ) const { return x; }
510 unsigned long operator()( short x ) const { return (unsigned long)x; }
511 unsigned long operator()( unsigned short x ) const { return x; }
512 #if defined wxLongLong_t && !defined wxLongLongIsLong
513 wxULongLong_t operator()( wxLongLong_t x ) const { return wx_static_cast(wxULongLong_t, x); }
514 wxULongLong_t operator()( wxULongLong_t x ) const { return x; }
515 #endif
516
517 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
518 };
519
520 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
521
522 class WXDLLIMPEXP_BASE wxIntegerEqual
523 {
524 public:
525 wxIntegerEqual() { }
526 bool operator()( long a, long b ) const { return a == b; }
527 bool operator()( unsigned long a, unsigned long b ) const { return a == b; }
528 bool operator()( int a, int b ) const { return a == b; }
529 bool operator()( unsigned int a, unsigned int b ) const { return a == b; }
530 bool operator()( short a, short b ) const { return a == b; }
531 bool operator()( unsigned short a, unsigned short b ) const { return a == b; }
532 #if defined wxLongLong_t && !defined wxLongLongIsLong
533 bool operator()( wxLongLong_t a, wxLongLong_t b ) const { return a == b; }
534 bool operator()( wxULongLong_t a, wxULongLong_t b ) const { return a == b; }
535 #endif
536
537 wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; }
538 };
539
540 // pointers
541 class WXDLLIMPEXP_BASE wxPointerHash
542 {
543 public:
544 wxPointerHash() { }
545
546 #if wxUSE_STL && defined(HAVE_STL_HASH_MAP)
547 size_t operator()( const void* k ) const { return (size_t)k; }
548 #else
549 wxUIntPtr operator()( const void* k ) const { return wxPtrToUInt(k); }
550 #endif
551
552 wxPointerHash& operator=(const wxPointerHash&) { return *this; }
553 };
554
555 class WXDLLIMPEXP_BASE wxPointerEqual
556 {
557 public:
558 wxPointerEqual() { }
559 bool operator()( const void* a, const void* b ) const { return a == b; }
560
561 wxPointerEqual& operator=(const wxPointerEqual&) { return *this; }
562 };
563
564 // wxString, char*, wchar_t*
565 class WXDLLIMPEXP_BASE wxStringHash
566 {
567 public:
568 wxStringHash() {}
569 unsigned long operator()( const wxString& x ) const
570 { return stringHash( x.wx_str() ); }
571 unsigned long operator()( const wchar_t* x ) const
572 { return stringHash( x ); }
573 unsigned long operator()( const char* x ) const
574 { return stringHash( x ); }
575
576 #if WXWIN_COMPATIBILITY_2_8
577 static unsigned long wxCharStringHash( const wxChar* x )
578 { return stringHash(x); }
579 #if wxUSE_UNICODE
580 static unsigned long charStringHash( const char* x )
581 { return stringHash(x); }
582 #endif
583 #endif // WXWIN_COMPATIBILITY_2_8
584
585 static unsigned long stringHash( const wchar_t* );
586 static unsigned long stringHash( const char* );
587
588 wxStringHash& operator=(const wxStringHash&) { return *this; }
589 };
590
591 class WXDLLIMPEXP_BASE wxStringEqual
592 {
593 public:
594 wxStringEqual() {}
595 bool operator()( const wxString& a, const wxString& b ) const
596 { return a == b; }
597 bool operator()( const wxChar* a, const wxChar* b ) const
598 { return wxStrcmp( a, b ) == 0; }
599 #if wxUSE_UNICODE
600 bool operator()( const char* a, const char* b ) const
601 { return strcmp( a, b ) == 0; }
602 #endif // wxUSE_UNICODE
603
604 wxStringEqual& operator=(const wxStringEqual&) { return *this; }
605 };
606
607 #if !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
608
609 #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
610 _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
611 _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
612 _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
613 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
614 { \
615 public: \
616 typedef VALUE_T mapped_type; \
617 _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \
618 \
619 wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \
620 key_equal eq = key_equal() ) \
621 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \
622 CLASSNAME##_wxImplementation_KeyEx() ) {} \
623 \
624 mapped_type& operator[]( const const_key_type& key ) \
625 { \
626 bool created; \
627 return GetOrCreateNode( \
628 CLASSNAME##_wxImplementation_Pair( key, mapped_type() ), \
629 created)->m_value.second; \
630 } \
631 \
632 const_iterator find( const const_key_type& key ) const \
633 { \
634 return const_iterator( GetNode( key ), this ); \
635 } \
636 \
637 iterator find( const const_key_type& key ) \
638 { \
639 return iterator( GetNode( key ), this ); \
640 } \
641 \
642 Insert_Result insert( const value_type& v ) \
643 { \
644 bool created; \
645 Node *node = GetOrCreateNode( \
646 CLASSNAME##_wxImplementation_Pair( v.first, v.second ), \
647 created); \
648 if ( !created ) \
649 node->m_value.second = v.second; \
650 return Insert_Result(iterator(node, this), created); \
651 } \
652 \
653 size_type erase( const key_type& k ) \
654 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
655 void erase( const iterator& it ) { erase( it->first ); } \
656 void erase( const const_iterator& it ) { erase( it->first ); } \
657 \
658 /* count() == 0 | 1 */ \
659 size_type count( const const_key_type& key ) \
660 { \
661 /* explicit cast needed to suppress CodeWarrior warnings */ \
662 return (size_type)(GetNode( key ) ? 1 : 0); \
663 } \
664 }
665
666 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
667
668 // these macros are to be used in the user code
669 #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
670 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
671
672 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
673 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
674 CLASSNAME, class )
675
676 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
677 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
678 CLASSNAME, class )
679
680 // and these do exactly the same thing but should be used inside the
681 // library
682 #define WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \
683 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL )
684
685 #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
686 WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, \
687 CLASSNAME, class WXDLLEXPORT )
688
689 #define WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
690 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
691 CLASSNAME, DECL )
692
693 #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
694 WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \
695 class WXDLLEXPORT )
696
697 #define WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \
698 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
699 CLASSNAME, DECL )
700
701 #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
702 WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \
703 class WXDLLEXPORT )
704
705 // delete all hash elements
706 //
707 // NB: the class declaration of the hash elements must be visible from the
708 // place where you use this macro, otherwise the proper destructor may not
709 // be called (a decent compiler should give a warning about it, but don't
710 // count on it)!
711 #define WX_CLEAR_HASH_MAP(type, hashmap) \
712 { \
713 type::iterator it, en; \
714 for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \
715 delete it->second; \
716 (hashmap).clear(); \
717 }
718
719 //---------------------------------------------------------------------------
720 // Declarations of common hashmap classes
721
722 WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual,
723 wxLongToLongHashMap, class WXDLLIMPEXP_BASE );
724
725
726 #endif // _WX_HASHMAP_H_