Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6. enum CollisionHashFunction{
  7.     linear,
  8.     classic
  9. };
  10.  
  11. class HashTable{
  12.  
  13.     public:
  14.         CollisionHashFunction mode;
  15.         int array[997];
  16.         bool insert(int);
  17.         int hashFunction(int);
  18.         int hashFunctionOnCollision(int);
  19.         bool remove(int);
  20.         HashTable();
  21.         void display(int,int);
  22.         bool insertBulk(int);
  23.         int find(int);
  24.  
  25. };
  26.  
  27. int HashTable::find(int key){
  28.     int value = hashFunction(key);
  29.  
  30.     while(array[value]!=0 && array[value]!=key){
  31.         if(mode==linear){
  32.             value++;
  33.         }
  34.         if(mode==classic){
  35.             value+=hashFunctionOnCollision(key);
  36.         }
  37.     }
  38.     return value;
  39. }
  40.  
  41. //int HashTable::find(int key, int incr){
  42. //    int value = hashFunction(key);
  43. //
  44. //    while(array[value]!=0 && array[value]!=key){
  45. //            value+=incr;
  46. //    }
  47. //    return value;
  48. //}
  49.  
  50. bool HashTable::insertBulk(int n){
  51.     int counter = 1;
  52.     while(counter<=n){
  53.         if(insert((rand()%20000)+20001)){
  54.             counter++;
  55.         }
  56.  
  57.     }
  58. }
  59.  
  60. void HashTable::display(int start, int stop){
  61.     for(int i=start;i<stop+1;i++){
  62.         cout << array[i] << " ";
  63.     }
  64. }
  65.  
  66. bool HashTable::insert(int key){
  67.     int value = hashFunction(key);
  68.     while(array[value]!=0){
  69.         if(mode==linear){
  70.             value++;
  71.         }
  72.         if(mode==classic){
  73.             value+=hashFunctionOnCollision(key);
  74.         }
  75.     }
  76.     array[value]=key;
  77.     return true;
  78. }
  79.  
  80. bool HashTable::remove(int key) {
  81.     int index = find(key);
  82.     if(array[index]==key){
  83.             array[index]=-1;
  84.         return true;
  85.     }
  86.     return false;
  87. }
  88.  
  89. int HashTable::hashFunctionOnCollision(int key){
  90.     return ((3*key) %19)+1;
  91. }
  92.  
  93. int HashTable::hashFunction(int key) {
  94.     return ((key %1000)+2^(key%10)+1)%997;
  95. }
  96.  
  97. HashTable::HashTable(){
  98.     for(int i=0;i<997;i++){
  99.         array[i]=0;
  100.     }
  101. }
  102.  
  103.  
  104. int main() {
  105.     srand(time(NULL));
  106.     clock_t begin, end;
  107.     double time_spent;
  108.     FILE* fp = fopen("inlab05.txt", "r");
  109.     int X;
  110.     int k1, k2, k3, k4;
  111.     if (fp == NULL)
  112.         return -1;
  113.     fscanf (fp, "%d %d %d %d %d", &X ,&k1, &k2, &k3, &k4);
  114.     fclose(fp);
  115.     begin = clock();
  116.  
  117.  
  118.     HashTable* ht = new HashTable();
  119.     ht->mode = classic;
  120.     ht->remove(k1);
  121.     ht->insert(k1);
  122.     ht->display(0,100);
  123.     ht->insertBulk(X);
  124.     ht->display(0,100);
  125.     ht->insert(k2);
  126.     ht->insert(k3);
  127.     ht->insert(k4);
  128.     ht->display(0,100);
  129.     ht->display(500,600);
  130.  
  131.     end = clock();
  132.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  133.  
  134.     cout << endl << to_string(time_spent);
  135.  
  136.     begin = clock();
  137.     ht = new HashTable();
  138.     ht->mode = linear;
  139.     ht->remove(k1);
  140.     ht->insert(k1);
  141.     ht->display(0,100);
  142.     ht->insertBulk(X);
  143.     ht->display(0,100);
  144.     ht->insert(k2);
  145.     ht->insert(k3);
  146.     ht->insert(k4);
  147.     ht->display(0,100);
  148.     ht->display(500,600);
  149.  
  150.     end = clock();
  151.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  152.  
  153.     cout << endl << to_string(time_spent);
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement