Advertisement
dykow

AISD_hashmap1

May 8th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <random>
  5. #include <algorithm>
  6.  
  7. int h(int x, int i, int T)
  8. {
  9.   if (i == 0)
  10.     return x % T;
  11.  
  12.   return (h(x, 0, T) + i) % T;
  13. }
  14.  
  15.  
  16. //~ int h1(int x, int T)
  17. //~ {
  18.   //~ return x % T;
  19. //~ }
  20.  
  21. //~ int h2(int x, int T)
  22. //~ {
  23.   //~ return (((x/T) % (T/2)) * 2) + 1;
  24. //~ }
  25.  
  26. //~ int h(int x, int i, int T)
  27. //~ {
  28.   //~ return (h1(x, T) + i*h2(x, T)) % T;
  29. //~ }
  30.  
  31. bool hash_al_wstaw(int* A, int x, int T, int &wywolanie)
  32. {
  33.   for (int i = 0; i < T; i++)
  34.   {
  35.     wywolanie++;
  36.     int k = h(x, i, T);
  37.     if (A[k] == -1)
  38.     {
  39.       A[k] = x;
  40.       return true;
  41.     }
  42.   }
  43.  
  44.   return false;
  45. }
  46.  
  47. bool hash_al_szukaj(int *A, int x, int T, int &wywolanie)
  48. {
  49.   for(int i = 0; i < T; i++)
  50.   {
  51.     wywolanie++;
  52.     int k = h(x, i, T);
  53.     if (A[k] == x)
  54.       return true;
  55.     else if (A[k] == -1)
  56.       return false;
  57.   }
  58.  
  59.   return false;
  60. }
  61.  
  62. int* hash_al_przep(int *A, int &T, int &wywolanie)
  63. {
  64.   T *= 2;
  65.  
  66.   int *B = new int[T];
  67.   for (int i = 0; i < T; i++)
  68.     B[i] = -1;
  69.    
  70.   for (int i = 0; i < T / 2; i++)
  71.     if (A[i] != -1)
  72.       hash_al_wstaw(B, A[i], T, wywolanie);
  73.  
  74.   delete[] A;
  75.  
  76.   return B;
  77. }
  78.  
  79. int main()
  80. {
  81.   const int T = 1048576;
  82.   int *tab_miesz = new int[T];
  83.  
  84.   for (int i = 0; i < T; i++)
  85.     tab_miesz[i] = -1;
  86.  
  87.   const int length = 12000000;
  88.   int *tab = new int [length];
  89.  
  90.   for (int i = 0; i < length; i++)
  91.     tab[i] = i;
  92.  
  93.   std::random_shuffle(tab, tab + length);
  94.  
  95.   int wywolanie = 0;
  96.   int procent10 = T / 10;
  97.   int procent = 0;
  98.   int zapelnienie = 0;
  99.   int suma_wywolan = 0;
  100.   double czas = 0;
  101.  
  102.   std::cerr << "\t \t ***Pomiar wypełniania tablicy*** \n \n";
  103.   for (int i = 0; i < T * 0.9; i++)
  104.   {
  105.     hash_al_wstaw(tab_miesz, tab[i], T, wywolanie);
  106.      
  107.     if (i == procent)
  108.     {
  109.       wywolanie = 0;
  110.       auto start = std::chrono::high_resolution_clock::now();
  111.        
  112.       for (;i < procent + 10000; i++)
  113.         hash_al_wstaw(tab_miesz, tab[i], T, wywolanie);
  114.          
  115.       auto end = std::chrono::high_resolution_clock::now();
  116.        
  117.       std::chrono::duration<double, std::micro> duration = end - start;
  118.        
  119.       std::cerr << zapelnienie << "% " << "Uplynelo: " << duration.count() << "us\n";
  120.       std::cerr << zapelnienie << "% " << "Wywolania: " << wywolanie << "\n" << "\n";
  121.      
  122.       procent += procent10;
  123.       zapelnienie += 10;
  124.     }
  125.   }
  126.  
  127.   wywolanie = 0;
  128.   procent10 = T / 10;
  129.   procent = T / 10;
  130.   zapelnienie = 10;
  131.    
  132.   for (int i = 0; i < T; i++)
  133.     tab_miesz[i] = -1;
  134.  
  135.   std::cerr << "\n \t \t ***Pomiar wyszukiwania w tablicy*** \n \n";
  136.   for (int i = 0; i < T * 0.9; i++)
  137.   {
  138.     hash_al_wstaw(tab_miesz, tab[i], T, wywolanie);
  139.    
  140.     if (i == procent)
  141.     {
  142.       wywolanie = 0;
  143.       auto start = std::chrono::high_resolution_clock::now();
  144.      
  145.       for (int j = 0 ; j < procent; j += procent / 10000)
  146.         hash_al_szukaj(tab_miesz, tab[j], T, wywolanie);
  147.        
  148.       auto end = std::chrono::high_resolution_clock::now();
  149.      
  150.       std::chrono::duration<double, std::micro> duration = end - start;
  151.  
  152.       std::cerr << zapelnienie << "% " << "Uplynelo: " << duration.count() << "us\n";
  153.       std::cerr << zapelnienie << "% " << "Wywolania: " << wywolanie << "\n" << "\n";
  154.    
  155.       czas += duration.count();
  156.       suma_wywolan += wywolanie;
  157.      
  158.       procent += procent10;
  159.       zapelnienie += 10;
  160.     }
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement