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: hashset.h 55215 2008-08-23 18:54:04Z VZ $
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_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP )\
26         typedef std::unordered_set< KEY_T, HASH_T, KEY_EQ_T > CLASSNAME
27 #elif defined(HAVE_TR1_UNORDERED_SET)
28     #include <tr1/unordered_set>
29     #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP )\
30         typedef std::tr1::unordered_set< KEY_T, HASH_T, KEY_EQ_T > CLASSNAME
31 #else
32 #error Update this code: unordered_set is available, but I do not know where.
33 #endif
34 
35 #elif wxUSE_STL && defined(HAVE_STL_HASH_MAP)
36 
37 #if defined(HAVE_EXT_HASH_MAP)
38     #include <ext/hash_set>
39 #elif defined(HAVE_HASH_MAP)
40     #include <hash_set>
41 #endif
42 
43 #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP )\
44     typedef WX_HASH_MAP_NAMESPACE::hash_set< KEY_T, HASH_T, KEY_EQ_T > CLASSNAME
45 
46 #else // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
47 
48 // this is a complex way of defining an easily inlineable identity function...
49 #define _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME, CLASSEXP )            \
50 CLASSEXP CLASSNAME                                                           \
51 {                                                                            \
52     typedef KEY_T key_type;                                                  \
53     typedef const key_type const_key_type;                                   \
54     typedef const_key_type& const_key_reference;                             \
55 public:                                                                      \
56     CLASSNAME() { }                                                          \
57     const_key_reference operator()( const_key_reference key ) const          \
58         { return key; }                                                      \
59                                                                              \
60     /* the dummy assignment operator is needed to suppress compiler */       \
61     /* warnings from hash table class' operator=(): gcc complains about */   \
62     /* "statement with no effect" without it */                              \
63     CLASSNAME& operator=(const CLASSNAME&) { return *this; }                 \
64 };
65 
66 #define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP )\
67 _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
68 _WX_DECLARE_HASHTABLE( KEY_T, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
69 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable             \
70 {                                                                            \
71 public:                                                                      \
72     _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP )              \
73                                                                              \
74     wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(),        \
75                           key_equal eq = key_equal() )                       \
76         : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq,              \
77                       CLASSNAME##_wxImplementation_KeyEx() ) {}              \
78                                                                              \
79     Insert_Result insert( const key_type& key )                              \
80     {                                                                        \
81         bool created;                                                        \
82         Node *node = GetOrCreateNode( key, created );                        \
83         return Insert_Result( iterator( node, this ), created );             \
84     }                                                                        \
85                                                                              \
86     const_iterator find( const const_key_type& key ) const                   \
87     {                                                                        \
88         return const_iterator( GetNode( key ), this );                       \
89     }                                                                        \
90                                                                              \
91     iterator find( const const_key_type& key )                               \
92     {                                                                        \
93         return iterator( GetNode( key ), this );                             \
94     }                                                                        \
95                                                                              \
96     size_type erase( const key_type& k )                                     \
97         { return CLASSNAME##_wxImplementation_HashTable::erase( k ); }       \
98     void erase( const iterator& it ) { erase( *it ); }                       \
99     void erase( const const_iterator& it ) { erase( *it ); }                 \
100                                                                              \
101     /* count() == 0 | 1 */                                                   \
102     size_type count( const const_key_type& key )                             \
103         { return GetNode( key ) ? 1 : 0; }                                   \
104 }
105 
106 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
107 
108 // these macros are to be used in the user code
109 #define WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \
110     _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
111 
112 // and these do exactly the same thing but should be used inside the
113 // library
114 #define WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \
115     _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL )
116 
117 #define WX_DECLARE_EXPORTED_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \
118     WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, \
119                                    CLASSNAME, class WXDLLEXPORT )
120 
121 // delete all hash elements
122 //
123 // NB: the class declaration of the hash elements must be visible from the
124 //     place where you use this macro, otherwise the proper destructor may not
125 //     be called (a decent compiler should give a warning about it, but don't
126 //     count on it)!
127 #define WX_CLEAR_HASH_SET(type, hashset)                                     \
128     {                                                                        \
129         type::iterator it, en;                                               \
130         for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it )  \
131             delete *it;                                                      \
132         (hashset).clear();                                                   \
133     }
134 
135 #endif // _WX_HASHSET_H_
136