fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int id_max = 7e7, s_max = (1 << 17)-1, x_max = 2;
  6.  
  7. class data_tuple
  8. {
  9. struct bit_fields
  10. {
  11. uint64_t id: 27;
  12. uint64_t s1: 17;
  13. uint64_t s2: 17;
  14. uint64_t x: 2;
  15. uint64_t u: 1;
  16. };
  17.  
  18. union
  19. {
  20. uint64_t key;
  21. bit_fields data;
  22. };
  23.  
  24. static inline uint64_t score_to_int(double s)
  25. {
  26. return round(s *= s_max);
  27. }
  28.  
  29. static inline double int_to_score(int s)
  30. {
  31. return double(s)/s_max;
  32. }
  33.  
  34. public:
  35. void set_id(int id)
  36. {
  37. data.id = id;
  38. }
  39.  
  40. void set_score1(double x)
  41. {
  42. data.s1 = score_to_int(x);
  43. }
  44.  
  45. void set_score2(double x)
  46. {
  47. data.s2 = score_to_int(x);
  48. }
  49.  
  50. void set_x(int x)
  51. {
  52. data.x = x;
  53. }
  54.  
  55. int get_id() const
  56. {
  57. return data.id;
  58. }
  59.  
  60. double get_score1() const
  61. {
  62. return int_to_score(data.s1);
  63. }
  64.  
  65. double get_score2() const
  66. {
  67. return int_to_score(data.s2);
  68. }
  69.  
  70. int get_x() const
  71. {
  72. return data.x;
  73. }
  74.  
  75. data_tuple(int id = 0, double s1 = 0, double s2 = 0, int x = 0, int u = 0)
  76. {
  77. data.id = id,
  78. data.s1 = score_to_int(s1),
  79. data.s2 = score_to_int(s2),
  80. data.x = x,
  81. data.u = u;
  82. }
  83.  
  84. friend istream& operator>>(istream& in, data_tuple& a)
  85. {
  86. int id, x; double s1, s2;
  87.  
  88. in >> id >> s1 >> s2 >> x,
  89. a.data.id = id,
  90. a.set_score1(s1),
  91. a.set_score2(s2),
  92. a.data.x = x;
  93.  
  94. return in;
  95. }
  96.  
  97. static inline ostream&
  98. write(ostream& out, int id, double s1, double s2, int x)
  99. {
  100. out << id << ' ',
  101. out << fixed << setprecision(5) << s1 << ' ',
  102. out << fixed << setprecision(5) << s2 << ' ',
  103. out << x;
  104.  
  105. return out;
  106. }
  107.  
  108. friend ostream& operator<<(ostream& out, const data_tuple& a)
  109. {
  110. return write(out,a.data.id,a.get_score1(),a.get_score2(),a.data.x);
  111. }
  112.  
  113. uint64_t hash_key() const
  114. {
  115. return key;
  116. }
  117. };
  118.  
  119. int main()
  120. {
  121. srand(time(NULL));
  122.  
  123. int id = 1+(rand()%id_max);
  124. double s1 = double(rand()%(s_max+1))/s_max;
  125. double s2 = double(rand()%(s_max+1))/s_max;
  126. int x = rand()%(x_max+1);
  127.  
  128. data_tuple a(id,s1,s2,x), b;
  129.  
  130. cin >> b,
  131. cout << "testing 64-bit data_tuple" << endl,
  132. cout << "randomly generated tuple" << endl,
  133. data_tuple::write(cout,id,s1,s2,x), cout << endl,
  134. cout << a << endl,
  135. cout << a.hash_key() << endl,
  136. cout << "input tuple" << endl,
  137. cout << b << endl,
  138. cout << b.hash_key() << endl,
  139. cout << "data_tuple size = " << 8*sizeof(a) << " bits";
  140. }
Success #stdin #stdout 0s 15240KB
stdin
22 0.222 0.2222 2
stdout
testing 64-bit data_tuple
randomly generated tuple
19307685 0.35630 0.38342 0
19307685 0.35630 0.38342 0
884101577649331365
input tuple
22 0.22200 0.22220 2
5124044750252408854
data_tuple size = 64 bits