Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "date.h"
- #include <map>
- #include <set>
- #include <vector>
- #include <memory>
- #include <algorithm>
- using namespace std;
- class Database
- {
- public:
- void Add(const Date& _Date, const string& _Event);
- void Print(ostream& os) const;
- string Last(const Date& _Date) const;
- template <typename FuncName>
- int RemoveIf(const FuncName& _Func);
- template <typename FuncName>
- vector <pair <Date, string>> FindIf(const FuncName& _Func);
- private:
- map <Date, pair <set <string>, vector <set <string>::iterator>>> Records;
- };
- template <typename FuncName>
- int Database::RemoveIf(const FuncName& _Func)
- {
- int counter = 0;
- for (auto& [key, value] : Records)
- {
- auto it = stable_partition(value.second.begin(), value.second.end(), [&_Func, &key, &value, &counter](const auto& _Event) -> bool
- {
- if (_Func(key, *_Event))
- {
- ++counter;
- value.first.erase(*_Event);
- return false;
- }
- return true;
- });
- value.second.erase(it, value.second.end());
- }
- for (auto eraseIt = Records.cbegin(), nextIt = eraseIt; eraseIt != Records.end(); eraseIt = nextIt)
- {
- ++nextIt;
- if (eraseIt->second.first.empty())
- {
- Records.erase(eraseIt);
- }
- }
- return counter;
- }
- template <typename FuncName>
- vector <pair <Date, string>> Database::FindIf(const FuncName& _Func)
- {
- vector <pair <Date, string>> ans;
- for (const auto& [_Key, _Value] : Records)
- {
- auto it = find_if(_Value.second.begin(), _Value.second.end(), [&_Key, &ans, &_Func](const auto& _Event)
- {
- if (_Func(_Key, *_Event))
- {
- ans.push_back({ _Key, *_Event });
- }
- return false;
- });
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment