Advertisement
Guest User

program

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <chrono>
  2. #include <thread>
  3. #include <random>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. double wywolan = 0;  //wstawianie
  9. double wywolan1 = 0; //wyszukaj
  10. double wywolanres = 0; //restrukuryzacja
  11. int n = 10000;
  12.  
  13. int losowa_liczba(int min = 0, int max = std::numeric_limits<int>::max())
  14. {
  15.     static std::default_random_engine gen(std::random_device{}());
  16.     static std::uniform_int_distribution<int> dist;
  17.     return dist(gen, std::uniform_int_distribution<int>::param_type{ min, max });
  18. }
  19. ///////////////////////////////////////////////////////////////
  20.  
  21.  
  22. struct tabhaszujaca {
  23.     int* tab;
  24.     int size;
  25.     int writein; //wpisane
  26.     int sizeall; //rozmiar całej tablicy
  27. };
  28.  
  29.  
  30.  
  31.  
  32. bool hash_al_wstaw(int x, tabhaszujaca& tablica)
  33. {
  34.     if ((double)tablica.writein / tablica.size > 0.6)         //   <0.6 ; 0,7 ; 0;8 ; 0.9>
  35.     {
  36.         wywolanres++; // ilośc wywołań restrukturyzacji
  37.         tabhaszujaca nowa;
  38.         nowa.size = tablica.size * 2;
  39.         nowa.tab = new int[nowa.size];
  40.         nowa.writein = 0; //alokacja nowych
  41.  
  42.  
  43.  
  44.         for (int i = 0; i < nowa.size; i++)
  45.             nowa.tab[i] = -1;
  46.         for (int j = 0; j < tablica.size; j++)
  47.         {
  48.             if (tablica.tab[j] != -1)
  49.                 hash_al_wstaw(tablica.tab[j], nowa);
  50.         }
  51.  
  52.  
  53.  
  54.         delete[] tablica.tab; //zwolnienie pam
  55.         tablica.size = nowa.size; //all
  56.         tablica.tab = nowa.tab;
  57.         tablica.writein = nowa.writein;
  58.     }
  59.  
  60.  
  61.  
  62.  
  63.     int k = 0;
  64.     for (int i = 0; i<tablica.size; i++)
  65.     {
  66.         wywolan++;
  67.         int N = tablica.size;
  68.        
  69.        
  70.         int k = ((x%N)+i) % N;                           //Liniowe
  71.  
  72.  
  73.        
  74.         /*int h1 = x%N;                                            
  75.         int h2 = (((x / N) % (N / 2)) * 2) + 1;         //Podwójne
  76.         k = (h1 + i*h2) % N;*/
  77.        
  78.  
  79.  
  80.         if (tablica.tab[k] == -1)
  81.         {
  82.             tablica.tab[k] = x;
  83.             tablica.writein++;
  84.             return true;
  85.         }
  86.     }
  87.     return false;
  88. }
  89.  
  90. ///////////////////////////////////////////////
  91.  
  92.  
  93.  
  94. bool hash_al_szukaj(int x, tabhaszujaca& tablica)
  95. {
  96.     int k = 0;
  97.     int N = tablica.size;
  98.  
  99.  
  100.     for (int i = 0; i <N; i++)
  101.     {
  102.         wywolan1++;
  103.  
  104.  
  105.         int k = ((x%N)+i) % N;                           //Liniowe
  106.  
  107.  
  108.  
  109.         /*int h1 = x%N;
  110.         int h2 = (((x / N) % (N / 2)) * 2) + 1;         //Podwójne
  111.         k = (h1 + i*h2) % N;*/
  112.  
  113.  
  114.  
  115.         if (tablica.tab[k] == x)
  116.             return true;
  117.         if (tablica.tab[k] == -1)
  118.             return false;
  119.     }
  120.     return false;
  121. }
  122.  
  123.  
  124.  
  125. ///////////////////-main-///////////////////////////////
  126.  
  127.  
  128.  
  129. int main()
  130. {
  131.     std::default_random_engine gen(std::random_device{}());
  132.     tabhaszujaca tab_miesz;
  133.  
  134.  
  135.     tab_miesz.size = 10000;
  136.     tab_miesz.tab = new int[tab_miesz.size];
  137.     tab_miesz.writein = 0;
  138.     tab_miesz.sizeall = 10000;
  139.     int *tab_random = new int[tab_miesz.size * 100];
  140.  
  141.  
  142.  
  143.  
  144.     for (int i = 0; i < tab_miesz.size; i++)
  145.     {
  146.         tab_miesz.tab[i] = -1;
  147.     }
  148.     for (int j = 0; j < (tab_miesz.size * 100); j++)
  149.     {
  150.         tab_random[j] = losowa_liczba();
  151.     }
  152.  
  153.    
  154.  
  155.  
  156.     for (int k = 0; k < 100; k++)
  157.     {
  158.         wywolan1 = wywolan = 0;
  159.         auto start = std::chrono::high_resolution_clock::now();
  160.  
  161.  
  162.         for (int i = 0; i <tab_miesz.sizeall; i++)
  163.         {
  164.             hash_al_wstaw(tab_random[i + k*tab_miesz.sizeall], tab_miesz);
  165.         }
  166.  
  167.         auto end = std::chrono::high_resolution_clock::now();
  168.         std::chrono::duration<double, std::micro> duration = end - start;
  169.  
  170.  
  171.  
  172.        
  173.  
  174.  
  175.  
  176.  
  177.         /*cerr << endl << " Dla " << k << "%" << "             Wstawianie " << (double)duration.count() / tab_miesz.sizeall << "us"
  178.             << "       Srednia liczba wywolan: " << wywolan / tab_miesz.sizeall << endl;;*/
  179.  
  180.  
  181.  
  182.         ////////////////////
  183.  
  184.         if (k > 1)
  185.         {
  186.             shuffle(tab_random, tab_random + (1 + k)*tab_miesz.sizeall, gen);
  187.         }
  188.  
  189.         start = std::chrono::high_resolution_clock::now();
  190.  
  191.         for (int i = 0; i < tab_miesz.sizeall; i++)
  192.         {
  193.             hash_al_szukaj(tab_random[i], tab_miesz);
  194.         }
  195.  
  196.  
  197.         end = std::chrono::high_resolution_clock::now();
  198.         duration = end - start;
  199.  
  200.  
  201.  
  202.         cerr << endl << " Dla      " << k << "     %" << "     Wstawianie              " << (double)duration.count() / tab_miesz.sizeall << "              us"
  203.             << "       Srednia liczba wywolan:               " << wywolan / tab_miesz.sizeall << "                      Szukanie                            " << (double)duration.count() / tab_miesz.sizeall << "               us"
  204.             << "       Srednia liczba wywolan:               " << wywolan1 / tab_miesz.sizeall
  205.             << "                                 Restrukturyzacje:             " << wywolanres;
  206.  
  207.  
  208.         /*cerr << " Dla " << k << "%" << "             Szukanie " << (double)duration.count() / tab_miesz.sizeall << "us"
  209.             << "       Srednia liczba wywolan: " << wywolan1 / tab_miesz.sizeall << endl << endl << endl
  210.             << "Restrukturyzacje: " << wywolanres << endl << "----------------------------------------------------------------" << endl << endl << endl;
  211.             */
  212.  
  213.     }
  214.  
  215.  
  216.     ////////////////////////////////////////
  217.  
  218.     system("pause");
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement