Re: Key strategies for hash_map
devdude ha scritto:
Greetings,
I have a hash_map indexed via a std::string. There are times when I
only have the value payload (e.g. key->value) but need to quickly find
it's key. Is it possible to quickly find the key in a hash_map given
it's paired value?
Assuming it's not, I'll presumably carry the key around in the value
payload. This obviously has a disadvantage of duplicating memory (for
key and payload). I intend to carry millions of records around in
this hash_map. My first thought is to use a shared_ptr<std::string>
as both the key and w/in the value payload, but it would seemingly be
awkward to force consumers to index via shared_ptr<std::string> and
likewise it would be awkward for me to translate incoming std::string
keys to shared_ptr<std::string> keys for millions of lookups/inserts.
Is there a better way?
Use a hash_set<>. This has the inconvenient that it makes the value part
immutable, so you have to litter the code with a const_cast, but if that
doesn't scare you, it works. Specifically:
class Value { /* whatever */ };
class Key { /* whatever */ };
class KeyValuePair : public Key, public Value
{
public:
// constructors and adapters
// be sure to avoid the "keying" methods like operator==
// and hash() depending on the Value subobject
Value& GetValue() const
{
// ok, this is ugly, but we need it and it doesn't
// violate the contract with hash_set
return const_cast<KeyValuePair&>(*this);
}
};
const Key& KeyFromValue(const Value& value)
{
#ifdef _DEBUG
// safe but slow version in debug builds
return dynamic_cast<const KeyValue&>(value);
#else
// unsafe but quick version in release builds
return static_cast<const KeyValue&>(value);
#endif
}
hash_set<KeyValuePair /* blabla */> container;
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]