Boost logo

Boost Users :

Subject: Re: [Boost-users] [Unordered] template find() request
From: joaquin_at_[hidden]
Date: 2009-03-09 03:10:56


Kenneth Heafield escribió:
> Hello Boost,
>
> Suppose I have a boost::unordered_set<std::string>. Now I want to
> find() a const char *, std::string, or even a string without zero
> termination (Google StringPiece). Converting these to std::string just
> to do a lookup is wasteful. Instead, I want to provide functors like:
>
> struct either_hash {
> size_t operator()(const std::string &str) const {
> return boost::hash_range(str.begin(), str.end());
> }
>
> size_t operator()(const char *str) const {
> return boost::hash_range(str, str + std::strlen(str));
> }
> };
>

You'd also have to provide a polymorphic replacement for
std::equal_to<std::string> allowing
interoperability between std::string's and const char *s.

> This raises the question: what is either_hash::argument_type ? But it
> seems this is not used by boost::unordered_set . It should probably be
> std::string to match the underlying hash table.
>
> Would it make sense to change find() to template <class query_type>
> const_iterator find(const query_type& q) const ?

FWIW, Boost.MultiIndex hashed indices do have a notion of "compatible
keys" allowing for
such heterogeneous lookup operations:

http://www.boost.org/libs/multi_index/doc/tutorial/indices.html#hash_lookup
http://www.boost.org/libs/multi_index/doc/reference/hash_indices.html#lookup

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net