Debian Codegraph @

dune/common/typetree/test/testtypetreeutilities.hh

raw
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
5
6 #ifndef DUNE_COMMON_TYPETREE_TEST_TESTTYPETREEUTILITIES_HH
7 #define DUNE_COMMON_TYPETREE_TEST_TESTTYPETREEUTILITIES_HH
8
9 #include <array>
10 #include <string>
11 #include <tuple>
12 #include <vector>
13
14 #include <dune/common/indices.hh>
15 #include <dune/common/typetree/traversal.hh>
16
17
18 // Leaf node
19 template<class Payload>
20 class Leaf
21 {
22 Payload value_;
23 public:
24
25 Leaf() = default;
26
27 Leaf(Payload value)
28 : value_(value)
29 {}
30
31 // For testing
32 int value() const { return value_; };
33 int& value() { return value_; };
34 auto name() const { return std::string("Leaf<") + std::to_string(value()) + std::string(">"); }
35
36 // Node interface
37 static constexpr auto degree() { return Dune::Indices::_0; }
38 };
39
40
41 // Uniform inner node with static degree
42 template<class Payload, class C, std::size_t n>
43 class UniformStaticInner
44 {
45 Payload value_;
46 std::array<C, n> children_;
47 public:
48
49 UniformStaticInner() = default;
50
51 UniformStaticInner(Payload value, C c)
52 : value_(value)
53 {
54 for(std::size_t i=0; i<n; ++i)
55 children_[i] = c;
56 }
57
58 UniformStaticInner(Payload value, C c, Dune::index_constant<n>)
59 : UniformStaticInner(value, c)
60 {}
61
62 // For testing
63 int value() const { return value_; };
64 int& value() { return value_; };
65 auto name() const { return std::string("UniformStaticInner"); }
66
67 // Node interface
68 auto degree() const { return n; }
69 auto& child(std::size_t i) { return children_[i]; };
70 const auto& child(std::size_t i) const { return children_[i]; };
71 };
72
73 // Uniform inner node with dynamic degree
74 template<class Payload, class C>
75 class UniformDynamicInner
76 {
77 Payload value_;
78 C prototype_;
79 std::vector<C> children_;
80 public:
81
82 UniformDynamicInner() = default;
83
84 UniformDynamicInner(Payload value, C c, std::size_t n)
85 : value_(value)
86 , prototype_(c)
87 {
88 resize(n);
89 }
90
91 // For testing
92 int value() const { return value_; };
93 int& value() { return value_; };
94 auto name() const { return std::string("UniformDynamicInner"); }
95 void resize(std::size_t n) { children_.resize(n, prototype_); }
96
97 // Node interface
98 auto degree() const { return children_.size(); }
99 auto& child(std::size_t i) { return children_[i]; };
100 const auto& child(std::size_t i) const { return children_[i]; };
101 };
102
103 // Inner node with static degree
104 template<class Payload, class... CC>
105 class NonUniformInner
106 {
107 Payload value_;
108 std::tuple<CC...> children_;
109 public:
110
111 NonUniformInner() = default;
112
113 NonUniformInner(Payload value, CC... cc)
114 : value_(value)
115 , children_{std::move(cc)...}
116 {}
117
118 // For testing
119 int value() const { return value_; };
120 int& value() { return value_; };
121 auto name() const { return std::string("NonUniformInner"); }
122
123 // Node interface
124 static constexpr auto degree() { return Dune::index_constant<sizeof...(CC)>{}; }
125 template<std::size_t i>
126 auto& child(Dune::index_constant<i>) { return std::get<i>(children_); }
127 template<std::size_t i>
128 const auto& child(Dune::index_constant<i>) const { return std::get<i>(children_); }
129 };
130
131
132
133 template<class Tree>
134 std::string treeName(const Tree& tree)
135 {
136 using namespace std::string_literals;
137 auto name = std::string{};
138 Dune::TypeTree::forEachNode(tree,
139 [&](auto&& node) { name += node.name() + "<"s; },
140 [&](auto&& node) { name += node.name() + ","s; },
141 [&](auto&& node) { name += ">"s; }
142 );
143 return name;
144 }
145
146 #endif // DUNE_COMMON_TYPETREE_TEST_TESTTYPETREEUTILITIES_HH