OpenKalman
clip_square_shaped.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) 2023 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_CLIP_SQUARE_SHAPED_HPP
18 #define OPENKALMAN_CLIP_SQUARE_SHAPED_HPP
19 
20 
21 namespace OpenKalman::internal
22 {
23 
24  namespace detail
25  {
26  template<std::size_t...Ix, typename Arg>
27  constexpr decltype(auto) clip_square_shaped_impl(std::index_sequence<Ix...>, Arg&& arg)
28  {
29  auto dim = internal::smallest_vector_space_descriptor<scalar_type_of_t<Arg>>(get_index_dimension_of<0>(arg), get_index_dimension_of<1>(arg));
30  auto ret {get_slice(std::forward<Arg>(arg),
31  std::forward_as_tuple(std::integral_constant<std::size_t, static_cast<decltype(Ix)>(0)>{}...),
32  std::forward_as_tuple((Ix==0?dim:dim)...))};
33  return ret;
34  }
35 
36  } // namespace detail
37 
38 
43 #ifdef __cpp_concepts
44  template<indexible Arg>
45  constexpr square_shaped<Applicability::permitted> decltype(auto)
46 #else
47  template<typename Arg>
48  constexpr decltype(auto)
49 #endif
50  clip_square_shaped(Arg&& arg)
51  {
52  if constexpr (square_shaped<Arg>) return std::forward<Arg>(arg);
53  else
54  {
55  static_assert(index_count_v<Arg> <= 2);
56  return detail::clip_square_shaped_impl(std::make_index_sequence<index_count_v<Arg>>{}, std::forward<Arg>(arg));
57  }
58  }
59 
60 } // namespace OpenKalman::internal
61 
62 #endif //OPENKALMAN_CLIP_SQUARE_SHAPED_HPP
decltype(auto) constexpr get_slice(Arg &&arg, const std::tuple< Offset... > &offsets, const std::tuple< Extent... > &extents)
Extract a slice from a matrix or tensor.
Definition: get_slice.hpp:101
Definition: tuple_reverse.hpp:103
Definition: basics.hpp:48