diff options
Diffstat (limited to 'src/util/areastore.h')
-rw-r--r-- | src/util/areastore.h | 118 |
1 files changed, 60 insertions, 58 deletions
diff --git a/src/util/areastore.h b/src/util/areastore.h index da7876396..20e9bdfc5 100644 --- a/src/util/areastore.h +++ b/src/util/areastore.h @@ -39,122 +39,122 @@ with this program; if not, write to the Free Software Foundation, Inc., struct 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); } u32 id; - v3s16 minedge; - v3s16 maxedge; + v3s16 minedge, maxedge; std::string data; }; class AreaStore { -protected: - void invalidateCache(); - virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0; - u32 getNextId() { return m_next_id++; } - - // 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: + 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 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); + 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: + void invalidateCache(); + virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0; + u32 getNextId() { return m_next_id++; } + + // 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; + private: 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() LRUCache<v3s16, std::vector<Area *> > m_res_cache; + u32 m_next_id; - bool m_cache_enabled; }; 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 bool removeArea(u32 id); virtual void getAreasInArea(std::vector<Area *> *result, 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: std::vector<Area *> m_areas; }; + #if USE_SPATIAL class SpatialAreaStore : public AreaStore { -protected: - virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos); public: SpatialAreaStore(); + virtual ~SpatialAreaStore(); + virtual bool insertArea(Area *a); virtual bool removeArea(u32 id); virtual void getAreasInArea(std::vector<Area *> *result, 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: SpatialIndex::ISpatialIndex *m_tree; SpatialIndex::IStorageManager *m_storagemanager; class VectorResultVisitor : public SpatialIndex::IVisitor { - private: - SpatialAreaStore *m_store; - std::vector<Area *> *m_result; public: - VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) - { - m_store = store; - m_result = result; - } + VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) : + m_store(store), + 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) { @@ -171,10 +171,12 @@ private: visitData(*(v[i])); } - ~VectorResultVisitor() {} + private: + SpatialAreaStore *m_store; + std::vector<Area *> *m_result; }; }; -#endif +#endif // USE_SPATIAL #endif // AREA_STORE_H_ |