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