Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
Maxwell Krohn edited this page Jan 17, 2012 · 1 revision

Create and Insert

Let's deal with the case of a map from key_t to refcounted pointers of type value_t, which will look like this:

qhash<key_t, ptr<value_t> > hash;
hash.insert (key_t (0), NULL);
hash.insert (key_t (1), New refcounted<value_t> ("v1"));

Note: you can also store regular (non-refcounted) objects in a qhash too:

qhash<int, int> squares;
sqaures.insert (0,0);
squares.insert (1,1);
sqaures.insert (2,4);
squares.insert (3,9);

The underlying container class really doesn't care what the value type is.

Fetching Something Out

The get interface to qhash returns a pointer to an object in the qhash, so you can distinguish between the lookup failing, and the lookup succeeding but returning a NULL pointer. For example, after the 2 insertions above, the following assertions will all succeed:

ptr<value_t> *result;
result = hash[key_t (0)]; assert (result && !*result);
result = hash[key_t (1)]; assert (result && *result);
result = hash[key_t (2)]; assert (!result);

int *i_result;
i_result = squares[0]; assert (i_result && *i_result == 0);
i_result = squares[1]; assert (i_result && *i_result == 1);
i_result = squares[9]; assert (!i_result);

Sizing It Up

Get the number of elements in a qhash via:

size_t n_elem = hash.size ();

Walking the Line

You can iterate through a qhash with a qhash_iterator_t:

qhash_iterator_t<key_t, ptr<value_t> > iter (hash);
const key_t *k;
ptr<value_t> v;
while ((k = iter.next (&v))) {
  // do something interesting with k and v
}
Clone this wiki locally