OpenKalman
diagonal_of_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-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 
17 #ifndef OPENKALMAN_DIAGONAL_OF_MDSPAN_POLICIES_HPP
18 #define OPENKALMAN_DIAGONAL_OF_MDSPAN_POLICIES_HPP
19 
20 #include <iostream>
21 #include "patterns/patterns.hpp"
22 
23 namespace OpenKalman::interface
24 {
29  template<typename NestedLayout, typename NestedExtents>
31  {
32  template<class Extents>
33  struct mapping
34  {
35  using extents_type = Extents;
36  using index_type = typename extents_type::index_type;
37  using size_type = typename extents_type::size_type;
38  using rank_type = typename extents_type::rank_type;
40 
41  private:
42 
43  using nested_extents_type = NestedExtents;
44  using nested_mapping_type = typename NestedLayout::template mapping<nested_extents_type>;
45 
46  template<typename...Is>
47  constexpr index_type
48  access_with_padded_indices(Is...is) const
49  {
50  if constexpr (sizeof...(Is) < nested_extents_type::rank())
51  return access_with_padded_indices(is..., 0_uz);
52  else
53  return nested_mapping_(is...);
54  }
55 
56  public:
57 
58  constexpr explicit
59  mapping(const nested_mapping_type& map, const extents_type& e)
60  : nested_mapping_(map), extents_(e) {}
61 
62  constexpr const extents_type&
63  extents() const noexcept { return extents_; }
64 
65 #ifdef __cpp_concepts
66  constexpr index_type
67  operator() () const requires (extents_type::rank() == 0)
68 #else
69  template<bool Enable = true, std::enable_if_t<Enable and (extents_type::rank() == 0), int> = 0>
70  constexpr index_type
71  operator() () const
72 #endif
73  {
74  return access_with_padded_indices();
75  }
76 
77 #ifdef __cpp_concepts
78  constexpr index_type
79  operator() (
80  std::convertible_to<index_type> auto i0,
81  std::convertible_to<index_type> auto...is) const requires (1 + sizeof...(is) == extents_type::rank())
82 #else
83  template<typename IndexType0, typename...IndexTypes, std::enable_if_t<
84  std::is_convertible_v<IndexType0, index_type> and
85  (... and std::is_convertible_v<IndexTypes, index_type>) and
86  (1 + sizeof...(IndexTypes) == extents_type::rank()), int> = 0>
87  constexpr index_type
88  operator() (IndexType0 i0, IndexTypes...is) const
89 #endif
90  {
91  return access_with_padded_indices(i0, i0, is...);
92  }
93 
94  constexpr index_type
95  required_span_size() const noexcept { return nested_mapping_.required_span_size(); }
96 
97  static constexpr bool is_always_unique() noexcept { return nested_mapping_type::is_always_unique(); }
98 
99  static constexpr bool
100  is_always_exhaustive() noexcept
101  {
102  if constexpr (not nested_mapping_type::is_always_exhaustive()) return false;
103  else if constexpr (nested_extents_type::rank() == 0) return true;
104  else if constexpr (nested_extents_type::rank() == 1) return nested_extents_type::static_extent(0) == 1;
105  else return nested_extents_type::static_extent(0) == 1 and nested_extents_type::static_extent(1) == 1;
106  }
107 
108  static constexpr bool
109  is_always_strided() noexcept { return nested_mapping_type::is_always_strided(); }
110 
111  constexpr bool
112  is_unique() const { return nested_mapping_type::is_unique(); }
113 
114  constexpr bool
115  is_exhaustive() const
116  {
117  if (not nested_mapping_type::is_exhaustive()) return false;
118  if constexpr (nested_extents_type::rank() == 0) return true;
119  else if constexpr (nested_extents_type::rank() == 1) return nested_mapping_.extents().extent(0) == 1;
120  else return nested_mapping_.extents().extent(0) == 1 and nested_mapping_.extents().extent(1) == 1;
121  }
122 
123  constexpr bool
124  is_strided() const { return nested_mapping_type::is_strided(); }
125 
126  constexpr index_type
127  stride(std::size_t r) const
128  {
129  if (r == 0) return nested_mapping_.stride(0) + nested_mapping_.stride(1);
130  return nested_mapping_.stride(r - 1);
131  }
132 
133  template<class OtherExtents>
134  friend constexpr bool
135  operator==(const mapping& lhs, const mapping<OtherExtents>& rhs) noexcept
136  {
137  return patterns::compare_pattern_collections(lhs.extents(), rhs.extents());
138  }
139 
140  private:
141 
142  nested_mapping_type nested_mapping_;
143  extents_type extents_;
144 
145  };
146  };
147 
148 
149 }
150 
151 #endif
Definition: basics.hpp:41
Definition: diagonal_of_mdspan_policies.hpp:33
constexpr auto compare_pattern_collections(const A &a, const B &b)
Compare each element of two pattern_collection objects lexicographically.
Definition: compare_pattern_collections.hpp:137
Definition: diagonal_of_mdspan_policies.hpp:30