Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5.  
  6.  
  7.  
  8.  
  9. struct cell {
  10. cell(const std::string &_key) : key(_key), Del(false) {}
  11. std::string key;
  12. bool Del;
  13. };
  14.  
  15.  
  16. class CHashTable {
  17. public:
  18. CHashTable() : table(8, NULL), _size(0) {}
  19.  
  20. bool insert(const std::string &key);
  21. bool remove(const std::string &key);
  22. bool found(const std::string &key) const;
  23. void ReHash();
  24.  
  25. private:
  26. std::vector<cell*> table;
  27. int _size;
  28. };
  29.  
  30.  
  31. int Hash1(const std::string& key, int m) {
  32. int hash = 0;
  33. int a = 11;
  34. for (int i = 0; i < static_cast<int>(key.size()); i++) {
  35. hash = (hash * a + key[i]) % m;
  36. }
  37. return hash;
  38. }
  39.  
  40.  
  41.  
  42. int HashFunction(int h1, int i, int m) {
  43. if (m != 0) {
  44. return(h1 + i) % m;
  45. }
  46. else {
  47. return 0;
  48. }
  49. }
  50.  
  51.  
  52. bool CHashTable::insert(const std::string &key) {
  53.  
  54. if (static_cast<double>(_size) / static_cast<double>(static_cast<int>(table.size())) >= 0.75) {
  55. ReHash();
  56. }
  57.  
  58. int h1 = Hash1(key, static_cast<int>(table.size()));
  59.  
  60. int h = HashFunction(h1, 0, static_cast<int>(table.size()));
  61.  
  62. for (int i = 0; i < static_cast<int>(table.size()) && table[h] != NULL; i++) {
  63.  
  64. if (table[h]->key == key && table[h]->Del == false) {
  65.  
  66. return false;
  67. }
  68. /*
  69. if (table[h]->Del == true) {
  70. table[h]->key = key;
  71. table[h]->Del = false;
  72. _size++;
  73. return true;
  74.  
  75. }
  76. */
  77. h = HashFunction(h, i + 1, static_cast<int>(table.size()));
  78. }
  79.  
  80. table[h] = new cell(key);
  81. _size++;
  82.  
  83. return true;
  84. }
  85.  
  86.  
  87. bool CHashTable::remove(const std::string &key) {
  88. int h1 = Hash1(key, static_cast<int>(table.size()));
  89.  
  90. int j = HashFunction(h1, 0, static_cast<int>(table.size()));
  91.  
  92. for (int i = 0; i < static_cast<int>(table.size()); i++) {
  93.  
  94. if (table[j] != NULL) {
  95.  
  96. if (table[j]->key == key && table[j]->Del == false) {
  97.  
  98. table[j]->Del = true;
  99. return true;
  100. }
  101. }
  102.  
  103. else {
  104.  
  105. return false;
  106. }
  107.  
  108. j = HashFunction(j, i + 1, static_cast<int>(table.size()));
  109. }
  110. }
  111.  
  112. bool CHashTable::found(const std::string &key) const {
  113.  
  114. int i = 0;
  115. int h1 = Hash1(key, static_cast<int>(table.size()));
  116.  
  117. int h = h1;
  118. while (table[h] != NULL && i < static_cast<int>(table.size())) {
  119.  
  120. if (table[h]->key == key && table[h]->Del == false) {
  121. return true;
  122. }
  123.  
  124. h = HashFunction(h, i + 1, static_cast<int>(table.size()));
  125. i++;
  126. }
  127.  
  128. return false;
  129. }
  130.  
  131. void CHashTable::ReHash() {
  132.  
  133. int newBufferSize = static_cast<int>(table.size()) * 2;
  134. std::vector<cell*> newBuffer(newBufferSize, NULL);
  135. int new_size = 0;
  136. for (int i = 0; i < static_cast<int>(table.size()); i++) {
  137.  
  138. if (table[i] != NULL && table[i]->Del == false) {
  139. new_size++;
  140. int j = 0;
  141. int h1 = Hash1(table[i]->key, newBufferSize);
  142. int h = HashFunction(h1, j, newBufferSize);
  143.  
  144. while (j < newBufferSize) {
  145.  
  146. if (newBuffer[h] == NULL) {
  147. break;
  148. }
  149.  
  150. j++;
  151. h = HashFunction(h, j, newBufferSize);
  152. }
  153. newBuffer[h] = table[i];
  154. }
  155. }
  156. this->_size = new_size;
  157. table = newBuffer;
  158. }
  159.  
  160. int main()
  161. {
  162. CHashTable table;
  163.  
  164.  
  165. while (std::cin) {
  166.  
  167. char a = 0;
  168. std::string b;
  169.  
  170. std::cin >> a >> b;
  171.  
  172. if (std::cin.fail())
  173. {
  174. break;
  175. }
  176.  
  177. if (a == '+') {
  178. std::cout << (table.insert(b) ? "OK" : "FAIL") << std::endl;
  179. } else if (a=='-'){
  180. std::cout << (table.remove(b) ? "OK" : "FAIL") << std::endl;
  181. }
  182. else {
  183. std::cout << (table.found(b) ? "OK" : "FAIL") << std::endl;
  184. }
  185. }
  186.  
  187.  
  188. system("pause");
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement