dune-functions  2.6-dev
defaultnodetorangemap.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 #ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_DEFAULTNODETORANGEMAP_HH
4 #define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_DEFAULTNODETORANGEMAP_HH
5 
6 
7 #include <dune/common/concept.hh>
8 
10 
11 #include <dune/typetree/traversal.hh>
12 #include <dune/typetree/visitor.hh>
13 
14 
15 namespace Dune {
16 namespace Functions {
17 
18 
19 
37 template<class Tree>
39 {
40 
41  // A simple visitor for computing lexicographic
42  // subtree indices. To identify a leaf node
43  // we use its treeIndex() which is unique
44  // wrt the whole tree and store the computed
45  // index in a vector indexed by the tree indices.
46  struct Visitor
47  : public TypeTree::TreeVisitor
48  , public TypeTree::DynamicTraversal
49  {
50  Visitor(std::vector<std::size_t>& indices) :
51  indices_(indices),
52  counter_(0)
53  {}
54 
55  template<typename Node, typename TreePath>
56  void leaf(Node& node, TreePath treePath)
57  {
58  if (indices_.size() < node.treeIndex()+1)
59  indices_.resize(node.treeIndex()+1);
60  indices_[node.treeIndex()] = counter_;
61  ++counter_;
62  }
63 
64  std::vector<std::size_t>& indices_;
65  std::size_t counter_;
66  };
67 
78  DefaultNodeToRangeMap(const Tree& tree)
79  {
80  TypeTree::applyToTree(tree, Visitor(indices_));
81  }
82 
83  template<class Node, class Range,
84  typename std::enable_if<
85  models<Concept::HasIndexAccess, Range, decltype(std::declval<Node>().treeIndex())>() and not Tree::isLeaf, int>::type = 0>
86  auto operator()(const Node& node, Range&& y) const
87  -> decltype(y[0])
88  {
89  return y[indices_[node.treeIndex()]];
90  }
91 
92  template<class Node, class Range,
93  typename std::enable_if< not models<Concept::HasIndexAccess, Range, decltype(std::declval<Node>().treeIndex())>() or Tree::isLeaf, int>::type = 0>
94  auto operator()(const Node& node, Range&& y) const
95  -> decltype(std::forward<Range>(y))
96  {
97  return std::forward<Range>(y);
98  }
99 
100  std::vector<std::size_t> indices_;
101 };
102 
103 
104 
105 template<class Tree>
107 {
108  return DefaultNodeToRangeMap<Tree>(tree);
109 }
110 
111 
112 
113 template<class Basis, class TreePath>
114 auto makeDefaultNodeToRangeMap(const Basis& basis, TreePath&& treePath)
115  -> decltype(makeDefaultNodeToRangeMap(TypeTree::child(basis.localView().tree(),treePath)))
116 {
117  auto&& localView = basis.localView();
118  localView.bind(*basis.gridView().template begin<0>());
119  auto&& tree = TypeTree::child(localView.tree(),treePath);
120  return makeDefaultNodeToRangeMap(tree);
121 }
122 
123 
124 
125 } // namespace Dune::Functions
126 } // namespace Dune
127 
128 
129 #endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_DEFAULTNODETORANGEMAP_HH
void leaf(Node &node, TreePath treePath)
Definition: defaultnodetorangemap.hh:56
A simple node to range map using lexicographic ordering.
Definition: defaultnodetorangemap.hh:38
auto operator()(const Node &node, Range &&y) const -> decltype(y[0])
Definition: defaultnodetorangemap.hh:86
std::size_t counter_
Definition: defaultnodetorangemap.hh:65
DefaultNodeToRangeMap(const Tree &tree)
Construct DefaultNodeToRangeMap.
Definition: defaultnodetorangemap.hh:78
DefaultNodeToRangeMap< Tree > makeDefaultNodeToRangeMap(const Tree &tree)
Definition: defaultnodetorangemap.hh:106
std::vector< std::size_t > & indices_
Definition: defaultnodetorangemap.hh:64
auto operator()(const Node &node, Range &&y) const -> decltype(std::forward< Range >(y))
Definition: defaultnodetorangemap.hh:94
Definition: defaultnodetorangemap.hh:46
Visitor(std::vector< std::size_t > &indices)
Definition: defaultnodetorangemap.hh:50
Definition: polynomial.hh:7
std::vector< std::size_t > indices_
Definition: defaultnodetorangemap.hh:100