OpenKalman
dimensions.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 
16 #ifndef OPENKALMAN_COORDINATES_VIEWS_DIMENSIONS_HPP
17 #define OPENKALMAN_COORDINATES_VIEWS_DIMENSIONS_HPP
18 
22 
24 {
28  template<typename T>
30  {
31  static_assert(collections::uniformly_gettable<T>);
32 
33 
34 #ifdef __cpp_lib_concepts
35  constexpr dimensions_tuple_view() requires std::default_initializable<T> = default;
36 #else
37  template<bool Enable = true, std::enable_if_t<Enable and stdex::default_initializable<T>, int> = 0>
38  constexpr dimensions_tuple_view() {};
39 #endif
40 
41 
42 #ifdef __cpp_lib_concepts
43  template<typename Arg> requires std::constructible_from<T, Arg&&>
44 #else
45  template<typename Arg, std::enable_if_t<stdex::constructible_from<T, Arg&&>, int> = 0>
46 #endif
47  explicit constexpr dimensions_tuple_view(Arg&& arg) : t {std::forward<Arg>(arg)} {}
48 
49 
53  template<std::size_t i>
54  friend constexpr decltype(auto)
55  get(const dimensions_tuple_view& v)
56  {
57  if constexpr (i < collections::size_of_v<T>)
58  return get_dimension(collections::get<i>(v.t));
59  else
60  return std::integral_constant<std::size_t, 1>{};
61  }
62 
63 
67  template<size_t i>
68  friend constexpr decltype(auto)
70  {
71  if constexpr (i < collections::size_of_v<T>)
72  return get_dimension(collections::get<i>(std::move(v).t));
73  else
74  return std::integral_constant<std::size_t, 1>{};
75  }
76 
77  private:
78 
79  T t;
80  };
81 
82 }
83 
84 
85 namespace std
86 {
87  template<typename T>
88  struct tuple_size<OpenKalman::coordinates::dimensions_tuple_view<T>>
90 
91 
92  template<std::size_t i, typename T>
93  struct tuple_element<i, OpenKalman::coordinates::dimensions_tuple_view<T>>
94  : OpenKalman::coordinates::dimension_of<OpenKalman::collections::collection_element_t<i, T>> {};
95 }
96 
97 
99 {
100  namespace detail
101  {
103  {
104  #ifdef __cpp_concepts
105  template<pattern_collection P> requires collections::viewable_collection<P> or collections::uniformly_gettable<P>
106  #else
107  template<typename P, std::enable_if_t<pattern_collection<P> and
108  (collections::viewable_collection<P> or collections::uniformly_gettable<P>), int> = 0>
109  #endif
110  constexpr auto
111  operator() (P&& p) const
112  {
113  if constexpr (collections::viewable_collection<P>)
114  {
115  auto f = [pt = std::make_tuple(collections::views::all(std::forward<P>(p)))](auto&& i)
116  {
117  if constexpr (values::size_compares_with<decltype(i), collections::size_of<P>, &stdex::is_gteq>)
118  {
119  return std::integral_constant<std::size_t, 1>{};
120  }
121  else if constexpr (values::size_compares_with<decltype(i), collections::size_of<P>, &stdex::is_lt>)
122  {
123  return get_dimension(collections::get_element(std::get<0>(pt), std::forward<decltype(i)>(i)));
124  }
125  else if (i < collections::get_size(std::get<0>(pt)))
126  {
127  return static_cast<std::size_t>(get_dimension(collections::get_element(std::get<0>(pt), std::forward<decltype(i)>(i))));
128  }
129  else
130  {
131  return 1_uz;
132  }
133  };
134  return collections::views::generate(std::move(f));
135  }
136  else
137  {
138  return dimensions_tuple_view<P>{std::forward<P>(p)} | collections::views::all;
139  }
140  }
141  };
142 
143  }
144 
145 
150 
151 }
152 
153 
154 #endif
Definition for pattern_collection.
The size of a sized object (including a collection).
Definition: size_of.hpp:33
The size of a coordinates::pattern.
Definition: dimension_of.hpp:36
constexpr auto get_dimension(const Arg &arg)
Get the vector dimension of coordinates::pattern Arg.
Definition: get_dimension.hpp:54
The root namespace for OpenKalman.
Definition: basics.hpp:34
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.
The namespace for views for coordinates::pattern object.
Definition: coordinates.hpp:51
A view to the dimensions of a uniformly_gettable pattern_collection.
Definition: dimensions.hpp:29
constexpr bool size_compares_with
T and U are sizes that compare in a particular way based on parameter comp.
Definition: size_compares_with.hpp:98
constexpr detail::generate_adaptor generate
a collection_view generator associated with generate_view.
Definition: generate.hpp:335
frienddecltype(auto) constexpr get(const dimensions_tuple_view &v)
Get element i of a dimensions_tuple_view.
Definition: dimensions.hpp:55
Definition for coordinates::get_dimension.
constexpr detail::dimensions_adaptor dimensions
a std::ranges::range_adaptor_closure for the dimensions of a pattern_collection.
Definition: dimensions.hpp:149
decltype(auto) constexpr get_element(Arg &&arg, I i)
A generalization of std::get and the range subscript operator.
Definition: get_element.hpp:122
constexpr auto get_size(Arg &&arg)
Get the size of a sized object (e.g, a collection)
Definition: get_size.hpp:188