OpenKalman
get_dimension.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) 2020-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 
16 #ifndef OPENKALMAN_COORDINATES_GET_DIMENSION_HPP
17 #define OPENKALMAN_COORDINATES_GET_DIMENSION_HPP
18 
23 
25 {
26  namespace detail
27  {
28  template<std::size_t i = 0, typename T>
29  static constexpr auto get_dimension_fixed(const T& t)
30  {
31  if constexpr (i < collections::size_of_v<T>)
32  {
33  return values::operation(
34  std::plus{},
35  internal::get_descriptor_dimension(collections::get<i>(t)),
36  get_dimension_fixed<i + 1>(t));
37  }
38  else return std::integral_constant<std::size_t, 0_uz>{};
39  }
40  }
41 
42 
46 #ifdef __cpp_concepts
47  template<pattern Arg> requires descriptor<Arg> or collections::sized<Arg>
48  constexpr values::index auto
49 #else
50  template<typename Arg, std::enable_if_t<pattern<Arg> and
51  (descriptor<Arg> or collections::sized<Arg>), int> = 0>
52  constexpr auto
53 #endif
54  get_dimension(const Arg& arg)
55  {
56  if constexpr (descriptor<Arg>)
57  {
58  return internal::get_descriptor_dimension(arg);
59  }
61  {
62  return std::integral_constant<std::size_t, 0_uz>{};
63  }
64  else if constexpr (values::fixed_value_compares_with<collections::size_of<Arg>, stdex::dynamic_extent, &stdex::is_neq>)
65  {
66  return detail::get_dimension_fixed(arg);
67  }
68  else
69  {
70  using C = decltype(internal::get_descriptor_dimension(std::declval<collections::common_collection_type_t<Arg>>()));
71  if constexpr (values::fixed_value_compares_with<C, 0_uz>)
72  {
73  return std::integral_constant<std::size_t, 0_uz>{};
74  }
75  else if constexpr (values::fixed<C>)
76  {
77  return values::fixed_value_of_v<C> * collections::get_size(arg);
78  }
79  else
80  {
81 #ifdef __cpp_lib_ranges_fold
82  return std::ranges::fold_left(collections::views::all(arg), 0_uz,
83  [](const auto& a, const auto& b) { return a + internal::get_descriptor_dimension(b); });
84 #else
85  std::size_t ret = 0_uz;
86  for (const auto& c : collections::views::all(arg)) { ret += internal::get_descriptor_dimension(c); }
87  return ret;
88 #endif
89  }
90  }
91  }
92 
93 
94 }
95 
96 #endif
typename common_collection_type< T >::type common_collection_type_t
Helper template for common_collection_type.
Definition: common_collection_type.hpp:81
The size of a sized object (including a collection).
Definition: size_of.hpp:33
Definition for coordinates::pattern.
constexpr auto get_dimension(const Arg &arg)
Get the vector dimension of coordinates::pattern Arg.
Definition: get_dimension.hpp:54
Definition for coordinates::descriptor.
The namespace for features relating to coordinates::pattern object.
Definition: compares_with.hpp:25
constexpr detail::all_closure all
a std::ranges::range_adaptor_closure which returns a view to all members of its collection argument...
Definition: all.hpp:72
Inclusion file for collections.
constexpr bool fixed_value_compares_with
T has a fixed value that compares with N in a particular way based on parameter comp.
Definition: fixed_value_compares_with.hpp:74
constexpr bool index
T is an index value.
Definition: index.hpp:62
constexpr auto get_size(Arg &&arg)
Get the size of a sized object (e.g, a collection)
Definition: get_size.hpp:188
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98