]>
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.
14 In particular, the interface is modeled after std::set, and the various,
15 non-standard, std::hash_map.
19 class MyClass { ... };
21 // same, with MyClass* keys (only uses pointer equality!)
22 WX_DECLARE_HASH_SET( MyClass*, wxPointerHash, wxPointerEqual, MySet1 );
23 // same, with int keys
24 WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual, MySet2 );
25 // declare a hash set with string keys
26 WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, MySet3 );
32 // store and retrieve values
33 h1.insert( new MyClass( 1 ) );
39 int size = h3.size(); // now is three
40 bool has_foo = h3.find( "foo" ) != h3.end();
42 h3.insert( "bar" ); // still has size three
44 // iterate over all the elements in the class
46 for( it = h3.begin(); it != h3.end(); ++it )
49 // do something useful with key
54 @section hashset_declaringnew Declaring new hash set types
57 WX_DECLARE_HASH_SET( KEY_T, // type of the keys
59 KEY_EQ_T, // key equality predicate
60 CLASSNAME); // name of the class
62 The HASH_T and KEY_EQ_T are the types used for the hashing function and key
63 comparison. wxWidgets provides three predefined hashing functions:
64 wxIntegerHash for integer types ( int, long, short, and their unsigned counterparts ),
65 wxStringHash for strings ( wxString, wxChar*, char* ), and wxPointerHash for
67 Similarly three equality predicates: wxIntegerEqual, wxStringEqual, wxPointerEqual
68 are provided. Using this you could declare a hash set using int values like this:
71 WX_DECLARE_HASH_SET( int,
76 // using an user-defined class for keys
85 unsigned long operator()( const MyKey& k ) const
90 MyKeyHash& operator=(const MyKeyHash&) { return *this; }
93 // comparison operator
98 bool operator()( const MyKey& a, const MyKey& b ) const
100 // compare for equality
103 MyKeyEqual& operator=(const MyKeyEqual&) { return *this; }
106 WX_DECLARE_HASH_SET( MyKey, // type of the keys
108 MyKeyEqual, // key equality predicate
109 CLASSNAME); // name of the class
113 @section hashset_types Types
115 In the documentation below you should replace wxHashSet with the name you
116 used in the class declaration.
118 - wxHashSet::key_type: Type of the hash keys
119 - wxHashSet::mapped_type: Type of hash keys
120 - wxHashSet::value_type: Type of hash keys
121 - wxHashSet::iterator: Used to enumerate all the elements in a hash set;
122 it is similar to a value_type*
123 - wxHashSet::const_iterator: Used to enumerate all the elements in a constant
124 hash set; it is similar to a const value_type*
125 - wxHashSet::size_type: Used for sizes
126 - wxHashSet::Insert_Result: The return value for insert()
129 @section hashset_iter Iterators
131 An iterator is similar to a pointer, and so you can use the usual pointer
132 operations: ++it ( and it++ ) to move to the next element, *it to access the
133 element pointed to, *it to access the value of the element pointed to.
134 Hash sets provide forward only iterators, this means that you can't use --it,
138 @category{containers}
144 The size parameter is just a hint, the table will resize automatically
145 to preserve performance.
147 wxHashSet(size_type size
= 10);
152 wxHashSet(const wxHashSet
& set
);
156 Returns an iterator pointing at the first element of the hash set.
157 Please remember that hash sets do not guarantee ordering.
159 const_iterator
begin() const;
164 Removes all elements from the hash set.
169 Counts the number of elements with the given key present in the set.
170 This function returns only 0 or 1.
172 size_type
count(const key_type
& key
) const;
175 Returns @true if the hash set does not contain any elements, @false otherwise.
181 Returns an iterator pointing at the one-after-the-last element of the hash set.
182 Please remember that hash sets do not guarantee ordering.
184 const_iterator
end() const;
189 Erases the element with the given key, and returns the number of elements
190 erased (either 0 or 1).
192 size_type
erase(const key_type
& key
);
196 Erases the element pointed to by the iterator. After the deletion
197 the iterator is no longer valid and must not be used.
199 void erase(iterator it
);
200 void erase(const_iterator it
);
205 If an element with the given key is present, the functions returns
206 an iterator pointing at that element, otherwise an invalid iterator
209 hashset.find( non_existent_key ) == hashset.end()
212 iterator
find(const key_type
& key
) const;
213 const_iterator
find(const key_type
& key
) const;
217 Inserts the given value in the hash set.
218 The return value is equivalent to a
219 @code std::pair<wxHashMap::iterator, bool> @endcode
220 The iterator points to the inserted element, the boolean value is @true
221 if @a v was actually inserted.
223 Insert_Result
insert(const value_type
& v
);
226 Returns the number of elements in the set.
228 size_type
size() const;