wxhashmapthis is a simple, type-safe, and reasonably efficient hash map class, whose interface is a subset of the interface of stl containers. in particular, the interface is modeled after std::map, and the various, non-standard, std::hash_map. example
class myclass { /* ... */ };
// declare a hash map with string keys and int values
wx_declare_string_hash_map( int, myhash5 );
// same, with int keys and myclass* values
wx_declare_hash_map( int, myclass*, wxintegerhash, wxintegerequal, myhash1 );
// same, with wxstring keys and int values
wx_declare_string_hash_map( int, myhash3 );
// same, with wxstring keys and values
wx_declare_string_hash_map( wxstring, myhash2 );
myhash1 h1;
myhash2 h2;
// store and retrieve values
h1[1] = new myclass( 1 );
h1[10000000] = null;
h1[50000] = new myclass( 2 );
h2["bill"] = "abc";
wxstring tmp = h2["bill"];
// since element with key "joe" is not present, this will return
// the default value, which is an empty string in the case of wxstring
myclass tmp2 = h2["joe"];
// iterate over all the elements in the class
myhash2::iterator it;
for( it = h2.begin(); it != h2.end(); ++it )
{
wxstring key = it->first, value = it->second;
// do something useful with key and value
}
declaring new hash table types
wx_declare_string_hash_map( value_t, // type of the values
classname ); // name of the class
declares a hash map class named classname, with wxstring keys
and value_t values.
wx_declare_voidptr_hash_map( value_t, // type of the values
classname ); // name of the class
declares a hash map class named classname, with void* keys
and value_t values.
wx_declare_hash_map( key_t, // type of the keys
value_t, // type of the values
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 map mapping int values to wxstring like this:
wx_declare_hash_map( int,
wxstring,
wxintegerhash,
wxintegerequal,
myhash );
// 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_map( mykey, // type of the keys
some_type, // any type you like
mykeyhash, // hasher
mykeyequal, // key equality predicate
classname); // name of the class
typesin the documentation below you should replace wxhashmap 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->first ( it->second ) to access the key ( value ) of the element pointed to. hash maps provide forward only iterators, this means that you can't use --it, it + 3, it1 - it2. include files <wx/hashmap.h> members
wxhashmap::wxhashmap
wxhashmap::wxhashmapwxhashmap(size_type size = 10) the size parameter is just a hint, the table will resize automatically to preserve performance. wxhashmap(const wxhashmap& map) copy constructor.
wxhashmap::beginconst_iterator begin() const iterator begin() returns an iterator pointing at the first element of the hash map. please remember that hash maps do not guarantee ordering.
wxhashmap::clearvoid clear() removes all elements from the hash map.
wxhashmap::countsize_type count(const key_type& key) const counts the number of elements with the given key present in the map. this function returns only 0 or 1.
wxhashmap::emptybool empty() const returns true if the hash map does not contain any elements, false otherwise.
wxhashmap::endconst_iterator end() const iterator end() returns an iterator pointing at the one-after-the-last element of the hash map. please remember that hash maps do not guarantee ordering.
wxhashmap::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.
wxhashmap::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. hashmap.find( non_existent_key ) == hashmap.end()).
wxhashmap::insertinsert_result insert(const value_type& v) inserts the given value in the hash map. 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.
wxhashmap::operator[]mapped_type& operator[](const key_type& key) use the key as an array subscript. the only difference is that if the given key is not present in the hash map, an element with the default value_type() is inserted in the table.
wxhashmap::sizesize_type size() const returns the number of elements in the map.
|