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