SHARE
TWEET

Untitled

a guest Dec 6th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <functional>
  3. #include <utility>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <unordered_map>
  8.  
  9. bool operator>(const GameObject& a, const GameObject& b) { return a.id > b.id; }
  10.  
  11. template <class Tp, template <class> class Compare>
  12. class DereferenceCompare {
  13.     Compare<Tp> comp;
  14.  
  15. public:
  16.     bool operator()(const Tp* const a, const Tp* const b) const {
  17.         return comp(*a, *b);
  18.     }
  19. };
  20.  
  21. class GameDatabase {
  22. public:
  23.     GameDatabase() = default;
  24.  
  25.     void Insert(ObjectId id, string name, size_t x, size_t y) {
  26.         auto iter = map_find_id.find(id);
  27.  
  28.         GameObject tmp;
  29.         tmp.id = id;
  30.         tmp.name = name;
  31.         tmp.x = x;
  32.         tmp.y = y;
  33.  
  34.         Remove(iter->second.id);
  35.  
  36.         map_find_id.insert({id, tmp});
  37.  
  38.         iter = map_find_id.find(id);
  39.  
  40.         map_find_name[{iter->second.name}].insert(&iter->second);
  41.  
  42.         map_find_pos[{iter->second.x, iter->second.y}].insert(&iter->second);
  43.     }
  44.  
  45.     void Remove(ObjectId id) {
  46.         auto iter = map_find_id.find(id);
  47.         if (iter != map_find_id.end()) {
  48.  
  49.             map_find_name[iter -> second.name].erase(&iter->second);
  50.  
  51.             map_find_pos[std::pair<size_t, size_t>(iter->second.x,
  52.                                                    iter->second.y)]
  53.                     .erase(&iter->second);
  54.  
  55.             map_find_id.erase(id);
  56.         }
  57.     }
  58.  
  59.     vector<GameObject> DataByName(string name) const {
  60.         auto iter = map_find_name.find(name);
  61.         vector<GameObject> h;
  62.  
  63.         if (iter != map_find_name.end()) {
  64.             for (auto i = iter->second.begin(); i != iter->second.end(); ++i) {
  65.                 h.push_back(**i);
  66.             }
  67.         }
  68.  
  69.         return h;
  70.     }
  71.  
  72.     vector<GameObject> DataByPosition(size_t x, size_t y) const {
  73.         auto iter = map_find_pos.find(std::pair<size_t, size_t>(x, y));
  74.         vector<GameObject> h;
  75.  
  76.         if (iter != map_find_pos.end()) {
  77.             for (auto i = iter->second.begin(); i != iter->second.end(); ++i) {
  78.                 h.push_back(**i);
  79.             }
  80.         }
  81.  
  82.         return h;
  83.     }
  84.  
  85.     vector<GameObject> Data() const {
  86.         vector<GameObject> h;
  87.         for (auto i = map_find_id.begin(); i != map_find_id.end(); ++i) {
  88.             h.push_back(i->second);
  89.         }
  90.         return h;
  91.     }
  92.  
  93. private:
  94.     std::map<ObjectId, GameObject, std::greater<ObjectId>> map_find_id;
  95.  
  96.     std::map<std::pair<size_t, size_t>,
  97.             std::set<GameObject*, DereferenceCompare<GameObject, std::greater>>>
  98.             map_find_pos;
  99.  
  100.     std::unordered_map<
  101.             string,
  102.             std::set<GameObject*, DereferenceCompare<GameObject, std::greater>>>
  103.             map_find_name;
  104. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top