Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "map"
- #include "vector"
- #include "string"
- #include "date.h"
- #include "list"
- #include "functional"
- #include "algorithm"
- #include "deque"
- #include "node.h"
- #include "queue"
- #include "set"
- using namespace std;
- class Database{
- public:
- Database(){}
- void Add(const Date &dt,const string &ev);
- void Print(ostream &outs) const;
- map <Date,vector<string>> data;
- //typedef bool (*func)(/*const Date&, const std::string&*/);
- // using func = bool (*)(const Date&, const std::string&);
- template<typename T>
- vector<pair<const Date*,const string*>> FindIf(T func) const{
- vector<pair<const Date*,const string*>> res;
- for(std::map<Date,vector<string,allocator<string>>,less<Date>,allocator<pair<const Date,vector<string,allocator<string>>>>>::const_iterator i = data.begin();i!=data.end();i++){
- const Date &space=i->first;
- find_if(i->second.begin(),i->second.end(),[&space,&res,func](const string &str){
- if(func(space,str)){
- res.push_back(make_pair(&space,&str));
- }
- return false;
- });
- }
- return res;
- }
- // template<typename T1,typename T2>
- // int do_stuff(T1 &i,T2 &Func, bool fe){
- // int removes=0;
- // Date space = i->first;
- // auto partition = stable_partition(i->second.begin(),i->second.end(),[Func,space](string str){
- // return !Func(space,str);
- // });
- // if(partition == i->second.begin()){
- // removes += count_if(partition,i->second.end(),[](string &str){return true;});
- // if(!fe)
- // data.erase(i--);
- // else
- // data.erase(i);
- // }else{
- // auto polzun = i->second.end();
- // while(polzun != partition){
- // removes++;
- // i->second.erase(polzun--);
- // }
- // if(!fe)
- // i--;
- // }
- // return removes;
- // }
- template<typename T2>
- int RemoveIf(T2 Func){
- int removes=0;
- auto it = data.begin();
- while(it!=data.end()){
- removes+=it->second.size();
- const Date &space= it->first;
- auto partition = stable_partition(it->second.begin(),it->second.end(),[Func,space](string str){
- return !Func(space,str);
- });
- it->second.erase(partition,it->second.end());
- removes-= it->second.size();
- if(it->second.empty()){
- data.erase(it++);
- }else{
- ++it;
- }
- }
- return removes;
- }
- // template<typename T2>
- // int RemoveIf(T2 Func){
- // queue<Date> erdates;
- // int removes=0;
- // for(auto &i:data){
- // removes+=i.second.size();
- // const Date &space= i.first;
- // i.second.remove_if([space,Func](string &str){
- // return Func(space,str);
- // });
- // removes-= i.second.size();
- // if(i.second.empty()){
- // erdates.push(space);
- // }
- // }
- // while(!erdates.empty()){
- // data.erase(erdates.front());
- // erdates.pop();
- // }
- //
- // return removes;
- // }
- // template<typename T2>
- // int RemoveIf(T2 Func){
- // if(data.empty()){
- // return 0;
- // }
- // int removes = 0;
- // auto i = --data.end();
- // while(i!=data.begin()){
- // removes += do_stuff(i,Func,false);
- // }
- // removes += do_stuff(i,Func,true);
- // return removes;
- // }
- // for(auto &i:data){
- // Date space = i.first;
- // auto partition = stable_partition(i.second.begin(),i.second.end(),[Func,space](string str){
- // return !Func(space,str);
- // });
- // if(partition == i.second.begin()){
- // removes += count_if(partition,i.second.end(),[](string &str){return true;});
- //
- // erdates.push_back(space);
- // continue;
- // }
- // auto polzun = i.second.end()--;
- // while(polzun != partition){
- // removes++;
- // i.second.erase(polzun--);
- // }
- //// removes += count_if(partition,i.second.end(),[](string &str){return true;});
- ////
- //// i.second.erase(partition,i.second.end());
- // }
- // while(!erdates.empty()){
- // data.erase(erdates.front());
- // erdates.pop_front();
- // }
- // return removes;
- //
- // }
- // removes += count_if(partition,i.second.end(),[](string &str){return true;});
- // if(partition == i.second.begin()){
- // erdates.push_back(i.first);
- // }
- // i.second.erase(partition,i.second.end());
- // while(!erdates.empty()){
- // data.erase(erdates.front());
- // erdates.pop_front();
- // }
- // int count=0;
- //
- // map <Date,std::vector<std::string>> new_data;
- // if(FindIf(Func).empty()){
- // return 0;
- // }
- // for(auto i:data){
- // for(auto j: i.second){
- // if(!Func(i.first,j)){
- // new_data[i.first].push_back(j);
- // }
- // else{
- // count++;
- // }
- // }
- // }
- // data = new_data;
- // vector<string> wanted_list = FindIf(Func);
- // vector<pair<Date, string>> wanted_pair;
- // if(wanted_list.empty()){
- // //data.clear();
- // return 0;
- // }
- // for (const auto& pair : wanted_list) {
- // istringstream stream(pair);
- // auto d = ParseDate(stream);
- // stream.ignore(1);
- // string event;
- // getline(stream, event);
- // wanted_pair.push_back(make_pair(d, event));
- // }
- //
- // count = wanted_pair.size();
- //
- // for (const auto& pair : wanted_pair) {
- // Date space = pair.first;
- // string event = pair.second;
- //
- // if (data[pair.first].size() == 1) {
- // data.erase(space);
- // //stable_partition(data[space].begin(),data[space].end(),[](string str){return true;});
- // }
- // else {
- // auto it = find(data[space].begin(), data[space].end(), event);
- // //data[space].erase(it);
- // stable_partition(data[space].begin(),data[space].end(),[&it](string &str){return str == *it;});
- // }
- // }
- std::string Last(const Date &dt) const;
- };
- //Date ParseDate(istream &sstr);
- ostream &operator <<(ostream &ostr,const pair<const Date*,const string*> &pr);
Add Comment
Please, Sign In to add comment