wxhashsetthis is a simple, type-safe, and reasonably efficient hash set class, whose interface is a subset of the interface of stl containers. in particular, the interface is modeled after std::set, and the various, non-standard, std::hash_map. example
class myclass { /* ... */ };
// same, with myclass* keys (only uses pointer equality!)
wx_declare_hash_set( myclass*, wxpointerhash, wxpointerequal, myset1 );
// same, with int keys
wx_declare_hash_set( int, wxintegerhash, wxintegerequal, myset2 );
// declare a hash set with string keys
wx_declare_hash_set( wxstring, wxstringhash, wxstringequal, myset3 );
myset1 h1;
myset2 h1;
myset3 h3;
// store and retrieve values
h1.insert( new myclass( 1 ) );
h3.insert( "foo" );
h3.insert( "bar" );
h3.insert( "baz" );
int size = h3.size(); // now is three
bool has_foo = h3.find( "foo" ) != h3.end();
h3.insert( "bar" ); // still has size three
// iterate over all the elements in the class
myset3::iterator it;
for( it = h3.begin(); it != h3.end(); ++it )
{
wxstring key = *it;
// do something useful with key
}
declaring new hash set types
wx_declare_hash_set( key_t, // type of the keys
hash_t, // hasher
key_eq_t, // key equality predicate
classname); // name of the class
the hash_t and key_eq_t are the types
used for the hashing function and key comparison. wxwidgets provides
three predefined hashing functions: wxintegerhash
for integer types ( int, long, short,
and their unsigned counterparts ), wxstringhash for strings
( wxstring, wxchar*, char* ), and
wxpointerhash for any kind of pointer.
similarly three equality predicates:
wxintegerequal, wxstringequal, wxpointerequal are provided.using this you could declare a hash set using int values like this:
wx_declare_hash_set( int,
wxintegerhash,
wxintegerequal,
myset );
// using an user-defined class for keys
class mykey { /* ... */ };
// hashing function
class mykeyhash
{
public:
mykeyhash() { }
unsigned long operator()( const mykey& k ) const
{ /* compute the hash */ }
mykeyhash& operator=(const mykeyhash&) { return *this; }
};
// comparison operator
class mykeyequal
{
public:
mykeyequal() { }
bool operator()( const mykey& a, const mykey& b ) const
{ /* compare for equality */ }
mykeyequal& operator=(const mykeyequal&) { return *this; }
};
wx_declare_hash_set( mykey, // type of the keys
mykeyhash, // hasher
mykeyequal, // key equality predicate
classname); // name of the class
typesin the documentation below you should replace wxhashset with the name you used in the class declaration.
iterators an iterator is similar to a pointer, and so you can use the usual pointer operations: ++it ( and it++ ) to move to the next element, *it to access the element pointed to, *it to access the value of the element pointed to. hash sets provide forward only iterators, this means that you can't use --it, it + 3, it1 - it2. include files <wx/hashset.h> members
wxhashset::wxhashset
wxhashset::wxhashsetwxhashset(size_type size = 10) the size parameter is just a hint, the table will resize automatically to preserve performance. wxhashset(const wxhashset& set) copy constructor.
wxhashset::beginconst_iterator begin() const iterator begin() returns an iterator pointing at the first element of the hash set. please remember that hash sets do not guarantee ordering.
wxhashset::clearvoid clear() removes all elements from the hash set.
wxhashset::countsize_type count(const key_type& key) const counts the number of elements with the given key present in the set. this function returns only 0 or 1.
wxhashset::emptybool empty() const returns true if the hash set does not contain any elements, false otherwise.
wxhashset::endconst_iterator end() const iterator end() returns an iterator pointing at the one-after-the-last element of the hash set. please remember that hash sets do not guarantee ordering.
wxhashset::erasesize_type erase(const key_type& key) erases the element with the given key, and returns the number of elements erased (either 0 or 1). void erase(iterator it) void erase(const_iterator it) erases the element pointed to by the iterator. after the deletion the iterator is no longer valid and must not be used.
wxhashset::finditerator find(const key_type& key) const_iterator find(const key_type& key) const if an element with the given key is present, the functions returns an iterator pointing at that element, otherwise an invalid iterator is returned (i.e. hashset.find( non_existent_key ) == hashset.end()).
wxhashset::insertinsert_result insert(const value_type& v) inserts the given value in the hash set. the return value is equivalent to a std::pair<wxhashmap::iterator, bool>; the iterator points to the inserted element, the boolean value is true if v was actually inserted.
wxhashset::sizesize_type size() const returns the number of elements in the set.
|