TreeContainer.hpp 6.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

#include <array>
#include <functional>
#include <type_traits>
#include <utility>

#include <dune/common/indices.hh>
#include <dune/common/tuplevector.hh>

#include <dune/typetree/treepath.hh>

#include <amdis/common/Apply.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
14
#include <amdis/common/TypeTraits.hpp>
15
#include <amdis/typetree/TreePath.hpp>
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

// NOTE: backport of dune/typetree/treecontainer.hh

namespace AMDiS
{
  namespace Impl
  {
    /*
      * \brief A factory class creating a hybrid container compatible with a type tree
      *
      * This class allows to create a nested hybrid container having the same structure
      * as a given type tree. Power nodes are represented as std::array's while composite
      * nodes are represented as Dune::TupleVector's. The stored values for the leaf nodes
      * are creating using a given predicate. Once created, the factory provides an
      * operator() creating the container for the tree given as argument.
      *
      * \tparam LeafToValue Type of a predicate that determines the stored values at the leafs
      */
    template<class LeafToValue>
    class ContainerFactory
    {
    public:

      /**
        * \brief Create ContainerFactory
        *
        * The given predicate will be stored by value.
        *
        * \param A predicate used to generate the stored values for the leaves
        */
      ContainerFactory(LeafToValue leafToValue) :
        leafToValue_(leafToValue)
      {}

      template<class Node,
        std::enable_if_t<Node::isLeaf, int> = 0>
      auto operator()(const Node& node)
      {
        return leafToValue_(node);
      }

      template<class Node,
        std::enable_if_t<Node::isPower, int> = 0>
      auto operator()(const Node& node)
      {
        using TransformedChild = decltype((*this)(node.child(0)));
        return std::array<TransformedChild, Node::degree()>();
      }

      template<class Node,
        std::enable_if_t<Node::isComposite, int> = 0>
      auto operator()(const Node& node)
      {
69
70
71
        return Tools::apply_indices([&](auto... indices) {
          return Dune::makeTupleVector((*this)(node.child(indices))...);
          }, index_t<Node::degree()>{});
72
73
74
75
76
77
78
79
80
81
82
83
84
      }

    private:
      LeafToValue leafToValue_;
    };


    /*
      * \brief Wrap nested container to provide a VectorBackend
      */
    template<class Container>
    class TreeContainerVectorBackend
    {
85
86
      using Self = TreeContainerVectorBackend;

87
88
89
90
91
92
93
94
95
96
      template<class C>
      static constexpr decltype(auto) accessByTreePath(C&& container, const Dune::TypeTree::HybridTreePath<>& path)
      {
        return container;
      }

      template<class C, class... T>
      static constexpr decltype(auto) accessByTreePath(C&& container, const Dune::TypeTree::HybridTreePath<T...>& path)
      {
        auto head = Dune::TypeTree::treePathEntry(path,Dune::Indices::_0);
97
        return accessByTreePath(container[head], pop_front(path));
98
99
100
      }

    public:
101
      TreeContainerVectorBackend() = default;
102

103
104
      TreeContainerVectorBackend(Container&& container)
        : container_(std::move(container))
105
106
      {}

107
108
109
110
111
112
      TreeContainerVectorBackend(const Self&)  = default;
      TreeContainerVectorBackend(Self&&) = default;

      Self& operator=(const Self&) = default;
      Self& operator=(Self&&) = default;

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
      template<class... T>
      decltype(auto) operator[](const Dune::TypeTree::HybridTreePath<T...>&  path) const
      {
        return accessByTreePath(container_, path);
      }

      template<class... T>
      decltype(auto) operator[](const Dune::TypeTree::HybridTreePath<T...>&  path)
      {
        return accessByTreePath(container_, path);
      }

      const Container& data() const
      {
        return container_;
      }

      Container& data()
      {
        return container_;
      }

135
136
137
138
139
140
141
142
143
144
      bool operator==(TreeContainerVectorBackend const& other) const
      {
        return container_ == other.container_;
      }

      bool operator!=(TreeContainerVectorBackend const& other) const
      {
        return container_ != other.container_;
      }

145
146
147
148
149
150
151
    private:
      Container container_;
    };

    template<class Container>
    auto makeTreeContainerVectorBackend(Container&& container)
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
152
      return TreeContainerVectorBackend<remove_cvref_t<Container>>(FWD(container));
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    }

  } // end namespace Impl

    /** \addtogroup TypeTree
     *  \{
     */

    /**
     * \brief Create container havin the same structure as the given tree
     *
     * This class allows to create a nested hybrid container having the same structure
     * as a given type tree. Power nodes are represented as std::array's while composite
     * nodes are represented as Dune::TupleVector's. The stored values for the leaf nodes
     * are creating using a given predicate. For convenience the created container is
     * not returned directly. Instead, the returned object stores the container and
     * provides operator[] access using a HybridTreePath.
     *
     * \param tree The tree which should be mapper to a container
     * \param leafToValue A predicate used to generate the stored values for the leaves
     *
     * \returns A container matching the tree structure
     */
    template<class Tree, class LeafToValue>
    auto makeTreeContainer(const Tree& tree, LeafToValue&& leafToValue)
    {
      auto f = std::ref(leafToValue);
      auto factory = Impl::ContainerFactory<decltype(f)>(f);
      return Impl::makeTreeContainerVectorBackend(factory(tree));
    }

    /**
     * \brief Create container havin the same structure as the given tree
     *
     * This class allows to create a nested hybrid container having the same structure
     * as a given type tree. Power nodes are represented as std::array's while composite
     * nodes are represented as Dune::TupleVector's. The stored values for the leaf nodes
     * are of the given type Value. For convenience the created container is
     * not returned directly. Instead, the returned object stores the container and
     * provides operator[] access using a HybridTreePath.
     *
     * \tparam Value Type of the values to be stored for the leafs. Should be default constructible.
     * \param leafToValue A predicate used to generate the stored values for the leaves
     *
     * \returns A container matching the tree structure
     */
    template<class Value, class Tree>
    auto makeTreeContainer(const Tree& tree)
    {
      return makeTreeContainer(tree, [](const auto&) {return Value{};});
    }

    /**
     * \brief Alias to container type generated by makeTreeContainer for given value and tree type
     */
    template<class Value, class Tree>
Praetorius, Simon's avatar
Praetorius, Simon committed
209
    using TreeContainer = TYPEOF(makeTreeContainer<Value>(std::declval<const Tree&>()));
210
211
212
213

    //! \} group TypeTree

} //namespace AMDiS