ITK  6.0.0
Insight Toolkit
itkFixedArray.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFixedArray_h
19 #define itkFixedArray_h
20 
21 #include "itkMacro.h"
22 #include "itkMakeFilled.h"
23 #include <algorithm>
24 #include <array>
25 
26 namespace itk
27 {
28 
52 template <typename TValue, unsigned int VLength = 3>
53 class ITK_TEMPLATE_EXPORT FixedArray
54 {
55 public:
57  static constexpr unsigned int Length = VLength;
58 
60  static constexpr unsigned int Dimension = VLength;
61 
63  using ValueType = TValue;
64 
66  using CArray = ValueType[VLength];
67 
69  using Iterator = ValueType *;
70 
72  using ConstIterator = const ValueType *;
73 
74  class ConstReverseIterator;
75 
81  {
82  public:
84  : m_Iterator(i)
85  {}
88  {
89  return ReverseIterator(--m_Iterator);
90  }
93  {
94  return ReverseIterator(m_Iterator--);
95  }
98  {
99  return ReverseIterator(++m_Iterator);
100  }
103  {
104  return ReverseIterator(m_Iterator++);
105  }
106  Iterator
107  operator->() const
108  {
109  return (m_Iterator - 1);
110  }
111  ValueType &
112  operator*() const
113  {
114  return *(m_Iterator - 1);
115  }
116 
117  bool
118  operator==(const ReverseIterator & rit) const
119  {
120  return m_Iterator == rit.m_Iterator;
121  }
122 
123  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ReverseIterator);
124 
125  private:
127  friend class ConstReverseIterator;
128  };
129 
135  {
136  public:
138  : m_Iterator(i)
139  {}
140  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
143  {
144  return ConstReverseIterator(--m_Iterator);
145  }
148  {
149  return ConstReverseIterator(m_Iterator--);
150  }
153  {
154  return ConstReverseIterator(++m_Iterator);
155  }
158  {
159  return ConstReverseIterator(m_Iterator++);
160  }
162  operator->() const
163  {
164  return (m_Iterator - 1);
165  }
166  const ValueType &
167  operator*() const
168  {
169  return *(m_Iterator - 1);
170  }
171 
172  bool
173  operator==(const ConstReverseIterator & rit) const
174  {
175  return m_Iterator == rit.m_Iterator;
176  }
177 
178  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstReverseIterator);
179 
180  private:
182  };
183 
185  using pointer = ValueType *;
186 
188  using const_pointer = const ValueType *;
189 
191  using reference = ValueType &;
192 
194  using const_reference = const ValueType &;
195 
197  using iterator = ValueType *;
198 
200  using const_iterator = const ValueType *;
201 
202  using reverse_iterator = std::reverse_iterator<iterator>;
203 
204  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
205 
206  using SizeType = unsigned int;
207 
208 public:
211  FixedArray() = default;
212 
219  FixedArray(const ValueType r[VLength]);
220  FixedArray(const ValueType &);
224  explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
225  {
226  std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
227  }
228 
230  template <typename TFixedArrayValueType>
232  {
233  auto input = r.cbegin();
234 
235  for (auto & element : m_InternalArray)
236  {
237  element = static_cast<TValue>(*input++);
238  }
239  }
240 
241  template <typename TScalarValue>
242  FixedArray(const TScalarValue * r)
243  {
244  std::copy_n(r, VLength, m_InternalArray);
245  }
246 
253  template <typename TFixedArrayValueType>
254  FixedArray &
256  {
257  auto input = r.cbegin();
258 
259  for (auto & element : m_InternalArray)
260  {
261  element = static_cast<TValue>(*input++);
262  }
263  return *this;
264  }
265 
266  FixedArray &
267  operator=(const ValueType r[VLength]);
268 
272  bool
273  operator==(const FixedArray & r) const;
274 
275  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray);
276 
277 
280  // false positive warnings with GCC
281  ITK_GCC_PRAGMA_PUSH
282  ITK_GCC_SUPPRESS_Warray_bounds
283  constexpr reference
284  operator[](unsigned int index)
285  {
286  return m_InternalArray[index];
287  }
288  constexpr const_reference
289  operator[](unsigned int index) const
290  {
291  return m_InternalArray[index];
292  }
293  ITK_GCC_PRAGMA_POP
297  void
298  SetElement(unsigned int index, const_reference value)
299  {
300  m_InternalArray[index] = value;
301  }
302  const_reference
303  GetElement(unsigned int index) const
304  {
305  return m_InternalArray[index];
306  }
310  ValueType *
312  {
313  return m_InternalArray;
314  }
315 
316  const ValueType *
318  {
319  return m_InternalArray;
320  }
321 
322  ValueType *
324  {
325  return m_InternalArray;
326  }
327 
328  const ValueType *
329  data() const
330  {
331  return m_InternalArray;
332  }
333 
335  Iterator
336  Begin();
337 
339  ConstIterator
340  Begin() const;
341 
343  Iterator
344  End();
345 
347  ConstIterator
348  End() const;
349 
351  itkLegacyMacro(ReverseIterator rBegin();)
352 
354  itkLegacyMacro(ConstReverseIterator rBegin() const;)
355 
357  itkLegacyMacro(ReverseIterator rEnd();)
358 
360  itkLegacyMacro(ConstReverseIterator rEnd() const;)
361 
362  constexpr const_iterator
363  cbegin() const noexcept
364  {
365  return m_InternalArray;
366  }
367 
368  constexpr iterator
369  begin() noexcept
370  {
371  return m_InternalArray;
372  }
373 
374  constexpr const_iterator
375  begin() const noexcept
376  {
377  return this->cbegin();
378  }
379 
380  constexpr const_iterator
381  cend() const noexcept
382  {
383  return m_InternalArray + VLength;
384  }
385 
386  constexpr iterator
387  end() noexcept
388  {
389  return m_InternalArray + VLength;
390  }
391 
392  constexpr const_iterator
393  end() const noexcept
394  {
395  return this->cend();
396  }
397 
398  reverse_iterator
400  {
401  return reverse_iterator{ this->end() };
402  }
403 
404  const_reverse_iterator
405  crbegin() const
406  {
407  return const_reverse_iterator{ this->cend() };
408  }
409 
410  const_reverse_iterator
411  rbegin() const
412  {
413  return this->crbegin();
414  }
415 
416  reverse_iterator
418  {
419  return reverse_iterator{ this->begin() };
420  }
421 
422  const_reverse_iterator
423  crend() const
424  {
425  return const_reverse_iterator{ this->cbegin() };
426  }
427 
428  const_reverse_iterator
429  rend() const
430  {
431  return this->crend();
432  }
433 
435  SizeType
436  Size() const;
437 
438  constexpr SizeType
439  size() const
440  {
441  return VLength;
442  }
443 
445  void
446  Fill(const ValueType &);
447 
448  void
449  swap(FixedArray & other) noexcept
450  {
451  std::swap(m_InternalArray, other.m_InternalArray);
452  }
453 
454 private:
457 
458 public:
460  static constexpr FixedArray
461  Filled(const ValueType & value)
462  {
463  return MakeFilled<FixedArray>(value);
464  }
465 };
468 template <typename TValue, unsigned int VLength>
469 std::ostream &
470 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
471 
472 
473 template <typename TValue, unsigned int VLength>
474 inline void
476 {
477  a.swap(b);
478 }
479 
480 } // namespace itk
481 
482 #ifndef ITK_MANUAL_INSTANTIATION
483 # include "itkFixedArray.hxx"
484 #endif
485 
487 
488 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:429
itk::FixedArray::cbegin
constexpr const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:363
itk::FixedArray::ConstReverseIterator::m_Iterator
ConstIterator m_Iterator
Definition: itkFixedArray.h:181
itk::FixedArray::FixedArray
FixedArray(const TScalarValue *r)
Definition: itkFixedArray.h:242
itk::FixedArray::swap
void swap(FixedArray &other) noexcept
Definition: itkFixedArray.h:449
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::FixedArray< TOutput, VPointDimension >::CArray
ValueType[VLength] CArray
Definition: itkFixedArray.h:66
itk::FixedArray::data
const ValueType * data() const
Definition: itkFixedArray.h:329
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--(int)
Definition: itkFixedArray.h:102
itk::FixedArray::size
constexpr SizeType size() const
Definition: itkFixedArray.h:439
itk::FixedArray< TOutput, VPointDimension >::const_reference
const ValueType & const_reference
Definition: itkFixedArray.h:194
itk::FixedArray::begin
constexpr const_iterator begin() const noexcept
Definition: itkFixedArray.h:375
itk::FixedArray::ConstReverseIterator
A const reverse iterator through an array.
Definition: itkFixedArray.h:134
itk::FixedArray::FixedArray
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:231
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++(int)
Definition: itkFixedArray.h:147
itk::FixedArray::ReverseIterator
A reverse iterator through an array.
Definition: itkFixedArray.h:80
itk::FixedArray::SetElement
ITK_GCC_PRAGMA_POP void SetElement(unsigned int index, const_reference value)
Definition: itkFixedArray.h:298
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::FixedArray::m_InternalArray
CArray m_InternalArray
Definition: itkFixedArray.h:456
itk::swap
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b) noexcept
Definition: itkFixedArray.h:475
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(const ReverseIterator &rit)
Definition: itkFixedArray.h:140
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::FixedArray::ReverseIterator::m_Iterator
Iterator m_Iterator
Definition: itkFixedArray.h:126
itk::FixedArray::GetElement
const_reference GetElement(unsigned int index) const
Definition: itkFixedArray.h:303
itk::FixedArray< TOutput, VPointDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:185
itk::FixedArray::end
constexpr iterator end() noexcept
Definition: itkFixedArray.h:387
itk::FixedArray::operator=
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:255
itk::FixedArray::operator[]
constexpr const_reference operator[](unsigned int index) const
Definition: itkFixedArray.h:289
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:311
itk::FixedArray::ReverseIterator::ReverseIterator
ReverseIterator(Iterator i)
Definition: itkFixedArray.h:83
itk::FixedArray< TOutput, VPointDimension >::ConstIterator
const ValueType * ConstIterator
Definition: itkFixedArray.h:72
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--(int)
Definition: itkFixedArray.h:157
itkMacro.h
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(ConstIterator i)
Definition: itkFixedArray.h:137
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:317
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--()
Definition: itkFixedArray.h:97
itk::FixedArray::ConstReverseIterator::operator->
ConstIterator operator->() const
Definition: itkFixedArray.h:162
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++(int)
Definition: itkFixedArray.h:92
itk::FixedArray< TOutput, VPointDimension >::const_pointer
const ValueType * const_pointer
Definition: itkFixedArray.h:188
itk::FixedArray::cend
constexpr const_iterator cend() const noexcept
Definition: itkFixedArray.h:381
itkNumericTraitsFixedArrayPixel.h
itkMakeFilled.h
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++()
Definition: itkFixedArray.h:87
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:552
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--()
Definition: itkFixedArray.h:152
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::FixedArray< TOutput, VPointDimension >::iterator
ValueType * iterator
Definition: itkFixedArray.h:197
itk::FixedArray< TOutput, VPointDimension >::SizeType
unsigned int SizeType
Definition: itkFixedArray.h:206
itk::FixedArray::ReverseIterator::operator*
ValueType & operator*() const
Definition: itkFixedArray.h:112
itk::FixedArray::data
ValueType * data()
Definition: itkFixedArray.h:323
itk::FixedArray< TOutput, VPointDimension >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkFixedArray.h:202
itk::FixedArray::end
constexpr const_iterator end() const noexcept
Definition: itkFixedArray.h:393
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::FixedArray::crbegin
const_reverse_iterator crbegin() const
Definition: itkFixedArray.h:405
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:142
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:399
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:107
itk::FixedArray::begin
constexpr iterator begin() noexcept
Definition: itkFixedArray.h:369
itk::FixedArray::operator[]
ITK_GCC_PRAGMA_PUSH constexpr ITK_GCC_SUPPRESS_Warray_bounds reference operator[](unsigned int index)
Definition: itkFixedArray.h:284
itk::FixedArray::ConstReverseIterator::operator*
const ValueType & operator*() const
Definition: itkFixedArray.h:167
itk::FixedArray::Filled
static constexpr FixedArray Filled(const ValueType &value)
Definition: itkFixedArray.h:461
itk::FixedArray::ReverseIterator::operator==
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:118
itk::FixedArray< TOutput, VPointDimension >::reference
ValueType & reference
Definition: itkFixedArray.h:191
itk::FixedArray::ConstReverseIterator::operator==
bool operator==(const ConstReverseIterator &rit) const
Definition: itkFixedArray.h:173
itk::FixedArray::rend
reverse_iterator rend()
Definition: itkFixedArray.h:417
itk::FixedArray< TOutput, VPointDimension >::const_iterator
const ValueType * const_iterator
Definition: itkFixedArray.h:200
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray::crend
const_reverse_iterator crend() const
Definition: itkFixedArray.h:423
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:411
itk::FixedArray< TOutput, VPointDimension >::Iterator
ValueType * Iterator
Definition: itkFixedArray.h:69
itk::FixedArray< TOutput, VPointDimension >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkFixedArray.h:204
itk::FixedArray::ValueType
TValue ValueType
Definition: itkFixedArray.h:63
itk::FixedArray::FixedArray
FixedArray(const std::array< ValueType, VLength > &stdArray)
Definition: itkFixedArray.h:224