Argentum_02

database.h

Apr 9th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "map"
  4. #include "vector"
  5. #include "string"
  6. #include "date.h"
  7. #include "list"
  8. #include "functional"
  9. #include "algorithm"
  10. #include "deque"
  11. #include "node.h"
  12. #include "queue"
  13. #include "set"
  14.  
  15. using namespace std;
  16.  
  17. class Database{
  18. public:
  19.     Database(){}
  20.     void Add(const Date &dt,const string &ev);
  21.     void Print(ostream &outs) const;
  22.     map <Date,vector<string>> data;
  23.     //typedef bool (*func)(/*const Date&, const std::string&*/);
  24. //  using func = bool (*)(const Date&, const std::string&);
  25.  
  26.  
  27.  
  28.     template<typename T>
  29.     vector<pair<const Date*,const string*>> FindIf(T func) const{
  30.         vector<pair<const Date*,const string*>> res;
  31.         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++){
  32.             const Date &space=i->first;
  33.             find_if(i->second.begin(),i->second.end(),[&space,&res,func](const string &str){
  34.                 if(func(space,str)){
  35.                     res.push_back(make_pair(&space,&str));
  36.                 }
  37.                 return false;
  38.             });
  39.         }
  40.         return res;
  41.     }
  42.  
  43. //  template<typename T1,typename T2>
  44. //      int do_stuff(T1 &i,T2 &Func, bool fe){
  45. //      int removes=0;
  46. //      Date space = i->first;
  47. //      auto partition = stable_partition(i->second.begin(),i->second.end(),[Func,space](string str){
  48. //                  return !Func(space,str);
  49. //      });
  50. //                  if(partition == i->second.begin()){
  51. //                      removes += count_if(partition,i->second.end(),[](string &str){return true;});
  52. //                      if(!fe)
  53. //                      data.erase(i--);
  54. //                      else
  55. //                      data.erase(i);
  56. //                  }else{
  57. //                      auto polzun = i->second.end();
  58. //                      while(polzun != partition){
  59. //                          removes++;
  60. //                          i->second.erase(polzun--);
  61. //                      }
  62. //                      if(!fe)
  63. //                      i--;
  64. //                  }
  65. //                  return removes;
  66. //      }
  67.  
  68.  
  69.     template<typename T2>
  70.             int RemoveIf(T2 Func){
  71.                 int removes=0;
  72.                 auto it = data.begin();
  73.                 while(it!=data.end()){
  74.                     removes+=it->second.size();
  75.                     const Date &space= it->first;
  76.                     auto partition = stable_partition(it->second.begin(),it->second.end(),[Func,space](string str){
  77.                                         return !Func(space,str);
  78.                             });
  79.                     it->second.erase(partition,it->second.end());
  80.                     removes-= it->second.size();
  81.                     if(it->second.empty()){
  82.                         data.erase(it++);
  83.                     }else{
  84.                         ++it;
  85.                     }
  86.                 }
  87.  
  88.                 return removes;
  89.             }
  90.  
  91. //  template<typename T2>
  92. //      int RemoveIf(T2 Func){
  93. //          queue<Date> erdates;
  94. //          int removes=0;
  95. //          for(auto &i:data){
  96. //              removes+=i.second.size();
  97. //              const Date &space= i.first;
  98. //              i.second.remove_if([space,Func](string &str){
  99. //                  return Func(space,str);
  100. //              });
  101. //              removes-= i.second.size();
  102. //              if(i.second.empty()){
  103. //                  erdates.push(space);
  104. //              }
  105. //          }
  106. //          while(!erdates.empty()){
  107. //              data.erase(erdates.front());
  108. //              erdates.pop();
  109. //          }
  110. //
  111. //          return removes;
  112. //      }
  113. //  template<typename T2>
  114. //  int RemoveIf(T2 Func){
  115. //      if(data.empty()){
  116. //          return 0;
  117. //      }
  118. //      int removes = 0;
  119. //      auto i = --data.end();
  120. //      while(i!=data.begin()){
  121. //          removes += do_stuff(i,Func,false);
  122. //      }
  123. //      removes += do_stuff(i,Func,true);
  124. //      return removes;
  125. //  }
  126. //      for(auto &i:data){
  127. //          Date space = i.first;
  128. //          auto partition = stable_partition(i.second.begin(),i.second.end(),[Func,space](string str){
  129. //              return !Func(space,str);
  130. //          });
  131. //          if(partition == i.second.begin()){
  132. //              removes += count_if(partition,i.second.end(),[](string &str){return true;});
  133. //
  134. //              erdates.push_back(space);
  135. //              continue;
  136. //          }
  137. //          auto polzun = i.second.end()--;
  138. //          while(polzun != partition){
  139. //              removes++;
  140. //              i.second.erase(polzun--);
  141. //          }
  142. ////            removes += count_if(partition,i.second.end(),[](string &str){return true;});
  143. ////
  144. ////            i.second.erase(partition,i.second.end());
  145. //      }
  146. //      while(!erdates.empty()){
  147. //          data.erase(erdates.front());
  148. //          erdates.pop_front();
  149. //      }
  150. //      return removes;
  151. //
  152. //      }
  153. //          removes += count_if(partition,i.second.end(),[](string &str){return true;});
  154. //          if(partition == i.second.begin()){
  155. //              erdates.push_back(i.first);
  156. //          }
  157. //          i.second.erase(partition,i.second.end());
  158. //      while(!erdates.empty()){
  159. //          data.erase(erdates.front());
  160. //          erdates.pop_front();
  161. //      }
  162. //      int count=0;
  163. //
  164. //      map <Date,std::vector<std::string>> new_data;
  165. //      if(FindIf(Func).empty()){
  166. //          return 0;
  167. //      }
  168. //      for(auto i:data){
  169. //          for(auto j: i.second){
  170. //              if(!Func(i.first,j)){
  171. //                  new_data[i.first].push_back(j);
  172. //              }
  173. //              else{
  174. //                  count++;
  175. //              }
  176. //          }
  177. //      }
  178. //      data = new_data;
  179. //      vector<string> wanted_list = FindIf(Func);
  180. //      vector<pair<Date, string>> wanted_pair;
  181. //      if(wanted_list.empty()){
  182. //          //data.clear();
  183. //          return 0;
  184. //      }
  185. //      for (const auto& pair : wanted_list) {
  186. //                  istringstream stream(pair);
  187. //                  auto d = ParseDate(stream);
  188. //                  stream.ignore(1);
  189. //                  string event;
  190. //                  getline(stream, event);
  191. //                  wanted_pair.push_back(make_pair(d, event));
  192. //              }
  193. //
  194. //      count =  wanted_pair.size();
  195. //
  196. //        for (const auto& pair : wanted_pair) {
  197. //                  Date space = pair.first;
  198. //                  string event = pair.second;
  199. //
  200. //                  if (data[pair.first].size() == 1) {
  201. //                                  data.erase(space);
  202. //                                  //stable_partition(data[space].begin(),data[space].end(),[](string str){return  true;});
  203. //                              }
  204. //                              else {
  205. //                                  auto it = find(data[space].begin(), data[space].end(), event);
  206. //                                  //data[space].erase(it);
  207. //                                  stable_partition(data[space].begin(),data[space].end(),[&it](string &str){return str == *it;});
  208. //                              }
  209. //                          }
  210.  
  211.  
  212.     std::string Last(const Date &dt) const;
  213. };
  214.  
  215. //Date ParseDate(istream &sstr);
  216.  
  217. ostream &operator <<(ostream &ostr,const pair<const Date*,const string*> &pr);
Add Comment
Please, Sign In to add comment