Dear all:
I started to use the unordered_set
class from the tr1
namespace to speed-up access against the plain (tree-based) STL map
. However, I wanted to store references to threads ID in boost (boost::thread::id
), and realized that the API of those identifiers is so opaque that you cannot clearly obtain a hash of it.
Surprisingly, boost implements parts of the tr1
(including hash
and unordered_set
), but it does not define a hash class that is able to hash a thread ID.
Looking at the documentation of boost::thread::id
I found that thread IDs can be output to a stream, so my solution for doing hashing was kind of:
struct boost_thread_id_hash
{
size_t operator()(boost::thread::id const& id) const
{
std::stringstream ostr;
ostr << id;
std::tr1::hash<std::string> h;
return h(ostr.str());
}
};
That is, serialize it, apply the hash to the resulting string. However, this seems to be less efficient than actually using the STL map<boost::thread::id>
.
So, my questions: Do you find a better way of doing this? Is it a clear inconsistency in both boost and tr1 not to force the existence of a hash<boost::thread::id>
class?
Thanks.