#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using std::string; using std::map; using std::vector; class GameDatabase { private: map data; public: GameDatabase() = default; /// вставляет в базу объект с именем [name] и позицией [x, y] /// если объект с таким id в базе уже есть, заменяет его новым void Insert(ObjectId id, string name, size_t x, size_t y) { Remove(id); GameObject Object; Object.id = id; Object.name = name; Object.x = x; Object.y = y; data[id] = Object; } /// удаляет элемент по id /// если такого элемента нет, ничего не делает void Remove(ObjectId id) { if (data.find(id) != data.end()) { data.erase(data.find(id)); } } /// возвращает вектор объектов c именем [name] /// сортировка по убыванию id vector DataByName(string name) const { vector res; for (auto i = data.rbegin(); i != data.rend(); i++) { if (i->next.name == name) { res.push_back(i->next); } } return res; } /// возвращает вектор объектов, находящихся в позиции [x, y] /// сортировка по убыванию id vector DataByPosition(size_t x, size_t y) const { vector res; for (auto i = data.rbegin(); i != data.rend(); i++) { if (i->next.x == x && i->next.y == y) { res.push_back(i->next); } } return res; } /// возвращает вектор всех объектов из базы /// сортировка по убыванию id vector Data() const { vector res; for (auto i = data.rbegin(); i != data.rend(); i++) { res.push_back(i->next); } return res; } };