diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 568492383..fdd4d7b79 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -54,19 +54,12 @@ AreaStore *AreaStore::getOptimalImplementation() #endif } -u16 AreaStore::size() const -{ - return areas_map.size(); -} - const Area *AreaStore::getArea(u32 id) const { - const Area *res = NULL; - std::map::const_iterator itr = areas_map.find(id); - if (itr != areas_map.end()) { - res = &itr->second; - } - return res; + AreaMap::const_iterator it = areas_map.find(id); + if (it == areas_map.end()) + return NULL; + return &it->second; } #if 0 @@ -234,7 +227,7 @@ void VectorAreaStore::getAreasInArea(std::vector *result, } #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) { if (callback(m_areas[i], arg)) { @@ -308,7 +301,7 @@ void SpatialAreaStore::getAreasInArea(std::vector *result, } #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) return false; 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,21 +39,55 @@ 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 { +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 *result, v3s16 pos); + virtual void getAreasInArea(std::vector *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: void invalidateCache(); virtual void getAreasForPosImpl(std::vector *result, v3s16 pos) = 0; @@ -63,98 +97,64 @@ protected: // references would be invalidated on rehash. typedef std::map AreaMap; 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 *result, v3s16 pos); - virtual void getAreasInArea(std::vector *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: static void cacheMiss(void *data, const v3s16 &mpos, std::vector *dest); + + bool m_cache_enabled; u8 m_cacheblock_radius; // if you modify this, call invalidateCache() LRUCache > m_res_cache; + u32 m_next_id; - bool m_cache_enabled; }; class VectorAreaStore : public AreaStore { -protected: - virtual void getAreasForPosImpl(std::vector *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 *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 *result, v3s16 pos); + private: std::vector m_areas; }; + #if USE_SPATIAL class SpatialAreaStore : public AreaStore { -protected: - virtual void getAreasForPosImpl(std::vector *result, v3s16 pos); public: SpatialAreaStore(); + virtual ~SpatialAreaStore(); + virtual bool insertArea(Area *a); virtual bool removeArea(u32 id); virtual void getAreasInArea(std::vector *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 *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 *m_result; public: - VectorResultVisitor(std::vector *result, SpatialAreaStore *store) - { - m_store = store; - m_result = result; - } + VectorResultVisitor(std::vector *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 *m_result; }; }; -#endif +#endif // USE_SPATIAL #endif // AREA_STORE_H_