Advertisement
RobertDeMilo

BB3.6 логическая константность и mutable

Jun 13th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. //#include <iostream>
  2. //#include <algorithm>
  3. //#include <optional>
  4. //#include <vector>
  5. //#include <map>
  6. //#include <unordered_map>
  7. //using namespace std;
  8. //
  9. //struct Record
  10. //{
  11. //  string id;
  12. //  string title;
  13. //  string user;
  14. //  int timestamp;
  15. //  int karma;
  16. //};
  17. //
  18. //class Database
  19. //{
  20. //public:
  21. //  bool Put(const Record& record)
  22. //  {
  23. //      auto [it, inserted] = storage.insert({ record.id, Data{record,{},{},{} } });
  24. //
  25. //      if (!inserted)
  26. //      {
  27. //          return false;
  28. //      }
  29. //
  30. //      auto& data = it->second;
  31. //
  32. //      const Record* ptr = &data.record;
  33. //
  34. //      data.timestamp_iter_ = timestamp_index.insert({ record.timestamp, ptr });
  35. //
  36. //      data.karma_iter_ = karma_index.insert({ record.karma, ptr });
  37. //
  38. //      data.user_iter_ = user_index.insert({ record.user,ptr });
  39. //
  40. //      cache.reset();
  41. //
  42. //      return true;
  43. //  }
  44. //
  45. //  const Record* GetById(const string& id) const
  46. //  {
  47. //      auto it = storage.find(id);
  48. //
  49. //      if (it == storage.end())
  50. //      {
  51. //          return nullptr;
  52. //      }
  53. //      return &it->second.record;
  54. //  }
  55. //
  56. //  bool Erase(const string& id)
  57. //  {
  58. //      auto it = storage.find(id);
  59. //
  60. //      if (it == storage.end())
  61. //      {
  62. //          return false;
  63. //      }
  64. //
  65. //      const auto& data = it->second;
  66. //
  67. //      timestamp_index.erase(data.timestamp_iter_);
  68. //
  69. //      karma_index.erase(data.karma_iter_);
  70. //
  71. //      user_index.erase(data.user_iter_);
  72. //
  73. //      storage.erase(it);
  74. //
  75. //      cache.reset();
  76. //
  77. //      return true;
  78. //  }
  79. //
  80. //  template<typename CallBack>
  81. //  void RangeByTimestamp(int low, int high, const CallBack& callback) const
  82. //  {
  83. //      auto it_begin = timestamp_index.lower_bound(low);
  84. //      auto it_end = timestamp_index.upper_bound(high);
  85. //
  86. //      for (auto it = it_begin(); it!- it_end(); ++it)
  87. //      {
  88. //          if (!callBack(*it->second))
  89. //          {
  90. //              break;
  91. //          }
  92. //      }
  93. //  }
  94. //
  95. //  template<typename CallBack>
  96. //  void RangeByKarma(int low, int high, const CallBack& callback) const
  97. //  {
  98. //      if (!cache || cache->low != low || cache->high != high)
  99. //      {
  100. //          cache = Cache{ low,high, karma_index.lower_bound(low) , karma_index.upper_bound(high) };
  101. //      }
  102. //      for (auto it = cache->begin; it != cache->end; ++it)
  103. //      {
  104. //          if (!callBack(*it->second))
  105. //          {
  106. //              break;
  107. //          }
  108. //      }
  109. //  }
  110. //
  111. //  template <typename CallBack>
  112. //  void AllByUser(const string& user, const CallBack& callback) const
  113. //  {
  114. //      auto [it_begin, it_end] = user_index.equal_range(----);
  115. //
  116. //      for (auto it = it_begin(); it != it_end(); ++it)
  117. //      {
  118. //          if (!callBack(*it->second))
  119. //          {
  120. //              break;
  121. //          }
  122. //      }
  123. //  }
  124. //
  125. //private:
  126. //
  127. //  template<typename Type>
  128. //  using Index = multimap <Type, const Record*>;
  129. //
  130. //  struct Data
  131. //  {
  132. //      Record record;
  133. //      Index<int> ::iterator timestamp_iter_;
  134. //      Index<int> ::iterator karma_iter_;
  135. //      Index<string> ::iterator user_iter_;
  136. //  };
  137. //
  138. //private:
  139. //  unordered_map<string, Data> storage;
  140. //  Index <int> timestamp_index;
  141. //  Index <int> karma_index;
  142. //  Index <string> user_index;
  143. //
  144. //  struct Cache
  145. //  {
  146. //      int low, high;
  147. //      Index<int>::const_iterator begin, end;
  148. //  };
  149. //
  150. //  mutable std::optional<Cache> cache;
  151. //};
  152. //
  153. //int FindMinimalKarma(const Database& db);
  154. //
  155. //int FindMinimalKarma(const Database& db)
  156. //{
  157. //  int result = numeric_limits<int>::max();
  158. //  db.RangeByKarma(numeric_limits<int>::min(), numeric_limits<int>::max(), [&](const Record& r)
  159. //      {
  160. //          result = std::min(result, r.karma);
  161. //          return false;
  162. //      });
  163. //  return result;
  164. //}
  165. //
  166. //vector<string> FindUsersWithGivenKarma(const Database& db, int value);
  167. //vector<string> FindUsersWithGivenKarma(const Database& db, int value)
  168. //{
  169. //  vector<string> result;
  170. //  db.RangeByKarma(value, value, [&result](const Record& r)
  171. //      {
  172. //          result.push_back(r.id);
  173. //          return true;
  174. //      });
  175. //
  176. //  return result;
  177. //}
  178. //
  179. //vector<string> WorstFiveUsers(const Database& db);
  180. //vector<string> WorstFiveUsers(const Database& db)
  181. //{
  182. //  vector<string> result;
  183. //  db.RangeByKarma(numeric_limits<int>::min(), numeric_limits<int>::max(), [&](const Record& r)
  184. //      {
  185. //          result.push_back(r.id);
  186. //          return result.size() < 5;
  187. //      });
  188. //  return result;
  189. //}
  190. //
  191. //
  192. //void TestRangeBoundaries()
  193. //{
  194. //  const int good_karma = 1000;
  195. //  const int bad_karma = -10;
  196. //
  197. //  Database db;
  198. //  db.Put({ "id1","Hello there","master",153 });
  199. //}
  200. //
  201. //int main()
  202. //{
  203. //  TestRunner tr;
  204. //  RUN_TEST(tr, TestRangeBoundaries);
  205. //  RUN_TEST(tr, TestSameUser);
  206. //  RUN_TEST(tr, TestReplacement);
  207. //
  208. //  Database db;
  209. //  FindMinimalKarma(db);
  210. //  FindUserWithGivenKarma(db, 100);
  211. //  FindUserWithGivenKarma(db, 100);
  212. //
  213. //  return 0;
  214. //}
  215.  
  216. // mutable позволяет изменять внутренне состояние объекта оставляя его методы константными
  217.  
  218. //#include <iostream>
  219. //#include <map>
  220. //#include <string>
  221. //#include <vector>
  222. //#include <future>
  223. //#include <optional>
  224. //#include <algorithm>
  225. //#include <thread>
  226. //
  227. //using namespace std;
  228. //
  229. //template <typename T>
  230. //class LazyValue
  231. //{
  232. //public:
  233. //  explicit LazyValue(std::function<T()> init) : init_(std::move(init)) {}
  234. //
  235. //  const T& Get() const
  236. //  {
  237. //      if (lock_guard g(m); !value)
  238. //      {
  239. //          value = init_();
  240. //      }
  241. //      return *value;
  242. //  }
  243. //private:
  244. //  std::function<T()> init_;
  245. //  mutable std::optional<T> value;
  246. //  mutable mutex m;
  247. //};
  248. //
  249. //int main()
  250. //{
  251. //  LazyValue<map<string, int>> city_population([&]
  252. //      {
  253. //          return map<string, int> {
  254. //              {"Москва", 11514330},
  255. //              { "Санкт-Петербург", 1498921 }
  256. //          };
  257. //      });
  258. //
  259. //  auto kernel = [&]
  260. //      {
  261. //          return city_population.Get().at("Тула");
  262. //      };
  263. //
  264. //  vector<std::future<int>> ts;
  265. //
  266. //  for (size_t i = 0; i < 25; ++i)
  267. //  {
  268. //      ts.push_back(async(kernel));
  269. //  }
  270. //  for (auto& t : ts)
  271. //  {
  272. //      t.get();
  273. //  }
  274. //  const string saratov = "Саратов";
  275. //
  276. //  cout << saratov << ' ' << city_population.Get().at(saratov);
  277. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement