From 721f0d7907294ef19e7346fbf0308ed5cc568e2d Mon Sep 17 00:00:00 2001
From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be>
Date: Wed, 24 Mar 2010 10:09:22 +0000
Subject: [PATCH] add dataCacheMap::parent

---
 Solver/function.cpp | 28 +++++++++++++++++++++++++---
 Solver/function.h   | 18 ++++++++++++++----
 2 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/Solver/function.cpp b/Solver/function.cpp
index fbb9a6df45..4995c02fa7 100644
--- a/Solver/function.cpp
+++ b/Solver/function.cpp
@@ -65,14 +65,33 @@ void dataCacheDouble::resize() {
 //dataCacheMap members
 dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) 
 {
-  dataCacheDouble *&r= _cacheDoubleMap[f];
-  if(r==NULL)
+  dataCacheDouble *&r = _cacheDoubleMap[f];
+  if (r==NULL && _parent) {
+    std::map<const function *, dataCacheDouble *>::iterator it = _parent->_cacheDoubleMap.find(f);
+    if (it != _parent->_cacheDoubleMap.end()) {
+      r = it->second;
+      for (std::set<dataCacheDouble*>::iterator dep = r->_iDependOn.begin(); dep != r->_iDependOn.end(); dep++) {
+        if (&(*dep)->_cacheMap == this) {
+          r = NULL;
+          break;
+        }
+      }
+    }
+  }
+  if (r==NULL)
     r = new dataCacheDouble(this, const_cast<function*>(f));
-  if(caller)
+  if (caller)
     r->addMeAsDependencyOf(caller);
   return *r;
 }
 
+dataCacheDouble &dataCacheMap::substitute(const function *f) 
+{
+  dataCacheDouble *&r= _cacheDoubleMap[f];
+  r = new dataCacheDouble(this, const_cast<function*>(f));
+  return *r;
+}
+
 dataCacheMap::~dataCacheMap()
 {
   for (std::set<dataCacheDouble*>::iterator it = _toDelete.begin();
@@ -333,6 +352,9 @@ void dataCacheMap::setNbEvaluationPoints(int nbEvaluationPoints) {
     (*it)->resize();
     (*it)->_valid = false;
   }
+    for(std::list<dataCacheMap*>::iterator it = _children.begin(); it!= _children.end(); it++) {
+      (*it)->setNbEvaluationPoints(nbEvaluationPoints);
+    }
 }
 
 //functionC
diff --git a/Solver/function.h b/Solver/function.h
index fd7b39a79c..3f5f252fe8 100644
--- a/Solver/function.h
+++ b/Solver/function.h
@@ -4,6 +4,7 @@
 #include "fullMatrix.h"
 #include <map>
 #include <set>
+#include <list>
 #include <string>
 #include <vector>
 class dataCacheMap;
@@ -143,10 +144,9 @@ public :
 // more explanation at the head of this file
 class dataCacheMap {
   friend class dataCacheDouble;
- private:
+  dataCacheMap  *_parent;
+  std::list<dataCacheMap*> _children;
   int _nbEvaluationPoints;
-  // keep track of the current element and all the dataCaches that
-  // depend on it
   std::map<const function*, dataCacheDouble*> _cacheDoubleMap;
   std::set<dataCacheDouble*> _toDelete;
   std::set<dataCacheDouble*> _toResize;
@@ -165,14 +165,24 @@ class dataCacheMap {
   }
  public:
   dataCacheDouble &get(const function *f, dataCacheDouble *caller=0);
+  dataCacheDouble &substitute(const function *f);
   inline void setElement(MElement *element) {
     _element=element;
     for(std::set<dataCacheDouble*>::iterator it = _toInvalidateOnElement.begin(); it!= _toInvalidateOnElement.end(); it++) {
       (*it)->_valid=false;
     }
+    for(std::list<dataCacheMap*>::iterator it = _children.begin(); it!= _children.end(); it++) {
+      (*it)->setElement(element);
+    }
   }
   inline MElement *getElement() {return _element;}
-  dataCacheMap(){
+  dataCacheMap() {
+    _nbEvaluationPoints = 0;
+    _parent=NULL;
+  }
+  dataCacheMap(dataCacheMap *parent) {
+    _parent = parent;
+    _parent->_children.push_back(this);
     _nbEvaluationPoints = 0;
   }
   void setNbEvaluationPoints(int nbEvaluationPoints);
-- 
GitLab