bhupixb

Safe Hash Unordered_map

Feb 15th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. auto random_address = [] { char *p = new char; delete p; return (uint64_t) p; };
  2.  
  3. struct safe_hash32 {
  4. static unsigned hash32(unsigned x) {
  5. x = (x ^ (x >> 16)) * 0x45d9f3b;
  6. x = (x ^ (x >> 16)) * 0x45d9f3b;
  7. return x ^ (x >> 16);
  8. }
  9. unsigned operator()(unsigned x) const {
  10. static const unsigned FIXED_RANDOM = hash32(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));
  11. return hash32(x ^ FIXED_RANDOM);
  12. }
  13. };
  14. unordered_map<unsigned int, int ,safe_hash32> mp;
Add Comment
Please, Sign In to add comment