Бурачинский Александр Дмитриевич
15.11.2021 21:02 (26天 19:02)
[5781453]
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <functional>
#include <set>
#include <queue>
#include <string>
#include <memory>
#include <map>
#include <iomanip>
#include <sstream>
#include <cassert>
using std::string;
using std::map;
using std::vector;
class GameDatabase{
private:
map<ObjectId, GameObject> 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<GameObject> DataByName(string name) const {
vector<GameObject> 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<GameObject> DataByPosition(size_t x, size_t y) const {
vector<GameObject> 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<GameObject> Data() const {
vector<GameObject> res;
for (auto i = data.rbegin(); i != data.rend(); i++) {
res.push_back(i->next);
}
return res;
}
};