Advertisement
shchuko

5b

Nov 16th, 2019
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. class StringPairList {
  7. private:
  8.     class Node {
  9.     public:
  10.         std::string key = "";
  11.         std::string value = "";
  12.         Node* p_prev = nullptr;
  13.         Node* p_next = nullptr;
  14.     };
  15.  
  16.     Node* p_begin = nullptr;
  17.     Node* p_end = nullptr;
  18.     long list_size = 0;
  19.  
  20.     Node* findKey(const std::string& _key);
  21.  
  22. public:
  23.     ~StringPairList();
  24.  
  25.     void addPair(const std::string& _key, const std::string& _value);
  26.  
  27.     void updatePair(const std::string& _key, const std::string& _value);
  28.  
  29.     bool isExist(const std::string& _key);
  30.  
  31.     void remove(const std::string& _key);
  32.  
  33.     bool getValue(const std::string& key, std::string& value);
  34.  
  35.     long size();
  36. };
  37.  
  38. class StringMap {
  39. private:
  40.     StringPairList* table = nullptr;
  41.     long table_size;
  42.     const double A = (sqrt(5) - 1) / 2;
  43.  
  44.     long hash(const std::string& _key);
  45. public:
  46.     explicit StringMap(long size);
  47.  
  48.     ~StringMap();
  49.  
  50.     void addPair(const std::string& key, const std::string& value);
  51.  
  52.     bool getValue(const std::string& key, std::string& value);
  53.  
  54.     bool isExist(const std::string& key);
  55.  
  56.     void remove(std::string& key);
  57. };
  58.  
  59. // StringPairList class methods
  60. StringPairList::~StringPairList() {
  61.     Node* p_look = p_begin;
  62.     while (p_look != nullptr) {
  63.         Node* temp = p_look;
  64.         p_look = p_look->p_next;
  65.         delete(temp);
  66.     }
  67.  
  68.     p_begin = nullptr;
  69.     p_end = nullptr;
  70. }
  71.  
  72. StringPairList::Node *StringPairList::findKey(const std::string &_key) {
  73.     if (!list_size)
  74.         return nullptr;
  75.  
  76.     Node *p_look = p_begin;
  77.     while (p_look != nullptr && p_look->key != _key)
  78.         p_look = p_look->p_next;
  79.     return p_look;
  80. }
  81.  
  82. void StringPairList::addPair(const std::string &_key, const std::string &_value) {
  83.     list_size++;
  84.     auto* temp = new Node;
  85.     temp->value = _value;
  86.     temp->key = _key;
  87.  
  88.     if (p_end == nullptr) {
  89.         p_begin = temp;
  90.         p_end = temp;
  91.         return;
  92.     }
  93.  
  94.     temp->p_prev = p_end;
  95.     p_end->p_next = temp;
  96.     p_end = temp;
  97.  
  98. }
  99.  
  100. void StringPairList::updatePair(const std::string &_key, const std::string &_value) {
  101.     Node* ptr_temp = findKey(_key);
  102.     if (ptr_temp == nullptr) {
  103.         addPair(_key, _value);
  104.         return;
  105.     }
  106.  
  107.     ptr_temp->value = _value;
  108. }
  109.  
  110. bool StringPairList::isExist(const std::string &_key) {
  111.     return findKey(_key) != nullptr;
  112. }
  113.  
  114. void StringPairList::remove(const std::string &_key) {
  115.     Node* ptr_temp = findKey(_key);
  116.     if (ptr_temp == nullptr)
  117.         return;
  118.  
  119.     if (ptr_temp == p_begin)
  120.         p_begin = ptr_temp->p_next;
  121.     if (ptr_temp == p_end)
  122.         p_end = ptr_temp->p_prev;
  123.  
  124.     if (ptr_temp->p_prev != nullptr)
  125.         ptr_temp->p_prev->p_next = ptr_temp->p_next;
  126.     if (ptr_temp->p_next != nullptr)
  127.         ptr_temp->p_next->p_prev = ptr_temp->p_prev;
  128.  
  129.     delete(ptr_temp);
  130.     --list_size;
  131. }
  132.  
  133. bool StringPairList::getValue(const std::string &key, std::string &value) {
  134.     Node* ptr_temp = findKey(key);
  135.     if (ptr_temp == nullptr)
  136.         return false;
  137.  
  138.     value = ptr_temp->value;
  139.     return true;
  140. }
  141.  
  142. long StringPairList::size() {
  143.     return list_size;
  144. }
  145.  
  146. // StringMap class methods
  147. StringMap::StringMap(long size) {
  148.     table = new StringPairList[size];
  149.     table_size = size;
  150. }
  151.  
  152. StringMap::~StringMap() {
  153.     delete[](table);
  154.     table = nullptr;
  155. }
  156.  
  157. long StringMap::hash(const std::string &_key) {
  158.     int keyVal = 0;
  159.     for (int i = 0; i < _key.length(); ++i) {
  160.         keyVal += _key[i] * (i + 1);
  161.     }
  162.     keyVal *= _key.length() + 1;
  163.  
  164.     double int_part;
  165.     return (long)(table_size * modf(keyVal * A, &int_part));
  166. }
  167.  
  168. void StringMap::addPair(const std::string &key, const std::string &value) {
  169.     table[hash(key)].updatePair(key, value);
  170. }
  171.  
  172. bool StringMap::getValue(const std::string &key, std::string &value) {
  173.     return table[hash(key)].getValue(key, value);
  174. }
  175.  
  176. bool StringMap::isExist(const std::string &key) {
  177.     return table[hash(key)].isExist(key);
  178. }
  179.  
  180. void StringMap::remove(std::string &key) {
  181.     table[hash(key)].remove(key);
  182. }
  183.  
  184. #define IN_FILE_NAME "map.in"
  185. #define OUT_FILE_NAME "map.out"
  186.  
  187. using std::ifstream;
  188. using std::ofstream;
  189. using std::string;
  190.  
  191. int main() {
  192.     ifstream fin(IN_FILE_NAME);
  193.     if (!fin.is_open())
  194.         return 2;
  195.  
  196.     ofstream fout(OUT_FILE_NAME);
  197.     if (!fout.is_open())
  198.         return 2;
  199.  
  200.     StringMap map(50);
  201.     while (true) {
  202.         string command;
  203.         fin >> command;
  204.  
  205.         if (command.empty())
  206.             break;
  207.  
  208.         string key;
  209.         fin >> key;
  210.         if (command == "put") {
  211.             string value;
  212.             fin >> value;
  213.             map.addPair(key, value);
  214.         }
  215.         else if (command == "get") {
  216.             string value;
  217.             if (map.getValue(key, value))
  218.                 fout << value << '\n';
  219.             else
  220.                 fout << "none\n";
  221.         }
  222.         else if (command == "delete") {
  223.             map.remove(key);
  224.         }
  225.     }
  226.     return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement