]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/hashset.h | |
3 | // Purpose: wxHashSet class | |
4 | // Author: Mattia Barbon | |
5 | // Modified by: | |
6 | // Created: 11/08/2003 | |
7 | // Copyright: (c) Mattia Barbon | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_HASHSET_H_ | |
12 | #define _WX_HASHSET_H_ | |
13 | ||
14 | #include "wx/hashmap.h" | |
15 | ||
16 | // see comment in wx/hashmap.h which also applies to different standard hash | |
17 | // set classes | |
18 | ||
19 | #if wxUSE_STD_CONTAINERS && \ | |
20 | (defined(HAVE_STD_UNORDERED_SET) || defined(HAVE_TR1_UNORDERED_SET)) | |
21 | ||
22 | #if defined(HAVE_STD_UNORDERED_SET) | |
23 | #include <unordered_set> | |
24 | #define WX_HASH_SET_BASE_TEMPLATE std::unordered_set | |
25 | #elif defined(HAVE_TR1_UNORDERED_SET) | |
26 | #include <tr1/unordered_set> | |
27 | #define WX_HASH_SET_BASE_TEMPLATE std::tr1::unordered_set | |
28 | #else | |
29 | #error Update this code: unordered_set is available, but I do not know where. | |
30 | #endif | |
31 | ||
32 | #elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP) | |
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 | ||
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 | |
45 | ||
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 | |
48 | #define _WX_DECLARE_HASH_SET_IMPL( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP ) \ | |
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 | ||
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 | ||
96 | #else // no appropriate STL class, use our own implementation | |
97 | ||
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 | ||
116 | #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP )\ | |
117 | _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \ | |
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 ) \ | |
121 | CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \ | |
122 | { \ | |
123 | public: \ | |
124 | _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \ | |
125 | \ | |
126 | wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \ | |
127 | key_equal eq = key_equal() ) \ | |
128 | : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \ | |
129 | CLASSNAME##_wxImplementation_KeyEx() ) {} \ | |
130 | \ | |
131 | Insert_Result insert( const key_type& key ) \ | |
132 | { \ | |
133 | bool created; \ | |
134 | Node *node = GetOrCreateNode( key, created ); \ | |
135 | return Insert_Result( iterator( node, this ), created ); \ | |
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 */ \ | |
154 | size_type count( const const_key_type& key ) const \ | |
155 | { return GetNode( key ) ? 1 : 0; } \ | |
156 | } | |
157 | ||
158 | #endif // STL/wx implementations | |
159 | ||
160 | ||
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) \ | |
163 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, class ) | |
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) \ | |
168 | _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, DECL ) | |
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, \ | |
172 | CLASSNAME, class WXDLLIMPEXP_CORE ) | |
173 | ||
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 | ||
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) \ | |
191 | { \ | |
192 | type::iterator it, en; \ | |
193 | for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it ) \ | |
194 | delete *it; \ | |
195 | (hashset).clear(); \ | |
196 | } | |
197 | ||
198 | #endif // _WX_HASHSET_H_ |