diff options
author | ShadowNinja <shadowninja@minetest.net> | 2015-10-30 00:04:10 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2016-03-07 16:33:20 -0500 |
commit | 5641da43d635ae46770eb20c5482a8971086202f (patch) | |
tree | 95a5424a224abbafc0c2a9073efe04cc8f1e3d9d | |
parent | 2e74e9ee20cee987a7326378c6ab73ec93e25457 (diff) | |
download | minetest-5641da43d635ae46770eb20c5482a8971086202f.tar.gz minetest-5641da43d635ae46770eb20c5482a8971086202f.tar.bz2 minetest-5641da43d635ae46770eb20c5482a8971086202f.zip |
Clean up VectorAreaStore
-rw-r--r-- | src/util/areastore.cpp | 48 | ||||
-rw-r--r-- | src/util/areastore.h | 8 |
2 files changed, 24 insertions, 32 deletions
diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index cf972586c..568492383 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -183,7 +183,7 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos) bool VectorAreaStore::insertArea(Area *a) { 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)); 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<u32, Area>::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<Area *>::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<Area *> *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<Area *> *result, v3s16 pos) void VectorAreaStore::getAreasInArea(std::vector<Area *> *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<Area *> *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 dee1f8bab..da7876396 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -59,8 +59,10 @@ protected: virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0; u32 getNextId() { return m_next_id++; } - // TODO change to unordered_map when we can - std::map<u32, Area> areas_map; + // Note: This can't be an unordered_map, since all + // references would be invalidated on rehash. + typedef std::map<u32, Area> 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<Area *> *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<Area *> *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap); |