Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // database header-file
- // Created by stas on 5/5/19.
- //
- #pragma once
- #include "date.h"
- #include "condition_parser.h"
- #include <sstream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <stack>
- #include <utility>
- #include <set>
- using namespace std;
- ostream& operator<< (ostream& os, const pair<Date, string>&);
- class Database {
- public:
- void Add(const Date& date, const string& event);
- void Print(ostream& os) const;
- template <typename Pr>
- int RemoveIf(Pr predicate);
- template <typename Pr>
- vector<pair<Date, string>> FindIf(Pr predicate) const;
- pair<Date, string> Last(const Date& date) const;
- private:
- map<Date, pair<vector<string>, set<string>>> database_;
- };
- template <typename Pr>
- int Database::RemoveIf(Pr predicate) {
- int count = 0;
- stack<Date> empty_dates;
- for (auto& c : database_) {
- const Date& date = c.first;
- auto& events = c.second.first;
- auto it = remove_if(events.begin(), events.end(),
- [date, predicate] (const string& s) {
- return predicate(date, s);
- });
- count += distance(it, events.end());
- if (it == events.begin())
- empty_dates.push(date);
- else
- events.erase(it, events.end());
- }
- while(!empty_dates.empty()) {
- database_.erase(empty_dates.top());
- empty_dates.pop();
- }
- return count;
- }
- template <typename Pr>
- vector<pair<Date, string>> Database::FindIf(Pr predicate) const {
- vector<pair<Date, string>> res;
- for (const auto& c : database_) {
- const Date& date = c.first;
- const vector<string>& events = c.second.first;
- for (const auto& event : events) {
- if (predicate(date, event))
- res.push_back(pair{date, event});
- }
- }
- return res;
- }
- //////////////////////////////////////////////////////////////////////////
- // database cpp-file
- // Created by stas on 5/5/19.
- //
- #include "database.h"
- using namespace std;
- ostream& operator<< (ostream& os, const pair<Date, string>& unit) {
- return os << unit.first << " " << unit.second;
- }
- void Database::Add(const Date& date, const string& event) {
- if(database_.count(date) > 0) {
- const set<string>& events = database_[date].second;
- if (events.count(event) != 0)
- return;
- }
- database_[date].first.push_back(event);
- database_[date].second.insert(event);
- }
- void Database::Print(ostream& os) const {
- for (const auto& c : database_) {
- const Date& date = c.first;
- const vector<string>& events = c.second.first;
- for (const string &event : events)
- os << date << " " << event << endl;
- }
- }
- pair<Date, string> Database::Last(const Date& date) const {
- auto p = database_.upper_bound(date);
- if (p == database_.begin())
- throw invalid_argument("RAWR");
- else
- return pair{prev(p)->first, *(prev(p)->second.first.rbegin())};
- }
- ///////////////////////////////////////////////////////////////////////
- // date header-file
- // Created by stas on 5/5/19.
- //
- #pragma once
- #include <iostream>
- using namespace std;
- struct Date {
- int year;
- int month;
- int day;
- };
- istream& operator >> (istream& is, Date& date);
- ostream& operator << (ostream& os, const Date& date);
- bool operator < (const Date& lhs, const Date& rhs);
- bool operator <= (const Date& lhs, const Date& rhs);
- bool operator > (const Date& lhs, const Date& rhs);
- bool operator >= (const Date& lhs, const Date& rhs);
- bool operator == (const Date& lhs, const Date& rhs);
- bool operator != (const Date& lhs, const Date& rhs);
- Date ParseDate(istream& is);
- /////////////////////////////////////////////////////////////
- // date cpp-file
- // Created by stas on 5/5/19.
- //
- #include "date.h"
- #include <iomanip>
- #include <tuple>
- using namespace std;
- istream& operator >> (istream& is, Date& date) {
- is >> date.year;
- is.ignore(1);
- is >> date.month;
- is.ignore(1);
- is >> date.day;
- return is;
- }
- ostream& operator << (ostream& os, const Date& date) {
- return os << setfill('0') << setw(4) << date.year
- << '-' << setw(2) << date.month
- << '-' << setw(2) << date.day;
- }
- bool operator == (const Date& lhs, const Date& rhs) {
- return tuple{lhs.year, lhs.month, lhs.day} == tuple{rhs.year, rhs.month, rhs.day};
- }
- bool operator != (const Date& lhs, const Date& rhs) {
- return !(lhs == rhs);
- }
- bool operator < (const Date& lhs, const Date& rhs) {
- return tuple{lhs.year, lhs.month, lhs.day} < tuple{rhs.year, rhs.month, rhs.day};
- }
- bool operator <= (const Date& lhs, const Date& rhs) {
- return lhs < rhs || lhs == rhs;
- }
- bool operator > (const Date& lhs, const Date& rhs) {
- return !(lhs <= rhs);
- }
- bool operator >= (const Date& lhs, const Date& rhs) {
- return !(lhs < rhs);
- }
- Date ParseDate(istream& is) {
- Date date;
- is >> date;
- return date;
- }
Advertisement
Add Comment
Please, Sign In to add comment