Schnek
dependencies.hpp
1 /*
2  * dependencies.hpp
3  *
4  * Created on: 10 Jul 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_DEPENDENCIES_HPP_
28 #define SCHNEK_DEPENDENCIES_HPP_
29 
30 #include "variables.hpp"
31 #include "blockparameters.hpp"
32 
33 #include <boost/variant.hpp>
34 #include <boost/shared_ptr.hpp>
35 #include <boost/foreach.hpp>
36 
37 #include <map>
38 #include <set>
39 #include <list>
40 
41 namespace schnek {
42 
43 class DependencyUpdater;
44 
50 {
51  private:
52  typedef std::set<long> DependencySet;
53  struct VarInfo
54  {
55  pVariable v;
56  DependencySet dependsOn;
57  DependencySet modifies;
58  int counter;
59  VarInfo() {}
60  VarInfo(pVariable v_, DependencySet dependsOn_, DependencySet modifies_)
61  : v(v_), dependsOn(dependsOn_), modifies(modifies_), counter(0) {
62  assert(v);
63  }
64  };
65 
66  typedef std::map<long, VarInfo> DepMap;
67 
69  typedef std::map<long, VarInfo*> RefDepMap;
70  typedef boost::shared_ptr<RefDepMap> pRefDepMap;
71 
72  typedef std::set<pVariable> VariableSet;
73  typedef std::list<pVariable> VariableList;
74 
75  DepMap dependencies;
76  pBlockVariables blockVars;
77 
78  pVariable dummyVar;
79 
80  friend class DependencyUpdater;
81 
82  void constructMapRecursive(const pBlockVariables vars);
83  void constructMap(const pBlockVariables vars);
84  void resetCounters();
85 
86  void makeUpdateList(const VariableSet &independentVars, const VariableSet &dependentVars, VariableList &updateList);
87  pRefDepMap makeUpdatePredecessors(const VariableSet &independentVars, const VariableSet &dependentVars);
88  pRefDepMap makeUpdateFollowers(const VariableSet &independentVars, pRefDepMap reverseDeps);
89  void makeUpdateOrder(pRefDepMap deps, VariableList &updateList);
90 
91  public:
92  DependencyMap(const pBlockVariables vars);
93  void recreate() { constructMap(blockVars); }
94  pBlockVariables getBlockVariables();
95  void updateAll();
96 
97 // bool hasRoots(pVariable v, pParametersGroup roots);
98 };
99 
100 typedef boost::shared_ptr<DependencyMap> pDependencyMap;
101 
103 {
104  private:
105  typedef std::set<pParameter> ParameterSet;
106  typedef std::set<pVariable> VariableSet;
107  typedef std::list<pVariable> VariableList;
108 
109  VariableList updateList;
110  VariableSet independentVars;
111  VariableSet dependentVars;
112  ParameterSet dependentParameters;
113 
114  pDependencyMap dependencies;
115  bool isValid;
116  public:
117  DependencyUpdater(pDependencyMap dependencies_);
118  void addIndependent(pParameter v);
119  void addDependent(pParameter v);
120  void clearDependent();
121 
122  template<int rank, template<int> class CheckingPolicy>
123  void addIndependentArray(Array<pParameter, rank, CheckingPolicy> v)
124  { for (int i=0; i<rank; ++i) addIndependent(v[i]); }
125 
126  template<int rank, template<int> class CheckingPolicy>
127  void addDependentArray(Array<pParameter, rank, CheckingPolicy> v)
128  { for (int i=0; i<rank; ++i) addDependent(v[i]); }
129 
134  void update()
135  {
136  if (!isValid) {
137  dependencies->makeUpdateList(independentVars, dependentVars, updateList);
138  isValid = true;
139  }
140  BOOST_FOREACH(pVariable v, updateList) v->evaluateExpression();
141  BOOST_FOREACH(pParameter p, dependentParameters) p->update();
142  }
143 };
144 
145 typedef boost::shared_ptr<DependencyUpdater> pDependencyUpdater;
146 
147 } // namespace
148 
149 
150 #endif // SCHNEK_DEPENDENCIES_HPP_
Definition: dependencies.hpp:49
Definition: array.hpp:49
Definition: algo.hpp:30
void update()
Definition: dependencies.hpp:134
Definition: dependencies.hpp:102