Guest User

Untitled

a guest
Jan 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <memory.h>
  6. #include <algorithm>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <vector>
  12. #include <ctime>
  13. #include <math.h>
  14. #include <fstream>
  15. #include <iterator>
  16. #include <math.h>
  17. #include <set>
  18. #include <sstream>
  19. #include <unordered_set>
  20.  
  21.  
  22.  
  23. double c = 1000;
  24. double eps = 0.1;
  25.  
  26.  
  27. namespace hash {
  28. inline uint64_t rotl64(uint64_t x, int8_t r) {
  29. return (x << r) | (x >> (64 - r));
  30. }
  31.  
  32. inline uint64_t fmix64(uint64_t k) {
  33. k ^= k >> 33;
  34. k *= 0xff51afd7ed558ccdull;
  35. k ^= k >> 33;
  36. k *= 0xc4ceb9fe1a85ec53ull;
  37. k ^= k >> 33;
  38. return k;
  39. }
  40.  
  41. class murmurhash3 {
  42. public:
  43. size_t seed;
  44. murmurhash3(size_t seed = 0) : seed(seed) {}
  45.  
  46. template<typename T>
  47. uint64_t operator()(const T &x) const {
  48. return operator()(&x, sizeof(x));
  49. }
  50.  
  51. uint64_t operator()(const void *key, const size_t len) const {
  52. uint64_t h1 = seed;
  53. uint64_t h2 = seed;
  54. const uint64_t c1 = 0x87c37b91114253d5ull;
  55. const uint64_t c2 = 0x4cf5ad432745937full;
  56. const uint64_t *blocks = (const uint64_t *) key;
  57. const size_t nblocks = len / 16;
  58. for (size_t i = 0; i < nblocks; i++) {
  59. h1 = (rotl64(h1 ^ (rotl64(*blocks++ * c1, 31) * c2), 27) + h2) * 5 + 0x52dce729ul;
  60. h2 = (rotl64(h2 ^ (rotl64(*blocks++ * c2, 33) * c1), 31) + h1) * 5 + 0x38495ab5ul;
  61. }
  62.  
  63. const uint8_t *tail = (const uint8_t *) blocks;
  64. uint64_t k1 = 0;
  65. uint64_t k2 = 0;
  66. switch (len & 15) {
  67. case 15:
  68. k2 ^= ((uint64_t) tail[14]) << 48;
  69. case 14:
  70. k2 ^= ((uint64_t) tail[13]) << 40;
  71. case 13:
  72. k2 ^= ((uint64_t) tail[12]) << 32;
  73. case 12:
  74. k2 ^= ((uint64_t) tail[11]) << 24;
  75. case 11:
  76. k2 ^= ((uint64_t) tail[10]) << 16;
  77. case 10:
  78. k2 ^= ((uint64_t) tail[9]) << 8;
  79. case 9:
  80. k2 ^= ((uint64_t) tail[8]) << 0;
  81. h2 ^= rotl64(k2 * c2, 33) * c1;
  82. case 8:
  83. k1 ^= ((uint64_t) tail[7]) << 56;
  84. case 7:
  85. k1 ^= ((uint64_t) tail[6]) << 48;
  86. case 6:
  87. k1 ^= ((uint64_t) tail[5]) << 40;
  88. case 5:
  89. k1 ^= ((uint64_t) tail[4]) << 32;
  90. case 4:
  91. k1 ^= ((uint64_t) tail[3]) << 24;
  92. case 3:
  93. k1 ^= ((uint64_t) tail[2]) << 16;
  94. case 2:
  95. k1 ^= ((uint64_t) tail[1]) << 8;
  96. case 1:
  97. k1 ^= ((uint64_t) tail[0]) << 0;
  98. h1 ^= rotl64(k1 * c1, 31) * c2;
  99. };
  100.  
  101. h1 ^= len;
  102. h2 ^= len;
  103. h1 += h2;
  104. h2 += h1;
  105. h1 = fmix64(h1);
  106. h2 = fmix64(h2);
  107. h1 += h2;
  108. h2 += h1;
  109. return h2;
  110. }
  111.  
  112.  
  113. };
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121. struct Bjkst {
  122. uint64_t z;
  123. std::unordered_set<uint64_t> B;
  124. uint64_t curr_approx;
  125. hash::murmurhash3 hash;
  126.  
  127.  
  128. };
  129.  
  130.  
  131. void bjkst(Bjkst &curr, uint64_t &elem) {
  132. //if (p(hash_combine(curr.seed, elem)) >= curr.z) {
  133. uint64_t curr_hash = curr.hash(elem);
  134.  
  135. if (!((curr_hash)& ((1ull << curr.z) - 1))) {
  136. curr.B.insert(curr_hash);
  137. while (curr.B.size() >= (c / (eps*eps))) {
  138. curr.z++;
  139. for (auto it = curr.B.begin(); it != curr.B.end(); it++) {
  140. if (*it & ((1ull << curr.z) - 1)) {
  141. curr.B.erase(it);
  142. } else
  143. curr.z++;
  144. }
  145. }
  146. }
  147. curr.curr_approx = curr.B.size() << curr.z;
  148. }
  149.  
  150.  
  151.  
  152. double CalcMHWScore(std::vector<Bjkst> &vec)
  153. {
  154. double median;
  155. size_t size = vec.size();
  156.  
  157. std::sort(vec.begin(), vec.end(),
  158. [](const Bjkst a, const Bjkst b) {
  159. return a.curr_approx < b.curr_approx;
  160. });
  161.  
  162. if (size % 2 == 0)
  163. {
  164. median = (vec[size / 2 - 1].curr_approx + vec[size / 2].curr_approx) / 2;
  165. }
  166. else
  167. {
  168. median = vec[size / 2].curr_approx;
  169. }
  170.  
  171. return median;
  172. }
  173.  
  174.  
  175. int main(int argc, char** argv) {
  176.  
  177. srand (time(NULL));
  178.  
  179. int m = log(eps)*(-1);
  180. std::vector<Bjkst> vec;
  181. std::set<uint64_t> a;
  182. for (int i = 0; i < m; ++i) {
  183. Bjkst curr;
  184. curr.z = 0;
  185. curr.hash.seed = i;
  186. vec.push_back(curr);
  187. }
  188. for(int i =0; i < 10000000; ++i) {
  189. uint64_t elem;
  190. uint64_t rand1 =rand() % 10000000 + 1;
  191. elem = i % rand1;
  192. a.insert(elem);
  193. for (int i = 0; i < m; ++i) {
  194. bjkst(vec[i], elem);
  195. }
  196. }
  197. std::cout << vec[0].B.size() << std::endl;
  198. std::cout << CalcMHWScore(vec) << std::endl;
  199. std::cout << a.size() << std::endl;
  200.  
  201.  
  202. }
  203.  
  204.  
  205. /*
  206.  
  207. int main(int argc, char** argv) {
  208. if (argc < 2) {
  209. std::cerr << "Usage: ./" << argv[0] << " {precision}]" << std::endl;
  210. return 1;
  211. }
  212.  
  213. {
  214. std::string precisionStr(argv[1]);
  215. std::istringstream is(precisionStr);
  216. is >> eps;
  217. }
  218. int m = log(eps)*(-1);
  219.  
  220.  
  221. std::vector<Bjkst> vec;
  222. for (int i = 0; i < m; ++i) {
  223. Bjkst curr;
  224. curr.z = 0;
  225. curr.seed = i*(i+1)+11;
  226. vec.push_back(curr);
  227. }
  228.  
  229. while (!(std::cin >> std::ws).eof()) {
  230. size_t elem;
  231. std::cin >> elem;
  232. for (int i = 0; i < m; ++i) {
  233. bjkst(vec[i],elem);
  234. }
  235. }
  236.  
  237.  
  238. std::cout << CalcMHWScore(vec);
  239.  
  240. }
  241. */
Add Comment
Please, Sign In to add comment