--sas

Untitled

May 12th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. // database header-file
  2. // Created by stas on 5/5/19.
  3. //
  4.  
  5. #pragma once
  6.  
  7. #include "date.h"
  8. #include "condition_parser.h"
  9.  
  10. #include <sstream>
  11. #include <map>
  12. #include <vector>
  13. #include <algorithm>
  14. #include <stack>
  15. #include <utility>
  16. #include <set>
  17.  
  18. using namespace std;
  19.  
  20. ostream& operator<< (ostream& os, const pair<Date, string>&);
  21.  
  22. class Database {
  23. public:
  24.     void Add(const Date& date, const string& event);
  25.  
  26.     void Print(ostream& os) const;
  27.  
  28.     template <typename Pr>
  29.     int RemoveIf(Pr predicate);
  30.  
  31.     template <typename Pr>
  32.     vector<pair<Date, string>> FindIf(Pr predicate) const;
  33.  
  34.     pair<Date, string> Last(const Date& date) const;
  35.  
  36. private:
  37.     map<Date, pair<vector<string>, set<string>>> database_;
  38. };
  39.  
  40. template <typename Pr>
  41. int Database::RemoveIf(Pr predicate) {
  42.     int count = 0;
  43.     stack<Date> empty_dates;
  44.     for (auto& c : database_) {
  45.         const Date& date = c.first;
  46.         auto& events = c.second.first;
  47.  
  48.         auto it = remove_if(events.begin(), events.end(),
  49.                 [date, predicate] (const string& s) {
  50.                     return predicate(date, s);
  51.         });
  52.  
  53.         count += distance(it, events.end());
  54.  
  55.         if (it == events.begin())
  56.             empty_dates.push(date);
  57.         else
  58.             events.erase(it, events.end());
  59.     }
  60.     while(!empty_dates.empty()) {
  61.         database_.erase(empty_dates.top());
  62.         empty_dates.pop();
  63.     }
  64.     return count;
  65. }
  66.  
  67. template <typename Pr>
  68. vector<pair<Date, string>> Database::FindIf(Pr predicate) const {
  69.     vector<pair<Date, string>> res;
  70.     for (const auto& c : database_) {
  71.         const Date& date = c.first;
  72.         const vector<string>& events = c.second.first;
  73.         for (const auto& event : events) {
  74.             if (predicate(date, event))
  75.                 res.push_back(pair{date, event});
  76.         }
  77.     }
  78.     return res;
  79. }
  80.  
  81. //////////////////////////////////////////////////////////////////////////
  82. // database cpp-file
  83. // Created by stas on 5/5/19.
  84. //
  85.  
  86. #include "database.h"
  87.  
  88. using namespace std;
  89.  
  90. ostream& operator<< (ostream& os, const pair<Date, string>& unit) {
  91.     return os << unit.first << " " << unit.second;
  92. }
  93.  
  94. void Database::Add(const Date& date, const string& event) {
  95.     if(database_.count(date) > 0) {
  96.         const set<string>& events = database_[date].second;
  97.         if (events.count(event) != 0)
  98.             return;
  99.     }
  100.     database_[date].first.push_back(event);
  101.     database_[date].second.insert(event);
  102. }
  103.  
  104. void Database::Print(ostream& os) const {
  105.     for (const auto& c : database_) {
  106.         const Date& date = c.first;
  107.         const vector<string>& events = c.second.first;
  108.         for (const string &event : events)
  109.             os << date << " " << event << endl;
  110.     }
  111. }
  112.  
  113. pair<Date, string> Database::Last(const Date& date) const {
  114.     auto p = database_.upper_bound(date);
  115.     if (p == database_.begin())
  116.         throw invalid_argument("RAWR");
  117.     else
  118.         return pair{prev(p)->first, *(prev(p)->second.first.rbegin())};
  119. }
  120.  
  121. ///////////////////////////////////////////////////////////////////////
  122. // date header-file
  123. // Created by stas on 5/5/19.
  124. //
  125. #pragma once
  126.  
  127. #include <iostream>
  128.  
  129. using namespace std;
  130.  
  131. struct Date {
  132.     int year;
  133.     int month;
  134.     int day;
  135. };
  136.  
  137. istream& operator >> (istream& is, Date& date);
  138.  
  139. ostream& operator << (ostream& os, const Date& date);
  140.  
  141. bool operator < (const Date& lhs, const Date& rhs);
  142. bool operator <= (const Date& lhs, const Date& rhs);
  143. bool operator > (const Date& lhs, const Date& rhs);
  144. bool operator >= (const Date& lhs, const Date& rhs);
  145. bool operator == (const Date& lhs, const Date& rhs);
  146. bool operator != (const Date& lhs, const Date& rhs);
  147.  
  148. Date ParseDate(istream& is);
  149.  
  150. /////////////////////////////////////////////////////////////
  151. // date cpp-file
  152. // Created by stas on 5/5/19.
  153. //
  154.  
  155. #include "date.h"
  156.  
  157. #include <iomanip>
  158. #include <tuple>
  159.  
  160. using namespace std;
  161.  
  162. istream& operator >> (istream& is, Date& date) {
  163.     is >> date.year;
  164.     is.ignore(1);
  165.     is >> date.month;
  166.     is.ignore(1);
  167.     is >> date.day;
  168.     return is;
  169. }
  170.  
  171. ostream& operator << (ostream& os, const Date& date) {
  172.     return os << setfill('0') << setw(4) << date.year
  173.     << '-' << setw(2) << date.month
  174.     << '-' << setw(2) << date.day;
  175. }
  176.  
  177. bool operator == (const Date& lhs, const Date& rhs) {
  178.     return tuple{lhs.year, lhs.month, lhs.day} == tuple{rhs.year, rhs.month, rhs.day};
  179. }
  180. bool operator != (const Date& lhs, const Date& rhs) {
  181.     return !(lhs == rhs);
  182. }
  183. bool operator < (const Date& lhs, const Date& rhs) {
  184.     return tuple{lhs.year, lhs.month, lhs.day} < tuple{rhs.year, rhs.month, rhs.day};
  185. }
  186. bool operator <= (const Date& lhs, const Date& rhs) {
  187.     return lhs < rhs || lhs == rhs;
  188. }
  189. bool operator > (const Date& lhs, const Date& rhs) {
  190.     return !(lhs <= rhs);
  191. }
  192. bool operator >= (const Date& lhs, const Date& rhs) {
  193.     return !(lhs < rhs);
  194. }
  195.  
  196. Date ParseDate(istream& is) {
  197.     Date date;
  198.     is >> date;
  199.     return date;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment