]>
Commit | Line | Data |
---|---|---|
8142d704 MB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/hashset.h | |
3 | // Purpose: wxHashSet class | |
4 | // Author: Mattia Barbon | |
5 | // Modified by: | |
6 | // Created: 11/08/2003 | |
8142d704 | 7 | // Copyright: (c) Mattia Barbon |
65571936 | 8 | // Licence: wxWindows licence |
8142d704 MB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifndef _WX_HASHSET_H_ | |
12 | #define _WX_HASHSET_H_ | |
13 | ||
14 | #include "wx/hashmap.h" | |
15 | ||
f380544a VZ |
16 | // see comment in wx/hashmap.h which also applies to different standard hash |
17 | // set classes | |
18 | ||
01871bf6 | 19 | #if wxUSE_STD_CONTAINERS && \ |
f380544a VZ |
20 | (defined(HAVE_STD_UNORDERED_SET) || defined(HAVE_TR1_UNORDERED_SET)) |
21 | ||
22 | #if defined(HAVE_STD_UNORDERED_SET) | |
23 | #include <unordered_set> | |
5e1d513e | 24 | #define WX_HASH_SET_BASE_TEMPLATE std::unordered_set |
f380544a VZ |
25 | #elif defined(HAVE_TR1_UNORDERED_SET) |
26 | #include <tr1/unordered_set> | |
5e1d513e | 27 | #define WX_HASH_SET_BASE_TEMPLATE std::tr1::unordered_set |
f380544a | 28 | #else |
5e1d513e | 29 | #error Update this code: unordered_set is available, but I do not know where. |
f380544a VZ |
30 | #endif |
31 | ||
01871bf6 | 32 | #elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP) |
bdcade0a MB |
33 | |
34 | #if defined(HAVE_EXT_HASH_MAP) | |
35 | #include <ext/hash_set> | |
36 | #elif defined(HAVE_HASH_MAP) | |
37 | #include <hash_set> | |
38 | #endif | |
39 | ||
5e1d513e VZ |
40 | #define WX_HASH_SET_BASE_TEMPLATE WX_HASH_MAP_NAMESPACE::hash_set |
41 | ||
42 | #endif // different hash_set/unordered_set possibilities | |
43 | ||
44 | #ifdef WX_HASH_SET_BASE_TEMPLATE | |
bdcade0a | 45 | |
5e1d513e VZ |
46 | // we need to define the class declared by _WX_DECLARE_HASH_SET as a class and |
47 | // not a typedef to allow forward declaring it | |
7a7fa93b | 48 | #define _WX_DECLARE_HASH_SET_IMPL( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP ) \ |
5e1d513e VZ |
49 | CLASSEXP CLASSNAME \ |
50 | : public WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T > \ | |
51 | { \ | |
52 | public: \ | |
53 | explicit CLASSNAME(size_type n = 3, \ | |
54 | const hasher& h = hasher(), \ | |
55 | const key_equal& ke = key_equal(), \ | |
56 | const allocator_type& a = allocator_type()) \ | |
57 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(n, h, ke, a) \ | |
58 | {} \ | |
59 | template <class InputIterator> \ | |
60 | CLASSNAME(InputIterator f, InputIterator l, \ | |
61 | const hasher& h = hasher(), \ | |
62 | const key_equal& ke = key_equal(), \ | |
63 | const allocator_type& a = allocator_type()) \ | |
64 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(f, l, h, ke, a)\ | |
65 | {} \ | |
66 | CLASSNAME(const WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >& s) \ | |
67 | : WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T >(s) \ | |
68 | {} \ | |
69 | } | |
70 | ||
7a7fa93b VZ |
71 | // In some standard library implementations (in particular, the libstdc++ that |
72 | // ships with g++ 4.7), std::unordered_set inherits privately from its hasher | |
73 | // and comparator template arguments for purposes of empty base optimization. | |
74 | // As a result, in the declaration of a class deriving from std::unordered_set | |
75 | // the names of the hasher and comparator classes are interpreted as naming | |
76 | // the base class which is inaccessible. | |
77 | // The workaround is to prefix the class names with 'struct'; however, don't | |
78 | // do this on MSVC because it causes a warning there if the class was | |
79 | // declared as a 'class' rather than a 'struct' (and MSVC's std::unordered_set | |
80 | // implementation does not suffer from the access problem). | |
81 | #ifdef _MSC_VER | |
82 | #define WX_MAYBE_PREFIX_WITH_STRUCT(STRUCTNAME) STRUCTNAME | |
83 | #else | |
84 | #define WX_MAYBE_PREFIX_WITH_STRUCT(STRUCTNAME) struct STRUCTNAME | |
85 | #endif | |
86 | ||
87 | #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP ) \ | |
88 | _WX_DECLARE_HASH_SET_IMPL( \ | |
89 | KEY_T, \ | |
90 | WX_MAYBE_PREFIX_WITH_STRUCT(HASH_T), \ | |
91 | WX_MAYBE_PREFIX_WITH_STRUCT(KEY_EQ_T), \ | |
92 | PTROP, \ | |
93 | CLASSNAME, \ | |
94 | CLASSEXP) | |
95 | ||
5e1d513e | 96 | #else // no appropriate STL class, use our own implementation |
bdcade0a | 97 | |
8142d704 MB |
98 | // this is a complex way of defining an easily inlineable identity function... |
99 | #define _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME, CLASSEXP ) \ | |
100 | CLASSEXP CLASSNAME \ | |
101 | { \ | |
102 | typedef KEY_T key_type; \ | |
103 | typedef const key_type const_key_type; \ | |
104 | typedef const_key_type& const_key_reference; \ | |
105 | public: \ | |
106 | CLASSNAME() { } \ | |
107 | const_key_reference operator()( const_key_reference key ) const \ | |
108 | { return key; } \ | |
109 | \ | |
110 | /* the dummy assignment operator is needed to suppress compiler */ \ | |
111 | /* warnings from hash table class' operator=(): gcc complains about */ \ | |
112 | /* "statement with no effect" without it */ \ | |
113 | CLASSNAME& operator=(const CLASSNAME&) { return *this; } \ | |
114 | }; | |
115 | ||
e4c8592e | 116 | #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP )\ |
8142d704 | 117 | _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \ |
e4c8592e VZ |
118 | _WX_DECLARE_HASHTABLE( KEY_T, KEY_T, HASH_T, \ |
119 | CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, PTROP, \ | |
120 | CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \ | |
8142d704 MB |
121 | CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \ |
122 | { \ | |
123 | public: \ | |
d7ab66a1 MB |
124 | _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \ |
125 | \ | |
d8771ac7 MB |
126 | wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \ |
127 | key_equal eq = key_equal() ) \ | |
8142d704 MB |
128 | : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \ |
129 | CLASSNAME##_wxImplementation_KeyEx() ) {} \ | |
130 | \ | |
d7ab66a1 | 131 | Insert_Result insert( const key_type& key ) \ |
8142d704 | 132 | { \ |
d7ab66a1 MB |
133 | bool created; \ |
134 | Node *node = GetOrCreateNode( key, created ); \ | |
135 | return Insert_Result( iterator( node, this ), created ); \ | |
8142d704 MB |
136 | } \ |
137 | \ | |
138 | const_iterator find( const const_key_type& key ) const \ | |
139 | { \ | |
140 | return const_iterator( GetNode( key ), this ); \ | |
141 | } \ | |
142 | \ | |
143 | iterator find( const const_key_type& key ) \ | |
144 | { \ | |
145 | return iterator( GetNode( key ), this ); \ | |
146 | } \ | |
147 | \ | |
148 | size_type erase( const key_type& k ) \ | |
149 | { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \ | |
150 | void erase( const iterator& it ) { erase( *it ); } \ | |
151 | void erase( const const_iterator& it ) { erase( *it ); } \ | |
152 | \ | |
153 | /* count() == 0 | 1 */ \ | |
fa531da0 | 154 | size_type count( const const_key_type& key ) const \ |
8142d704 | 155 | { return GetNode( key ) ? 1 : 0; } \ |
3f5c62f9 | 156 | } |
8142d704 | 157 | |
5e1d513e VZ |
158 | #endif // STL/wx implementations |
159 | ||
bdcade0a | 160 | |
8142d704 MB |
161 | // these macros are to be used in the user code |
162 | #define WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \ | |
e4c8592e | 163 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, class ) |
8142d704 MB |
164 | |
165 | // and these do exactly the same thing but should be used inside the | |
166 | // library | |
167 | #define WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \ | |
e4c8592e | 168 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, DECL ) |
8142d704 MB |
169 | |
170 | #define WX_DECLARE_EXPORTED_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \ | |
171 | WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, \ | |
53a2db12 | 172 | CLASSNAME, class WXDLLIMPEXP_CORE ) |
8142d704 | 173 | |
e4c8592e VZ |
174 | // Finally these versions allow to define hash sets of non-objects (including |
175 | // pointers, hence the confusing but wxArray-compatible name) without | |
176 | // operator->() which can't be used for them. This is mostly used inside the | |
177 | // library itself to avoid warnings when using such hash sets with some less | |
178 | // common compilers (notably Sun CC). | |
179 | #define WX_DECLARE_HASH_SET_PTR( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \ | |
180 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NOP, CLASSNAME, class ) | |
181 | #define WX_DECLARE_HASH_SET_WITH_DECL_PTR( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \ | |
182 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NOP, CLASSNAME, DECL ) | |
183 | ||
8142d704 MB |
184 | // delete all hash elements |
185 | // | |
186 | // NB: the class declaration of the hash elements must be visible from the | |
187 | // place where you use this macro, otherwise the proper destructor may not | |
188 | // be called (a decent compiler should give a warning about it, but don't | |
189 | // count on it)! | |
190 | #define WX_CLEAR_HASH_SET(type, hashset) \ | |
3115bfa8 VS |
191 | { \ |
192 | type::iterator it, en; \ | |
193 | for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it ) \ | |
194 | delete *it; \ | |
195 | (hashset).clear(); \ | |
196 | } | |
8142d704 MB |
197 | |
198 | #endif // _WX_HASHSET_H_ |