c++ - boost hash returning same value for different inputs -
i have 2 objects, account , transaction transaction unique pair of account , incrementing id number. want use boost::hash unique values these , have overloaded hash_value method per instructions: http://www.boost.org/doc/libs/1_53_0/doc/html/hash/custom.html
class account { ... }; class transaction { account account; unsigned int id; };
account's hash_value method works correctly, , value returned unique given account, make unique pair, transaction's method needs use hash _combine (per boost's instructions ):
inline std::size_t hash_value( const account& acct ) { boost::hash<int> hasher; size_t rval = hasher( acct.id() ); //just int. guaranteed unique return rval; } inline std::size_t hash_value( const transaction& t ) { std::size_t seed = 0; boost::hash_combine( seed, t.account ); boost::hash_combine( seed, t.id ); return seed; }
this returns same values different inputs sometimes. why?? have few thousand accounts, , id number goes few hundred thousand. doesn't seem upper bound issue.
does know if bug, or if need seed boost hash?
thanks
look perfect hashing, , birthday paradox, , completeness's sake pigeonhole principle.
what boils down hash functions produce collisions,unless you're hashing has specific properties you've taken advantage of. chances of seeing hash collision given set of keys going counterintuitively high because that's 1 of mathematical realities we're not wired for: 1/365 chance of getting particular hash, odds of collision 50/50 given 23 keys.
Comments
Post a Comment