VEndymionV

HashTable

Jan 9th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. // 39241.sdizo.lab06.main.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "cstdlib"
  7. #include "time.h"
  8. #include "fstream"
  9. #include "HashTable.h"
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     srand(time(NULL));
  16.  
  17.     clock_t start, stop;
  18.     double time_spent;
  19.  
  20.     fstream file;
  21.     file.open("inlab06.txt", ios::in);
  22.     if (!file.good()) return -1;
  23.  
  24.     int X;
  25.     int k1, k2, k3, k4;
  26.  
  27.     file >> X;
  28.     file >> k1;
  29.     file >> k2;
  30.     file >> k3;
  31.     file >> k4;
  32.  
  33.     start = clock();
  34.     HashTable lP(false);
  35.     lP.deleteKey(k1);
  36.     lP.add(k1);
  37.     lP.display(0, 100);
  38.     lP.addFew(X);
  39.     lP.display(0, 100);
  40.     lP.add(k2);
  41.     lP.add(k3);
  42.     lP.add(k4);
  43.     lP.display(0, 100);
  44.     lP.display(500, 600);
  45.     lP.deleteKey(k3);
  46.     lP.deleteKey(k4);
  47.     lP.display(0, 100);
  48.     lP.display(500, 600);
  49.     stop = clock();
  50.     time_spent = (double)(stop - start) / CLOCKS_PER_SEC;
  51.     cout << "Czas wykonania dla metody adresowania liniowego: " << time_spent << "s.\n";
  52.  
  53.     start = clock();
  54.     HashTable dH(true);
  55.     dH.deleteKey(k1);
  56.     dH.add(k1);
  57.     dH.display(0, 100);
  58.     dH.addFew(X);
  59.     dH.display(0, 100);
  60.     dH.add(k2);
  61.     dH.add(k3);
  62.     dH.add(k4);
  63.     dH.display(0, 100);
  64.     dH.display(500, 600);
  65.     dH.deleteKey(k3);
  66.     dH.deleteKey(k4);
  67.     dH.display(0, 100);
  68.     dH.display(500, 600);
  69.     stop = clock();
  70.     time_spent = (double)(stop - start) / CLOCKS_PER_SEC;
  71.     cout << "Czas wykonania dla metody mieszania podwojnego: " << time_spent << "s.\n";
  72.  
  73.     system("pause");
  74.     return 0;
  75. }
  76.  
  77. #pragma once
  78. class HashTable
  79. {
  80. private:
  81.     int hashArray[997] = { 0 }; // Tablica mieszająca
  82.     int keysArray[20001] = { 0 }; // Tablica powtórzeń - indeksowanie: key - 20000
  83.     bool dH; // Mieszanie podwójne
  84.     int arraySize; // Rozmiar tablicy - tutaj na sztywno 997
  85.     int hash1(int key); // Główna funkcja mieszająca.
  86.     int hash2(int key); // Dodatkowa funkcja mieszająca.
  87.     bool linearProbingAdd(int key, int index);
  88.     bool doubleHashingAdd(int key, int index);
  89.     int linearProbingSearch(int key, int index); // Zwraca indeks jeżeli znalazł, jeżeli nie znalazł to -1;
  90.     int doubleHashingSearch(int key, int index); // Zwraca indeks jeżeli znalazł, jeżeli nie znalazł to -1;
  91. public:
  92.     HashTable(bool dH, int arraySize = 997); // Konstruktor - inicjacja tablicy.
  93.     bool add(int key); // Dodaj jeden element.
  94.     void addFew(int amount); // Dodaj kilka elementów.
  95.     int search(int key); // Zwraca indeks jeżeli znalazł, jeżeli nie znalazł to -1;
  96.     bool deleteKey(int key);
  97.     void display(int start, int end);
  98.     ~HashTable();
  99. };
  100.  
  101. #include "stdafx.h"
  102. #include "iostream"
  103. #include "math.h"
  104. #include "cstdlib"
  105. #include "HashTable.h"
  106.  
  107. using namespace std;
  108.  
  109.  
  110. HashTable::HashTable(bool dH, int arraySize)
  111.     : dH(dH), arraySize(arraySize)
  112. {
  113.     cout << "Inicjacja tablicy mieszajacej.\n";
  114. }
  115.  
  116. int HashTable::hash1(int key)
  117. {
  118.     int hash = ((key % 1000) + pow(2, (key % 10)) + 1);
  119.     return (hash % 997);
  120. }
  121.  
  122. int HashTable::hash2(int key)
  123. {
  124.     int hash = ((3 * key) % 19 + 1);
  125.     return hash;
  126. }
  127.  
  128. bool HashTable::linearProbingAdd(int key, int index)
  129. {
  130.     bool pass = false;
  131.  
  132.     do
  133.     {
  134.         index++;
  135.         if (index >= arraySize)
  136.         {
  137.             cout << arraySize << endl;
  138.             if (pass) return false;
  139.             pass = true;
  140.             index = 0;
  141.         }
  142.     } while (hashArray[index] != 0 && hashArray[index] != -1);
  143.  
  144.     hashArray[index] = key;
  145.     return true;
  146. }
  147.  
  148. bool HashTable::doubleHashingAdd(int key, int index)
  149. {
  150.     bool pass = false;
  151.  
  152.     do
  153.     {
  154.         index += hash2(key);
  155.         if (index >= arraySize)
  156.         {
  157.             if (pass) return false;
  158.             pass = true;
  159.             index -= arraySize;
  160.         }
  161.     } while (hashArray[index] != 0 && hashArray[index] != -1);
  162.  
  163.     hashArray[index] = key;
  164.     return true;
  165. }
  166.  
  167. bool HashTable::add(int key)
  168. {
  169.     bool ok;
  170.     int index = hash1(key);
  171.    
  172.     if (hashArray[index] != 0 && hashArray[index] != -1)
  173.     {
  174.         if (dH) ok = doubleHashingAdd(key, index);
  175.         else ok = linearProbingAdd(key, index);
  176.     }
  177.     else
  178.     {
  179.         hashArray[index] = key;
  180.         ok = true;
  181.     }
  182.  
  183.     if (ok) keysArray[key - 20000] = 1;
  184.     else cout << "Nie mozna dodac elementu!\n";
  185.  
  186.     return ok;
  187. }
  188.  
  189. void HashTable::addFew(int amount)
  190. {
  191.     for (int i = 0; i < amount; i++)
  192.     {
  193.         int key;
  194.  
  195.         do
  196.         {
  197.             key = (((2 * rand()) % 20001) + 20000);
  198.         } while (keysArray[key - 20000]);
  199.  
  200.         add(key);
  201.     }
  202. }
  203.  
  204. int HashTable::linearProbingSearch(int key, int index)
  205. {
  206.     bool pass = false;
  207.  
  208.     do
  209.     {
  210.         index++;
  211.         if (index >= arraySize)
  212.         {
  213.             if (pass) return -1;
  214.             pass = true;
  215.             index = 0;
  216.         }
  217.     } while (hashArray[index] != key);
  218.  
  219.     return index;
  220. }
  221.  
  222. int HashTable::doubleHashingSearch(int key, int index)
  223. {
  224.     bool pass = false;
  225.  
  226.     do
  227.     {
  228.         index += hash2(key);
  229.         if (index >= arraySize)
  230.         {
  231.             if (pass) return -1;
  232.             pass = true;
  233.             index -= arraySize;
  234.         }
  235.     } while (hashArray[index] != 0);
  236.  
  237.     return index;
  238. }
  239.  
  240. int HashTable::search(int key)
  241. {
  242.     int index = hash1(key);
  243.  
  244.     if (hashArray[index] != key)
  245.     {
  246.         if (dH) index = doubleHashingSearch(key, index);
  247.         else index = linearProbingSearch(key, index);
  248.     }
  249.    
  250.     if (index != -1) cout << "Element zostal odnaleziony.\n";
  251.     else cout << "Element nie zostal odnaleziony.\n";
  252.  
  253.     return index;
  254. }
  255.  
  256. bool HashTable::deleteKey(int key)
  257. {
  258.     int index = search(key);
  259.     if (index != -1)
  260.     {
  261.         hashArray[index] = -1;
  262.         keysArray[key - 20000] = 0;
  263.         return true;
  264.     }
  265.     else return false;
  266. }
  267.  
  268. void HashTable::display(int start, int end)
  269. {
  270.     if (start < 0 || end > arraySize) cout << "Zle indeksy!\n";
  271.     else
  272.     {
  273.         cout << "Wyswietlanie od indeksu " << start << " do indeksu " << end << ":\n";
  274.         for (int i = start; i < end; i++)
  275.         {
  276.             cout << hashArray[i] << endl;
  277.         }
  278.         cout << "Koniec wyswietlania.\n";
  279.     }
  280. }
  281.  
  282. HashTable::~HashTable()
  283. {
  284.     cout << "Cala tablica zostala usunieta." << endl;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment