Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
tuple.hpp
Go to the documentation of this file.
1 // Copyright (c) 2018 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_TUPLE_HPP
22 #define FUB_TUPLE_HPP
23 
24 #include <array>
25 #include <tuple>
26 
27 namespace fub {
28 
29 template <std::size_t... Is, typename... Ts>
30 [[nodiscard]] constexpr auto Take(std::index_sequence<Is...>,
31  const std::tuple<Ts...>& t) {
32  return std::tuple{std::get<Is>(t)...};
33 }
34 
35 template <std::size_t N, typename... Ts>
36 [[nodiscard]] constexpr auto Take(const std::tuple<Ts...>& t) {
37  return Take(std::make_index_sequence<N>(), t);
38 }
39 
40 template <std::size_t N, std::size_t... Is, typename... Ts>
41 [[nodiscard]] constexpr auto Drop(std::index_sequence<Is...>,
42  const std::tuple<Ts...>& t) {
43  return std::tuple{std::get<N + Is>(t)...};
44 }
45 
46 template <std::size_t N, typename... Ts>
47 [[nodiscard]] constexpr auto Drop(const std::tuple<Ts...>& t) {
48  constexpr std::size_t size = sizeof...(Ts);
49  return Drop<N>(std::make_index_sequence<size - N>(), t);
50 }
51 
52 template <typename Tuple, typename Function>
53 [[nodiscard]] constexpr auto Transform(Tuple&& tuple, Function f) {
54  return std::apply([&](auto&&... xs) { return std::tuple{f(xs)...}; }, tuple);
55 }
56 
57 template <std::size_t... Is, typename T, typename... Ts>
58 [[nodiscard]] constexpr auto AsArray(std::index_sequence<Is...>,
59  const std::tuple<T, Ts...>& t) {
60  return std::array<T, sizeof...(Is)>{std::get<Is>(t)...};
61 }
62 
63 template <typename T, typename... Ts>
64 [[nodiscard]] constexpr auto AsArray(const std::tuple<T, Ts...>& t) {
65  return AsArray(std::make_index_sequence<sizeof...(Ts) + 1>(), t);
66 }
67 
68 template <typename TupleLike>
69 [[nodiscard]] constexpr auto AsTuple(const TupleLike& t) {
70  return std::apply([](const auto&... xs) { return std::tuple{xs...}; }, t);
71 }
72 
73 } // namespace fub
74 
75 #endif
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
constexpr auto Transform(Tuple &&tuple, Function f)
Definition: tuple.hpp:53
constexpr auto Drop(std::index_sequence< Is... >, const std::tuple< Ts... > &t)
Definition: tuple.hpp:41
constexpr std::array< std::ptrdiff_t, Extents::rank()> AsArray(Extents e) noexcept
Definition: PatchDataView.hpp:154
make_integer_sequence< std::size_t, N > make_index_sequence
Definition: type_traits.hpp:173
constexpr auto Take(std::index_sequence< Is... >, const std::tuple< Ts... > &t)
Definition: tuple.hpp:30
constexpr auto AsTuple(const TupleLike &t)
Definition: tuple.hpp:69