OTB  9.0.0
Orfeo Toolbox
otbMdSpan.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbMdSpan_h
21 #define otbMdSpan_h
22 
23 #include "otbExtents.h"
24 #include "otbLayouts.h"
25 #include "otbSpan.h"
26 #include <type_traits>
27 
28 namespace otb
29 {
30 struct narrow {};
31 
38 template<class ElementType,
39  class Extents,
40  class LayoutPolicy = layout_right>
42 {
43 public:
44 
45  // Domain and codomain types
46  using extents_type = Extents;
47  using layout_type = LayoutPolicy;
48  // using accessor_type = AccessorPolicy;
49  using mapping_type = typename layout_type::template mapping<extents_type>;
50  // using element_type = typename accessor_type::element_type;
51  using element_type = ElementType;
52  using value_type = std::remove_cv_t<element_type>;
53  using index_type = ptrdiff_t ;
54  using difference_type = ptrdiff_t;
57 
58  // [mdspan.basic.cons], basic_mdspan constructors, assignment, and destructor
59  constexpr basic_mdspan() noexcept = default;
60  constexpr basic_mdspan(basic_mdspan const&) noexcept = default;
61  constexpr basic_mdspan(basic_mdspan &&) noexcept = default;
62 
67  template<class... IndexType>
68  explicit constexpr basic_mdspan(pointer p, IndexType... dynamic_extents) noexcept
69  : m_ptr(p)
70  , m_map(extents_type{dynamic_extents...})
71  {}
73 
80  template<class... IndexType>
81  constexpr basic_mdspan(pointer p, narrow, IndexType... dynamic_extents) noexcept
82  : m_ptr(p)
83  , m_map(extents_type{static_cast<index_type>(dynamic_extents)...})
84  {}
86 
88  template<class IndexType, size_t N>
89  constexpr basic_mdspan(pointer p, std::array<IndexType, N> const& dynamic_extents) noexcept
90  : m_ptr(p)
91  , m_map(extents_type{dynamic_extents})
92  {}
94 
96  constexpr basic_mdspan(pointer p, mapping_type const& m) noexcept
97  : m_ptr(p), m_map(m) {}
98 
99  template<class OtherElementType, class OtherExtents, class OtherLayoutPolicy>
101  : m_ptr(other.m_ptr), m_map(other.m_map)
102  {}
103 
104  ~basic_mdspan() = default;
105 
106  constexpr basic_mdspan& operator=(basic_mdspan const&) noexcept = default;
107  constexpr basic_mdspan& operator=(basic_mdspan &&) noexcept = default;
108 
109  template<class OtherElementType, class OtherExtents, class OtherLayoutPolicy>
110  constexpr basic_mdspan& operator=(const basic_mdspan<OtherElementType, OtherExtents, OtherLayoutPolicy>& other) noexcept
111  {
112  m_ptr = other.m_ptr;
113  m_map = other.m_map;
114  return *this;
115  }
116 
117  // [mdspan.basic.mapping], basic_mdspan mapping domain multidimensional index to access codomain element
118  constexpr reference operator[](index_type) const noexcept;
119  template<class... IndexType>
120  constexpr reference operator()(IndexType... indices) const noexcept
121  {
122  return m_ptr[m_map(indices...)];
123  }
124 
125  template<class IndexType, size_t N>
126  constexpr reference operator()(std::array<IndexType, N> const& indices) const noexcept
127  {
128  return m_ptr[m_map(indices)];
129  }
130 
131  // [mdspan.basic.domobs], basic_mdspan observers of the domain multidimensional index space
132  static constexpr int rank() noexcept
133  { return extents_type::rank(); }
134  static constexpr int rank_dynamic() noexcept
135  { return extents_type::rank_dynamic(); }
136  static constexpr index_type static_extent(size_t k) noexcept
137  { return extents_type::static_extent(k); }
138 
139  constexpr extents_type extents() const noexcept
140  { return m_map.extents(); }
141  constexpr index_type extent(size_t k) const noexcept
142  { return extents().extent(k); }
143  constexpr index_type size() const noexcept
144  {
145  if (rank() == 0)
146  return 0;
147  index_type r = 1;
148  for (std::size_t k=0; k<rank(); ++k)
149  r *= extent(k);
150  return r;
151  }
152  constexpr index_type unique_size() const noexcept;
153 
154  // [mdspan.basic.codomain], basic_mdspan observers of the codomain
155  constexpr Span<element_type> span() const noexcept
156  { return Span<element_type>{data(), mapping().required_span_size()}; }
157  constexpr pointer data() const noexcept
158  { return m_ptr; }
159 
160  // [mdspan.basic.obs], basic_mdspan observers of the mapping
161  static constexpr bool is_always_unique() noexcept
162  { return mapping_type::is_always_unique(); }
163  static constexpr bool is_always_contiguous() noexcept
164  { return mapping_type::is_always_contiguous(); }
165  static constexpr bool is_always_strided() noexcept
166  { return mapping_type::is_always_strided(); }
167 
168  constexpr mapping_type mapping() const noexcept
169  { return m_map; }
170  constexpr bool is_unique() const noexcept
171  { return mapping().is_unique(); }
172  constexpr bool is_contiguous() const noexcept
173  { return mapping().is_contiguous(); }
174  constexpr bool is_strided() const noexcept
175  { return mapping().is_strided(); }
176  constexpr index_type stride(size_t k) const noexcept
177  { return mapping().stride(k); }
178 
179 private:
180  pointer m_ptr = nullptr;
182 };
183 
184 template<class T, ptrdiff_t... Extents>
185 using mdspan = basic_mdspan<T, extents<Extents...>>;
186 
187 } // otb namespace
188 
189 #endif // otbMdSpan_h
otb::basic_mdspan::operator()
constexpr reference operator()(std::array< IndexType, N > const &indices) const noexcept
Definition: otbMdSpan.h:126
otb::basic_mdspan::basic_mdspan
constexpr basic_mdspan() noexcept=default
otb::basic_mdspan::basic_mdspan
constexpr basic_mdspan(pointer p, narrow, IndexType... dynamic_extents) noexcept
Definition: otbMdSpan.h:81
otb::basic_mdspan
Definition: otbMdSpan.h:41
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::reference
element_type & reference
Definition: otbMdSpan.h:56
otb::basic_mdspan::element_type
ElementType element_type
Definition: otbMdSpan.h:51
otb::extents
Definition: otbExtents.h:61
otb::basic_mdspan::span
constexpr Span< element_type > span() const noexcept
Definition: otbMdSpan.h:155
otb::basic_mdspan::basic_mdspan
constexpr basic_mdspan(const basic_mdspan< OtherElementType, OtherExtents, OtherLayoutPolicy > &other) noexcept
Definition: otbMdSpan.h:100
otb::narrow
Definition: otbMdSpan.h:30
otb::basic_mdspan::stride
constexpr index_type stride(vcl_size_t k) const noexcept
Definition: otbMdSpan.h:176
otb::basic_mdspan::is_always_unique
static constexpr bool is_always_unique() noexcept
Definition: otbMdSpan.h:161
otb::basic_mdspan::basic_mdspan
constexpr basic_mdspan(pointer p, mapping_type const &m) noexcept
Definition: otbMdSpan.h:96
otbLayouts.h
otb::basic_mdspan::basic_mdspan
constexpr basic_mdspan(pointer p, std::array< IndexType, N > const &dynamic_extents) noexcept
Definition: otbMdSpan.h:89
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::basic_mdspan::data
constexpr pointer data() const noexcept
Definition: otbMdSpan.h:157
otb::basic_mdspan::rank_dynamic
static constexpr int rank_dynamic() noexcept
Definition: otbMdSpan.h:134
otb::basic_mdspan::unique_size
constexpr index_type unique_size() const noexcept
otb::basic_mdspan::extents
constexpr extents_type extents() const noexcept
Definition: otbMdSpan.h:139
otb::basic_mdspan::operator=
constexpr basic_mdspan & operator=(basic_mdspan const &) noexcept=default
otb::basic_mdspan::mapping
constexpr mapping_type mapping() const noexcept
Definition: otbMdSpan.h:168
otbExtents.h
otb::basic_mdspan::static_extent
static constexpr index_type static_extent(vcl_size_t k) noexcept
Definition: otbMdSpan.h:136
otb::details::generic_layout
Definition: otbLayouts.h:185
otb::basic_mdspan::is_strided
constexpr bool is_strided() const noexcept
Definition: otbMdSpan.h:174
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::difference_type
ptrdiff_t difference_type
Definition: otbMdSpan.h:54
otb::basic_mdspan::is_always_contiguous
static constexpr bool is_always_contiguous() noexcept
Definition: otbMdSpan.h:163
otb::Span
Definition: otbSpan.h:60
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::mapping_type
typename layout_type::template mapping< extents_type > mapping_type
Definition: otbMdSpan.h:49
otb::basic_mdspan::is_contiguous
constexpr bool is_contiguous() const noexcept
Definition: otbMdSpan.h:172
otb::basic_mdspan::m_map
mapping_type m_map
Definition: otbMdSpan.h:181
otb::basic_mdspan::size
constexpr index_type size() const noexcept
Definition: otbMdSpan.h:143
otb::basic_mdspan::extent
constexpr index_type extent(vcl_size_t k) const noexcept
Definition: otbMdSpan.h:141
otb::basic_mdspan::operator[]
constexpr reference operator[](index_type) const noexcept
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::value_type
std::remove_cv_t< element_type > value_type
Definition: otbMdSpan.h:52
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::layout_type
dynamic_extent layout_type
Definition: otbMdSpan.h:47
otb::basic_mdspan::rank
static constexpr int rank() noexcept
Definition: otbMdSpan.h:132
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::index_type
ptrdiff_t index_type
Definition: otbMdSpan.h:53
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::pointer
element_type * pointer
Definition: otbMdSpan.h:55
otb::basic_mdspan::~basic_mdspan
~basic_mdspan()=default
otb::details::rank_dynamic
constexpr std::vcl_size_t rank_dynamic() noexcept
Definition: otbExtents.h:39
otb::basic_mdspan::is_always_strided
static constexpr bool is_always_strided() noexcept
Definition: otbMdSpan.h:165
otb::basic_mdspan< double, dynamic_extent, dynamic_extent >::extents_type
dynamic_extent extents_type
Definition: otbMdSpan.h:46
otbSpan.h
otb::basic_mdspan::m_ptr
pointer m_ptr
Definition: otbMdSpan.h:180
otb::basic_mdspan::operator()
constexpr reference operator()(IndexType... indices) const noexcept
Definition: otbMdSpan.h:120
otb::basic_mdspan::is_unique
constexpr bool is_unique() const noexcept
Definition: otbMdSpan.h:170