diff options
author | ShadowNinja <shadowninja@minetest.net> | 2015-10-29 23:08:32 -0400 |
---|---|---|
committer | ShadowNinja <shadowninja@minetest.net> | 2016-03-07 16:33:20 -0500 |
commit | 095db16990eea878ce01b29c1eb85a128f98381a (patch) | |
tree | 9845f990ebdb7c78c1ecf487cfa91eb29fd2b7db /src/areastore.cpp | |
parent | 725cb4eb0743a382851e011fb7de1b7d443ce51f (diff) | |
download | minetest-095db16990eea878ce01b29c1eb85a128f98381a.tar.gz minetest-095db16990eea878ce01b29c1eb85a128f98381a.tar.bz2 minetest-095db16990eea878ce01b29c1eb85a128f98381a.zip |
Simplify AreaStore ID management
Diffstat (limited to 'src/areastore.cpp')
-rw-r--r-- | src/areastore.cpp | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/src/areastore.cpp b/src/areastore.cpp index f9362c4a6..c2cc4ce97 100644 --- a/src/areastore.cpp +++ b/src/areastore.cpp @@ -49,21 +49,6 @@ u16 AreaStore::size() const return areas_map.size(); } -u32 AreaStore::getFreeId(v3s16 minedge, v3s16 maxedge) -{ - int keep_on = 100; - while (keep_on--) { - m_highest_id++; - // Handle overflows, we dont want to return 0 - if (m_highest_id == AREA_ID_INVALID) - m_highest_id++; - if (areas_map.find(m_highest_id) == areas_map.end()) - return m_highest_id; - } - // search failed - return AREA_ID_INVALID; -} - const Area *AreaStore::getArea(u32 id) const { const Area *res = NULL; @@ -185,11 +170,17 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos) //// -void VectorAreaStore::insertArea(const Area &a) +bool VectorAreaStore::insertArea(Area *a) { - areas_map[a.id] = a; - m_areas.push_back(&(areas_map[a.id])); + a->id = getNextId(); + std::pair<std::map<u32, Area>::iterator, bool> res = + areas_map.insert(std::make_pair(a->id, *a)); + if (!res.second) + // ID is not unique + return false; + m_areas.push_back(&res.first->second); invalidateCache(); + return true; } void VectorAreaStore::reserve(size_t count) @@ -273,11 +264,15 @@ static inline SpatialIndex::Point get_spatial_point(const v3s16 pos) } -void SpatialAreaStore::insertArea(const Area &a) +bool SpatialAreaStore::insertArea(Area *a) { - areas_map[a.id] = a; - m_tree->insertData(0, NULL, get_spatial_region(a.minedge, a.maxedge), a.id); + a->id = getNextId(); + if (!areas_map.insert(std::make_pair(a->id, *a)).second) + // ID is not unique + return false; + m_tree->insertData(0, NULL, get_spatial_region(a->minedge, a->maxedge), a->id); invalidateCache(); + return true; } bool SpatialAreaStore::removeArea(u32 id) |