Good hash function over C++ unordered_set -
i'm looking implement hash function on c++ std::unordered_set<char>
. tried using boost::hash_range:
namespace std { template<> struct hash<unordered_set<char> > size_t operator(const unordered_set<char> &s)( { return boost::hash_range(begin(s), end(s)) }; }
but realised because set unordered, iteration order isn't stable, , hash function wrong. better options me? guess std::set
instead of std::unordered_set
, using ordered set because it's easier hash seems ... wrong.
a similar question, albeit in c#, asked here:
hash function on list independant of order of items in it
over there, per gave nice language-independent answer should put on right track. in short, input
x1, …, xn
you should map to
f(x1) op … op f(xn)
where
- f hash function single elements (integer in case)
- op commutative operator, such xor or plus
hashing integer may seam pointless @ first, goal make 2 neighboring integers dissimilar each other, when combined op not create same result. e.g. if use + operator, want f(1)+f(2) give different result f(0)+f(3).
if standard hashing functions not candidates f , cannot find one, check linked answer more details...
Comments
Post a Comment