dune-typetree  2.3.1
accumulate_static.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_TYPETREE_ACCUMULATE_STATIC_HH
5 #define DUNE_TYPETREE_ACCUMULATE_STATIC_HH
6 
9 
10 
11 namespace Dune {
12  namespace TypeTree {
13 
19  template<typename result_type>
21  struct or_
22  {
23  template<result_type r1, result_type r2>
24  struct reduce
25  {
26  static const result_type result = r1 || r2;
27  };
28  };
29 
31  template<typename result_type>
32  struct and_
33  {
34  template<result_type r1, result_type r2>
35  struct reduce
36  {
37  static const result_type result = r1 && r2;
38  };
39  };
40 
42  template<typename result_type>
43  struct plus
44  {
45  template<result_type r1, result_type r2>
46  struct reduce
47  {
48  static const result_type result = r1 + r2;
49  };
50  };
51 
53  template<typename result_type>
54  struct minus
55  {
56  template<result_type r1, result_type r2>
57  struct reduce
58  {
59  static const result_type result = r1 - r2;
60  };
61  };
62 
64  template<typename result_type>
65  struct multiply
66  {
67  template<result_type r1, result_type r2>
68  struct reduce
69  {
70  static const result_type result = r1 * r2;
71  };
72  };
73 
75  template<typename result_type>
76  struct min
77  {
78  template<result_type r1, result_type r2>
79  struct reduce
80  {
81  static const result_type result = r1 < r2 ? r1 : r2;
82  };
83  };
84 
86  template<typename result_type>
87  struct max
88  {
89  template<result_type r1, result_type r2>
90  struct reduce
91  {
92  static const result_type result = r1 > r2 ? r1 : r2;
93  };
94  };
95 
96 
97  namespace {
98 
99  // implementation of the traversal algoritm
100 
102  template<typename Node, typename Functor, typename Reduction, typename Functor::result_type current_value, typename TreePath, bool doVisit>
103  struct accumulate_node_helper
104  {
105 
106  typedef typename Functor::result_type result_type;
107 
108  static const result_type result = current_value;
109 
110  };
111 
113  template<typename Node, typename Functor, typename Reduction, typename Functor::result_type current_value, typename TreePath>
114  struct accumulate_node_helper<Node,Functor,Reduction,current_value,TreePath,true>
115  {
116 
117  typedef typename Functor::result_type result_type;
118 
119  static const result_type result = Reduction::template reduce<current_value,Functor::template visit<Node,TreePath>::result>::result;
120 
121  };
122 
124  template<typename Tree, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath, typename Tag>
125  struct accumulate_value;
126 
128  template<typename LeafNode, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath>
129  struct accumulate_value<LeafNode,Functor,Reduction,ParentChildReduction,current_value,TreePath,LeafNodeTag>
130  {
131 
132  typedef typename Functor::result_type result_type;
133 
134  static const result_type result =
135 
137 
138  };
139 
141  template<typename Node, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath, std::size_t i, std::size_t n>
142  struct accumulate_over_children
143  {
144 
145  typedef typename Functor::result_type result_type;
146 
147  typedef typename TreePathPushBack<TreePath,i>::type child_tree_path;
148 
149  typedef typename Node::template Child<i>::Type child;
150 
152 
154 
155  };
156 
158  template<typename Node, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath, std::size_t n>
159  struct accumulate_over_children<Node,Functor,Reduction,ParentChildReduction,current_value,TreePath,n,n>
160  {
161 
162  typedef typename Functor::result_type result_type;
163 
164  static const result_type result = current_value;
165 
166  };
167 
170  template<typename Node, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath>
171  struct accumulate_value_generic_composite_node
172  {
173 
174  typedef typename Functor::result_type result_type;
175 
177 
178  static const result_type result =
180 
181 
182  };
183 
185  template<typename PowerNode, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath>
186  struct accumulate_value<PowerNode,Functor,Reduction,ParentChildReduction,current_value,TreePath,PowerNodeTag>
187  : public accumulate_value_generic_composite_node<PowerNode,Functor,Reduction,ParentChildReduction,current_value,TreePath>
188  {};
189 
191  template<typename CompositeNode, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath>
192  struct accumulate_value<CompositeNode,Functor,Reduction,ParentChildReduction,current_value,TreePath,CompositeNodeTag>
193  : public accumulate_value_generic_composite_node<CompositeNode,Functor,Reduction,ParentChildReduction,current_value,TreePath>
194  {};
195 
196 #if HAVE_VARIADIC_TEMPLATES
197 
199  template<typename VariadicCompositeNode, typename Functor, typename Reduction, typename ParentChildReduction, typename Functor::result_type current_value, typename TreePath>
200  struct accumulate_value<VariadicCompositeNode,Functor,Reduction,ParentChildReduction,current_value,TreePath,VariadicCompositeNodeTag>
201  : public accumulate_value_generic_composite_node<VariadicCompositeNode,Functor,Reduction,ParentChildReduction,current_value,TreePath>
202  {};
203 
204 #endif // HAVE_VARIADIC_TEMPLATES
205 
206  } // anonymous namespace
207 
209 
265  template<typename Tree, typename Functor, typename Reduction, typename Functor::result_type startValue, typename ParentChildReduction = Reduction>
267  {
268 
270  typedef typename Functor::result_type result_type;
271 
273  static const result_type result = accumulate_value<Tree,Functor,Reduction,ParentChildReduction,startValue,TreePath<>,typename Tree::NodeTag>::result;
274 
275  };
276 
279  struct flattened_reduction;
280 
283  struct bottom_up_reduction;
284 
285  namespace {
286 
287  // implementation of the traversal algoritm
288 
291  template<typename Node, typename Functor, typename Reduction, typename current_type, typename TreePath, bool doVisit>
292  struct accumulate_type_node_helper
293  {
294 
295  typedef current_type type;
296 
297  };
298 
300  template<typename Node, typename Functor, typename Reduction, typename current_type, typename TreePath>
301  struct accumulate_type_node_helper<Node,Functor,Reduction,current_type,TreePath,true>
302  {
303 
304  typedef typename Reduction::template reduce<
305  current_type,
306  typename Functor::template visit<
307  Node,
308  TreePath
309  >::type
310  >::type type;
311 
312  };
313 
315  template<typename Tree, typename Policy, typename current_type, typename TreePath, typename Tag>
316  struct accumulate_type;
317 
319  template<typename LeafNode, typename Policy, typename current_type, typename TreePath>
320  struct accumulate_type<LeafNode,Policy,current_type,TreePath,LeafNodeTag>
321  {
322 
323  typedef typename accumulate_type_node_helper<
324  LeafNode,
325  typename Policy::functor,
326  typename Policy::sibling_reduction,
327  current_type,
328  TreePath,
329  Policy::functor::template doVisit<
330  LeafNode,
331  TreePath>::value
332  >::type type;
333 
334  };
335 
336 
339  template<typename current_type, typename tree_path, typename start_type, typename reduction_strategy>
340  struct propagate_type_down_tree;
341 
343  template<typename current_type, typename tree_path, typename start_type>
344  struct propagate_type_down_tree<
345  current_type,
346  tree_path,
347  start_type,
348  flattened_reduction
349  >
350  {
351  typedef current_type type;
352  };
353 
355  template<typename current_type, typename tree_path, typename start_type>
356  struct propagate_type_down_tree<
357  current_type,
358  tree_path,
359  start_type,
360  bottom_up_reduction
361  >
362  {
363  typedef typename Dune::conditional<
365  start_type,
366  current_type
367  >::type type;
368  };
369 
370 
372  template<typename Node, typename Policy, typename current_type, typename TreePath, std::size_t i, std::size_t n>
373  struct accumulate_type_over_children
374  {
375 
376  typedef typename TreePathPushBack<TreePath,i>::type child_tree_path;
377 
378  typedef typename Node::template Child<i>::Type child;
379 
380  typedef typename accumulate_type<
381  child,
382  Policy,
383  // apply reduction choice (flat / hierarchic)
384  typename propagate_type_down_tree<
385  current_type,
386  child_tree_path,
387  typename Policy::start_type,
388  typename Policy::reduction_strategy
389  >::type,
390  child_tree_path,
391  typename child::NodeTag
392  >::type child_result_type;
393 
394  typedef typename accumulate_type_over_children<
395  Node,
396  Policy,
397  child_result_type,
398  TreePath,
399  i+1,
400  n
401  >::type type;
402 
403  };
404 
406  template<typename Node, typename Policy, typename current_type, typename TreePath, std::size_t n>
407  struct accumulate_type_over_children<Node,Policy,current_type,TreePath,n,n>
408  {
409 
410  typedef current_type type;
411 
412  };
413 
414 
417  template<typename Node, typename Policy, typename current_type, typename TreePath>
418  struct accumulate_type_generic_composite_node
419  {
420 
421  typedef typename accumulate_type_over_children<
422  Node,
423  Policy,
424  current_type,
425  TreePath,
426  0,
427  Node::CHILDREN
428  >::type children_result_type;
429 
430  typedef typename accumulate_type_node_helper<
431  Node,
432  typename Policy::functor,
433  typename Policy::parent_child_reduction,
434  children_result_type,
435  TreePath,
436  Policy::functor::template doVisit<
437  Node,
438  TreePath
439  >::value
440  >::type type;
441 
442  };
443 
445  template<typename PowerNode, typename Policy, typename current_type, typename TreePath>
446  struct accumulate_type<PowerNode,Policy,current_type,TreePath,PowerNodeTag>
447  : public accumulate_type_generic_composite_node<PowerNode,Policy,current_type,TreePath>
448  {};
449 
451  template<typename CompositeNode, typename Policy, typename current_type, typename TreePath>
452  struct accumulate_type<CompositeNode,Policy,current_type,TreePath,CompositeNodeTag>
453  : public accumulate_type_generic_composite_node<CompositeNode,Policy,current_type,TreePath>
454  {};
455 
456 #if HAVE_VARIADIC_TEMPLATES
457 
459  template<typename VariadicCompositeNode, typename Policy, typename current_type, typename TreePath>
460  struct accumulate_type<VariadicCompositeNode,Policy,current_type,TreePath,VariadicCompositeNodeTag>
461  : public accumulate_type_generic_composite_node<VariadicCompositeNode,Policy,current_type,TreePath>
462  {};
463 
464 #endif // HAVE_VARIADIC_TEMPLATES
465 
466  } // anonymous namespace
467 
468 
476  template<
477  typename Functor,
478  typename Reduction,
479  typename StartType,
480  typename ParentChildReduction = Reduction,
481  typename ReductionAlgorithm = flattened_reduction
482  >
484  {
485 
513  typedef Functor functor;
514 
534  typedef Reduction sibling_reduction;
535 
542  typedef ParentChildReduction parent_child_reduction;
543 
550  typedef StartType start_type;
551 
556  typedef ReductionAlgorithm reduction_strategy;
557  };
558 
559 
561 
569  template<typename Tree, typename Policy>
571  {
572 
574  typedef typename accumulate_type<
575  Tree,
576  Policy,
577  typename Policy::start_type,
578  TreePath<>,
579  typename Tree::NodeTag
581 
582  };
583 
584 
586 
587  } // namespace TypeTree
588 } //namespace Dune
589 
590 #endif // DUNE_TYPETREE_ACCUMULATE_STATIC_HH
Definition: accumulate_static.hh:46
Definition: treepath.hh:32
Statically combine two values of type result_type using ||.
Definition: accumulate_static.hh:21
Statically accumulate a type over the nodes of a TypeTree.
Definition: accumulate_static.hh:570
static const result_type result
Definition: accumulate_static.hh:48
static const result_type result
Definition: accumulate_static.hh:70
Definition: accumulate_static.hh:35
Definition: accumulate_static.hh:90
static const result_type result
Definition: accumulate_static.hh:37
static const result_type result
Definition: accumulate_static.hh:92
Functor functor
Definition: accumulate_static.hh:513
static const result_type result
Definition: accumulate_static.hh:81
static const result_type result
Definition: accumulate_static.hh:108
Statically combine two values of type result_type by returning their maximum.
Definition: accumulate_static.hh:87
ReductionAlgorithm reduction_strategy
Definition: accumulate_static.hh:556
Definition: accumulate_static.hh:57
ParentChildReduction parent_child_reduction
Definition: accumulate_static.hh:542
accumulate_type< Tree, Policy, typename Policy::start_type, TreePath<>, typename Tree::NodeTag >::type type
The accumulated result of the computation.
Definition: accumulate_static.hh:580
Statically combine two values of type result_type using -.
Definition: accumulate_static.hh:54
StartType start_type
Definition: accumulate_static.hh:550
Definition: accumulate_static.hh:483
Definition: accumulate_static.hh:24
Statically combine two values of type result_type using +.
Definition: accumulate_static.hh:43
Reduction sibling_reduction
Definition: accumulate_static.hh:534
Definition: accumulate_static.hh:68
Statically combine two values of type result_type using &&.
Definition: accumulate_static.hh:32
static const std::size_t value
Definition: compositenode.hh:38
static const result_type result
The accumulated result of the computation.
Definition: accumulate_static.hh:273
Functor::result_type result_type
The result type of the computation.
Definition: accumulate_static.hh:270
Statically combine two values of type result_type using *.
Definition: accumulate_static.hh:65
Statically combine two values of type result_type by returning their minimum.
Definition: accumulate_static.hh:76
static const result_type child_result
Definition: accumulate_static.hh:151
static const result_type result
Definition: accumulate_static.hh:59
Statically accumulate a value over the nodes of a TypeTree.
Definition: accumulate_static.hh:266
Type
Definition: treepath.hh:26
static const result_type result
Definition: accumulate_static.hh:26
Definition: accumulate_static.hh:79