1 \section{\class{wxHashSet
}}\label{wxhashset
}
3 This is a simple, type-safe, and reasonably efficient hash set class,
4 whose interface is a subset of the interface of STL containers. In
5 particular, the interface is modeled after std::set, and the various,
6 non standard, std::hash
\_map.
11 class MyClass
{ /* ... */
};
13 // same, with MyClass* keys (only uses pointer equality!)
14 WX_DECLARE_HASH_SET( MyClass*, wxPointerHash, wxPointerEqual, MySet1 );
15 // same, with int keys
16 WX_DECLARE_HASH_SET( int, wxIntegerHash, wxIntegerEqual, MySet2 );
17 // declare a hash set with string keys
18 WX_DECLARE_HASH_SET( wxString, wxStringHash, wxStringEqual, MySet3 );
24 // store and retrieve values
25 h1.insert( new MyClass(
1 ) );
31 int size = h3.size(); // now is three
32 bool has_foo = h3.find( "foo" ) != h3.end();
34 h3.insert( "bar" ); // still has size three
36 // iterate over all the elements in the class
38 for( it = h3.begin(); it != h3.end(); ++it )
41 // do something useful with key
45 \wxheading{Declaring new hash set types
}
48 WX_DECLARE_HASH_SET( KEY_T, // type of the keys
50 KEY_EQ_T, // key equality predicate
51 CLASSNAME); // name of the class
54 The HASH
\_T and KEY
\_EQ\_T are the types
55 used for the hashing function and key comparison. wxWidgets provides
56 three predefined hashing functions:
{\tt wxIntegerHash
}
57 for integer types (
{\tt int
},
{\tt long
},
{\tt short
},
58 and their unsigned counterparts ),
{\tt wxStringHash
} for strings
59 (
{\tt wxString
},
{\tt wxChar*
},
{\tt char*
} ), and
60 {\tt wxPointerHash
} for any kind of pointer.
61 Similarly three equality predicates:
62 {\tt wxIntegerEqual
},
{\tt wxStringEqual
},
{\tt wxPointerEqual
} are provided.
64 Using this you could declare a hash set using
{\tt int
} values like this:
67 WX_DECLARE_HASH_SET( int,
72 // using an user-defined class for keys
73 class MyKey
{ /* ... */
};
81 unsigned long operator()( const MyKey& k ) const
82 { /* compute the hash */
}
84 MyKeyHash& operator=(const MyKeyHash&)
{ return *this;
}
87 // comparison operator
92 bool operator()( const MyKey& a, const MyKey& b ) const
93 { /* compare for equality */
}
95 MyKeyEqual& operator=(const MyKeyEqual&)
{ return *this;
}
98 WX_DECLARE_HASH_SET( MyKey, // type of the keys
100 MyKeyEqual, // key equality predicate
101 CLASSNAME); // name of the class
104 \latexignore{\rtfignore{\wxheading{Types
}}}
106 In the documentation below you should replace wxHashSet with the name
107 you used in the class declaration.
110 \twocolitem{wxHashSet::key
\_type}{Type of the hash keys
}
111 \twocolitem{wxHashSet::mapped
\_type}{Type of hash keys
}
112 \twocolitem{wxHashSet::value
\_type}{Type of hash keys
}
113 \twocolitem{wxHashSet::iterator
}{Used to enumerate all the elements in a hash
114 set; it is similar to a
{\tt value
\_type*
}}
115 \twocolitem{wxHashSet::const
\_iterator}{Used to enumerate all the elements
116 in a constant hash set; it is similar to a
{\tt const value
\_type*
}}
117 \twocolitem{wxHashSet::size
\_type}{Used for sizes
}
118 \twocolitem{wxHashSet::Insert
\_Result}{The return value for
119 \helpref{insert()
}{wxhashsetinsert
}}
122 \wxheading{Iterators
}
124 An iterator is similar to a pointer, and so you can use the usual pointer
125 operations:
{\tt ++it
} ( and
{\tt it++
} ) to move to the next element,
126 {\tt *it
} to access the element pointed to,
{\tt *it
}
127 to access the value of the element pointed to.
128 Hash sets provide forward only iterators, this
129 means that you can't use
{\tt --it
},
{\tt it +
3},
{\tt it1 - it2
}.
131 \wxheading{Include files
}
135 \latexignore{\rtfignore{\wxheading{Members
}}}
137 \membersection{wxHashSet::wxHashSet
}\label{wxhashsetctor
}
139 \func{}{wxHashSet
}{\param{size
\_type}{ size =
10}}
141 The size parameter is just a hint, the table will resize automatically
142 to preserve performance.
144 \func{}{wxHashSet
}{\param{const wxHashSet\&
}{ set
}}
148 \membersection{wxHashSet::begin
}\label{wxhashsetbegin
}
150 \constfunc{const
\_iterator}{begin
}{}
152 \func{iterator
}{begin
}{}
154 Returns an iterator pointing at the first element of the hash set.
155 Please remember that hash sets do not guarantee ordering.
157 \membersection{wxHashSet::clear
}\label{wxhashsetclear
}
161 Removes all elements from the hash set.
163 \membersection{wxHashSet::count
}\label{wxhashsetcount
}
165 \constfunc{size
\_type}{count
}{\param{const key
\_type\&
}{ key
}}
167 Counts the number of elements with the given key present in the set.
168 This function returns only
0 or
1.
170 \membersection{wxHashSet::empty
}\label{wxhashsetempty
}
172 \constfunc{bool
}{empty
}{}
174 Returns true if the hash set does not contain any elements, false otherwise.
176 \membersection{wxHashSet::end
}\label{wxhashsetend
}
178 \constfunc{const
\_iterator}{end
}{}
180 \func{iterator
}{end
}{}
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 \membersection{wxHashSet::erase
}\label{wxhashseterase
}
187 \func{size
\_type}{erase
}{\param{const key
\_type\&
}{ key
}}
189 Erases the element with the given key, and returns the number of elements
190 erased (either
0 or
1).
192 \func{void
}{erase
}{\param{iterator
}{ it
}}
194 \func{void
}{erase
}{\param{const
\_iterator}{ it
}}
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 \membersection{wxHashSet::find
}\label{wxhashsetfind
}
201 \func{iterator
}{find
}{\param{const key
\_type\&
}{ key
}}
203 \constfunc{const
\_iterator}{find
}{\param{const key
\_type\&
}{ key
}}
205 If an element with the given key is present, the functions returns
206 an iterator pointing at that element, otherwise an invalid iterator
207 is returned (i.e. hashset.find( non
\_existent\_key ) == hashset.end()).
209 \membersection{wxHashSet::insert
}\label{wxhashsetinsert
}
211 \func{Insert
\_Result}{insert
}{\param{const value
\_type\&
}{ v
}}
213 Inserts the given value in the hash set. The return value is
214 equivalent to a
\texttt{std::pair<wxHashMap::iterator, bool>
};
215 the iterator points to the inserted element, the boolean value
216 is
\texttt{true
} if
\texttt{v
} was actually inserted.
218 \membersection{wxHashSet::size
}\label{wxhashsetsize
}
220 \constfunc{size
\_type}{size
}{}
222 Returns the number of elements in the set.