Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. class Database {
  2.  public:
  3.     auto Put(const Record& record) -> bool;
  4.     auto GetById(const string& id) const -> const Record*;
  5.     auto Erase(const string& id) -> bool;
  6.  
  7.     template <typename Callback> void RangeByTimestamp(int low, int high, Callback callback) const;
  8.     template <typename Callback> void RangeByKarma(int low, int high, Callback callback) const;
  9.     template <typename Callback> void AllByUser(const string& user, Callback callback) const;
  10.  
  11.  private:
  12.     unordered_map<string, Record> mDatabase;
  13.     map<int, unordered_set<string>> mKarmaIndex;
  14.     map<int, unordered_set<string>> mTimestampIndex;
  15.     unordered_map<string, unordered_set<string>> mUserIndex;
  16. };
  17.  
  18. auto Database::Put(const Record& record) -> bool {
  19.     const auto[recordIterator, bResult] = mDatabase.insert({record.id, record});
  20.     if (bResult) {
  21.         mKarmaIndex[record.karma].insert(record.id);
  22.         mTimestampIndex[record.timestamp].insert(record.id);
  23.         mUserIndex[record.user].insert(record.id);
  24.     }
  25.     return bResult;
  26. }
  27. auto Database::GetById(const string& id) const -> const Record* {
  28.     if (mDatabase.count(id)) {
  29.         return &mDatabase.at(id);
  30.     } else {
  31.         return nullptr;
  32.     }
  33. }
  34. auto Database::Erase(const string& id) -> bool {
  35.     const auto foundRecord = mDatabase.find(id);
  36.     if (foundRecord != mDatabase.end()) {
  37.         mKarmaIndex[foundRecord->second.karma].erase(id);
  38.         mTimestampIndex[foundRecord->second.timestamp].erase(id);
  39.         mUserIndex[foundRecord->second.user].erase(id);
  40.         mDatabase.erase(id);
  41.         return true;
  42.     } else {
  43.         return false;
  44.     }
  45. }
  46. template<typename Callback> void Database::RangeByTimestamp(int low, int high, Callback callback) const {
  47.     auto timestampCur = mTimestampIndex.lower_bound(low);
  48.  
  49.     while(true) {
  50.         if (timestampCur == mTimestampIndex.end() || timestampCur->first > high) {
  51.             return;
  52.         }
  53.  
  54.         for (const auto& item : timestampCur->second) {
  55.             if (!callback(mDatabase.at(item))) {
  56.                 return;
  57.             }
  58.         }
  59.         ++timestampCur;
  60.     }
  61. }
  62. template<typename Callback> void Database::RangeByKarma(int low, int high, Callback callback) const {
  63.     auto karmaCur = mKarmaIndex.lower_bound(low);
  64.  
  65.     while(true) {
  66.         if (karmaCur == mKarmaIndex.end() || karmaCur->first > high) {
  67.             return;
  68.         }
  69.  
  70.         for (const auto& item : karmaCur->second) {
  71.             if (!callback(mDatabase.at(item))) {
  72.                 return;
  73.             }
  74.         }
  75.         ++karmaCur;
  76.     }
  77. }
  78. template<typename Callback> void Database::AllByUser(const string& user, Callback callback) const {
  79.     if (!mUserIndex.count(user)) {
  80.         return;
  81.     }
  82.     for (const auto& item : mUserIndex.at(user)) {
  83.         if (!callback(mDatabase.at(item))) {
  84.             return;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement