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