DUNE-FUNCTIONS (unstable)

utility.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_COMMON_UTILITY_HH
8#define DUNE_FUNCTIONS_COMMON_UTILITY_HH
9
10
11#include <utility>
12#include <type_traits>
13
14#include <dune/common/overloadset.hh>
15#include <dune/common/indices.hh>
16
17#include <dune/functions/common/functionconcepts.hh>
18
19namespace Dune {
20namespace Functions {
21
22
23template<class F, class size_type, size_type firstValue, class... Args>
24[[deprecated("This function will be removed after Dune 2.11")]]
25auto forwardAsStaticInteger(std::integer_sequence<size_type, firstValue> values, const size_type& i, F&& f, Args&&... args)
26 ->decltype(f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...))
27{
28 return f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...);
29}
30
31template<class F, class size_type, size_type firstValue, size_type secondValue, size_type... otherValues, class... Args>
32[[deprecated("This function will be removed after Dune 2.11")]]
33auto forwardAsStaticInteger(std::integer_sequence<size_type, firstValue, secondValue, otherValues...> values, const size_type i, F&& f, Args&&... args)
34 ->decltype(f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...))
35{
36 if (i==firstValue)
37 return f(std::integral_constant<size_type, firstValue>(), std::forward<Args>(args)...);
38 return forwardAsStaticInteger(std::integer_sequence<size_type, secondValue, otherValues...>(), i, std::forward<F>(f), std::forward<Args>(args)...);
39}
40
41
42
66template<std::size_t end, class F, class size_type, class... Args>
67[[deprecated("This function will be removed after Dune 2.11, use Dune::Hybrid::switchCases.")]]
68auto forwardAsStaticIndex(const size_type& i, F&& f, Args&&... args)
69 ->decltype(f(Dune::Indices::_0, std::forward<Args>(args)...))
70{
71 return forwardAsStaticInteger(std::make_index_sequence<end>{}, i, std::forward<F>(f), std::forward<Args>(args)...);
72}
73
74
75
76namespace Imp {
77
78 template<template<class...> class T, class List>
79 struct ExpandTupleHelper
80 {};
81
82 template<template<class...> class T, template<class...> class ListType, class... Args>
83 struct ExpandTupleHelper<T, ListType<Args...>>
84 {
85 using Type = T<Args...>;
86 };
87
88} // end namespace Imp
89
101template<template<class...> class T, class ArgTuple>
102using ExpandTuple = typename Imp::ExpandTupleHelper<T, ArgTuple>::Type;
103
104
105
106namespace Imp {
107
108 template<template<class...> class T, class... Tuple>
109 struct TransformTupleHelper
110 {};
111
112 template<template<class...> class T, class... Args1>
113 struct TransformTupleHelper<T, typename std::tuple<Args1...>>
114 {
115 using Type = std::tuple<T<Args1>...>;
116 };
117
118 template<template<class...> class T, class... Args1, class... Args2>
119 struct TransformTupleHelper<T, typename std::tuple<Args1...>, typename std::tuple<Args2...>>
120 {
121 using Type = std::tuple<T<Args1, Args2>...>;
122 };
123
124} // end namespace Imp
125
138template<template<class...> class F, class... Tuples>
139using TransformTuple = typename Imp::TransformTupleHelper<F, Tuples...>::Type;
140
141
142
143namespace Imp {
144
145 template<class F, class... T, std::size_t... k>
146 auto transformTupleHelper(F&& f, const std::tuple<T...>& tuple, std::index_sequence<k...>)
147 -> decltype(std::make_tuple(f(std::get<k>(tuple))...))
148 {
149 return std::make_tuple(f(std::get<k>(tuple))...);
150 }
151
152 template<class F, class... T1, class...T2, std::size_t... k>
153 auto transformTupleHelper(F&& f, const std::tuple<T1...>& tuple1, const std::tuple<T2...>& tuple2, std::index_sequence<k...>)
154 -> decltype(std::make_tuple(f(std::get<k>(tuple1), std::get<k>(tuple2))...))
155 {
156 return std::make_tuple(f(std::get<k>(tuple1), std::get<k>(tuple2))...);
157 }
158
159} // end namespace Imp
160
172template<class F, class... T>
173auto transformTuple(F&& f, const std::tuple<T...>& tuple)
174 -> decltype(Imp::transformTupleHelper(std::forward<F>(f), tuple, std::index_sequence_for<T...>{}))
175{
176 return Imp::transformTupleHelper(std::forward<F>(f), tuple, std::index_sequence_for<T...>{});
177}
178
192template<class F, class... T1, class... T2>
193auto transformTuple(F&& f, const std::tuple<T1...>& tuple1, const std::tuple<T2...>& tuple2)
194 -> decltype(Imp::transformTupleHelper(std::forward<F>(f), tuple1, tuple2, std::index_sequence_for<T1...>{}))
195{
196 return Imp::transformTupleHelper(std::forward<F>(f), tuple1, tuple2, std::index_sequence_for<T1...>{});
197}
198
199
200
201namespace Imp {
202
203 template<class IntegerSequence>
204 struct IntegerSequenceTupleHelper
205 {};
206
207 template<class I, I... k>
208 struct IntegerSequenceTupleHelper<std::integer_sequence<I, k...>>
209 {
210 using Type = std::tuple<std::integral_constant<I, k>...>;
211 };
212
213} // end namespace Imp
214
218template<class IntegerSequence>
219using IntegerSequenceTuple= typename Imp::IntegerSequenceTupleHelper<IntegerSequence>::Type;
220
221
222
228template<class... T>
230{
231 using type = std::tuple_element_t<sizeof...(T)-1, std::tuple<T...>>;
232};
233
234
235
236namespace Imp {
237
238template<class T, class I>
239struct RotateHelper;
240
241template<class... T, std::size_t... I>
242struct RotateHelper<std::tuple<T...>, std::index_sequence<I...> >
243{
244 using type = typename std::tuple<typename LastType<T...>::type, std::tuple_element_t<I,std::tuple<T...>>...>;
245};
246
247} // end namespace Imp
248
249
257template<class... T>
259{
260 using type = typename Imp::RotateHelper<std::tuple<T...>, std::make_index_sequence<sizeof...(T)-1>>::type;
261};
262
263
264
286template<class Expression>
287auto callableCheck(Expression f)
288{
289 return [f](auto&&... args){
290 return Functions::Concept::isCallable(f, std::forward<decltype(args)>(args)...);
291 };
292}
293
294
295
311template<class Check>
312auto negatePredicate(Check check)
313{
314 return [check](auto&&... args){
315 auto negate = overload(
316 [](std::true_type) { return std::false_type{};},
317 [](std::false_type) { return std::true_type{};},
318 [](bool v) { return not v;});
319 return negate(check(std::forward<decltype(args)>(args)...));
320 };
321}
322
323
324namespace Impl {
325
326 // Wrapper to capture values in a lambda for perfect forwarding.
327 // This captures value types by value and reference types by reference.
328 template <typename T>
329 struct ForwardCaptureWrapper;
330
331 template <typename T>
332 struct ForwardCaptureWrapper
333 {
334 template <typename TT>
335 ForwardCaptureWrapper(TT&& t) : t_{std::forward<TT>(t)} {}
336
337 auto forward() const { return std::move(t_); }
338
339 T t_;
340 };
341
342 template <typename T>
343 struct ForwardCaptureWrapper<T&>
344 {
345 ForwardCaptureWrapper(T& t) : t_{t} {}
346
347 T& forward() const { return t_; };
348
349 T& t_;
350 };
351
352 template <typename T>
353 struct ForwardCaptureWrapper<const T&>
354 {
355 ForwardCaptureWrapper(const T& t) : t_{t} {}
356
357 const T& forward() const { return t_; };
358
359 const T& t_;
360 };
361
362} // end namespace Dune::Functions::Impl
363
364
365
379template <class T>
380auto forwardCapture(T&& t)
381{
382 return Impl::ForwardCaptureWrapper<T>(std::forward<T>(t));
383}
384
385
386
387} // namespace Dune::Functions
388} // namespace Dune
389
390
391#endif // DUNE_FUNCTIONS_COMMON_UTILITY_HH
typename Imp::ExpandTupleHelper< T, ArgTuple >::Type ExpandTuple
Expand tuple arguments as template arguments.
Definition: utility.hh:102
typename Imp::TransformTupleHelper< F, Tuples... >::Type TransformTuple
Transform tuple types argument using type-functor.
Definition: utility.hh:139
static constexpr auto isCallable()
Check if f is callable with given argument list.
Definition: functionconcepts.hh:51
auto callableCheck(Expression f)
Create a predicate for checking validity of expressions.
Definition: utility.hh:287
auto forwardAsStaticIndex(const size_type &i, F &&f, Args &&... args) -> decltype(f(Dune::Indices::_0, std::forward< Args >(args)...))
Transform dynamic index to static index_constant.
Definition: utility.hh:68
auto transformTuple(F &&f, const std::tuple< T1... > &tuple1, const std::tuple< T2... > &tuple2) -> decltype(Imp::transformTupleHelper(std::forward< F >(f), tuple1, tuple2, std::index_sequence_for< T1... >{}))
Transform tuple value using a binary functor.
Definition: utility.hh:193
auto negatePredicate(Check check)
Negate given predicate.
Definition: utility.hh:312
Definition: monomialset.hh:19
Get last entry of type list.
Definition: utility.hh:230
Rotate type list by one, such that last entry is moved to first position.
Definition: utility.hh:259
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jan 9, 23:34, 2026)