| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/hashset.h |
| 3 | // Purpose: wxHashSet class |
| 4 | // Author: Mattia Barbon |
| 5 | // Modified by: |
| 6 | // Created: 11/08/2003 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Mattia Barbon |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_HASHSET_H_ |
| 13 | #define _WX_HASHSET_H_ |
| 14 | |
| 15 | #include "wx/hashmap.h" |
| 16 | |
| 17 | // see comment in wx/hashmap.h which also applies to different standard hash |
| 18 | // set classes |
| 19 | |
| 20 | #if wxUSE_STL && \ |
| 21 | (defined(HAVE_STD_UNORDERED_SET) || defined(HAVE_TR1_UNORDERED_SET)) |
| 22 | |
| 23 | #if defined(HAVE_STD_UNORDERED_SET) |
| 24 | #include <unordered_set> |
| 25 | #define WX_HASH_SET_BASE_TEMPLATE std::unordered_set |
| 26 | #elif defined(HAVE_TR1_UNORDERED_SET) |
| 27 | #include <tr1/unordered_set> |
| 28 | #define WX_HASH_SET_BASE_TEMPLATE std::tr1::unordered_set |
| 29 | #else |
| 30 | #error Update this code: unordered_set is available, but I do not know where. |
| 31 | #endif |
| 32 | |
| 33 | #elif wxUSE_STL && defined(HAVE_STL_HASH_MAP) |
| 34 | |
| 35 | #if defined(HAVE_EXT_HASH_MAP) |
| 36 | #include <ext/hash_set> |
| 37 | #elif defined(HAVE_HASH_MAP) |
| 38 | #include <hash_set> |
| 39 | #endif |
| 40 | |
| 41 | #define WX_HASH_SET_BASE_TEMPLATE WX_HASH_MAP_NAMESPACE::hash_set |
| 42 | |
| 43 | #endif // different hash_set/unordered_set possibilities |
| 44 | |
| 45 | #ifdef WX_HASH_SET_BASE_TEMPLATE |
| 46 | |
| 47 | // we need to define the class declared by _WX_DECLARE_HASH_SET as a class and |
| 48 | // not a typedef to allow forward declaring it |
| 49 | #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ |
| 50 | CLASSEXP CLASSNAME \ |
| 51 | : public WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T > \ |
| 52 | { \ |
| 53 | public: \ |
| 54 | explicit CLASSNAME(size_type n = 3, \ |
| 55 | const hasher& h = hasher(), \ |
| 56 | const key_equal& ke = key_equal(), \ |
| 57 | const allocator_type& a = allocator_type()) \ |
| 58 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(n, h, ke, a) \ |
| 59 | {} \ |
| 60 | template <class InputIterator> \ |
| 61 | CLASSNAME(InputIterator f, InputIterator l, \ |
| 62 | const hasher& h = hasher(), \ |
| 63 | const key_equal& ke = key_equal(), \ |
| 64 | const allocator_type& a = allocator_type()) \ |
| 65 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(f, l, h, ke, a)\ |
| 66 | {} \ |
| 67 | CLASSNAME(const WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >& s) \ |
| 68 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(s) \ |
| 69 | {} \ |
| 70 | } |
| 71 | |
| 72 | #else // no appropriate STL class, use our own implementation |
| 73 | |
| 74 | // this is a complex way of defining an easily inlineable identity function... |
| 75 | #define _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME, CLASSEXP ) \ |
| 76 | CLASSEXP CLASSNAME \ |
| 77 | { \ |
| 78 | typedef KEY_T key_type; \ |
| 79 | typedef const key_type const_key_type; \ |
| 80 | typedef const_key_type& const_key_reference; \ |
| 81 | public: \ |
| 82 | CLASSNAME() { } \ |
| 83 | const_key_reference operator()( const_key_reference key ) const \ |
| 84 | { return key; } \ |
| 85 | \ |
| 86 | /* the dummy assignment operator is needed to suppress compiler */ \ |
| 87 | /* warnings from hash table class' operator=(): gcc complains about */ \ |
| 88 | /* "statement with no effect" without it */ \ |
| 89 | CLASSNAME& operator=(const CLASSNAME&) { return *this; } \ |
| 90 | }; |
| 91 | |
| 92 | #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP )\ |
| 93 | _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \ |
| 94 | _WX_DECLARE_HASHTABLE( KEY_T, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \ |
| 95 | CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \ |
| 96 | { \ |
| 97 | public: \ |
| 98 | _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \ |
| 99 | \ |
| 100 | wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \ |
| 101 | key_equal eq = key_equal() ) \ |
| 102 | : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \ |
| 103 | CLASSNAME##_wxImplementation_KeyEx() ) {} \ |
| 104 | \ |
| 105 | Insert_Result insert( const key_type& key ) \ |
| 106 | { \ |
| 107 | bool created; \ |
| 108 | Node *node = GetOrCreateNode( key, created ); \ |
| 109 | return Insert_Result( iterator( node, this ), created ); \ |
| 110 | } \ |
| 111 | \ |
| 112 | const_iterator find( const const_key_type& key ) const \ |
| 113 | { \ |
| 114 | return const_iterator( GetNode( key ), this ); \ |
| 115 | } \ |
| 116 | \ |
| 117 | iterator find( const const_key_type& key ) \ |
| 118 | { \ |
| 119 | return iterator( GetNode( key ), this ); \ |
| 120 | } \ |
| 121 | \ |
| 122 | size_type erase( const key_type& k ) \ |
| 123 | { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \ |
| 124 | void erase( const iterator& it ) { erase( *it ); } \ |
| 125 | void erase( const const_iterator& it ) { erase( *it ); } \ |
| 126 | \ |
| 127 | /* count() == 0 | 1 */ \ |
| 128 | size_type count( const const_key_type& key ) const \ |
| 129 | { return GetNode( key ) ? 1 : 0; } \ |
| 130 | } |
| 131 | |
| 132 | #endif // STL/wx implementations |
| 133 | |
| 134 | |
| 135 | // these macros are to be used in the user code |
| 136 | #define WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \ |
| 137 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, class ) |
| 138 | |
| 139 | // and these do exactly the same thing but should be used inside the |
| 140 | // library |
| 141 | #define WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \ |
| 142 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL ) |
| 143 | |
| 144 | #define WX_DECLARE_EXPORTED_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \ |
| 145 | WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, \ |
| 146 | CLASSNAME, class WXDLLIMPEXP_CORE ) |
| 147 | |
| 148 | // delete all hash elements |
| 149 | // |
| 150 | // NB: the class declaration of the hash elements must be visible from the |
| 151 | // place where you use this macro, otherwise the proper destructor may not |
| 152 | // be called (a decent compiler should give a warning about it, but don't |
| 153 | // count on it)! |
| 154 | #define WX_CLEAR_HASH_SET(type, hashset) \ |
| 155 | { \ |
| 156 | type::iterator it, en; \ |
| 157 | for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it ) \ |
| 158 | delete *it; \ |
| 159 | (hashset).clear(); \ |
| 160 | } |
| 161 | |
| 162 | #endif // _WX_HASHSET_H_ |