Advertisement
kutuzzzov

Урок 7-2 Искусство хэш-функций

Feb 22nd, 2023
1,503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct Circle {
  7.     double x;
  8.     double y;
  9.     double r;
  10. };
  11.  
  12. struct Dumbbell {
  13.     Circle circle1;
  14.     Circle circle2;
  15.     string text;
  16. };
  17.  
  18. struct DumbbellHash {
  19.     size_t operator() (const Dumbbell& dumbbell) const {
  20.         auto h_x = d_hasher_(dumbbell.circle1.x);
  21.         auto h_y = d_hasher_(dumbbell.circle1.y);
  22.         auto h_r = d_hasher_(dumbbell.circle1.r);
  23.        
  24.         auto circle1_hash = h_x + h_y * 37 + h_r * (37 * 37);
  25.        
  26.         auto h_x2 = d_hasher_(dumbbell.circle2.x);
  27.         auto h_y2 = d_hasher_(dumbbell.circle2.y);
  28.         auto h_r2 = d_hasher_(dumbbell.circle2.r);
  29.        
  30.         auto circle2_hash =  h_x2 + h_y2 * 37 + h_r2 * (37 * 37);
  31.    
  32.         return (circle1_hash * (37 * 37 * 37) + circle2_hash + s_hasher_(dumbbell.text));
  33.     }
  34.  
  35. private:
  36.     std::hash<double> d_hasher_;
  37.     hash<string> s_hasher_;
  38. };
  39.  
  40. int main() {
  41.     DumbbellHash hash;
  42.     Dumbbell dumbbell{{10, 11.5, 2.3}, {3.14, 15, -8}, "abc"s};
  43.     cout << "Dumbbell hash "s << hash(dumbbell);
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement