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