diff options
author | Marius Kintel <marius@kintel.net> | 2012-01-09 22:59:53 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2012-01-09 23:01:50 (GMT) |
commit | 22b98cd05217acf0bf78969a3baaf4568781670f (patch) | |
tree | 005e046609df534e12f202e2233a3ba20b1d1abc /src | |
parent | c513ad9a287ee1b5cc5e456c665792958649e2ed (diff) |
Updated cache handling to keep the root node and issue a warning if it didn't fit into the cache. Better debug output
Diffstat (limited to 'src')
-rw-r--r-- | src/CGALCache.cc | 17 | ||||
-rw-r--r-- | src/CGALCache.h | 4 | ||||
-rw-r--r-- | src/CGALEvaluator.cc | 20 | ||||
-rw-r--r-- | src/CGALEvaluator.h | 1 | ||||
-rw-r--r-- | src/cache.h | 3 |
5 files changed, 27 insertions, 18 deletions
diff --git a/src/CGALCache.cc b/src/CGALCache.cc index 9f308f0..8a92082 100644 --- a/src/CGALCache.cc +++ b/src/CGALCache.cc @@ -8,12 +8,23 @@ CGALCache::CGALCache(size_t limit) : cache(limit) { } -void CGALCache::insert(const std::string &id, const CGAL_Nef_polyhedron &N) +const CGAL_Nef_polyhedron &CGALCache::get(const std::string &id) const { - this->cache.insert(id, new CGAL_Nef_polyhedron(N), N.weight()); + const CGAL_Nef_polyhedron &N = *this->cache[id]; #ifdef DEBUG - PRINTF("CGAL Cache insert: %s (%d bytes)", id.substr(0, 40).c_str(), N.weight()); + PRINTF("CGAL Cache hit: %s (%d bytes)", id.substr(0, 40).c_str(), N.weight()); #endif + return N; +} + +bool CGALCache::insert(const std::string &id, const CGAL_Nef_polyhedron &N) +{ + bool inserted = this->cache.insert(id, new CGAL_Nef_polyhedron(N), N.weight()); +#ifdef DEBUG + if (inserted) PRINTF("CGAL Cache insert: %s (%d bytes)", id.substr(0, 40).c_str(), N.weight()); + else PRINTF("CGAL Cache insert failed: %s (%d bytes)", id.substr(0, 40).c_str(), N.weight()); +#endif + return inserted; } size_t CGALCache::maxSize() const diff --git a/src/CGALCache.h b/src/CGALCache.h index ee4c6c3..831ef27 100644 --- a/src/CGALCache.h +++ b/src/CGALCache.h @@ -13,8 +13,8 @@ public: static CGALCache *instance() { if (!inst) inst = new CGALCache; return inst; } bool contains(const std::string &id) const { return this->cache.contains(id); } - const class CGAL_Nef_polyhedron &get(const std::string &id) const { return *this->cache[id]; } - void insert(const std::string &id, const CGAL_Nef_polyhedron &N); + const class CGAL_Nef_polyhedron &get(const std::string &id) const; + bool insert(const std::string &id, const CGAL_Nef_polyhedron &N); size_t maxSize() const; void setMaxSize(size_t limit); void clear(); diff --git a/src/CGALEvaluator.cc b/src/CGALEvaluator.cc index c403e85..bbf8cca 100644 --- a/src/CGALEvaluator.cc +++ b/src/CGALEvaluator.cc @@ -42,16 +42,12 @@ CGAL_Nef_polyhedron CGALEvaluator::evaluateCGALMesh(const AbstractNode &node) if (!isCached(node)) { Traverser evaluate(*this, node, Traverser::PRE_AND_POSTFIX); evaluate.execute(); - // FIXME: If the root node didn't fit into the cache, the following assert - // will fail. We should handle this differently: - // 1) Return a NULL polyhedron, so the caller knows what happens - // 2) Return the polyhedron in a reference parameter and return a status code - // explicitly telling the caller what happened - // 3) Somehow ask the user to increase cache size and continue processing - assert(isCached(node)); + return this->root; assert(this->visitedchildren.empty()); } - return CGALCache::instance()->get(this->tree.getIdString(node)); + else { + return CGALCache::instance()->get(this->tree.getIdString(node)); + } } bool CGALEvaluator::isCached(const AbstractNode &node) const @@ -360,11 +356,11 @@ void CGALEvaluator::addToParent(const State &state, const AbstractNode &node, co else { // Root node, insert into cache if (!isCached(node)) { - CGALCache::instance()->insert(this->tree.getIdString(node), N); - // FIXME: If the root node didn't fit into the cache, the following assert - // will fail. See evaluateCGALMesh(const AbstractNode &node) - assert(isCached(node)); + if (!CGALCache::instance()->insert(this->tree.getIdString(node), N)) { + PRINTF("WARNING: CGAL Evaluator: Root node didn't fit into cache"); + } } + this->root = N; } } diff --git a/src/CGALEvaluator.h b/src/CGALEvaluator.h index 7330796..42af5a1 100644 --- a/src/CGALEvaluator.h +++ b/src/CGALEvaluator.h @@ -41,6 +41,7 @@ private: std::map<int, ChildList> visitedchildren; const Tree &tree; + CGAL_Nef_polyhedron root; public: // FIXME: Do we need to make this visible? Used for cache management // Note: psevaluator constructor needs this->tree to be initialized first diff --git a/src/cache.h b/src/cache.h index ae12647..27eb1f9 100644 --- a/src/cache.h +++ b/src/cache.h @@ -46,6 +46,7 @@ #define CACHE_H #include <boost/unordered_map.hpp> +#include <boost/format.hpp> #include "printutils.h" template <class Key, class T> @@ -177,7 +178,7 @@ void Cache<Key,T>::trim(int m) Node *u = n; n = n->p; #ifdef DEBUG - PRINTF("Trimming cache: %p", u->t); + PRINTF("Trimming cache: %s (%d bytes)", str(boost::format("%1%") % *u->keyPtr).c_str(), u->c); #endif unlink(*u); } |