Sort AreaStore header

This commit is contained in:
ShadowNinja 2015-10-30 00:52:45 -04:00
parent 5641da43d6
commit 6e9d71342a
2 changed files with 70 additions and 75 deletions

@ -54,19 +54,12 @@ AreaStore *AreaStore::getOptimalImplementation()
#endif #endif
} }
u16 AreaStore::size() const
{
return areas_map.size();
}
const Area *AreaStore::getArea(u32 id) const const Area *AreaStore::getArea(u32 id) const
{ {
const Area *res = NULL; AreaMap::const_iterator it = areas_map.find(id);
std::map<u32, Area>::const_iterator itr = areas_map.find(id); if (it == areas_map.end())
if (itr != areas_map.end()) { return NULL;
res = &itr->second; return &it->second;
}
return res;
} }
#if 0 #if 0
@ -234,7 +227,7 @@ void VectorAreaStore::getAreasInArea(std::vector<Area *> *result,
} }
#if 0 #if 0
bool SimpleAreaStore::forEach(bool (*callback)(void *args, Area *a), void *args) const bool SimpleAreaStore::forEach(ForEachCallback callback, void *arg) const
{ {
for (size_t i = 0; i < m_areas.size(); ++i) { for (size_t i = 0; i < m_areas.size(); ++i) {
if (callback(m_areas[i], arg)) { if (callback(m_areas[i], arg)) {
@ -308,7 +301,7 @@ void SpatialAreaStore::getAreasInArea(std::vector<Area *> *result,
} }
#if 0 #if 0
bool SpatialAreaStore::forEach(bool (*callback)(void *args, Area *a), void *args) const bool SpatialAreaStore::forEach(ForEachCallback callback, void *arg) const
{ {
// TODO ?? (this is only needed for serialisation, but libspatial has its own serialisation) // TODO ?? (this is only needed for serialisation, but libspatial has its own serialisation)
return false; return false;

@ -39,21 +39,55 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct Area { struct Area {
Area() {} Area() {}
Area(const v3s16 &mine, const v3s16 &maxe) Area(const v3s16 &mine, const v3s16 &maxe) :
minedge(mine), maxedge(maxe)
{ {
minedge = mine;
maxedge = maxe;
sortBoxVerticies(minedge, maxedge); sortBoxVerticies(minedge, maxedge);
} }
u32 id; u32 id;
v3s16 minedge; v3s16 minedge, maxedge;
v3s16 maxedge;
std::string data; std::string data;
}; };
class AreaStore { class AreaStore {
public:
AreaStore() :
m_cache_enabled(true),
m_cacheblock_radius(64),
m_res_cache(1000, &cacheMiss, this),
m_next_id(0)
{}
virtual ~AreaStore() {}
static AreaStore *getOptimalImplementation();
virtual void reserve(size_t count) {};
size_t size() const { return areas_map.size(); }
// Updates the area's ID
virtual bool insertArea(Area *a) = 0;
virtual bool removeArea(u32 id) = 0;
void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
virtual void getAreasInArea(std::vector<Area *> *result,
v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
void setCacheParams(bool enabled, u8 block_radius, size_t limit);
const Area *getArea(u32 id) const;
#if 0
typedef bool (*ForEachCallback)(const Area *a, void *arg);
// Calls a passed function for every stored area, until the
// callback returns true. If that happens, it returns true,
// if the search is exhausted, it returns false.
virtual bool forEach(ForEachCallback, void *arg=NULL) const = 0;
void serialize(std::ostream &is) const;
bool deserialize(std::istream &is);
#endif
protected: protected:
void invalidateCache(); void invalidateCache();
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0; virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
@ -63,98 +97,64 @@ protected:
// references would be invalidated on rehash. // references would be invalidated on rehash.
typedef std::map<u32, Area> AreaMap; typedef std::map<u32, Area> AreaMap;
AreaMap areas_map; AreaMap areas_map;
public:
// Updates the area's ID
virtual bool insertArea(Area *a) = 0;
virtual void reserve(size_t count) {};
virtual bool removeArea(u32 id) = 0;
void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
virtual void getAreasInArea(std::vector<Area *> *result,
v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
#if 0
// calls a passed function for every stored area, until the
// callback returns true. If that happens, it returns true,
// if the search is exhausted, it returns false
virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const = 0;
#endif
virtual ~AreaStore()
{}
AreaStore() :
m_cacheblock_radius(64),
m_res_cache(1000, &cacheMiss, this),
m_next_id(0),
m_cache_enabled(true)
{
}
void setCacheParams(bool enabled, u8 block_radius, size_t limit);
const Area *getArea(u32 id) const;
u16 size() const;
static AreaStore *getOptimalImplementation();
#if 0
bool deserialize(std::istream &is);
void serialize(std::ostream &is) const;
#endif
private: private:
static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest); static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
bool m_cache_enabled;
u8 m_cacheblock_radius; // if you modify this, call invalidateCache() u8 m_cacheblock_radius; // if you modify this, call invalidateCache()
LRUCache<v3s16, std::vector<Area *> > m_res_cache; LRUCache<v3s16, std::vector<Area *> > m_res_cache;
u32 m_next_id; u32 m_next_id;
bool m_cache_enabled;
}; };
class VectorAreaStore : public AreaStore { class VectorAreaStore : public AreaStore {
protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
public: public:
virtual void reserve(size_t count) { m_areas.reserve(count); } virtual void reserve(size_t count) { m_areas.reserve(count); }
virtual bool insertArea(Area *a); virtual bool insertArea(Area *a);
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);
// virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const; //virtual bool forEach(ForEachCallback, void *arg) const;
protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
private: private:
std::vector<Area *> m_areas; std::vector<Area *> m_areas;
}; };
#if USE_SPATIAL #if USE_SPATIAL
class SpatialAreaStore : public AreaStore { class SpatialAreaStore : public AreaStore {
protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
public: public:
SpatialAreaStore(); SpatialAreaStore();
virtual ~SpatialAreaStore();
virtual bool insertArea(Area *a); virtual bool insertArea(Area *a);
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);
// virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const; //virtual bool forEach(ForEachCallback, void *arg) const;
protected:
virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
virtual ~SpatialAreaStore();
private: private:
SpatialIndex::ISpatialIndex *m_tree; SpatialIndex::ISpatialIndex *m_tree;
SpatialIndex::IStorageManager *m_storagemanager; SpatialIndex::IStorageManager *m_storagemanager;
class VectorResultVisitor : public SpatialIndex::IVisitor { class VectorResultVisitor : public SpatialIndex::IVisitor {
private:
SpatialAreaStore *m_store;
std::vector<Area *> *m_result;
public: public:
VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
{ m_store(store),
m_store = store; m_result(result)
m_result = result; {}
} ~VectorResultVisitor() {}
virtual void visitNode(const SpatialIndex::INode &in) virtual void visitNode(const SpatialIndex::INode &in) {}
{
}
virtual void visitData(const SpatialIndex::IData &in) virtual void visitData(const SpatialIndex::IData &in)
{ {
@ -171,10 +171,12 @@ private:
visitData(*(v[i])); visitData(*(v[i]));
} }
~VectorResultVisitor() {} private:
SpatialAreaStore *m_store;
std::vector<Area *> *m_result;
}; };
}; };
#endif #endif // USE_SPATIAL
#endif // AREA_STORE_H_ #endif // AREA_STORE_H_