Advertisement
shchuko

5c

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