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