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