From 44458a86ae90c659bd8d2c52808733d7409b165f Mon Sep 17 00:00:00 2001
From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be>
Date: Wed, 24 Mar 2010 10:09:18 +0000
Subject: [PATCH] merge function::data into dataCacheDouble

---
 Solver/function.cpp | 43 +++++++++++-----------------------
 Solver/function.h   | 57 +++++++++++++++++++++++++--------------------
 2 files changed, 46 insertions(+), 54 deletions(-)

diff --git a/Solver/function.cpp b/Solver/function.cpp
index 6df8a03ebc..8c66c89b4f 100644
--- a/Solver/function.cpp
+++ b/Solver/function.cpp
@@ -24,35 +24,7 @@ void function::call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const
     default : Msg::Error("function are not implemented for %i arguments\n", dep.size());
   }
 }
-class function::data : public dataCacheDouble {
-  function *_function;
-  dataCacheMap *_m;
-  std::vector<dataCacheDouble *> _dependencies;
-  std::vector<const fullMatrix<double> *> _depM;
-  public:
-  data(function *f, dataCacheMap *m):
-    dataCacheDouble(*m,1,f->_nbCol)
-  {
-    _function = f;
-    _m = m;
-    _dependencies.resize ( _function->dep.size());
-    _depM.resize (_function->dep.size());
-    for (int i=0;i<_function->dep.size();i++)
-      _dependencies[i] = &m->get(_function->dep[i],this);
-  }
-  void _eval()
-  {
-    for(int i=0;i<_dependencies.size(); i++)
-      _depM[i] = &(*_dependencies[i])();
-    _function->call(_m, _value, _depM);
-  }
-};
 function::function(int nbCol):_nbCol(nbCol){};
-dataCacheDouble *function::newDataCache(dataCacheMap *m)
-{
-  return new data(this, m);
-}
-
 
 void dataCacheDouble::addMeAsDependencyOf (dataCacheDouble *newDep)
 {
@@ -78,7 +50,7 @@ dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller)
 {
   dataCacheDouble *&r= _cacheDoubleMap[f];
   if(r==NULL)
-    r = const_cast<function*>(f)->newDataCache(this);
+    r = new dataCacheDouble(this, const_cast<function*>(f));
   return returnDataCacheDouble(r,caller);
 }
 dataCacheDouble &dataCacheMap::getSolution(dataCacheDouble *caller) 
@@ -337,6 +309,19 @@ dataCacheDouble::dataCacheDouble(dataCacheMap &map,int nRowByPoint, int nCol):
     _nRowByPoint=nRowByPoint;
     map.addDataCacheDouble(this);
 };
+
+dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f):
+  _cacheMap(*m),_value(m->getNbEvaluationPoints(),f->getNbCol())
+{
+  _nRowByPoint=1;
+  m->addDataCacheDouble(this);
+  _function = f;
+  _dependencies.resize ( _function->dep.size());
+  _depM.resize (_function->dep.size());
+  for (int i=0;i<_function->dep.size();i++)
+    _dependencies[i] = &m->get(_function->dep[i],this);
+}
+
 void dataCacheDouble::resize() {
   _value = fullMatrix<double>(_nRowByPoint==0?1:_nRowByPoint*_cacheMap.getNbEvaluationPoints(),_value.size2());
 }
diff --git a/Solver/function.h b/Solver/function.h
index 174eb03de1..37ff1c7553 100644
--- a/Solver/function.h
+++ b/Solver/function.h
@@ -29,6 +29,28 @@ class dgDofContainer;
 // a node in the dependency tree. The usefull field is _dependOnMe which is the set of every other nodes that depend on me. When the value of this node change all nodes depending on this one are marked as "invalid" and will be recomputed the next time their data are accessed. To be able to maintain _dependOnMe up to date when a new node is inserted in the tree, we need _iDependOn list. So we do not really store a tree but instead each node contain a complete list of all it's parents and all it's children (and the parents of the parents of ... of its parents and the children of the children of ... of it's children). This way invalidate all the dependencies of a node is really fast and does not involve a complex walk accross the tree structure.
 
 class function;
+class dataCacheDouble;
+
+// An abstract interface to functions 
+// more explanation at the head of this file
+class function {
+  int _nbCol;
+  protected :
+  virtual void call (dataCacheMap *m, fullMatrix<double> &res) {throw;}
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &res) {throw;};
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &res) {throw;};
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, fullMatrix<double> &res) {throw;};
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, fullMatrix<double> &res) {throw;};
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, fullMatrix<double> &res) {throw;};
+  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, const fullMatrix<double> &arg5, fullMatrix<double> &res) {throw;};
+  public :
+  std::vector<const function*> dep;
+  virtual ~function(){};
+  static void registerBindings(binding *b);
+  virtual void call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const fullMatrix<double>*> &depM);
+  function(int nbCol);
+  inline int getNbCol()const {return _nbCol;}
+};
 
 // dataCache when the value is a  matrix of double 
 // the user should provide the number of rows by evaluating points and the number of columns
@@ -61,16 +83,21 @@ public :
     return _iDependOn.size();
   }
 
-
-
-
+  std::vector<dataCacheDouble*> _dependencies;
+  std::vector<const fullMatrix<double>*> _depM;
 
   int _nRowByPoint;
   dataCacheMap &_cacheMap;
+  function *_function;
  protected:
   fullMatrix<double> _value;
   // do the actual computation and put the result into _value
-  virtual void _eval()=0;
+  virtual void _eval()
+  {
+    for(int i=0;i<_dependencies.size(); i++)
+      _depM[i] = &(*_dependencies[i])();
+    _function->call(&_cacheMap, _value, _depM);
+  }
  public:
   //set the value (without computing it by _eval) and invalidate the dependencies
   inline void set(const fullMatrix<double> &mat) {
@@ -123,30 +150,10 @@ public :
   }
   void resize();
   dataCacheDouble(dataCacheMap &map,int nRowByPoints, int nCol);
+  dataCacheDouble(dataCacheMap *,function *f);
   virtual ~dataCacheDouble(){};
 };
 
-// An abstract interface to functions 
-// more explanation at the head of this file
-class function {
-  int _nbCol;
-  protected :
-  std::vector<const function*> dep;
-  virtual void call (dataCacheMap *m, fullMatrix<double> &res) {throw;}
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &res) {throw;};
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &res) {throw;};
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, fullMatrix<double> &res) {throw;};
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, fullMatrix<double> &res) {throw;};
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, fullMatrix<double> &res) {throw;};
-  virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, const fullMatrix<double> &arg5, fullMatrix<double> &res) {throw;};
-  public :
-  virtual ~function(){};
-  static void registerBindings(binding *b);
-  virtual void call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const fullMatrix<double>*> &depM);
-  class data;
-  function(int nbCol);
-  dataCacheDouble *newDataCache(dataCacheMap *m);
-};
 
 // more explanation at the head of this file
 class dataCacheMap {
-- 
GitLab