OpenKalman
tuple_flatten.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2025 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_TUPLE_FLATTEN_HPP
18 #define OPENKALMAN_TUPLE_FLATTEN_HPP
19 
20 #include <type_traits>
21 #include <tuple>
23 
25 {
26  namespace detail
27  {
28  template<typename Arg> constexpr auto
29  tuple_flatten_impl(Arg&&); // forward declaration
30 
31 
32  template<typename Arg, std::size_t...Ix>
33  constexpr auto
34  tuple_flatten_impl(Arg&& arg, std::index_sequence<Ix...>)
35  {
36  if constexpr ((... or tuple_like<std::tuple_element_t<Ix, std::decay_t<Arg>>>))
37  return std::tuple_cat(tuple_flatten_impl(std::get<Ix>(std::forward<Arg>(arg)))...);
38  else
39  return std::forward<Arg>(arg);
40  }
41 
42 
43  template<typename Arg>
44  constexpr auto
45  tuple_flatten_impl(Arg&& arg)
46  {
47  if constexpr (tuple_like<Arg>)
48  {
49  constexpr auto seq = std::make_index_sequence<std::tuple_size_v<std::decay_t<Arg>>>{};
50  return tuple_flatten_impl(std::forward<Arg>(arg), seq);
51  }
52  else return std::tuple {std::forward<Arg>(arg)};
53  }
54  } // namespace detail
55 
56 
61 #ifdef __cpp_concepts
62  template<tuple_like Arg>
63  constexpr tuple_like auto
64 #else
65  template<typename Arg, std::enable_if_t<tuple_like<Arg>, int> = 0>
66  constexpr auto
67 #endif
68  tuple_flatten(Arg&& arg)
69  {
70  return detail::tuple_flatten_impl(std::forward<Arg>(arg));
71  }
72 
73 } // namespace OpenKalman::collections
74 
75 #endif //OPENKALMAN_TUPLE_FLATTEN_HPP
Definition for collections::tuple_like.
Namespace for collections.
Definition: collections.hpp:27
decltype(auto) constexpr get(Arg &&arg, I i)
A generalization of std::get.
Definition: get.hpp:62
Definition: tuple_reverse.hpp:103
constexpr bool tuple_like
T is a non-empty tuple, pair, array, or other type that acts like a tuple.
Definition: tuple_like.hpp:51