Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HashTable

3 views
Skip to first unread message

carl

unread,
Nov 28, 2009, 5:05:59 PM11/28/09
to
I have found various hash_table implementation in c++ on the net. But I have
not found any that returns the chain of objects associated with a hased
index.

When more that one object hash to the same index it gets added to a chain of
elements. Are there any implmentations that returns the whole chain based on
a hashed index?

Its pretty much the functionality of a std::map that for each key contains a
list (or std::vector) of the stored objects. But I would like the constant
time lookup of the hash-table instead of the lg n time of a map.

Chris M. Thomasson

unread,
Nov 28, 2009, 7:10:10 PM11/28/09
to
"carl" <carl@.com> wrote in message
news:4b119ed2$0$275$1472...@news.sunsite.dk...

>I have found various hash_table implementation in c++ on the net. But I
>have not found any that returns the chain of objects associated with a
>hased index.
>
> When more that one object hash to the same index it gets added to a chain
> of elements.

Sometimes.


> Are there any implmentations that returns the whole chain based on a
> hashed index?

Perhaps. It would be trivial to create one yourself. Just create a static
hash table with a container per-bucket. Add a lookup function which returns
a reference to the hashed container.

Pavel

unread,
Nov 28, 2009, 9:53:09 PM11/28/09
to
The functionality in std::map is actually slightly different and it *is*
available in hash maps:

You can iterate a sequence of objects with keys "Equal" or more
precisely, equivalent, to your given key using equal_range() methods of
both std::[multi]map and unordered_[multi]map (available in many current
C++ implementations in std::tr1 namespace or you can use
boost::unordered_map that provides the identical functionality).
(unordered_map is a hash table).

Additionaly to equal_range(), the following hash table-specific
functionality is available in unordered_map: you can retrieve a bucket
with bucket(key) call and iterate through the bucket but, if my reading
of C++0x specs is correct, you may encounter more than one hashcode in
the bucket (although it is guaranteed that you you will receive all
objects with the hash value equal to the one of your key in one bucket
and that the *average* length of the bucket is not greater than a
reasonably low constant).

If neither of the above alternatives is right for you, you may have to
implement your own hash table (or use the existing one and somehow
support the list or vector of elements with the equal hash codes on your
own). I am not aware of an implementation that does exactly what you
described (this certainly does not mean it is not available).

Hope this will help
-Pavel

Message has been deleted
0 new messages