OTB  9.0.0
Orfeo Toolbox
otbLayouts.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 otbLayouts_h
21 #define otbLayouts_h
22 
23 #include "otbExtents.h"
24 
25 namespace otb
26 {
27 namespace details
28 {
29 namespace policy
30 {
40 template <typename Extents>
42 {
43  using index_type = typename Extents::index_type;
44 
45  constexpr root_layout_mapping() noexcept = default;
46  constexpr root_layout_mapping(root_layout_mapping const&) noexcept = default;
47  constexpr root_layout_mapping(root_layout_mapping &&) noexcept = default;
48  constexpr root_layout_mapping& operator=(root_layout_mapping const&) noexcept = default;
49  constexpr root_layout_mapping& operator=(root_layout_mapping &&) noexcept = default;
50 
51  constexpr root_layout_mapping(Extents const& e) noexcept
52  : m_extents(e) {}
53 
54  ~root_layout_mapping() = default;
55 
56  constexpr Extents const& extents() const noexcept { return m_extents ; }
57 
58  static constexpr bool is_always_unique() noexcept { return true ; }
59  static constexpr bool is_always_strided() noexcept { return true ; }
60 
61  constexpr bool is_unique() const noexcept { return true ; }
62  constexpr bool is_strided() const noexcept { return true ; }
63 
64 private:
65  Extents m_extents;
66 };
67 
78 template <typename Extents>
79 struct left_ : root_layout_mapping<Extents>
80 {
84 
85  constexpr index_type stride(const size_t r) const noexcept
86  {
87  assert(r < this->extents().rank());
88  index_type res = 1;
89  for(size_t k = 0 ; k < r; ++k)
90  res *= this->extents().extent(k);
91  return res;
92  }
93  static constexpr bool is_always_contiguous() noexcept { return true; }
94  constexpr bool is_contiguous() const noexcept { return true; }
95 };
96 
107 template <typename Extents>
108 struct right_ : root_layout_mapping<Extents>
109 {
113 
114  constexpr index_type stride(const size_t r) const noexcept
115  {
116  assert(r < this->extents().rank());
117  index_type res = 1;
118  for(size_t k = r+1 ; k < this->extents().rank(); ++k)
119  res *= this->extents().extent(k);
120  return res;
121  }
122  static constexpr bool is_always_contiguous() noexcept { return true; }
123  constexpr bool is_contiguous() const noexcept { return true; }
124 };
125 
138 template <typename Extents>
139 struct stride_ : root_layout_mapping<Extents>
140 {
142  using stride_t = std::array<index_type, Extents::rank()> ;
144 
145  // constructors
147  constexpr stride_(Extents const& e, stride_t const& s) noexcept
149  , m_strides(s)
150  , m_contigous(false)
151  {
152  // TODO: implement m_contigous computation
153  }
154 
155  constexpr index_type stride(const size_t R) const noexcept
156  {
157  assert(R < this->extents().rank());
158  return m_strides[R];
159  }
160 
161  static constexpr bool is_always_contiguous() noexcept { return false; }
162  constexpr bool is_contiguous() const noexcept { return m_contigous; }
163 
164 private:
167 };
168 
169 } // (otb::details::)policy namespace
170 
184 template <template <typename> class LayoutPolicy>
186 {
187 public:
188  template <typename Extents>
189  class mapping : public LayoutPolicy<Extents>
190  {
191  public:
192  using extents_type = Extents;
193  using index_type = typename LayoutPolicy<Extents>::index_type;
194  using LayoutPolicy<Extents>::LayoutPolicy;
196 
197  template<class... Indices>
198  constexpr index_type operator()(Indices... i) const noexcept
199  {
200  std::array<index_type, extents_type::rank()> idx{i...};
201  index_type r = 0;
202  for (size_t k = 0 ; k < extents_type::rank() ; ++k)
203  {
204  r += idx[k] * this->stride(k);
205  }
206  return r;
207  }
208 
209  constexpr index_type required_span_size() const noexcept
210  {
211  index_type r = 0;
212  for (size_t k = 0 ; k < Extents::rank() ; ++k)
213  r = std::max(r, this->stride(k) * this->extents().extent(k));
214  return r;
215  // TODO: use `r *= this->extents().extent(k);` for contiguous cases
216  }
217  };
218 };
219 
220 } // (otb::)details namespace
221 
237 
253 
263 
264 } // otb namespace
265 
266 #endif // otbLayouts_h
otb::details::policy::left_::is_contiguous
constexpr bool is_contiguous() const noexcept
Definition: otbLayouts.h:94
otb::details::generic_layout::mapping::operator()
constexpr index_type operator()(Indices... i) const noexcept
Definition: otbLayouts.h:198
otb::details::policy::right_::stride
constexpr index_type stride(const vcl_size_t r) const noexcept
Definition: otbLayouts.h:114
otb::details::policy::stride_::m_strides
stride_t m_strides
Definition: otbLayouts.h:165
otb::extents
Definition: otbExtents.h:61
otb::details::generic_layout::mapping::index_type
typename LayoutPolicy< Extents >::index_type index_type
Definition: otbLayouts.h:193
otb::details::policy::left_::stride
constexpr index_type stride(const vcl_size_t r) const noexcept
Definition: otbLayouts.h:85
otb::details::generic_layout::mapping::extents_type
Extents extents_type
Definition: otbLayouts.h:192
otb::details::policy::root_layout_mapping::is_unique
constexpr bool is_unique() const noexcept
Definition: otbLayouts.h:61
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::details::policy::root_layout_mapping
Definition: otbLayouts.h:41
otb::details::policy::root_layout_mapping::index_type
typename Extents::index_type index_type
Definition: otbLayouts.h:43
otb::details::policy::root_layout_mapping::is_always_strided
static constexpr bool is_always_strided() noexcept
Definition: otbLayouts.h:59
otb::details::policy::left_
Definition: otbLayouts.h:79
otb::extents::extent
constexpr index_type extent(vcl_size_t k) const noexcept
Definition: otbExtents.h:122
otb::details::policy::stride_::m_contigous
bool m_contigous
Definition: otbLayouts.h:166
otbExtents.h
otb::details::generic_layout
Definition: otbLayouts.h:185
otb::details::policy::right_
Definition: otbLayouts.h:108
otb::details::policy::left_::is_always_contiguous
static constexpr bool is_always_contiguous() noexcept
Definition: otbLayouts.h:93
otb::details::policy::root_layout_mapping::is_strided
constexpr bool is_strided() const noexcept
Definition: otbLayouts.h:62
otb::details::policy::stride_::is_contiguous
constexpr bool is_contiguous() const noexcept
Definition: otbLayouts.h:162
otb::details::policy::stride_::stride_
constexpr stride_(Extents const &e, stride_t const &s) noexcept
Definition: otbLayouts.h:147
otb::details::policy::stride_::index_type
typename root_layout_mapping< Extents >::index_type index_type
Definition: otbLayouts.h:141
otb::details::policy::right_::is_always_contiguous
static constexpr bool is_always_contiguous() noexcept
Definition: otbLayouts.h:122
otb::details::policy::root_layout_mapping::extents
constexpr Extents const & extents() const noexcept
Definition: otbLayouts.h:56
otb::details::policy::root_layout_mapping::root_layout_mapping
constexpr root_layout_mapping() noexcept=default
otb::details::policy::root_layout_mapping::~root_layout_mapping
~root_layout_mapping()=default
otb::details::policy::right_::is_contiguous
constexpr bool is_contiguous() const noexcept
Definition: otbLayouts.h:123
otb::details::policy::stride_::stride_t
std::array< index_type, Extents::rank()> stride_t
Definition: otbLayouts.h:142
otb::details::policy::stride_::stride
constexpr index_type stride(const vcl_size_t R) const noexcept
Definition: otbLayouts.h:155
otb::details::generic_layout::mapping::required_span_size
constexpr index_type required_span_size() const noexcept
Definition: otbLayouts.h:209
otb::details::policy::stride_::is_always_contiguous
static constexpr bool is_always_contiguous() noexcept
Definition: otbLayouts.h:161
otb::details::policy::stride_
Definition: otbLayouts.h:139
otb::details::policy::root_layout_mapping::m_extents
Extents m_extents
Definition: otbLayouts.h:65
otb::details::generic_layout::mapping
Definition: otbLayouts.h:189
otb::details::policy::root_layout_mapping::is_always_unique
static constexpr bool is_always_unique() noexcept
Definition: otbLayouts.h:58