Dune Core Modules (unstable)

testtypetreeutilities.hh
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
19template<class Payload>
20class Leaf
21{
22 Payload value_;
23public:
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
42template<class Payload, class C, std::size_t n>
43class UniformStaticInner
44{
45 Payload value_;
46 std::array<C, n> children_;
47public:
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
74template<class Payload, class C>
75class UniformDynamicInner
76{
77 Payload value_;
78 C prototype_;
79 std::vector<C> children_;
80public:
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
104template<class Payload, class... CC>
105class NonUniformInner
106{
107 Payload value_;
108 std::tuple<CC...> children_;
109public:
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
133template<class Tree>
134std::string treeName(const Tree& tree)
135{
136 using namespace std::string_literals;
137 auto name = std::string{};
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
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:52
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
void forEachNode(Tree &&tree, PreNodeFunc &&preNodeFunc, LeafNodeFunc &&leafNodeFunc, PostNodeFunc &&postNodeFunc)
Traverse tree and visit each node.
Definition: traversal.hh:133
decltype(auto) child(Node &&node, TreePath< Indices... > treePath)
Extracts the child of a node given by a TreePath object.
Definition: childaccess.hh:55
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jan 10, 23:35, 2026)