1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-11-15 22:05:07 -05:00

Clean up VectorAreaStore

This commit is contained in:
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)
{
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);
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;
}
}
areas_map.erase(it);
invalidateCache();
return true;
}
}
// we should never get here, it means we did find it in map,
// but not in the vector
}
return false;
}
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,8 +224,7 @@ 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++) {
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)) {
@ -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;
}
}

View File

@ -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);