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() { }

unsignedlong 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

Types

In the documentation below you should replace wxHashSet with the name you used in the class declaration.

wxHashSet::key_type: Type of the hash keys

wxHashSet::mapped_type: Type of hash keys

wxHashSet::value_type: Type of hash keys

wxHashSet::iterator: Used to enumerate all the elements in a hash set; it is similar to a value_type*

wxHashSet::const_iterator: Used to enumerate all the elements in a constant hash set; it is similar to a const value_type*

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.