OpenKalman
access.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) 2022-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_ACCESS_HPP
17 #define OPENKALMAN_ACCESS_HPP
18 
23 
24 namespace OpenKalman
25 {
26  namespace detail
27  {
28  template<typename Indices, std::size_t...i>
29  constexpr decltype(auto)
30  make_index_array(const Indices& indices, std::index_sequence<i...>)
31  {
32  return std::array<std::size_t, sizeof...(i)> { collections::get<i>(indices)...};
33  }
34 
35 
36  template<std::size_t i, typename Indices>
37  constexpr std::size_t
38  get_index(const Indices& indices, std::size_t s)
39  {
40  if (i < s)
41  return collections::get<i>(indices);
42  else
43  return collections::get<0>(indices);
44  }
45 
46 
47  template<typename Indices, std::size_t...i>
48  constexpr decltype(auto)
49  make_index_array_padded(const Indices& indices, std::size_t s, std::index_sequence<i...>)
50  {
51  return std::array<std::size_t, sizeof...(i)> {get_index<i>(indices, s)...};
52  }
53  }
54 
55 
63 #ifdef __cpp_lib_concepts
64  template<indexible Arg, index_collection_for<Arg> Indices> requires
65  (not empty_object<Arg>) and
66  (not values::size_compares_with<collections::size_of<Indices>, index_count<Arg>, &stdex::is_lt>)
67  constexpr values::value decltype(auto)
68 #else
69  template<typename Arg, typename Indices, std::enable_if_t<
70  index_collection_for<Indices, Arg> and (not empty_object<Arg>) and
71  (not values::size_compares_with<collections::size_of<Indices>, index_count<Arg>, &stdex::is_lt>), int> = 0>
72  constexpr decltype(auto)
73 #endif
74  access(Arg&& arg, const Indices& indices)
75  {
76  decltype(auto) mds = get_mdspan(std::forward<Arg>(arg));
77  constexpr std::size_t rank = std::decay_t<decltype(mds)>::rank();
78  std::size_t s = collections::get_size(indices);
79  if (s < rank)
80  return mds[detail::make_index_array_padded(indices, s, std::make_index_sequence<rank>{})];
81  if constexpr (stdex::ranges::range<Indices>)
82  return mds[stdex::span(indices).template subspan<0, rank>()];
83  else
84  return mds[detail::make_index_array(indices, std::make_index_sequence<rank>{})];
85  }
86 
87 
92 #ifdef __cpp_lib_concepts
93  template<indexible Arg, values::index...I> requires
94  (not empty_object<Arg>) and
95  (not values::size_compares_with<std::integral_constant<std::size_t, sizeof...(I)>, index_count<Arg>, &stdex::is_lt>)
96  constexpr values::value decltype(auto)
97 #else
98  template<typename Arg, typename...I, std::enable_if_t<indexible<Arg> and (... and values::index<I>) and
99  (not empty_object<Arg>) and
100  (not values::size_compares_with<std::integral_constant<std::size_t, sizeof...(I)>, index_count<Arg>, &stdex::is_lt>), int> = 0>
101  constexpr decltype(auto)
102 #endif
103  access(Arg&& arg, I...i)
104  {
105  return access(std::forward<Arg>(arg), std::array<std::size_t, sizeof...(I)>{std::move(i)...});
106  }
107 
108 
109 }
110 
111 #endif
constexpr auto get_mdspan(T &t)
Get the coordinates::pattern_collection associated with indexible object T.
Definition: get_mdspan.hpp:35
Definition for index_collection_for.
constexpr bool indexible
T is a multidimensional array type.
Definition: indexible.hpp:32
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
The root namespace for OpenKalman.
Definition: basics.hpp:34
decltype(auto) constexpr access(Arg &&arg, const Indices &indices)
Access a component of an indexible object at a given set of indices.
Definition: access.hpp:74
Inclusion file for collections.
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
Definition for index_count.
Definition for empty_object.
constexpr bool index
T is an index value.
Definition: index.hpp:62
The minimum number of indices needed to access all the components of an object (i.e., the rank or order).
Definition: index_count.hpp:34
constexpr auto get_size(Arg &&arg)
Get the size of a sized object (e.g, a collection)
Definition: get_size.hpp:188