OpenKalman
transpose_mdspan_policies.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 
17 #ifndef OPENKALMAN_TRANSPOSE_MDSPAN_POLICIES_HPP
18 #define OPENKALMAN_TRANSPOSE_MDSPAN_POLICIES_HPP
19 
20 #include "basics/basics.hpp"
21 
22 namespace OpenKalman::interface
23 {
28  template<typename NestedMapping, std::size_t indexa, std::size_t indexb>
30  {
31  template<class E>
32  struct mapping
33  {
34  using extents_type = E;
35  using index_type = typename extents_type::index_type;
36  using size_type = typename extents_type::size_type;
37  using rank_type = typename extents_type::rank_type;
39 
40  private:
41 
42  template<std::size_t...i, typename...Ix>
43  constexpr index_type
44  access_with_indices_adjusted(std::index_sequence<i...> seq, Ix...ix) const
45  {
46  constexpr std::size_t N = sizeof...(Ix);
47  if constexpr (sizeof...(i) > N)
48  {
49  return access_with_indices_adjusted(seq, std::move(ix)..., 0_uz);
50  }
51  else
52  {
53  return nested_mapping_(std::get<
54  i == indexa and indexb < N ? indexb :
55  i == indexb and indexa < N ? indexa : i>(
56  std::forward_as_tuple(std::move(ix)...))...);
57  }
58  }
59 
60  public:
61 
62  constexpr explicit
63  mapping(const NestedMapping& map, const extents_type& e) : nested_mapping_(map), extents_(e) {}
64 
65  constexpr const extents_type&
66  extents() const noexcept { return extents_; }
67 
68 #ifdef __cpp_concepts
69  template<std::convertible_to<index_type>...IndexTypes> requires (sizeof...(IndexTypes) == extents_type::rank())
70 #else
71  template<typename...IndexTypes, std::enable_if_t<
72  (... and std::is_convertible_v<IndexTypes, index_type>) and
73  (sizeof...(IndexTypes) == extents_type::rank()), int> = 0>
74 #endif
75  index_type
76  operator() (IndexTypes...i) const
77  {
78  constexpr std::size_t rank = NestedMapping::extents_type::rank();
79  return access_with_indices_adjusted(std::make_index_sequence<rank>{}, std::move(i)...);
80  }
81 
82  constexpr index_type
83  required_span_size() const noexcept(noexcept(nested_mapping_.required_span_size()))
84  {
85  return nested_mapping_.required_span_size();
86  }
87 
88  const NestedMapping&
89  nested_mapping() const { return nested_mapping_; }
90 
91  static constexpr bool
92  is_always_unique() noexcept { return NestedMapping::is_always_unique(); }
93 
94  static constexpr bool
95  is_always_exhaustive() noexcept { return NestedMapping::is_always_contiguous(); }
96 
97  static constexpr bool
98  is_always_strided() noexcept { return NestedMapping::is_always_strided(); }
99 
100  constexpr bool
101  is_unique() const { return nested_mapping_.is_unique(); }
102 
103  constexpr bool
104  is_exhaustive() const { return nested_mapping_.is_exhaustive(); }
105 
106  constexpr bool
107  is_strided() const { return nested_mapping_.is_strided(); }
108 
109  constexpr index_type
110  stride(size_t r) const
111  {
112  assert(this->is_strided());
113  assert(r < extents_type::rank());
114  return nested_mapping_.stride(r == indexa ? indexb : r == indexb ? indexa : r);
115  }
116 
117  template<class OtherExtents>
118  friend constexpr bool
119  operator==(const mapping& lhs, const mapping<OtherExtents>& rhs) noexcept
120  {
121  return lhs.nested_mapping_ == rhs.nested_mapping_;
122  }
123 
124  private:
125 
126  NestedMapping nested_mapping_;
127  extents_type extents_;
128 
129  };
130  };
131 
132 
133 }
134 
135 #endif
constexpr detail_get::get_impl< i > get
A generalization of std::get, where the index is known at compile time.
Definition: get.hpp:50
Definition: basics.hpp:41
Definition: transpose_mdspan_policies.hpp:29
Definition: trait_backports.hpp:64
Basic definitions for OpenKalman as a whole.
Definition: transpose_mdspan_policies.hpp:32