Clean up VectorAreaStore

master
ShadowNinja 2015-10-30 00:04:10 -04:00
parent 2e74e9ee20
commit 5641da43d6
2 changed files with 24 additions and 32 deletions

View File

@ -183,7 +183,7 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos)
bool VectorAreaStore::insertArea(Area *a) bool VectorAreaStore::insertArea(Area *a)
{ {
a->id = getNextId(); a->id = getNextId();
std::pair<std::map<u32, Area>::iterator, bool> res = std::pair<AreaMap::iterator, bool> res =
areas_map.insert(std::make_pair(a->id, *a)); areas_map.insert(std::make_pair(a->id, *a));
if (!res.second) if (!res.second)
// ID is not unique // ID is not unique
@ -193,35 +193,27 @@ bool VectorAreaStore::insertArea(Area *a)
return true; return true;
} }
void VectorAreaStore::reserve(size_t count)
{
m_areas.reserve(count);
}
bool VectorAreaStore::removeArea(u32 id) bool VectorAreaStore::removeArea(u32 id)
{ {
std::map<u32, Area>::iterator itr = areas_map.find(id); AreaMap::iterator it = areas_map.find(id);
if (itr != areas_map.end()) { if (it == areas_map.end())
size_t msiz = m_areas.size(); return false;
for (size_t i = 0; i < msiz; i++) { Area *a = &it->second;
Area * b = m_areas[i]; for (std::vector<Area *>::iterator v_it = m_areas.begin();
if (b->id == id) { v_it != m_areas.end(); ++v_it) {
areas_map.erase(itr); if (*v_it == a) {
m_areas.erase(m_areas.begin() + i); m_areas.erase(v_it);
invalidateCache(); break;
return true;
}
} }
// we should never get here, it means we did find it in map,
// but not in the vector
} }
return false; areas_map.erase(it);
invalidateCache();
return true;
} }
void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
{ {
size_t msiz = m_areas.size(); for (size_t i = 0; i < m_areas.size(); ++i) {
for (size_t i = 0; i < msiz; i++) {
Area *b = m_areas[i]; Area *b = m_areas[i];
if (AST_CONTAINS_PT(b, pos)) { if (AST_CONTAINS_PT(b, pos)) {
result->push_back(b); result->push_back(b);
@ -232,9 +224,8 @@ void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
void VectorAreaStore::getAreasInArea(std::vector<Area *> *result, void VectorAreaStore::getAreasInArea(std::vector<Area *> *result,
v3s16 minedge, v3s16 maxedge, bool accept_overlap) v3s16 minedge, v3s16 maxedge, bool accept_overlap)
{ {
size_t msiz = m_areas.size(); for (size_t i = 0; i < m_areas.size(); ++i) {
for (size_t i = 0; i < msiz; i++) { Area *b = m_areas[i];
Area * b = m_areas[i];
if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, b) : if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, b) :
AST_CONTAINS_AREA(minedge, maxedge, b)) { AST_CONTAINS_AREA(minedge, maxedge, b)) {
result->push_back(b); result->push_back(b);
@ -243,11 +234,10 @@ void VectorAreaStore::getAreasInArea(std::vector<Area *> *result,
} }
#if 0 #if 0
bool VectorAreaStore::forEach(bool (*callback)(void *args, Area *a), void *args) const bool SimpleAreaStore::forEach(bool (*callback)(void *args, Area *a), void *args) const
{ {
size_t msiz = m_areas.size(); for (size_t i = 0; i < m_areas.size(); ++i) {
for (size_t i = 0; i < msiz; i++) { if (callback(m_areas[i], arg)) {
if (callback(args, m_areas[i])) {
return true; return true;
} }
} }

View File

@ -59,8 +59,10 @@ protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0; virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
u32 getNextId() { return m_next_id++; } u32 getNextId() { return m_next_id++; }
// TODO change to unordered_map when we can // Note: This can't be an unordered_map, since all
std::map<u32, Area> areas_map; // references would be invalidated on rehash.
typedef std::map<u32, Area> AreaMap;
AreaMap areas_map;
public: public:
// Updates the area's ID // Updates the area's ID
virtual bool insertArea(Area *a) = 0; virtual bool insertArea(Area *a) = 0;
@ -111,8 +113,8 @@ class VectorAreaStore : public AreaStore {
protected: protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos); virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
public: public:
virtual void reserve(size_t count) { m_areas.reserve(count); }
virtual bool insertArea(Area *a); virtual bool insertArea(Area *a);
virtual void reserve(size_t count);
virtual bool removeArea(u32 id); virtual bool removeArea(u32 id);
virtual void getAreasInArea(std::vector<Area *> *result, virtual void getAreasInArea(std::vector<Area *> *result,
v3s16 minedge, v3s16 maxedge, bool accept_overlap); v3s16 minedge, v3s16 maxedge, bool accept_overlap);