loulett

HW4_hash_table_open_adressing

Mar 16th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. struct HashTableCell
  6. {
  7.     std::string str;
  8.     int state = 0;
  9. };
  10.  
  11. class HashTable
  12. {
  13. public:
  14.     HashTable();
  15.     HashTable(size_t base, size_t startCapacity);
  16.     bool Add(const std::string &value);
  17.     bool Find(const std::string &value);
  18.     bool Delete(const std::string &value);
  19. private:
  20.     std::vector <HashTableCell> hash_table;
  21.     size_t hash_base;
  22.     size_t size;
  23.     bool isNull(size_t index) const;
  24.     bool isDeleted(size_t index) const;
  25.     size_t hash(const std::string &value, int hash_base);
  26.     size_t linear_step(const std::string &value, size_t iter) { return (hash(value, hash_base) + iter + 1) % hash_table.size(); }
  27.     size_t double_step(const std::string &value, size_t iter) { return (hash(value, hash_base) + iter * hash(value, 5)) % hash_table.size(); }
  28.     size_t quadratic_step(size_t key, size_t iter);
  29.     void rehash();
  30. };
  31.  
  32. HashTable::HashTable(size_t base, size_t startCapacity) : hash_table(startCapacity),
  33. hash_base(base), size(0)
  34. {}
  35.  
  36. void HashTable::rehash()
  37. {
  38.     auto old_data(std::move(hash_table));
  39.     size_t old_size = size;
  40.     hash_table.clear();
  41.     hash_table.resize(old_data.size() * 2);
  42.     size = 0;
  43.     for (auto& i : old_data)
  44.         if (1 == i.state)
  45.             Add(i.str);
  46. }
  47.  
  48. size_t HashTable::quadratic_step(size_t key, size_t iter)
  49. {
  50.     return (key + iter + 1) % hash_table.size();
  51. }
  52.  
  53. size_t HashTable::hash(const std::string& value, int hash_base)
  54. {
  55.     size_t ans = 1;
  56.     for (size_t i = 0; i < value.size(); i++)
  57.         ans = (static_cast<size_t>(value[i]) + ans) * hash_base;
  58.     return ans;
  59. }
  60.  
  61. bool HashTable::isNull(size_t index) const
  62. {
  63.     return hash_table[index].state == 0;
  64. }
  65.  
  66. bool HashTable::isDeleted(size_t index) const
  67. {
  68.     return hash_table[index].state == -1;
  69. }
  70.  
  71. bool HashTable::Add(const std::string& value)
  72. {
  73.     if (Find(value))
  74.         return false;
  75.     size_t key = hash(value, hash_base) % hash_table.size();
  76.     for (size_t i = 0; i < hash_table.size(); i++)
  77.     {
  78.         if (isNull(key) || isDeleted(key))
  79.         {
  80.             hash_table[key].str = value;
  81.             hash_table[key].state = 1;
  82.             size++;
  83.             if (2 * size >= hash_table.size())
  84.                 rehash();
  85.             return true;
  86.         }
  87.         key = quadratic_step(key, i);
  88.     }
  89.     return false;
  90. }
  91.  
  92. bool HashTable::Find(const std::string& value)
  93. {
  94.     size_t key = hash(value, hash_base) % hash_table.size();
  95.     for (size_t i = 0; i < hash_table.size(); i++)
  96.     {
  97.         if (isNull(key))
  98.             return false;
  99.         if (hash_table[key].str == value)
  100.             return true;
  101.         key = quadratic_step(key, i);
  102.     }
  103.     return false;
  104. }
  105.  
  106. bool HashTable::Delete(const std::string& value)
  107. {
  108.     if (!Find(value))
  109.         return false;
  110.     size_t key = hash(value, hash_base) % hash_table.size();
  111.     for (size_t i = 0; i < hash_table.size(); i++)
  112.     {
  113.         if (isNull(key))
  114.             return false;
  115.         if (hash_table[key].str == value)
  116.         {
  117.             hash_table[key].state = -1;
  118.             hash_table[key].str = "";
  119.             size--;
  120.             return true;
  121.         }
  122.         key = quadratic_step(key, i);
  123.     }
  124.     return false;
  125. }
  126.  
  127. void result(bool res)
  128. {
  129.     if (res)
  130.         std::cout << "OK\n";
  131.     else
  132.         std::cout << "FAIL\n";
  133. }
  134.  
  135. int main()
  136. {
  137.     size_t N = 8;
  138.     HashTable h(3, N);
  139.     char operation;
  140.     std::string str;
  141.     while (std::cin)
  142.     {
  143.         std::cin >> operation >> str;
  144.         if (operation == '+')
  145.             result(h.Add(str));
  146.         else if (operation == '-')
  147.             result(h.Delete(str));
  148.         else
  149.             result(h.Find(str));
  150.     }
  151.     return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment