Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.02 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #pragma warning(disable:4244)
  3. #pragma warning(disable:4239)
  4. #pragma warning(disable:4018)
  5.  
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <stdlib.h>
  10. #include <memory>
  11. #include <vector>
  12. #include <functional>
  13. #include <array>
  14. #include <tuple>
  15. #include <cassert>
  16. #include <time.h>
  17. #include <set>
  18. #include <string>
  19. #include <ctime>
  20.  
  21. using namespace std;
  22.  
  23. typedef unsigned long long ull;
  24. typedef string Filename;
  25.  
  26. //const int BLOCK = 8000;
  27. //const int BLOCK = 15000;
  28. const int BLOCK = 3100;
  29. //const int BLOCK = 8;
  30. const int ALL_BYTES = 170000;
  31. unsigned char gl_buf[ALL_BYTES + 3000];
  32.  
  33. vector<unsigned int> ex;
  34. set<unsigned int> ss;
  35. struct RNG {
  36. int operator() (int n) {
  37. return std::rand() / (1.0 + RAND_MAX) * n;
  38. }
  39. };
  40.  
  41. //double tot_sort_time, tot_join_time;//
  42.  
  43. void prepare_file_without_memory() {
  44. FILE* input = fopen("input.bin", "wb");
  45. //setvbuf(input, NULL, _IONBF, BUFSIZ);
  46. const int n = 1000;
  47. //const int n = 5;
  48. //const int n = 650000;
  49. //const int n = 200000;//////..
  50.  
  51. fwrite(&n, 4, 1, input);
  52.  
  53. for (int i = 0; i < n; i++) {
  54. unsigned int cur = i + 1;
  55. unsigned int cur2 = (i + 1) % n + 1;
  56. fwrite(&cur, 4, 1, input);
  57. fwrite(&cur2, 4, 1, input);
  58. }
  59. fclose(input);
  60. }
  61.  
  62. void prepare_file() {
  63. FILE* input = fopen("input.bin", "wb");
  64.  
  65. const int n = 10;
  66. //const int n = 600000;
  67. //const int n = 30000;
  68. for (int i = 1; i <= n; i++) {
  69. //unsigned int cur = rand() % (1 << 30);
  70. unsigned int cur = i;
  71. if (ss.find(cur) != ss.end()) {
  72. i--;
  73. continue;
  74. }
  75. ss.insert(cur);
  76. ex.push_back(cur);
  77. }
  78. random_shuffle(ex.begin(), ex.end(), RNG());
  79. fwrite(&n, 4, 1, input);
  80.  
  81. vector<pair<int, int> > edges;
  82. for (int i = 0; i < n; i++) {
  83. //edges.push_back({ ex[i], ex[(i + 1) % n] });
  84. edges.push_back(make_pair( ex[i], ex[(i + 1) % n] ) );
  85. }
  86.  
  87. random_shuffle(edges.begin(), edges.end());
  88.  
  89. for (int i = 0; i < n; i++) {
  90. fwrite(&edges[i].first, 4, 1, input);
  91. fwrite(&edges[i].second, 4, 1, input);
  92. }
  93.  
  94. fclose(input);
  95.  
  96. int min_element_pos, min_element = (1 << 30) + 1;
  97.  
  98. for (int i = 0; i < ex.size(); i++) {
  99. if (ex[i] < min_element) {
  100. min_element = ex[i];
  101. min_element_pos = i;
  102. }
  103. }
  104. rotate(ex.begin(), ex.begin() + min_element_pos, ex.end());
  105. }
  106.  
  107. void prepate_file_test() {
  108. FILE* input = fopen("input.bin", "wb");
  109. vector<unsigned int> v{4, 1, 2, 2, 3, 2, 4};
  110. fclose(input);
  111. }
  112.  
  113. void check_file(const char* filename, int ln = 1) {
  114. cerr << "---" << endl;
  115. cerr << filename << endl;
  116. FILE* input = fopen(filename, "rb");
  117. unsigned int cur;
  118. //unsigned long long cur;
  119. int cnt = 0;
  120. while (fread(&cur, 4, 1, input)) {
  121. cerr << (int)cur << " ";
  122. cnt = (cnt + 1) % ln;
  123. if (cnt == 0) {
  124. cerr << endl;
  125. }
  126. }
  127. fclose(input);
  128. }
  129.  
  130. void check_file_sort(const char* filename, int ln = 1) {
  131. cerr << "---" << endl;
  132. cerr << filename << endl;
  133. FILE* input = fopen(filename, "rb");
  134. unsigned long long n;
  135. fread(&n, 8, 1, input);
  136. cerr << n << endl;
  137. unsigned long long cur;
  138.  
  139. int cnt = 0;
  140. while (fread(&cur, 8, 1, input)) {
  141. cerr << (unsigned long long)cur << " ";
  142. cnt = (cnt + 1) % ln;
  143. if (cnt == 0) {
  144. cerr << endl;
  145. }
  146. }
  147. fclose(input);
  148. }
  149.  
  150. void check_output() {
  151. FILE* input = fopen("output.bin", "rb");
  152. unsigned int cur;
  153. vector<unsigned int> res;
  154. while (fread(&cur, 4, 1, input)) {
  155. res.push_back(cur);
  156. }
  157.  
  158. if (ex != res) {
  159. cout << "wrong" << endl;
  160. for (int i = 0; i < ex.size(); i++) {
  161. if (ex[i] != res[i]) {
  162. cout << i << " " << ex[i] << " " << res[i] << endl;
  163. }
  164. }
  165. }
  166. else {
  167. cout << "ok" << endl;
  168. }
  169. }
  170.  
  171.  
  172. struct Link {
  173. unsigned int key;
  174. unsigned int next;
  175. Link() {}
  176. Link(unsigned int k, unsigned int nx) : key(k), next(nx) {}
  177. bool operator < (const Link &A) const {
  178. return key < A.key;
  179. }
  180. //~Link() = default;
  181. };
  182.  
  183. Filename get_filename(int id, const char* source) {
  184. return string(source) + to_string(static_cast<long long>(id) );
  185. }
  186.  
  187.  
  188. template<typename T>
  189. Filename do_simple_sort(int l, int r, int id, const char* filename, unsigned int offset) {
  190. T* sbuf = (T*)gl_buf;
  191. FILE* inp = fopen(filename, "rb");
  192. unsigned int uno_sz = sizeof(T);
  193. fseek(inp, l * uno_sz + offset, SEEK_SET);
  194. int sz = r - l + 1;
  195.  
  196. fread(sbuf, uno_sz, sz, inp);
  197. fclose(inp);
  198.  
  199. sort(sbuf, sbuf + sz);
  200.  
  201. Filename output_file_name_id = get_filename(id, filename);
  202.  
  203. FILE* out = fopen(output_file_name_id.c_str(), "wb");
  204. //setvbuf(out, NULL, _IONBF, BUFSIZ);
  205. fwrite(sbuf, uno_sz, sz, out);
  206. fclose(out);
  207.  
  208. return output_file_name_id;
  209. }
  210.  
  211.  
  212.  
  213. template<typename T>
  214. struct FileReader {
  215. T* buf;
  216. int len, cur_block, sz, block_size;
  217. unsigned int uno_sz;
  218. FILE* inp;
  219.  
  220. void reset(const char* filename, int w, int file_len, int block_sz) {
  221. buf = (T*)(gl_buf + w);
  222. block_size = block_sz;
  223. uno_sz = sizeof(T);
  224. len = file_len;
  225. cur_block = -1;
  226. inp = fopen(filename, "rb");
  227. load(0);
  228. }
  229.  
  230. void load(int idx) {
  231. int block = idx / block_size;
  232. if (cur_block != block) {
  233. cur_block = block;
  234. fseek(inp, uno_sz * block_size * block, SEEK_SET);
  235. sz = min(block_size, len - block * block_size);
  236. fread(buf, uno_sz, sz, inp);
  237. }
  238. }
  239.  
  240. T get(int idx) {
  241. load(idx);
  242. return buf[idx % block_size];
  243. }
  244. };
  245.  
  246.  
  247. template<typename T>
  248. Filename merge(int l, int r, const vector<Filename>& inputs, const int PARTS,
  249. const vector<int>& file_lens, int to_id, const char* filename) {
  250. //cerr << "merge " << l << " " << r << " " << to_id << " " << filename << endl;
  251. //check_file(inputs[0].c_str(), 2);
  252. //check_file(inputs[1].c_str(), 2);
  253. FileReader<T> file_readers[4];
  254.  
  255. Filename output_file_name_id = get_filename(to_id, filename);
  256. FILE* out = fopen(output_file_name_id.c_str(), "wb");
  257. //setvbuf(out, NULL, _IONBF, BUFSIZ);
  258.  
  259. const int block_sz = ALL_BYTES / sizeof(T) / (PARTS + 1);
  260. //cerr << "block_sz " << block_sz << " " << l << " " << r << endl;
  261. vector<int> cur_ptrs(PARTS);
  262.  
  263. for (int i = 0; i < PARTS; i++) {
  264. //cerr << file_lens[i] << " " << inputs[i] << endl;
  265. file_readers[i].reset(inputs[i].c_str(), sizeof(T) * i * block_sz, file_lens[i], block_sz);
  266. //setvbuf(file_readers[i].inp, NULL, _IONBF, BUFSIZ);
  267. }
  268.  
  269. T* output_buf = (T*)(gl_buf + PARTS * sizeof(T) * block_sz);
  270. int out_cnt = 0;
  271.  
  272. while (true) {
  273. int pos_min = -1;
  274. T min_element, cons;
  275. for (int i = 0; i < PARTS; i++) {
  276. if (cur_ptrs[i] < file_lens[i]) {
  277. cons = file_readers[i].get(cur_ptrs[i]);
  278. if (pos_min == -1 || cons < min_element) {
  279. min_element = cons;
  280. pos_min = i;
  281. }
  282. }
  283. }
  284. //cerr << "pos_min: " << pos_min << endl;
  285. if (pos_min == -1) {
  286. break;
  287. }
  288.  
  289. cur_ptrs[pos_min]++;
  290. //fwrite(&min_element, sizeof(T), 1, out);
  291.  
  292. output_buf[out_cnt++] = min_element;
  293. if (out_cnt == block_sz) {
  294. fwrite(output_buf, sizeof(T), out_cnt, out);
  295. out_cnt = 0;
  296. }
  297. }
  298.  
  299. if (out_cnt > 0) {
  300. fwrite(output_buf, sizeof(T), out_cnt, out);
  301. out_cnt = 0;
  302. }
  303.  
  304. fclose(out);
  305.  
  306. for (int i = 0; i < PARTS; i++) {
  307. fclose(file_readers[i].inp);
  308. }
  309. //cerr << "finished" << endl;
  310. return output_file_name_id;
  311. }
  312.  
  313. template<typename T>
  314. Filename ext_sort(int l, int r, int id, const char* filename, unsigned int offset) {
  315. //cerr << "sort " << l << " " << r << " " << id << " " << filename << " " << offset << endl;
  316. if (id == 0) {
  317. //BLOCK = ALL_BYTES / 2 / sizeof(T);
  318.  
  319. }
  320.  
  321. //clock_t begin = clock();
  322.  
  323. if (r - l + 1 <= ALL_BYTES / sizeof(T)) {
  324. Filename res = do_simple_sort<T>(l, r, id, filename, offset);
  325.  
  326. //clock_t end = clock();
  327. //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  328. if (id == 0) {
  329. //tot_sort_time += elapsed_secs;
  330. }
  331.  
  332. return res;
  333. }
  334. const int PARTS = 4;
  335. int all_sz = r - l + 1;
  336. int base_chunk_len = all_sz / PARTS;
  337.  
  338. vector<int> lens(PARTS);
  339. for (int i = 0; i < PARTS; i++) {
  340. lens[i] = base_chunk_len;
  341. }
  342. for (int i = 0; i < all_sz % PARTS; i++) {
  343. lens[i]++;
  344. }
  345. //cerr << "all sz chunk sz: " << all_sz << " " << chunk_sz << endl;
  346. vector<Filename> results(PARTS);
  347. int lb = l;
  348. for (int i = 0; i < PARTS; i++) {
  349. int rb = lb + lens[i] - 1;
  350. //cerr << "will call: " << lb << " " << rb << endl;
  351. results[i] = ext_sort<T>(lb, rb, id * PARTS + i + 1, filename, offset);
  352. lb += lens[i];
  353. }
  354.  
  355. Filename res = merge<T>(l, r, results, PARTS, lens, id, filename);
  356. for (int i = 0; i < PARTS; i++) {
  357. remove(results[i].c_str());
  358. }
  359. if (id == 0) {
  360. //BLOCK = 1500;
  361. }
  362.  
  363. //clock_t end = clock();
  364. //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  365. if (id == 0) {
  366. //tot_sort_time += elapsed_secs;
  367. }
  368.  
  369. return res;
  370. }
  371.  
  372. void prepare_link_files(const char* direct_filename, const char* reverse_filename, FILE* input_file, unsigned int n) {
  373. Link* sbuf = (Link*)gl_buf;
  374. FILE* reverse_file = fopen(reverse_filename, "wb");
  375. FILE* direct_file = fopen(direct_filename, "wb");
  376. //const int block_sz = ALL_BYTES / sizeof(Link);
  377. const int block_sz = BLOCK;
  378. for (int i = 0; i < n; i += block_sz) {
  379. int sz = min(block_sz, (int)n - i);
  380. fread(sbuf, sizeof(Link), sz, input_file);
  381.  
  382. fwrite(sbuf, sizeof(Link), sz, direct_file);
  383.  
  384. for (int j = 0; j < sz; j++) {
  385. Link& cur = sbuf[j];
  386. swap(cur.key, cur.next);
  387. }
  388.  
  389. fwrite(sbuf, sizeof(Link), sz, reverse_file);
  390.  
  391. }
  392.  
  393. fclose(reverse_file);
  394. fclose(direct_file);
  395. }
  396.  
  397. template<typename T1, typename T2, typename Tres>
  398. void join(const char* first_filename, const char* second_filename,
  399. const char* result_filename, function<Tres(const T1&, const T2&)> joiner) {
  400. const int WORK_SZ = ALL_BYTES / (sizeof(T1) + sizeof(T2) + sizeof(Tres));
  401. //clock_t begin = clock();
  402.  
  403. T1* sbuf = (T1*)gl_buf;
  404. T2* qbuf = (T2*)(gl_buf + WORK_SZ * sizeof(T1));
  405. Tres* output_buf = (Tres*)(gl_buf + WORK_SZ * (sizeof(T1) + sizeof(T2)));
  406.  
  407. unsigned int ptr1 = WORK_SZ, ptr2 = WORK_SZ;
  408. unsigned int sz1 = 0, sz2 = 0, sz_output = 0;
  409. FILE* result_file = fopen(result_filename, "wb");
  410.  
  411. FILE* t1_file = fopen(first_filename, "rb");
  412. FILE* t2_file = fopen(second_filename, "rb");
  413.  
  414. while (true) {
  415. if (ptr1 >= sz1) {
  416. sz1 = fread(sbuf, sizeof(T1), WORK_SZ, t1_file);
  417. if (sz1 <= 0) {
  418. break;
  419. }
  420. ptr1 = 0;
  421. }
  422.  
  423. if (ptr2 >= sz2) {
  424. sz2 = fread(qbuf, sizeof(T2), WORK_SZ, t2_file);
  425. if (sz2 <= 0) {
  426. break;
  427. }
  428. ptr2 = 0;
  429. }
  430.  
  431. while (ptr1 < sz1 && ptr2 < sz2 && sbuf[ptr1].key < qbuf[ptr2].key) {
  432. ptr1++;
  433. }
  434.  
  435. while (ptr2 < sz2 && ptr1 < sz1 && qbuf[ptr2].key < sbuf[ptr1].key) {
  436. ptr2++;
  437. }
  438.  
  439. if (ptr1 < sz1 && ptr2 < sz2 && sbuf[ptr1].key == qbuf[ptr2].key) {
  440. output_buf[sz_output] = joiner(sbuf[ptr1], qbuf[ptr2]);
  441. sz_output++; ptr1++; ptr2++;
  442. if (sz_output == WORK_SZ) {
  443. fwrite(&output_buf[0], sizeof(Tres), WORK_SZ, result_file);
  444. sz_output = 0;
  445. }
  446.  
  447. }
  448.  
  449. }
  450.  
  451. if (sz_output > 0) {
  452. fwrite(&output_buf[0], sizeof(Tres), sz_output, result_file);
  453. sz_output = 0;
  454. }
  455.  
  456. fclose(result_file);
  457. fclose(t1_file);
  458. fclose(t2_file);
  459.  
  460. //clock_t end = clock();
  461. //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  462. //tot_join_time += elapsed_secs;
  463. }
  464.  
  465.  
  466. Link joiner(Link A, Link B) {
  467. Link res(B.next, A.next);
  468. return res;
  469. }
  470.  
  471. Filename prepare_next_next_file(const char* direct_filename, const char* reverse_filename, int n) {
  472. Filename next_next_filename = get_filename(n, "next_next");
  473. join<Link, Link, Link>(direct_filename, reverse_filename, next_next_filename.c_str(), joiner);
  474.  
  475. return next_next_filename;
  476.  
  477. }
  478.  
  479.  
  480. tuple<Filename, Filename, Filename> next_next(const char* input_name, unsigned int n) {
  481. FILE* input_file = fopen(input_name, "rb");
  482.  
  483. Filename direct_filename = get_filename(n, "direct");
  484. Filename reverse_filename = get_filename(n, "reverse");
  485.  
  486. prepare_link_files(direct_filename.c_str(), reverse_filename.c_str(), input_file, n);
  487.  
  488. fclose(input_file);
  489.  
  490. Filename result_direct_file = ext_sort<Link>(0, n - 1, 0, direct_filename.c_str(), 0);
  491. Filename result_reverse_file = ext_sort<Link>(0, n - 1, 0, reverse_filename.c_str(), 0);
  492. Filename next_next_filename = prepare_next_next_file(result_direct_file.c_str(), result_reverse_file.c_str(), n);
  493. Filename sorted_next_next_filename = ext_sort<Link>(0, n - 1, 0, next_next_filename.c_str(), 0);
  494.  
  495. remove(next_next_filename.c_str());
  496. remove(direct_filename.c_str());
  497. remove(reverse_filename.c_str());
  498.  
  499. return make_tuple(move(result_direct_file), move(result_reverse_file), move(sorted_next_next_filename));
  500. }
  501.  
  502. void transform_file_from_irunner(FILE* input, const char* input_normal_name, unsigned int n) {
  503. ull* sbuf = (ull*)gl_buf;
  504. FILE* normal_file = fopen(input_normal_name, "wb");
  505.  
  506. for (int i = 0; i < n; i += BLOCK) {
  507. int sz = min(BLOCK, (int)n - i);
  508. fread(sbuf, 8, sz, input);
  509. fwrite(sbuf, 8, sz, normal_file);
  510. }
  511. fclose(normal_file);
  512. }
  513.  
  514.  
  515. void transform_output_to_irunner(const char* output_name, const char* direct_name, const char* next_next_name, unsigned int n) {
  516. FILE* output = fopen(output_name, "wb");
  517. FILE* direct_file = fopen(direct_name, "rb");
  518. FILE* next_next_file = fopen(next_next_name, "rb");
  519.  
  520. Link* sbuf = (Link*)gl_buf;
  521. Link* qbuf = (Link*)(gl_buf + BLOCK * sizeof(Link));
  522.  
  523. for (int i = 0; i < n; i += BLOCK) {
  524. int sz = min(BLOCK, (int)n - i);
  525. fread(sbuf, sizeof(Link), sz, direct_file);
  526. fread(qbuf, sizeof(Link), sz, next_next_file);
  527.  
  528. for (int j = 0; j < sz; j++) {
  529. unsigned int from_d = sbuf[j].key;
  530. unsigned int to_d = sbuf[j].next;
  531. unsigned int to_n = qbuf[j].next;
  532.  
  533. fwrite(&from_d, 4, 1, output);
  534. fwrite(&to_d, 4, 1, output);
  535. fwrite(&to_n, 4, 1, output);
  536. }
  537. }
  538.  
  539. fclose(output);
  540. fclose(direct_file);
  541. fclose(next_next_file);
  542. }
  543.  
  544. struct NodeWeight {
  545. unsigned int key;
  546. unsigned int weight;
  547. NodeWeight() {};
  548. NodeWeight(int k, int w) : key(k), weight(w) {};
  549. bool operator < (const NodeWeight& other) const {
  550. return key < other.key;
  551. }
  552. };
  553.  
  554.  
  555. Filename prepare_weights(const char* from, int n) {
  556. FILE* input = fopen(from, "rb");
  557.  
  558. Link* sbuf = (Link*)gl_buf;
  559.  
  560. Filename output_name = get_filename(n, "weights");
  561. FILE* output = fopen(output_name.c_str(), "wb");
  562.  
  563. for (int i = 0; i < n; i += BLOCK) {
  564. unsigned int sz = min(BLOCK, n - i);
  565. fread(sbuf, sizeof(Link), sz, input);
  566. for (int j = 0; j < sz; j++) {
  567. NodeWeight cur(sbuf[j].key, 1);
  568. fwrite(&cur, sizeof(NodeWeight), 1, output);
  569. }
  570. }
  571.  
  572. fclose(output);
  573. fclose(input);
  574.  
  575. Filename sorted_weights_name = ext_sort<Link>(0, n - 1, 0, output_name.c_str(), 0);
  576.  
  577. return sorted_weights_name;
  578. }
  579.  
  580. //double tot_flip_coins = 0.0;
  581.  
  582. Filename flip_coins(const char* input_now, int n) {
  583. //clock_t begin = clock();
  584.  
  585. FILE* input = fopen(input_now, "rb");
  586.  
  587. Link* sbuf = (Link*)gl_buf;
  588.  
  589. Filename output_name = get_filename(n, "coins");
  590. FILE* output = fopen(output_name.c_str(), "wb");
  591.  
  592. for (int i = 0; i < n; i += BLOCK) {
  593. unsigned int sz = min(BLOCK, n - i);
  594. fread(sbuf, sizeof(Link), sz, input);
  595. for (int j = 0; j < sz; j++) {
  596. NodeWeight cur(sbuf[j].key, (unsigned int)rand() % 2);
  597. fwrite(&cur, sizeof(NodeWeight), 1, output);
  598. }
  599. }
  600.  
  601. fclose(output);
  602. fclose(input);
  603.  
  604. //clock_t end = clock();
  605. //double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  606. //tot_flip_coins += elapsed_secs;
  607.  
  608. return output_name;
  609. }
  610.  
  611. struct NodeNext {
  612. unsigned int key;
  613. unsigned int next;
  614. unsigned int next_next;
  615. NodeNext() {};
  616. NodeNext(unsigned int k, unsigned int nx, unsigned int nx_nx) :
  617. key(k), next(nx), next_next(nx_nx) {};
  618. bool operator < (const NodeNext& other) const {
  619. return key < other.key;
  620. }
  621. };
  622.  
  623. struct NodeNextW1 : NodeNext {
  624. unsigned int w1;
  625. NodeNextW1() {};
  626. NodeNextW1(unsigned int a,
  627. unsigned int b,
  628. unsigned int c,
  629. unsigned int ww1) : NodeNext(a, b, c), w1(ww1) {};
  630. };
  631.  
  632. struct NodeNextW1F1 : NodeNextW1 {
  633. unsigned int f1;
  634. NodeNextW1F1() {};
  635. NodeNextW1F1(unsigned int a,
  636. unsigned int b,
  637. unsigned int c,
  638. unsigned int w1,
  639. unsigned int ff1) : NodeNextW1(a, b, c, w1), f1(ff1) {};
  640. };
  641.  
  642. struct NodeNextW2F1 : NodeNextW1F1 {
  643. unsigned int w2;
  644. NodeNextW2F1() {};
  645. NodeNextW2F1(unsigned int a,
  646. unsigned int b,
  647. unsigned int c,
  648. unsigned int w1,
  649. unsigned int f1,
  650. unsigned int ww2) : NodeNextW1F1(a, b, c, w1, f1), w2(ww2) {};
  651. };
  652.  
  653. struct NodeNextW2F2 : NodeNextW2F1 {
  654. unsigned int f2;
  655. NodeNextW2F2() {};
  656. NodeNextW2F2(unsigned int a,
  657. unsigned int b,
  658. unsigned int c,
  659. unsigned int w1,
  660. unsigned int f1,
  661. unsigned int w2,
  662. unsigned int ff2) : NodeNextW2F1(a, b, c, w1, f1, w2), f2(ff2) {};
  663. };
  664.  
  665. struct NodeNextW2F3 : NodeNextW2F2 {
  666. unsigned int f3;
  667. NodeNextW2F3() {};
  668. NodeNextW2F3(unsigned int a,
  669. unsigned int b,
  670. unsigned int c,
  671. unsigned int w1,
  672. unsigned int f1,
  673. unsigned int w2,
  674. unsigned int f2,
  675. unsigned int ff3) : NodeNextW2F2(a, b, c, w1, f1, w2, f2), f3(ff3) {};
  676. };
  677.  
  678. struct NodeNextW3F3 : NodeNextW2F3 {
  679. unsigned int w3;
  680. NodeNextW3F3() {};
  681. NodeNextW3F3(unsigned int a,
  682. unsigned int b,
  683. unsigned int c,
  684. unsigned int w1,
  685. unsigned int f1,
  686. unsigned int w2,
  687. unsigned int f2,
  688. unsigned int f3,
  689. unsigned int ww3) : NodeNextW2F3(a, b, c, w1, f1, w2, f2, f3), w3(ww3) {};
  690. };
  691.  
  692. void do_simple_ranking(const char* input_now, const char* weights_now, const char* output_name, unsigned int n) {
  693. FILE* input = fopen(input_now, "rb");
  694. FILE* output = fopen(output_name, "wb");
  695. FILE* weights_file = fopen(weights_now, "rb");
  696.  
  697. Link* links = (Link*)gl_buf;
  698. NodeWeight* weights = (NodeWeight*)(gl_buf + n * sizeof(Link));
  699.  
  700. fread(links, sizeof(Link), n, input);
  701. sort(links, links + n);
  702.  
  703. fread(weights, sizeof(NodeWeight), n, weights_file);
  704.  
  705. unsigned int now = links[0].key, ranking = 0;
  706. for (int i = 0; i < n; i++) {
  707. auto it_w = lower_bound(weights, weights + n, NodeWeight(now, 0));
  708. ranking += it_w->weight;
  709.  
  710. Link cur(now, ranking);
  711. fwrite(&cur, sizeof(Link), 1, output);
  712. auto it = lower_bound(links, links + n, Link(now, 0));
  713. now = it->next;
  714. }
  715.  
  716. fclose(input);
  717. fclose(output);
  718. fclose(weights_file);
  719. }
  720.  
  721. tuple<Filename, Filename, Filename, unsigned int> make_new_graph(const char* input_name, int old_n) {
  722. FILE* input = fopen(input_name, "rb");
  723. const int block_sz = ALL_BYTES / (sizeof(NodeNextW3F3) + sizeof(Link) + sizeof(Link) + sizeof(NodeWeight));
  724.  
  725. NodeNextW3F3* sbuf = (NodeNextW3F3*)gl_buf;
  726. Link* output_buf = (Link*)(gl_buf + block_sz * sizeof(NodeNextW3F3));
  727. Link* removed_edges_buf = (Link*)(gl_buf + block_sz * (sizeof(NodeNextW3F3) + sizeof(Link)));
  728. NodeWeight* new_weights_buf = (NodeWeight*)(gl_buf + block_sz * (sizeof(NodeNextW3F3) + sizeof(Link) + sizeof(Link)));
  729.  
  730. int output_buf_cnt = 0, removed_edges_cnt = 0, new_weights_cnt = 0;
  731. int new_n = old_n;
  732.  
  733. Filename new_graph = get_filename(old_n, "graph_from");
  734. FILE* output = fopen(new_graph.c_str(), "wb");
  735. //setvbuf(output, NULL, _IONBF, BUFSIZ);
  736.  
  737. Filename removed_ed_name = get_filename(old_n, "removed_ed");
  738. FILE* removed_edges = fopen(removed_ed_name.c_str(), "wb");
  739. //setvbuf(removed_edges, NULL, _IONBF, BUFSIZ);
  740.  
  741. Filename new_weights_name = get_filename(old_n, "new_weights");
  742. FILE* new_weights = fopen(new_weights_name.c_str(), "wb");
  743. //setvbuf(new_weights, NULL, _IONBF, BUFSIZ);
  744.  
  745. //exit(0);
  746. for (int i = 0; i < old_n; i += block_sz) {
  747. int sz = min(block_sz, old_n - i);
  748. fread(sbuf, sizeof(NodeNextW3F3), sz, input);
  749. for (int j = 0; j < sz; j++) {
  750. NodeNextW3F3 &cur = sbuf[j];
  751. // i n(i) n(n(i)) w(i) f(i) w(n(i)) f(n(i)) f(n(n(i)))
  752. //cerr << cur.key << " " << cur.next << " " << cur.next_next << " " << cur.w1 << " "
  753. // << cur.f1 << " " << cur.w2 << " " << cur.f2 << " " << cur.f3 << endl;
  754.  
  755. // considering what happens to n(i) - n(n(i)) edge
  756. // i == 1 and n(i) == 0 -> remove n(i)
  757. if (cur.f1 == 1 && cur.f2 == 0) {
  758. Link edge(cur.key, cur.next_next);
  759. //cerr << "remove and next" << cur.key << " " << cur.next_next << endl;
  760. output_buf[output_buf_cnt++] = edge;
  761. if (output_buf_cnt == block_sz) {
  762. fwrite(output_buf, sizeof(Link), output_buf_cnt, output);
  763. output_buf_cnt = 0;
  764. }
  765.  
  766. //fwrite(&edge, sizeof(Link), 1, output);
  767. new_n--;
  768.  
  769. Link removed_edge(cur.key, cur.next);
  770. //cerr << "removed edge " << cur.key << " " << cur.next << endl;
  771.  
  772. removed_edges_buf[removed_edges_cnt++] = removed_edge;
  773. if (removed_edges_cnt == block_sz) {
  774. fwrite(removed_edges_buf, sizeof(Link), removed_edges_cnt, removed_edges);
  775. removed_edges_cnt = 0;
  776. }
  777. //fwrite(&removed_edge, sizeof(Link), 1, removed_edges);
  778. }
  779. else {
  780. // n(i) -> n(n(i)) will exist
  781. if (!(cur.f2 == 1 && cur.f3 == 0)) {
  782. //cerr << "save " << cur.next << " " << cur.next_next << endl;
  783. Link edge(cur.next, cur.next_next);
  784. output_buf[output_buf_cnt++] = edge;
  785. if (output_buf_cnt == block_sz) {
  786. fwrite(output_buf, sizeof(Link), output_buf_cnt, output);
  787. output_buf_cnt = 0;
  788. }
  789. //fwrite(&edge, sizeof(Link), 1, output);
  790.  
  791. NodeWeight new_w(cur.next, cur.w2);
  792.  
  793. new_weights_buf[new_weights_cnt++] = new_w;
  794. if (new_weights_cnt == block_sz) {
  795. fwrite(new_weights_buf, sizeof(NodeWeight), new_weights_cnt, new_weights);
  796. new_weights_cnt = 0;
  797. }
  798. //fwrite(&new_w, sizeof(NodeWeight), 1, new_weights);
  799. }
  800. else {
  801. NodeWeight new_w(cur.next, cur.w2 + cur.w3);
  802.  
  803. new_weights_buf[new_weights_cnt++] = new_w;
  804. if (new_weights_cnt == block_sz) {
  805. fwrite(new_weights_buf, sizeof(NodeWeight), new_weights_cnt, new_weights);
  806. new_weights_cnt = 0;
  807. }
  808. //fwrite(&new_w, sizeof(NodeWeight), 1, new_weights);
  809. }
  810. }
  811. }
  812. }
  813.  
  814. if (output_buf_cnt > 0) {
  815. fwrite(output_buf, sizeof(Link), output_buf_cnt, output);
  816. output_buf_cnt = 0;
  817. }
  818.  
  819. if (removed_edges_cnt > 0) {
  820. fwrite(removed_edges_buf, sizeof(Link), removed_edges_cnt, removed_edges);
  821. removed_edges_cnt = 0;
  822. }
  823.  
  824. if (new_weights_cnt > 0) {
  825. fwrite(new_weights_buf, sizeof(NodeWeight), new_weights_cnt, new_weights);
  826. new_weights_cnt = 0;
  827. }
  828.  
  829. fclose(input);
  830. fclose(output);
  831. fclose(removed_edges);
  832. fclose(new_weights);
  833.  
  834. assert(new_n < old_n);
  835. Filename sorted_new_weights = ext_sort<NodeWeight>(0, new_n - 1, 0, new_weights_name.c_str(), 0);
  836.  
  837. remove(new_weights_name.c_str());
  838. return make_tuple(move(new_graph), move(removed_ed_name), move(sorted_new_weights), new_n);
  839.  
  840. }
  841.  
  842.  
  843. void copy_file(FILE* to, const char* from) {
  844. FILE* file_to_read = fopen(from, "rb");
  845. unsigned char* sbuf = gl_buf;
  846.  
  847. while (true) {
  848. int sz = fread(sbuf, 1, ALL_BYTES, file_to_read);
  849. if (sz <= 0) {
  850. break;
  851. }
  852. fwrite(sbuf, 1, sz, to);
  853. }
  854. fclose(file_to_read);
  855. }
  856.  
  857. void concat_files(const char* file_to, const char* file_add_A, const char* file_add_B) {
  858. FILE* file_to_write = fopen(file_to, "wb");
  859. copy_file(file_to_write, file_add_A);
  860. copy_file(file_to_write, file_add_B);
  861. fclose(file_to_write);
  862. }
  863.  
  864. Filename list_rank(const char* input_now, const char* weights_now, int n) {
  865. Filename result = get_filename(n, "raw_ranking");
  866. if (n < ALL_BYTES / (sizeof(Link) + sizeof(NodeWeight))) {
  867. do_simple_ranking(input_now, weights_now, result.c_str(), n);
  868. return result;
  869. }
  870. tuple<Filename, Filename, Filename> sorted_next_next = next_next(input_now, n);
  871. Filename next_next_file = get_filename(n, "next_nnext");
  872. join<Link, Link, NodeNext>(get<0>(sorted_next_next).c_str(),
  873. get<2>(sorted_next_next).c_str(),
  874. next_next_file.c_str(),
  875. [](const Link& next, const Link& next_next) -> NodeNext {
  876. // i n(i) n(n(i))
  877. return NodeNext(next.key, next.next, next_next.next);
  878. }
  879. );
  880.  
  881. remove(get<0>(sorted_next_next).c_str());
  882. remove(get<1>(sorted_next_next).c_str());
  883. remove(get<2>(sorted_next_next).c_str());
  884.  
  885. Filename coins = flip_coins(input_now, n);
  886. Filename sorted_coins = ext_sort<Link>(0, n - 1, 0, coins.c_str(), 0);
  887.  
  888. remove(coins.c_str());
  889.  
  890. Filename next_next_w1 = get_filename(n, "next_w1");
  891. join<NodeNext, NodeWeight, NodeNextW1>(next_next_file.c_str(),
  892. weights_now,
  893. next_next_w1.c_str(),
  894. [](const NodeNext& A, const NodeWeight& B) -> NodeNextW1 {
  895. // i n(i) n(n(i)) w(i)
  896. return NodeNextW1(A.key, A.next, A.next_next, B.weight);
  897. }
  898. );
  899.  
  900. remove(next_next_file.c_str());
  901.  
  902. Filename next_next_w1_f1 = get_filename(n, "next_w1_f1");
  903. join<NodeNextW1, Link, NodeNextW1F1>(next_next_w1.c_str(),
  904. sorted_coins.c_str(),
  905. next_next_w1_f1.c_str(),
  906. [](const NodeNextW1& A, const Link& B) -> NodeNextW1F1 {
  907. // n(i) i n(n(i)) w(i) f(i)
  908. return NodeNextW1F1(A.next, A.key, A.next_next, A.w1, B.next);
  909. }
  910. );
  911. Filename sorted_next_next_w1_f1 = ext_sort<NodeNextW1F1>(0, n - 1, 0, next_next_w1_f1.c_str(), 0);
  912.  
  913. remove(next_next_w1.c_str());
  914. remove(next_next_w1_f1.c_str());
  915.  
  916. Filename next_next_w2_f1 = get_filename(n, "next_w2_f1");
  917. join<NodeNextW1F1, NodeWeight, NodeNextW2F1>(sorted_next_next_w1_f1.c_str(),
  918. weights_now,
  919. next_next_w2_f1.c_str(),
  920. [](const NodeNextW1F1& A, const NodeWeight& B) -> NodeNextW2F1 {
  921. // n(i) i n(n(i)) w(i) f(i) w(n(i))
  922. return NodeNextW2F1(A.key, A.next, A.next_next, A.w1, A.f1, B.weight);
  923. }
  924. );
  925.  
  926. remove(sorted_next_next_w1_f1.c_str());
  927.  
  928. Filename next_next_w2_f2 = get_filename(n, "next_w2_f2");
  929. join<NodeNextW2F1, Link, NodeNextW2F2>(next_next_w2_f1.c_str(),
  930. sorted_coins.c_str(),
  931. next_next_w2_f2.c_str(),
  932. [](const NodeNextW2F1& A, const Link& B) -> NodeNextW2F2 {
  933. // n(n(i)) i n(i) w(i) f(i) w(n(i)) f(n(i))
  934. return NodeNextW2F2(A.next_next, A.next, A.key, A.w1, A.f1, A.w2, B.next);
  935. }
  936. );
  937. Filename sorted_next_next_w2_f2 = ext_sort<NodeNextW2F2>(0, n - 1, 0, next_next_w2_f2.c_str(), 0);
  938.  
  939. remove(next_next_w2_f1.c_str());
  940. remove(next_next_w2_f2.c_str());
  941.  
  942. Filename next_next_w2_f3 = get_filename(n, "next_w2_f3");
  943. join<NodeNextW2F2, Link, NodeNextW2F3>(sorted_next_next_w2_f2.c_str(),
  944. sorted_coins.c_str(),
  945. next_next_w2_f3.c_str(),
  946. [](const NodeNextW2F2& A, const Link& B) -> NodeNextW2F3 {
  947. // n(n(i)) i n(i) w(i) f(i) w(n(i)) f(n(i)) f(n(n(i)))
  948. return NodeNextW2F3(A.key, A.next, A.next_next, A.w1, A.f1, A.w2, A.f2, B.next);
  949. }
  950. );
  951.  
  952. remove(sorted_coins.c_str());
  953. remove(sorted_next_next_w2_f2.c_str());
  954.  
  955. Filename next_next_w3_f3 = get_filename(n, "next_w3_f3");
  956. join<NodeNextW2F3, NodeWeight, NodeNextW3F3>(next_next_w2_f3.c_str(),
  957. weights_now,
  958. next_next_w3_f3.c_str(),
  959. [](const NodeNextW2F3& A, const NodeWeight& B) -> NodeNextW3F3 {
  960. // i n(i) n(n(i)) w(i) f(i) w(n(i)) f(n(i)) f(n(n(i))) w(n(n(i)))
  961. return NodeNextW3F3(A.next, A.next_next, A.key, A.w1, A.f1, A.w2, A.f2, A.f3, B.weight);
  962. }
  963. );
  964.  
  965. remove(next_next_w2_f3.c_str());
  966.  
  967. tuple<Filename, Filename, Filename, int> new_graph_n = make_new_graph(next_next_w3_f3.c_str(), n);
  968.  
  969. remove(next_next_w3_f3.c_str());
  970.  
  971. Filename new_graph_filename = move(get<0>(new_graph_n));
  972. Filename removed_edges_filename = move(get<1>(new_graph_n));
  973. Filename new_weights_filename = move(get<2>(new_graph_n));
  974. unsigned int new_n = get<3>(new_graph_n);
  975.  
  976. unsigned int num_removed_edges = n - new_n;
  977.  
  978. Filename sorted_removed_edges = ext_sort<Link>(0, num_removed_edges - 1, 0, removed_edges_filename.c_str(), 0);
  979.  
  980. Filename rec_result = list_rank(new_graph_filename.c_str(), new_weights_filename.c_str(), new_n);
  981. Filename sorted_rec_result = ext_sort<NodeWeight>(0, new_n - 1, 0, rec_result.c_str(), 0);
  982.  
  983. Filename rankes_minus_new = get_filename(new_n, "rankes_minus_new");
  984. join<NodeWeight, NodeWeight, NodeWeight>(sorted_rec_result.c_str(),
  985. new_weights_filename.c_str(),
  986. rankes_minus_new.c_str(),
  987. [](const NodeWeight& A, const NodeWeight& B) -> NodeWeight {
  988. return NodeWeight(A.key, A.weight - B.weight);
  989. }
  990. );
  991.  
  992. Filename rankes_actual_new = get_filename(new_n, "rankes_act_n");
  993. join<NodeWeight, NodeWeight, NodeWeight>(rankes_minus_new.c_str(),
  994. weights_now,
  995. rankes_actual_new.c_str(),
  996. [](const NodeWeight& A, const NodeWeight& B) -> NodeWeight {
  997. return NodeWeight(A.key, A.weight + B.weight);
  998. }
  999. );
  1000.  
  1001. Filename rankes_removed_r = get_filename(new_n, "rankes_removed_r");
  1002.  
  1003. join<NodeWeight, Link, NodeWeight>(rankes_actual_new.c_str(),
  1004. sorted_removed_edges.c_str(),
  1005. rankes_removed_r.c_str(),
  1006. [](const NodeWeight& A, const Link& B) -> NodeWeight {
  1007. return NodeWeight(B.next, A.weight);
  1008. }
  1009. );
  1010.  
  1011. Filename sorted_rankes_removed_r = ext_sort<NodeWeight>(0, num_removed_edges - 1, 0, rankes_removed_r.c_str(), 0);
  1012.  
  1013. Filename removed_rankes = get_filename(new_n, "removed_rankes");
  1014. join<NodeWeight, NodeWeight, NodeWeight>(sorted_rankes_removed_r.c_str(),
  1015. weights_now,
  1016. removed_rankes.c_str(),
  1017. [](const NodeWeight& A, const NodeWeight& B) -> NodeWeight {
  1018. return NodeWeight(A.key, A.weight + B.weight);
  1019. }
  1020. );
  1021.  
  1022. concat_files(result.c_str(), rankes_actual_new.c_str(), removed_rankes.c_str());
  1023.  
  1024. return result;
  1025. }
  1026.  
  1027.  
  1028. void transform_to_outputbin(const char* input_name, const char* to, int n) {
  1029. FILE* input = fopen(input_name, "rb");
  1030.  
  1031. Filename rank_key_name = get_filename(n, "rank_key");
  1032. FILE* rank_key = fopen(rank_key_name.c_str(), "wb");
  1033.  
  1034. // Link contains key - rank
  1035. Link* sbuf = (Link*)gl_buf;
  1036. for (int i = 0; i < n; i += BLOCK) {
  1037. int sz = min(BLOCK, n - i);
  1038. fread(sbuf, sizeof(Link), sz, input);
  1039. for (int j = 0; j < sz; j++) {
  1040. swap(sbuf[j].key, sbuf[j].next);
  1041. }
  1042. fwrite(sbuf, sizeof(Link), sz, rank_key);
  1043. }
  1044. fclose(input);
  1045. fclose(rank_key);
  1046.  
  1047. Filename sorted_rank_key = ext_sort<Link>(0, n - 1, 0, rank_key_name.c_str(), 0);
  1048.  
  1049. rank_key = fopen(sorted_rank_key.c_str(), "rb");
  1050. FILE* output_bin = fopen(to, "wb");
  1051.  
  1052. unsigned int min_element = 1 << 30;
  1053. //unsigned int min_element = (((unsigned int)1 << 31) - 1) + ((unsigned int)1 << 31);
  1054. int min_element_pos;
  1055. for (int i = 0; i < n; i += BLOCK) {
  1056. int sz = min(BLOCK, n - i);
  1057. fread(sbuf, sizeof(Link), sz, rank_key);
  1058. for (int j = 0; j < sz; j++) {
  1059. if (sbuf[j].next < min_element) {
  1060. min_element = sbuf[j].next;
  1061. min_element_pos = i + j;
  1062. }
  1063. }
  1064. }
  1065.  
  1066. unsigned int* qbuf = (unsigned int*)gl_buf;
  1067.  
  1068. fseek(rank_key, min_element_pos * sizeof(Link), SEEK_SET);
  1069. for (int i = min_element_pos; i < n; i += BLOCK) {
  1070. int sz = min(BLOCK, n - i);
  1071. fread(sbuf, sizeof(Link), sz, rank_key);
  1072.  
  1073. for (int j = 0; j < sz; j++) {
  1074. qbuf[j] = sbuf[j].next;
  1075. }
  1076. fwrite(qbuf, 4, sz, output_bin);
  1077. }
  1078.  
  1079. fseek(rank_key, 0, SEEK_SET);
  1080. for (int i = 0; i < min_element_pos; i += BLOCK) {
  1081. int sz = min(BLOCK, min_element_pos - i);
  1082. fread(sbuf, sizeof(Link), sz, rank_key);
  1083.  
  1084. for (int j = 0; j < sz; j++) {
  1085. qbuf[j] = sbuf[j].next;
  1086. }
  1087. fwrite(qbuf, 4, sz, output_bin);
  1088. }
  1089.  
  1090. fclose(rank_key);
  1091. fclose(output_bin);
  1092. }
  1093.  
  1094. void transform_sort_to_irunner(const char* filename, unsigned long long n) {
  1095. FILE* input = fopen(filename, "rb");
  1096. FILE* output = fopen("output.bin", "wb");
  1097. fwrite(&n, 8, 1, output);
  1098. unsigned long long sbuf[BLOCK];
  1099. int sz = -1;
  1100. while (true) {
  1101. sz = fread(sbuf, 8, BLOCK, input);
  1102. if (sz <= 0) {
  1103. break;
  1104. }
  1105. fwrite(sbuf, 8, sz, output);
  1106. }
  1107. fclose(input);
  1108. fclose(output);
  1109. }
  1110.  
  1111. Filename do_list_ranking(const char* input, const char* to, unsigned int n) {
  1112. Filename init_weights_file = prepare_weights(input, n);
  1113. Filename solution = list_rank(input, init_weights_file.c_str(), n);
  1114.  
  1115. transform_to_outputbin(solution.c_str(), to, n);
  1116. }
  1117.  
  1118. struct EdgeType {
  1119. unsigned ;int from;
  1120. unsigned int to;
  1121. unsigned int type;
  1122. unsigned int id;
  1123. EdgeType() {};
  1124. EdgeType(unsigned int a, unsigned int b, unsigned int c, unsigned int d) {
  1125. from = a;
  1126. to = b;
  1127. type = c;
  1128. id = d;
  1129. }
  1130. bool operator < (const EdgeType &other) const {
  1131. if (from != other.from) {
  1132. return from < other.from;
  1133. }
  1134. if (type >= 2 && other.type >= 2) {
  1135. return to < other.to;
  1136. }
  1137. if (type != other.type) {
  1138. return type < other.type;
  1139. }
  1140. return to < other.to;
  1141. }
  1142. };
  1143.  
  1144. struct EdgeId {
  1145. unsigned int from;
  1146. unsigned int to;
  1147. unsigned int id;
  1148. EdgeType() {};
  1149. EdgeType(unsigned int a, unsigned int b, unsigned int c) {
  1150. from = a;
  1151. to = b;
  1152. id = c;
  1153. }
  1154.  
  1155. bool operator < (const EdgeType &other) const {
  1156. if (from != other.from) {
  1157. return from < other.from;
  1158. }
  1159. return to < other.to;
  1160. }
  1161. };
  1162.  
  1163. void prepare_type_edges(const char* filename, const char* to, unsigned int n) {
  1164. FILE* input = fopen(filename, "rb");
  1165. FILE* output = fopen(to, "wb");
  1166.  
  1167. Link sbuf[BLOCK];
  1168. int sz = -1;
  1169. unsigned int tot_edges = 0;
  1170. while (true) {
  1171. sz = fread(sbuf, sizeof(Link), BLOCK, input);
  1172. if (sz <= 0) {
  1173. break;
  1174. }
  1175. for (int i = 0; i < sz; i++) {
  1176. unsigned int from = sbuf[i].key;
  1177. unsigned int to = sbuf[i].to;
  1178.  
  1179. direct_edge_id = tot_edges++;
  1180. reverse_edge_id = tot_edges++;
  1181.  
  1182. EdgeType from_to(from, to, 100, direct_edge_id);
  1183. EdgeType to_from(to, from, 100, reverse_edge_id);
  1184.  
  1185. from_to.type = 2
  1186. fwrite(&from_to, sizeof(EdgeType), 1, output);
  1187.  
  1188.  
  1189. }
  1190.  
  1191. }
  1192. fclose(input);
  1193. fclose(output);
  1194. }
  1195.  
  1196.  
  1197. int main() {
  1198. //srand(2327);
  1199. //srand(2341);
  1200. //srand(time(NULL));
  1201. clock_t beg_prep = clock();
  1202. prepare_file();
  1203. //prepate_file_for_sort();
  1204.  
  1205.  
  1206. //prepare_file_without_memory();
  1207. clock_t end_prep = clock();
  1208.  
  1209. //cerr << "prepared in " << double(end_prep - beg_prep) / CLOCKS_PER_SEC << endl;
  1210.  
  1211. clock_t begin = clock();
  1212. const char* input_normal_name = "input_normal.bin";
  1213. unsigned int n;
  1214.  
  1215. FILE* input = fopen("input.bin", "rb");
  1216. fread(&n, 4, 1, input);
  1217. transform_file_from_irunner(input, input_normal_name, n - 1);
  1218. //check_file(input_normal_name, 2);
  1219. fclose(input);
  1220.  
  1221. prepare_type_edges(input_normal_name, "type_edges", n - 1);
  1222. //do_list_ranking(input_normal_name, "ranking.bin", n);
  1223.  
  1224.  
  1225. //check_file("output.bin");
  1226. //check_file("ranking.bin");
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232. clock_t end = clock();
  1233. double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
  1234. //cout << "total time: " << elapsed_secs << endl;
  1235. //cout << "total sort time: " << tot_sort_time << endl;
  1236. //cout << "total join time: " << tot_join_time << endl;
  1237. //cout << "total flip coins: " << tot_flip_coins << endl;
  1238.  
  1239. //check_output();
  1240. return 0;
  1241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement