]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/hashset.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashSet class
4 // Author: Mattia Barbon
7 // Copyright: (c) Mattia Barbon
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_HASHSET_H_
12 #define _WX_HASHSET_H_
14 #include "wx/hashmap.h"
16 // see comment in wx/hashmap.h which also applies to different standard hash
19 #if wxUSE_STD_CONTAINERS && \
20 (defined(HAVE_STD_UNORDERED_SET) || defined(HAVE_TR1_UNORDERED_SET))
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
29 #error Update this code: unordered_set is available, but I do not know where.
32 #elif wxUSE_STD_CONTAINERS && defined(HAVE_STL_HASH_MAP)
34 #if defined(HAVE_EXT_HASH_MAP)
35 #include <ext/hash_set>
36 #elif defined(HAVE_HASH_MAP)
40 #define WX_HASH_SET_BASE_TEMPLATE WX_HASH_MAP_NAMESPACE::hash_set
42 #endif // different hash_set/unordered_set possibilities
44 #ifdef WX_HASH_SET_BASE_TEMPLATE
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 ) \
50 : public WX_HASH_SET_BASE_TEMPLATE< KEY_T, HASH_T, KEY_EQ_T > \
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) \
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)\
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) \
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).
82 #define WX_MAYBE_PREFIX_WITH_STRUCT(STRUCTNAME) STRUCTNAME
84 #define WX_MAYBE_PREFIX_WITH_STRUCT(STRUCTNAME) struct STRUCTNAME
87 #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP ) \
88 _WX_DECLARE_HASH_SET_IMPL( \
90 WX_MAYBE_PREFIX_WITH_STRUCT(HASH_T), \
91 WX_MAYBE_PREFIX_WITH_STRUCT(KEY_EQ_T), \
96 #else // no appropriate STL class, use our own implementation
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 ) \
102 typedef KEY_T key_type; \
103 typedef const key_type const_key_type; \
104 typedef const_key_type& const_key_reference; \
107 const_key_reference operator()( const_key_reference key ) const \
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; } \
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 \
124 _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP ) \
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() ) {} \
131 Insert_Result insert( const key_type& key ) \
134 Node *node = GetOrCreateNode( key, created ); \
135 return Insert_Result( iterator( node, this ), created ); \
138 const_iterator find( const const_key_type& key ) const \
140 return const_iterator( GetNode( key ), this ); \
143 iterator find( const const_key_type& key ) \
145 return iterator( GetNode( key ), this ); \
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 ); } \
153 /* count() == 0 | 1 */ \
154 size_type count( const const_key_type& key ) const \
155 { return GetNode( key ) ? 1 : 0; } \
158 #endif // STL/wx implementations
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 )
165 // and these do exactly the same thing but should be used inside the
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 )
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 )
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 )
184 // delete all hash elements
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
190 #define WX_CLEAR_HASH_SET(type, hashset) \
192 type::iterator it, en; \
193 for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it ) \
198 #endif // _WX_HASHSET_H_