Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- class StringPairList {
- private:
- class Node {
- public:
- std::string key = "";
- std::string value = "";
- Node* p_prev = nullptr;
- Node* p_next = nullptr;
- };
- Node* p_begin = nullptr;
- Node* p_end = nullptr;
- long list_size = 0;
- Node* findKey(const std::string& _key);
- public:
- ~StringPairList();
- void addPair(const std::string& _key, const std::string& _value);
- void updatePair(const std::string& _key, const std::string& _value);
- bool isExist(const std::string& _key);
- void remove(const std::string& _key);
- bool getValue(const std::string& key, std::string& value);
- long size();
- };
- class StringMap {
- private:
- StringPairList* table = nullptr;
- long table_size;
- const double A = (sqrt(5) - 1) / 2;
- long hash(const std::string& _key);
- public:
- explicit StringMap(long size);
- ~StringMap();
- void addPair(const std::string& key, const std::string& value);
- bool getValue(const std::string& key, std::string& value);
- bool isExist(const std::string& key);
- void remove(std::string& key);
- };
- // StringPairList class methods
- StringPairList::~StringPairList() {
- Node* p_look = p_begin;
- while (p_look != nullptr) {
- Node* temp = p_look;
- p_look = p_look->p_next;
- delete(temp);
- }
- p_begin = nullptr;
- p_end = nullptr;
- }
- StringPairList::Node *StringPairList::findKey(const std::string &_key) {
- if (!list_size)
- return nullptr;
- Node *p_look = p_begin;
- while (p_look != nullptr && p_look->key != _key)
- p_look = p_look->p_next;
- return p_look;
- }
- void StringPairList::addPair(const std::string &_key, const std::string &_value) {
- list_size++;
- auto* temp = new Node;
- temp->value = _value;
- temp->key = _key;
- if (p_end == nullptr) {
- p_begin = temp;
- p_end = temp;
- return;
- }
- temp->p_prev = p_end;
- p_end->p_next = temp;
- p_end = temp;
- }
- void StringPairList::updatePair(const std::string &_key, const std::string &_value) {
- Node* ptr_temp = findKey(_key);
- if (ptr_temp == nullptr) {
- addPair(_key, _value);
- return;
- }
- ptr_temp->value = _value;
- }
- bool StringPairList::isExist(const std::string &_key) {
- return findKey(_key) != nullptr;
- }
- void StringPairList::remove(const std::string &_key) {
- Node* ptr_temp = findKey(_key);
- if (ptr_temp == nullptr)
- return;
- if (ptr_temp == p_begin)
- p_begin = ptr_temp->p_next;
- if (ptr_temp == p_end)
- p_end = ptr_temp->p_prev;
- if (ptr_temp->p_prev != nullptr)
- ptr_temp->p_prev->p_next = ptr_temp->p_next;
- if (ptr_temp->p_next != nullptr)
- ptr_temp->p_next->p_prev = ptr_temp->p_prev;
- delete(ptr_temp);
- --list_size;
- }
- bool StringPairList::getValue(const std::string &key, std::string &value) {
- Node* ptr_temp = findKey(key);
- if (ptr_temp == nullptr)
- return false;
- value = ptr_temp->value;
- return true;
- }
- long StringPairList::size() {
- return list_size;
- }
- // StringMap class methods
- StringMap::StringMap(long size) {
- table = new StringPairList[size];
- table_size = size;
- }
- StringMap::~StringMap() {
- delete[](table);
- table = nullptr;
- }
- long StringMap::hash(const std::string &_key) {
- int keyVal = 0;
- for (int i = 0; i < _key.length(); ++i) {
- keyVal += _key[i] * (i + 1);
- }
- keyVal *= _key.length() + 1;
- double int_part;
- return (long)(table_size * modf(keyVal * A, &int_part));
- }
- void StringMap::addPair(const std::string &key, const std::string &value) {
- table[hash(key)].updatePair(key, value);
- }
- bool StringMap::getValue(const std::string &key, std::string &value) {
- return table[hash(key)].getValue(key, value);
- }
- bool StringMap::isExist(const std::string &key) {
- return table[hash(key)].isExist(key);
- }
- void StringMap::remove(std::string &key) {
- table[hash(key)].remove(key);
- }
- #define IN_FILE_NAME "map.in"
- #define OUT_FILE_NAME "map.out"
- using std::ifstream;
- using std::ofstream;
- using std::string;
- int main() {
- ifstream fin(IN_FILE_NAME);
- if (!fin.is_open())
- return 2;
- ofstream fout(OUT_FILE_NAME);
- if (!fout.is_open())
- return 2;
- StringMap map(50);
- while (true) {
- string command;
- fin >> command;
- if (command.empty())
- break;
- string key;
- fin >> key;
- if (command == "put") {
- string value;
- fin >> value;
- map.addPair(key, value);
- }
- else if (command == "get") {
- string value;
- if (map.getValue(key, value))
- fout << value << '\n';
- else
- fout << "none\n";
- }
- else if (command == "delete") {
- map.remove(key);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement