]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/hashset.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxHashSet
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
12 This is a simple, type-safe, and reasonably efficient hash set class,
13 whose interface is a subset of the interface of STL containers.
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.
20 class MyClass { ... };
22 // same, with MyClass* keys (only uses pointer equality!)
23 WX_DECLARE_HASH_SET( MyClass*, ::wxPointerHash, ::wxPointerEqual, MySet1 );
24 // same, with int keys
25 WX_DECLARE_HASH_SET( int, ::wxIntegerHash, ::wxIntegerEqual, MySet2 );
26 // declare a hash set with string keys
27 WX_DECLARE_HASH_SET( wxString, ::wxStringHash, ::wxStringEqual, MySet3 );
33 // store and retrieve values
34 h1.insert( new MyClass( 1 ) );
40 int size = h3.size(); // now is three
41 bool has_foo = h3.find( "foo" ) != h3.end();
43 h3.insert( "bar" ); // still has size three
45 // iterate over all the elements in the class
47 for( it = h3.begin(); it != h3.end(); ++it )
50 // do something useful with key
55 @section hashset_declaringnew Declaring new hash set types
58 WX_DECLARE_HASH_SET( KEY_T, // type of the keys
60 KEY_EQ_T, // key equality predicate
61 CLASSNAME); // name of the class
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
68 Similarly three equality predicates: wxIntegerEqual, wxStringEqual, wxPointerEqual
69 are provided. Using this you could declare a hash set using int values like this:
72 WX_DECLARE_HASH_SET( int,
77 // using an user-defined class for keys
86 unsigned long operator()( const MyKey& k ) const
91 MyKeyHash& operator=(const MyKeyHash&) { return *this; }
94 // comparison operator
99 bool operator()( const MyKey& a, const MyKey& b ) const
101 // compare for equality
104 MyKeyEqual& operator=(const MyKeyEqual&) { return *this; }
107 WX_DECLARE_HASH_SET( MyKey, // type of the keys
108 ::MyKeyHash, // hasher
109 ::MyKeyEqual, // key equality predicate
110 CLASSNAME); // name of the class
114 @section hashset_types Types
116 In the documentation below you should replace wxHashSet with the name you
117 used in the class declaration.
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()
130 @section hashset_iter Iterators
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,
139 @category{containers}
145 The size parameter is just a hint, the table will resize automatically
146 to preserve performance.
148 wxHashSet(size_type size
= 10);
153 wxHashSet(const wxHashSet
& set
);
157 Returns an iterator pointing at the first element of the hash set.
158 Please remember that hash sets do not guarantee ordering.
160 const_iterator
begin() const;
165 Removes all elements from the hash set.
170 Counts the number of elements with the given key present in the set.
171 This function returns only 0 or 1.
173 size_type
count(const key_type
& key
) const;
176 Returns @true if the hash set does not contain any elements, @false otherwise.
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.
185 const_iterator
end() const;
190 Erases the element with the given key, and returns the number of elements
191 erased (either 0 or 1).
193 size_type
erase(const key_type
& key
);
197 Erases the element pointed to by the iterator. After the deletion
198 the iterator is no longer valid and must not be used.
200 void erase(iterator it
);
201 void erase(const_iterator it
);
206 If an element with the given key is present, the functions returns
207 an iterator pointing at that element, otherwise an invalid iterator
210 hashset.find( non_existent_key ) == hashset.end()
213 iterator
find(const key_type
& key
) const;
214 const_iterator
find(const key_type
& key
) const;
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.
224 Insert_Result
insert(const value_type
& v
);
227 Returns the number of elements in the set.
229 size_type
size() const;