AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
TreePath.hpp
1 #pragma once
2 
3 #include <cassert>
4 #include <sstream>
5 #include <string>
6 #include <type_traits>
7 
8 #include <dune/common/version.hh>
9 #include <dune/common/std/apply.hh>
10 #include <dune/typetree/treepath.hh>
11 #include <dune/typetree/typetraits.hh>
12 
13 #include <amdis/common/Apply.hpp>
14 #include <amdis/common/Literals.hpp>
15 #include <amdis/common/Logical.hpp>
16 
17 namespace AMDiS {
18 
19 using RootTreePath = Dune::TypeTree::HybridTreePath<>;
20 
21 namespace Concepts {
22 
27 namespace Definition
28 {
29  template <class Index>
31  : std::is_integral<Index>
32  {};
33 
34  template <class Index, Index I>
35  struct IsPreTreePath<std::integral_constant<Index, I>>
36  : std::is_integral<Index>
37  {};
38 
39  template <class Index, Index... I>
40  struct IsPreTreePath<std::integer_sequence<Index, I...>>
41  : std::is_integral<Index>
42  {};
43 
44  template <class... Indices>
45  struct IsPreTreePath<std::tuple<Indices...>>
46  : std::conjunction<std::is_integral<Indices>...>
47  {};
48 
49 } // end namespace Definition
50 
51 template <class TP>
52 constexpr bool PreTreePath = Dune::TypeTree::IsTreePath<TP>::value || Definition::IsPreTreePath<TP>::value;
53 
54 template <class TP>
55 using PreTreePath_t = bool_t<PreTreePath<TP>>;
56 
59 } // end namespace Concepts
60 
61 namespace Impl
62 {
63  template <class Index,
64  std::enable_if_t<std::is_integral_v<Index>, int> = 0>
65  std::size_t treePathIndex(Index i)
66  {
67  return std::size_t(i);
68  }
69 
70  template <class Index, Index i,
71  std::enable_if_t<std::is_integral_v<Index>, int> = 0>
72  auto treePathIndex(std::integral_constant<Index,i>)
73  {
74  return std::integral_constant<std::size_t,std::size_t(i)>{};
75  }
76 
77 } // end namespace Impl
78 
79 #ifdef DOXYGEN
80 
82 
103 template <class PreTreePath>
104 auto makeTreePath(PreTreePath tp);
105 
106 #else // DOXYGEN
107 
108 template <class... Indices>
109 auto makeTreePath(Indices... ii)
110  -> decltype( Dune::TypeTree::hybridTreePath(Impl::treePathIndex(ii)...) )
111 {
112  return Dune::TypeTree::hybridTreePath(Impl::treePathIndex(ii)...);
113 }
114 
115 inline auto makeTreePath()
116 {
117  return Dune::TypeTree::hybridTreePath();
118 }
119 
120 template <class Index, Index... I>
121 auto makeTreePath(std::integer_sequence<Index, I...>)
122 {
123  return makeTreePath(std::integral_constant<std::size_t, std::size_t(I)>{}...);
124 }
125 
126 template <class... T>
127 auto makeTreePath(std::tuple<T...> const& tp)
128 {
129  return std::apply([](auto... ii) { return makeTreePath(ii...); }, tp);
130 }
131 
132 template <class... T>
133 auto const& makeTreePath(Dune::TypeTree::HybridTreePath<T...> const& tp)
134 {
135  return tp;
136 }
137 
138 template <std::size_t... I>
139 auto makeTreePath(Dune::TypeTree::TreePath<I...>)
140 {
141  return Dune::TypeTree::hybridTreePath(std::integral_constant<std::size_t, I>{}...);
142 }
143 
144 #endif // DOXYGEN
145 
146 
148 template <char... digits>
149 constexpr auto operator"" _tp()
150 {
151  return Dune::TypeTree::hybridTreePath(std::integral_constant<std::size_t,Impl::char2digit(digits)>{}...);
152 }
153 
154 
155 // convert a treepath into a string, comma-separated
156 template <class... T>
157 std::string to_string(Dune::TypeTree::HybridTreePath<T...> const& tp)
158 {
159  auto entry = [&](auto i) { return std::to_string(std::size_t(Dune::TypeTree::treePathEntry<i>(tp))); };
160  auto first = [&] { return entry(std::integral_constant<std::size_t,0>()); };
161 
162  return Ranges::applyIndices<1,sizeof...(T)>([&](auto... i) -> std::string {
163  return (first() +...+ ("," + entry(i)));
164  });
165 }
166 
167 inline std::string to_string(Dune::TypeTree::HybridTreePath<> const&)
168 {
169  return "";
170 }
171 
172 // convert a treepath into an array
173 template <class... T>
174 auto to_array(Dune::TypeTree::HybridTreePath<T...> const& tp)
175 {
176  return Ranges::applyIndices<sizeof...(T)>([&](auto... i) {
177  return std::array<std::size_t,sizeof...(T)>{std::size_t(Dune::TypeTree::treePathEntry<i>(tp))...};
178  });
179 }
180 
182 template <class T0, class... T>
183 auto pop_front(Dune::TypeTree::HybridTreePath<T0,T...> const& tp)
184 {
185  return Ranges::applyIndices<sizeof...(T)>([&](auto... i) {
186  return Dune::TypeTree::hybridTreePath(Dune::TypeTree::treePathEntry<i+1>(tp)...);
187  });
188 }
189 
191 template <class... T, class TN>
192 auto pop_back(Dune::TypeTree::HybridTreePath<T...,TN> const& tp)
193 {
194  return Ranges::applyIndices<sizeof...(T)>([&](auto... i) {
195  return Dune::TypeTree::hybridTreePath(Dune::TypeTree::treePathEntry<i>(tp)...);
196  });
197 }
198 
200 template <class... S, class... T>
201 auto cat(Dune::TypeTree::HybridTreePath<S...> const& tp0, Dune::TypeTree::HybridTreePath<T...> const& tp1)
202 {
203  return Dune::TypeTree::HybridTreePath<S...,T...>(std::tuple_cat(tp0._data,tp1._data));
204 }
205 
206 } // end namespace AMDiS
Definition: FieldMatVec.hpp:12
Definition: AdaptBase.hpp:6