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-2026 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_PATTERNS_GET_DIMENSION_HPP
17 #define OPENKALMAN_PATTERNS_GET_DIMENSION_HPP
18 
23 
24 namespace OpenKalman::patterns
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<sized_pattern Arg>
48  constexpr values::index auto
49 #else
50  template<typename Arg, std::enable_if_t<sized_pattern<Arg>, int> = 0>
51  constexpr auto
52 #endif
53  get_dimension(const Arg& arg)
54  {
55  if constexpr (descriptor<Arg>)
56  {
57  return internal::get_descriptor_dimension(arg);
58  }
60  {
61  return std::integral_constant<std::size_t, 0_uz>{};
62  }
63  else if constexpr (values::fixed_value_compares_with<collections::size_of<Arg>, stdex::dynamic_extent, &stdex::is_neq>)
64  {
65  return detail::get_dimension_fixed(arg);
66  }
67  else // dynamic size
68  {
69  using C = decltype(internal::get_descriptor_dimension(std::declval<collections::common_collection_type_t<Arg>>()));
70  if constexpr (values::fixed_value_compares_with<C, 0_uz>)
71  return std::integral_constant<std::size_t, 0_uz>{};
72  else if constexpr (values::fixed<C>)
73  return values::fixed_value_of_v<C> * collections::get_size(arg);
74  else
75 #ifdef __cpp_lib_ranges_fold
76  return std::ranges::fold_left(collections::views::all(arg), 0_uz,
77  [](const auto& a, const auto& b) { return a + internal::get_descriptor_dimension(b); });
78 #else
79  {
80  std::size_t ret = 0_uz;
81  for (const auto& c : collections::views::all(arg)) { ret += internal::get_descriptor_dimension(c); }
82  return ret;
83  }
84 #endif
85  }
86  }
87 
88 
89 }
90 
91 #endif
constexpr auto get_size(Arg &&arg)
Get the size of a sized object (e.g, a collection)
Definition: get_size.hpp:224
Definition for patterns::sized_pattern.
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
The namespace for features relating to patterns::pattern object.
Definition: collection_compares_with.hpp:24
Definition for patterns::descriptor.
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:70
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_dimension(const Arg &arg)
Get the vector dimension of patterns::pattern Arg.
Definition: get_dimension.hpp:53
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98