diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index cf972586..56849238 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -183,7 +183,7 @@ void AreaStore::getAreasForPos(std::vector *result, v3s16 pos) bool VectorAreaStore::insertArea(Area *a) { a->id = getNextId(); - std::pair::iterator, bool> res = + std::pair res = areas_map.insert(std::make_pair(a->id, *a)); if (!res.second) // ID is not unique @@ -193,35 +193,27 @@ bool VectorAreaStore::insertArea(Area *a) return true; } -void VectorAreaStore::reserve(size_t count) -{ - m_areas.reserve(count); -} - bool VectorAreaStore::removeArea(u32 id) { - std::map::iterator itr = areas_map.find(id); - if (itr != areas_map.end()) { - size_t msiz = m_areas.size(); - for (size_t i = 0; i < msiz; i++) { - Area * b = m_areas[i]; - if (b->id == id) { - areas_map.erase(itr); - m_areas.erase(m_areas.begin() + i); - invalidateCache(); - return true; - } + AreaMap::iterator it = areas_map.find(id); + if (it == areas_map.end()) + return false; + Area *a = &it->second; + for (std::vector::iterator v_it = m_areas.begin(); + v_it != m_areas.end(); ++v_it) { + if (*v_it == a) { + m_areas.erase(v_it); + break; } - // 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 *result, v3s16 pos) { - size_t msiz = m_areas.size(); - for (size_t i = 0; i < msiz; i++) { + for (size_t i = 0; i < m_areas.size(); ++i) { Area *b = m_areas[i]; if (AST_CONTAINS_PT(b, pos)) { result->push_back(b); @@ -232,9 +224,8 @@ void VectorAreaStore::getAreasForPosImpl(std::vector *result, v3s16 pos) void VectorAreaStore::getAreasInArea(std::vector *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap) { - size_t msiz = m_areas.size(); - for (size_t i = 0; i < msiz; i++) { - Area * b = m_areas[i]; + for (size_t i = 0; i < m_areas.size(); ++i) { + Area *b = m_areas[i]; if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, b) : AST_CONTAINS_AREA(minedge, maxedge, b)) { result->push_back(b); @@ -243,11 +234,10 @@ void VectorAreaStore::getAreasInArea(std::vector *result, } #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 < msiz; i++) { - if (callback(args, m_areas[i])) { + for (size_t i = 0; i < m_areas.size(); ++i) { + if (callback(m_areas[i], arg)) { return true; } } diff --git a/src/util/areastore.h b/src/util/areastore.h index dee1f8ba..da787639 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -59,8 +59,10 @@ protected: virtual void getAreasForPosImpl(std::vector *result, v3s16 pos) = 0; u32 getNextId() { return m_next_id++; } - // TODO change to unordered_map when we can - std::map areas_map; + // Note: This can't be an unordered_map, since all + // references would be invalidated on rehash. + typedef std::map AreaMap; + AreaMap areas_map; public: // Updates the area's ID virtual bool insertArea(Area *a) = 0; @@ -111,8 +113,8 @@ class VectorAreaStore : public AreaStore { protected: virtual void getAreasForPosImpl(std::vector *result, v3s16 pos); public: + virtual void reserve(size_t count) { m_areas.reserve(count); } virtual bool insertArea(Area *a); - virtual void reserve(size_t count); virtual bool removeArea(u32 id); virtual void getAreasInArea(std::vector *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap);