Advertisement
Guest User

hash2

a guest
May 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #include<iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <random>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int losowa_liczba(int min = 0, int max = std::numeric_limits<int>::max())
  9. {
  10. static std::default_random_engine gen(std::random_device{}());
  11. static std::uniform_int_distribution<int> dist;
  12. return dist(gen, std::uniform_int_distribution<int>::param_type{ min, max });
  13. }
  14. int re = 0;
  15. int rozmiar = 10000;
  16. const int iloscLosowych = 1000000;
  17.  
  18. ///*
  19. int h(int x, int i, int T)
  20. {
  21.  
  22. int a;
  23. a = (x % T + i) % T;
  24. return a;
  25.  
  26. }
  27. //*/
  28. /*
  29. int h(int x, int i, int T)
  30. {
  31.  
  32. int a;
  33. a = (((x%T) + i * ((((x / T) % (T / 2)) * 2) + 1))) % T;
  34. return a;
  35. }
  36. */
  37. double wywF = 0;
  38. bool hash_al_wstaw(int tab[], int x, int m)
  39. {
  40.  
  41. int k;
  42. for (int i = 0; i < m; i++)
  43. {
  44. k = h(x, i, m);
  45.  
  46. wywF++;
  47.  
  48. if (tab[k] == -1)
  49. {
  50. tab[k] = x;
  51. return true;
  52. }
  53. }
  54. return false;
  55.  
  56. }
  57. double wywFsz = 0;
  58. bool hash_al_szukaj(int tab[], int x, int m)
  59. {
  60. int k;
  61. for (int i = 0; i < m; i++)
  62. {
  63. k = h(x, i, m);
  64. wywFsz++;
  65.  
  66. if (tab[k] == x)
  67. return true;
  68.  
  69. if (tab[k] == -1)
  70. return false;
  71. }
  72. return false;
  73. }
  74.  
  75. void rest(int* &tab)
  76. {
  77. int *tab3 = new int[2 * rozmiar];
  78. for (int i = 0;i < 2 * rozmiar;i++)
  79. tab3[i] = -1;
  80. for (int j = 0;j < rozmiar;j++)
  81. {
  82. if (tab[j] != -1)
  83. hash_al_wstaw(tab3, tab[j], 2 * rozmiar);
  84. }
  85. delete[] tab;
  86. tab = tab3;
  87. rozmiar=2*rozmiar;
  88. re++;
  89. }
  90.  
  91.  
  92. int main()
  93. {
  94. std::default_random_engine gen(std::random_device{}());
  95.  
  96.  
  97. const int probka = 10000;
  98. int dopelnij = 90000;
  99. double czasWstaw = 0;
  100. double czasWyszuk = 0;
  101.  
  102.  
  103.  
  104.  
  105. int *tab = new int[rozmiar];
  106. for (int i = 0; i < rozmiar; i++)
  107. tab[i] = -1;
  108.  
  109.  
  110. int *tab2 = new int[iloscLosowych];
  111. for (int i = 0; i < iloscLosowych; i++)
  112. tab2[i] = losowa_liczba();
  113.  
  114.  
  115.  
  116. for (int j = 0; j <100; j++)
  117. {
  118.  
  119.  
  120. /*
  121. for (int i = 0; i < dopelnij; i++)
  122. {
  123. if (j == 0)
  124. break;
  125. hash_al_wstaw(tab, losowa_liczba(), rozmiar);
  126. }
  127. */
  128.  
  129. wywF = 0;
  130. wywFsz = 0;
  131. czasWstaw = 0;
  132. czasWyszuk = 0;
  133.  
  134. auto start = std::chrono::high_resolution_clock::now();
  135.  
  136. for (int i = 0; i < probka; i++)
  137. {
  138. hash_al_wstaw(tab, tab2[i + j * probka], rozmiar);
  139. }
  140.  
  141. auto end = std::chrono::high_resolution_clock::now();
  142. std::chrono::duration<double, std::micro> duration = end - start;
  143. czasWstaw = duration.count();
  144.  
  145.  
  146. shuffle(tab2, tab2 + (j + 1)*probka, gen);
  147.  
  148. start = std::chrono::high_resolution_clock::now();
  149.  
  150. for (int i = 0; i < probka; i++)
  151. {
  152. if (!hash_al_szukaj(tab, tab2[i], rozmiar))
  153. cout << "brak elementu";
  154. }
  155.  
  156. end = std::chrono::high_resolution_clock::now();
  157. duration = end - start;
  158. czasWyszuk = duration.count();
  159.  
  160.  
  161.  
  162. cout << "Dla " << j*(rozmiar / iloscLosowych) << "%" << endl;
  163. cerr << "Srednio uplynelo: " << czasWstaw / probka << "s\n";
  164. cerr << "Srednia ilosc wywolan: " << wywF / probka << "\n";
  165. cerr << "Sredni czas wyszukiwania: " << czasWyszuk / probka << "s\n";
  166. cerr << "Srednia ilosc wywolan funkcji szukajacej: " << wywFsz / probka << "\n\n";
  167. cout<<"Liczba restrukturyzacji: "<< re << "\n\n";
  168. }
  169.  
  170. delete[] tab2;
  171. delete[] tab;
  172.  
  173. system("pause");
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement