contents up previous next

wxhashset

this 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
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*
wxhashset::size_type used for sizes
wxhashset::insert_result the return value for insert()

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::begin
wxhashset::clear
wxhashset::count
wxhashset::empty
wxhashset::end
wxhashset::erase
wxhashset::find
wxhashset::insert
wxhashset::size


wxhashset::wxhashset

wxhashset(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::begin

const_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::clear

void clear()

removes all elements from the hash set.


wxhashset::count

size_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::empty

bool empty() const

returns true if the hash set does not contain any elements, false otherwise.


wxhashset::end

const_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::erase

size_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::find

iterator 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::insert

insert_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::size

size_type size() const

returns the number of elements in the set.