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>
24 
26 {
27  namespace detail
28  {
29  template<typename Arg> constexpr auto
30  tuple_flatten_impl(Arg&&); // forward declaration
31 
32 
33  template<typename Arg, std::size_t...Ix>
34  constexpr auto
35  tuple_flatten_impl(Arg&& arg, std::index_sequence<Ix...>)
36  {
37  if constexpr ((... or uniformly_gettable<collection_element_t<Ix, std::decay_t<Arg>>>))
38  return std::tuple_cat(tuple_flatten_impl(std::get<Ix>(std::forward<Arg>(arg)))...);
39  else
40  return std::forward<Arg>(arg);
41  }
42 
43 
44  template<typename Arg>
45  constexpr auto
46  tuple_flatten_impl(Arg&& arg)
47  {
48  if constexpr (uniformly_gettable<Arg>)
49  {
50  constexpr auto seq = std::make_index_sequence<size_of_v<Arg>>{};
51  return tuple_flatten_impl(std::forward<Arg>(arg), seq);
52  }
53  else return std::tuple {std::forward<Arg>(arg)};
54  }
55  }
56 
57 
62 #ifdef __cpp_concepts
63  template<uniformly_gettable Arg>
64  constexpr tuple_like auto
65 #else
66  template<typename Arg, std::enable_if_t<uniformly_gettable<Arg>, int> = 0>
67  constexpr auto
68 #endif
69  tuple_flatten(Arg&& arg)
70  {
71  return detail::tuple_flatten_impl(std::forward<Arg>(arg));
72  }
73 
74 }
75 
76 #endif
Namespace for collections.
Definition: collections.hpp:27
constexpr bool uniformly_gettable
T is a fixed-size object that is gettable for all indices.
Definition: uniformly_gettable.hpp:55
constexpr detail_get::get_impl< i > get
A generalization of std::get, where the index is known at compile time.
Definition: get.hpp:50
Definition: trait_backports.hpp:64
typename collection_element< i, T >::type collection_element_t
Helper template for collection_element.
Definition: collection_element.hpp:116
Definition for collections::uniformly_gettable.
Definition for collections::collection_element.
constexpr bool tuple_like
T is a non-empty tuple, pair, array, or other type that acts like a tuple.
Definition: tuple_like.hpp:66