Advertisement
smatskevich

HashTable

Dec 12th, 2022
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4. #include <vector>
  5.  
  6. int Hash(const std::string& key) {
  7.   if (key.empty()) return 0;
  8.   return key.front();
  9. }
  10.  
  11. struct HTNode {
  12.   std::string Key;
  13.   HTNode* Next = nullptr;
  14.  
  15.   HTNode(std::string key) : Key(std::move(key)) {}
  16. };
  17.  
  18. class HashTable {
  19.  public:
  20.   explicit HashTable(int initial_size = 80) : table(initial_size) {}
  21.   ~HashTable();
  22.   bool Has(const std::string& key) const;
  23.   bool Add(const std::string& key);
  24.   bool Remove(const std::string& key);
  25.  
  26.  private:
  27.   std::vector<HTNode*> table;
  28. };
  29.  
  30. HashTable::~HashTable() {
  31. }
  32.  
  33. bool HashTable::Has(const std::string& key) const {
  34.   int hash = Hash(key) % static_cast<int>(table.size());
  35.   for (HTNode* node = table[hash]; node; node = node->Next)
  36.     if (node->Key == key) return true;
  37.   return false;
  38. }
  39.  
  40. bool HashTable::Add(const std::string& key) {
  41.   int hash = Hash(key) % static_cast<int>(table.size());
  42.   if (!table[hash]) {
  43.     table[hash] = new HTNode(key);
  44.     return true;
  45.   }
  46.   for (HTNode* node = table[hash]; true; node = node->Next) {
  47.     if (node->Key == key) return false;
  48.     if (!node->Next) {
  49.       node->Next = new HTNode(key);
  50.       return true;
  51.     }
  52.   }
  53. }
  54.  
  55. bool HashTable::Remove(const std::string& key) {
  56.   int hash = Hash(key) % static_cast<int>(table.size());
  57.   if (!table[hash]) return false;
  58.   if (table[hash]->Key == key) {
  59.     HTNode* to_delete = table[hash];
  60.     table[hash] = to_delete->Next;
  61.     delete to_delete;
  62.     return true;
  63.   }
  64.   for (HTNode* node = table[hash]; node->Next; node = node->Next) {
  65.     if (node->Next->Key == key) {
  66.       HTNode* to_delete = node->Next;
  67.       node->Next = to_delete->Next;
  68.       delete to_delete;
  69.       return true;
  70.     }
  71.   }
  72.   return false;
  73. }
  74.  
  75. int main1() {
  76.   HashTable my_table;
  77.   char command = 0;
  78.   std::string key;
  79.   while (std::cin >> command >> key) {
  80.     switch (command) {
  81.       case '?':
  82.         std::cout << (my_table.Has(key) ? "OK" : "FAIL") << std::endl;
  83.         break;
  84.       case '+':
  85.         std::cout << (my_table.Add(key) ? "OK" : "FAIL") << std::endl;
  86.         break;
  87.       case '-':
  88.         std::cout << (my_table.Remove(key) ? "OK" : "FAIL") << std::endl;
  89.         break;
  90.     }
  91.   }
  92.   return 0;
  93. }
  94.  
  95. class my_hash {
  96.  public:
  97.   size_t operator()(const std::string& str) const { return str.empty() ? 0 : str.front(); }
  98. };
  99.  
  100. int main() {
  101.   std::unordered_set<std::string, my_hash> table;
  102.   char command = 0;
  103.   std::string key;
  104.   while (std::cin >> command >> key) {
  105.     switch (command) {
  106.       case '?':
  107.         std::cout << (table.count(key) != 0 ? "OK" : "FAIL") << std::endl;
  108.         break;
  109.       case '+':
  110.         std::cout << (table.insert(key).second ? "OK" : "FAIL") << std::endl;
  111.         break;
  112.       case '-':
  113.         std::cout << (table.erase(key) ? "OK" : "FAIL") << std::endl;
  114.         break;
  115.     }
  116.   }
  117.   return 0;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement