shushus

Спринт 10. Искусство хеш-функций. Задание 2.

Mar 8th, 2023
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 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.    
  20.     size_t operator() (const Dumbbell& dumbbell) const {
  21.        
  22.         auto hash_1x = double_hash_(dumbbell.circle1.x);
  23.         auto hash_1y = double_hash_(dumbbell.circle1.y);
  24.         auto hash_1r = double_hash_(dumbbell.circle1.r);
  25.  
  26.         auto hash_1circle = hash_1x
  27.                             + hash_1y * 17
  28.                             + hash_1r * (17 * 17);
  29.  
  30.         auto hash_2x = double_hash_(dumbbell.circle2.x);
  31.         auto hash_2y = double_hash_(dumbbell.circle2.y);
  32.         auto hash_2r = double_hash_(dumbbell.circle2.r);
  33.  
  34.         auto hash_2circle = hash_2x
  35.                             + hash_2y * 17
  36.                             + hash_2r * (17 * 17);
  37.  
  38.         return (hash_1circle * (17 * 17 * 17)
  39.                 + hash_2circle
  40.                 + string_hash_(dumbbell.text));
  41.     }
  42.  
  43. private:
  44.     hash<string> string_hash_;
  45.     hash<double> double_hash_;
  46. };
  47.  
  48. int main() {
  49.     DumbbellHash hash;
  50.     Dumbbell dumbbell{{10, 11.5, 2.3}, {3.14, 15, -8}, "abc"s};
  51.     cout << "Dumbbell hash "s << hash(dumbbell);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment