]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: hashset.h | |
e54c96f1 | 3 | // Purpose: interface of wxHashSet |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
9 | /** | |
10 | @class wxHashSet | |
7c913512 | 11 | |
23324ae1 | 12 | This is a simple, type-safe, and reasonably efficient hash set class, |
9cc56d1f | 13 | whose interface is a subset of the interface of STL containers. |
9d6196a9 VZ |
14 | |
15 | The interface is similar to std::tr1::hash_set or std::set classes but | |
16 | notice that, unlike std::set, the contents of a hash set is not sorted. | |
7c913512 | 17 | |
9cc56d1f FM |
18 | Example: |
19 | @code | |
20 | class MyClass { ... }; | |
21 | ||
22 | // same, with MyClass* keys (only uses pointer equality!) | |
7a7fa93b | 23 | WX_DECLARE_HASH_SET( MyClass*, wxPointerHash, wxPointerEqual, MySet1 ); |
9cc56d1f | 24 | // same, with int keys |
7a7fa93b | 25 | WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual, MySet2 ); |
9cc56d1f | 26 | // declare a hash set with string keys |
7a7fa93b | 27 | WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, MySet3 ); |
9cc56d1f FM |
28 | |
29 | MySet1 h1; | |
30 | MySet2 h1; | |
31 | MySet3 h3; | |
32 | ||
33 | // store and retrieve values | |
34 | h1.insert( new MyClass( 1 ) ); | |
35 | ||
36 | h3.insert( "foo" ); | |
37 | h3.insert( "bar" ); | |
38 | h3.insert( "baz" ); | |
39 | ||
40 | int size = h3.size(); // now is three | |
41 | bool has_foo = h3.find( "foo" ) != h3.end(); | |
42 | ||
43 | h3.insert( "bar" ); // still has size three | |
44 | ||
45 | // iterate over all the elements in the class | |
46 | MySet3::iterator it; | |
47 | for( it = h3.begin(); it != h3.end(); ++it ) | |
48 | { | |
49 | wxString key = *it; | |
50 | // do something useful with key | |
51 | } | |
52 | @endcode | |
53 | ||
54 | ||
55 | @section hashset_declaringnew Declaring new hash set types | |
56 | ||
57 | @code | |
58 | WX_DECLARE_HASH_SET( KEY_T, // type of the keys | |
59 | HASH_T, // hasher | |
60 | KEY_EQ_T, // key equality predicate | |
61 | CLASSNAME); // name of the class | |
62 | @endcode | |
63 | The HASH_T and KEY_EQ_T are the types used for the hashing function and key | |
64 | comparison. wxWidgets provides three predefined hashing functions: | |
65 | wxIntegerHash for integer types ( int, long, short, and their unsigned counterparts ), | |
66 | wxStringHash for strings ( wxString, wxChar*, char* ), and wxPointerHash for | |
67 | any kind of pointer. | |
68 | Similarly three equality predicates: wxIntegerEqual, wxStringEqual, wxPointerEqual | |
69 | are provided. Using this you could declare a hash set using int values like this: | |
70 | ||
71 | @code | |
72 | WX_DECLARE_HASH_SET( int, | |
7a7fa93b VZ |
73 | wxIntegerHash, |
74 | wxIntegerEqual, | |
9cc56d1f FM |
75 | MySet ); |
76 | ||
77 | // using an user-defined class for keys | |
78 | class MyKey { ... }; | |
79 | ||
80 | // hashing function | |
81 | class MyKeyHash | |
82 | { | |
83 | public: | |
84 | MyKeyHash() { } | |
85 | ||
86 | unsigned long operator()( const MyKey& k ) const | |
87 | { | |
88 | // compute the hash | |
89 | } | |
90 | ||
91 | MyKeyHash& operator=(const MyKeyHash&) { return *this; } | |
92 | }; | |
93 | ||
94 | // comparison operator | |
95 | class MyKeyEqual | |
96 | { | |
97 | public: | |
98 | MyKeyEqual() { } | |
99 | bool operator()( const MyKey& a, const MyKey& b ) const | |
100 | { | |
101 | // compare for equality | |
102 | } | |
103 | ||
104 | MyKeyEqual& operator=(const MyKeyEqual&) { return *this; } | |
105 | }; | |
106 | ||
107 | WX_DECLARE_HASH_SET( MyKey, // type of the keys | |
7a7fa93b VZ |
108 | MyKeyHash, // hasher |
109 | MyKeyEqual, // key equality predicate | |
9cc56d1f FM |
110 | CLASSNAME); // name of the class |
111 | @endcode | |
112 | ||
113 | ||
114 | @section hashset_types Types | |
115 | ||
116 | In the documentation below you should replace wxHashSet with the name you | |
117 | used in the class declaration. | |
118 | ||
119 | - wxHashSet::key_type: Type of the hash keys | |
120 | - wxHashSet::mapped_type: Type of hash keys | |
121 | - wxHashSet::value_type: Type of hash keys | |
122 | - wxHashSet::iterator: Used to enumerate all the elements in a hash set; | |
123 | it is similar to a value_type* | |
124 | - wxHashSet::const_iterator: Used to enumerate all the elements in a constant | |
125 | hash set; it is similar to a const value_type* | |
126 | - wxHashSet::size_type: Used for sizes | |
127 | - wxHashSet::Insert_Result: The return value for insert() | |
128 | ||
129 | ||
130 | @section hashset_iter Iterators | |
131 | ||
132 | An iterator is similar to a pointer, and so you can use the usual pointer | |
133 | operations: ++it ( and it++ ) to move to the next element, *it to access the | |
134 | element pointed to, *it to access the value of the element pointed to. | |
135 | Hash sets provide forward only iterators, this means that you can't use --it, | |
136 | it + 3, it1 - it2. | |
137 | ||
23324ae1 | 138 | @library{wxbase} |
9cc56d1f | 139 | @category{containers} |
23324ae1 | 140 | */ |
7c913512 | 141 | class wxHashSet |
23324ae1 FM |
142 | { |
143 | public: | |
23324ae1 | 144 | /** |
9cc56d1f FM |
145 | The size parameter is just a hint, the table will resize automatically |
146 | to preserve performance. | |
23324ae1 FM |
147 | */ |
148 | wxHashSet(size_type size = 10); | |
9cc56d1f FM |
149 | |
150 | /** | |
151 | Copy constructor. | |
152 | */ | |
7c913512 | 153 | wxHashSet(const wxHashSet& set); |
23324ae1 FM |
154 | |
155 | //@{ | |
156 | /** | |
157 | Returns an iterator pointing at the first element of the hash set. | |
158 | Please remember that hash sets do not guarantee ordering. | |
159 | */ | |
9cc56d1f FM |
160 | const_iterator begin() const; |
161 | iterator begin(); | |
23324ae1 FM |
162 | //@} |
163 | ||
164 | /** | |
165 | Removes all elements from the hash set. | |
166 | */ | |
167 | void clear(); | |
168 | ||
169 | /** | |
170 | Counts the number of elements with the given key present in the set. | |
171 | This function returns only 0 or 1. | |
172 | */ | |
328f5751 | 173 | size_type count(const key_type& key) const; |
23324ae1 FM |
174 | |
175 | /** | |
176 | Returns @true if the hash set does not contain any elements, @false otherwise. | |
177 | */ | |
328f5751 | 178 | bool empty() const; |
23324ae1 FM |
179 | |
180 | //@{ | |
181 | /** | |
182 | Returns an iterator pointing at the one-after-the-last element of the hash set. | |
183 | Please remember that hash sets do not guarantee ordering. | |
184 | */ | |
9cc56d1f FM |
185 | const_iterator end() const; |
186 | iterator end(); | |
23324ae1 FM |
187 | //@} |
188 | ||
9cc56d1f FM |
189 | /** |
190 | Erases the element with the given key, and returns the number of elements | |
191 | erased (either 0 or 1). | |
192 | */ | |
193 | size_type erase(const key_type& key); | |
194 | ||
23324ae1 FM |
195 | //@{ |
196 | /** | |
197 | Erases the element pointed to by the iterator. After the deletion | |
198 | the iterator is no longer valid and must not be used. | |
199 | */ | |
7c913512 FM |
200 | void erase(iterator it); |
201 | void erase(const_iterator it); | |
23324ae1 FM |
202 | //@} |
203 | ||
204 | //@{ | |
205 | /** | |
206 | If an element with the given key is present, the functions returns | |
207 | an iterator pointing at that element, otherwise an invalid iterator | |
9cc56d1f FM |
208 | is returned; i.e. |
209 | @code | |
210 | hashset.find( non_existent_key ) == hashset.end() | |
211 | @endcode | |
23324ae1 | 212 | */ |
328f5751 FM |
213 | iterator find(const key_type& key) const; |
214 | const_iterator find(const key_type& key) const; | |
23324ae1 FM |
215 | //@} |
216 | ||
217 | /** | |
9cc56d1f FM |
218 | Inserts the given value in the hash set. |
219 | The return value is equivalent to a | |
220 | @code std::pair<wxHashMap::iterator, bool> @endcode | |
221 | The iterator points to the inserted element, the boolean value is @true | |
222 | if @a v was actually inserted. | |
23324ae1 FM |
223 | */ |
224 | Insert_Result insert(const value_type& v); | |
225 | ||
226 | /** | |
227 | Returns the number of elements in the set. | |
228 | */ | |
328f5751 | 229 | size_type size() const; |
23324ae1 | 230 | }; |
e54c96f1 | 231 |