Advertisement
KrimsN

Untitled

Feb 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #ifndef HASH_LAB_5
  2. #define HASH_LAB_5
  3.  
  4. #include <iomanip>
  5.  
  6.  
  7. class Hash_table;
  8. class Element_of_the_hash_table;
  9. class hash_struct;
  10.  
  11.  
  12. class hash_struct
  13. {
  14. public:
  15.     int value;
  16.     unsigned int hash;
  17.     friend std::ostream& operator<< (std::ostream &out, const hash_struct &elem);  
  18. };
  19.  
  20. class Element_of_the_hash_table
  21. {
  22. private:
  23.     bool Filled;
  24.     hash_struct _object;
  25. public:
  26.     Element_of_the_hash_table()
  27.     {
  28.         Filled = 0;
  29.     }
  30.     void fill(int value, unsigned int hash)
  31.     {
  32.         this->_object.value = value;
  33.         this->_object.hash = hash;
  34.         this->Filled = 1;
  35.     }
  36.     operator bool() { return Filled; }
  37.  
  38.     friend Hash_table;
  39. };
  40.  
  41.  
  42.  
  43. class Hash_table
  44. {
  45. private:
  46.     Element_of_the_hash_table * ptr;
  47.     unsigned int size;
  48.     unsigned int elems;
  49.     int first_hash(int value)
  50.     {
  51.         return value % this->size;
  52.     }
  53.     int second_hash(int value)
  54.     {
  55.         return value % 17;
  56.     }
  57.  
  58. public:
  59.  
  60.     Hash_table(unsigned int size = 2341)
  61.     {
  62.         this->ptr = new Element_of_the_hash_table[size];
  63.         this->size = size;
  64.         this->elems = 0;
  65.     }
  66.    
  67.     unsigned int add(int value)
  68.     {
  69.         unsigned int try_count = 1;
  70.         Element_of_the_hash_table * arr = this->ptr;
  71.         unsigned int hash = first_hash(value);
  72.         while(arr[hash])
  73.         {
  74.             hash += second_hash(value);
  75.             ++try_count;
  76.             if (hash >= this->size)
  77.                 hash %= this->size;        
  78.         }
  79.         arr[hash].fill(value, hash) ;
  80.         ++this->elems;
  81.         return try_count;
  82.     }
  83.     bool search(int value)
  84.     {
  85.         unsigned int try_count = 1;
  86.         Element_of_the_hash_table * arr = this->ptr;
  87.         unsigned int hash = first_hash(value);
  88.         while(arr[hash] && arr[hash]._object.value != value)
  89.         {
  90.             hash += second_hash(value);
  91.             ++try_count;
  92.             if (hash >= this->size)
  93.                 hash %= this->size;
  94.         }
  95.         if (arr[hash])
  96.             return true;
  97.         return false;
  98.     }
  99.     unsigned int search(int value, hash_struct &out_struct)
  100.     {
  101.         unsigned int try_count = 1;
  102.         Element_of_the_hash_table * arr = this->ptr;
  103.         unsigned int hash = first_hash(value);
  104.         while(arr[hash] && arr[hash]._object.value != value)
  105.         {
  106.             hash += second_hash(value);
  107.             ++try_count;
  108.             if (hash >= this->size)
  109.                 hash %= this->size;
  110.         }
  111.         if (arr[hash])
  112.             out_struct = arr[hash]._object;
  113.         else  
  114.         {
  115.             try_count = 0;
  116.             //std::cout << "elem with value " << value << " not found" << std::endl;
  117.         }
  118.         return try_count;
  119.     }
  120.     void print()
  121.     {
  122.         for(int i = 0; i < this->size; ++i)
  123.             if(this->ptr[i])
  124.                 std::cout << this->ptr[i]._object << std::endl;
  125.     }
  126.     ~Hash_table()
  127.     {
  128.         delete [](this->ptr);
  129.     }
  130.  
  131. };
  132.  
  133. std::ostream& operator<< (std::ostream &out, const hash_struct &elem)
  134. {
  135.     // поскольку operator<< является другом класса Point, то мы имеем прямой доступ к членам Point
  136.     out << "value - " << std::setw(15) << std::left << elem.value << "; hash - " << elem.hash;
  137.  
  138.     return out;
  139. }
  140.  
  141. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement