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 operator->() const { return (m_Iterator - 1); }
107  ValueType & operator*() const { return *(m_Iterator - 1); }
108 
109  bool
110  operator==(const ReverseIterator & rit) const
111  {
112  return m_Iterator == rit.m_Iterator;
113  }
114 
115  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ReverseIterator);
116 
117  private:
119  friend class ConstReverseIterator;
120  };
121 
127  {
128  public:
130  : m_Iterator(i)
131  {}
132  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
135  {
136  return ConstReverseIterator(--m_Iterator);
137  }
140  {
141  return ConstReverseIterator(m_Iterator--);
142  }
145  {
146  return ConstReverseIterator(++m_Iterator);
147  }
150  {
151  return ConstReverseIterator(m_Iterator++);
152  }
153  ConstIterator operator->() const { return (m_Iterator - 1); }
154  const ValueType & operator*() const { return *(m_Iterator - 1); }
155 
156  bool
157  operator==(const ConstReverseIterator & rit) const
158  {
159  return m_Iterator == rit.m_Iterator;
160  }
161 
162  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstReverseIterator);
163 
164  private:
166  };
167 
169  using pointer = ValueType *;
170 
172  using const_pointer = const ValueType *;
173 
175  using reference = ValueType &;
176 
178  using const_reference = const ValueType &;
179 
181  using iterator = ValueType *;
182 
184  using const_iterator = const ValueType *;
185 
186  using reverse_iterator = std::reverse_iterator<iterator>;
187 
188  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
189 
190  using SizeType = unsigned int;
191 
192 public:
195  FixedArray() = default;
196 
203  FixedArray(const ValueType r[VLength]);
204  FixedArray(const ValueType &);
208  explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
209  {
210  std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
211  }
212 
214  template <typename TFixedArrayValueType>
216  {
217  auto input = r.cbegin();
218 
219  for (auto & element : m_InternalArray)
220  {
221  element = static_cast<TValue>(*input++);
222  }
223  }
224 
225  template <typename TScalarValue>
226  FixedArray(const TScalarValue * r)
227  {
228  std::copy_n(r, VLength, m_InternalArray);
229  }
230 
237  template <typename TFixedArrayValueType>
238  FixedArray &
240  {
241  auto input = r.cbegin();
242 
243  for (auto & element : m_InternalArray)
244  {
245  element = static_cast<TValue>(*input++);
246  }
247  return *this;
248  }
249 
250  FixedArray &
251  operator=(const ValueType r[VLength]);
252 
256  bool
257  operator==(const FixedArray & r) const;
258 
259  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray);
260 
261 
264  // false positive warnings with GCC
265  ITK_GCC_PRAGMA_PUSH
266  ITK_GCC_SUPPRESS_Warray_bounds
267  constexpr reference operator[](unsigned int index) { return m_InternalArray[index]; }
268  constexpr const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
269  ITK_GCC_PRAGMA_POP
273  void
274  SetElement(unsigned int index, const_reference value)
275  {
276  m_InternalArray[index] = value;
277  }
278  const_reference
279  GetElement(unsigned int index) const
280  {
281  return m_InternalArray[index];
282  }
286  ValueType *
288  {
289  return m_InternalArray;
290  }
291 
292  const ValueType *
294  {
295  return m_InternalArray;
296  }
297 
298  ValueType *
300  {
301  return m_InternalArray;
302  }
303 
304  const ValueType *
305  data() const
306  {
307  return m_InternalArray;
308  }
309 
311  Iterator
312  Begin();
313 
315  ConstIterator
316  Begin() const;
317 
319  Iterator
320  End();
321 
323  ConstIterator
324  End() const;
325 
327  itkLegacyMacro(ReverseIterator rBegin();)
328 
330  itkLegacyMacro(ConstReverseIterator rBegin() const;)
331 
333  itkLegacyMacro(ReverseIterator rEnd();)
334 
336  itkLegacyMacro(ConstReverseIterator rEnd() const;)
337 
338  constexpr const_iterator
339  cbegin() const noexcept
340  {
341  return m_InternalArray;
342  }
343 
344  constexpr iterator
345  begin() noexcept
346  {
347  return m_InternalArray;
348  }
349 
350  constexpr const_iterator
351  begin() const noexcept
352  {
353  return this->cbegin();
354  }
355 
356  constexpr const_iterator
357  cend() const noexcept
358  {
359  return m_InternalArray + VLength;
360  }
361 
362  constexpr iterator
363  end() noexcept
364  {
365  return m_InternalArray + VLength;
366  }
367 
368  constexpr const_iterator
369  end() const noexcept
370  {
371  return this->cend();
372  }
373 
374  reverse_iterator
376  {
377  return reverse_iterator{ this->end() };
378  }
379 
380  const_reverse_iterator
381  crbegin() const
382  {
383  return const_reverse_iterator{ this->cend() };
384  }
385 
386  const_reverse_iterator
387  rbegin() const
388  {
389  return this->crbegin();
390  }
391 
392  reverse_iterator
394  {
395  return reverse_iterator{ this->begin() };
396  }
397 
398  const_reverse_iterator
399  crend() const
400  {
401  return const_reverse_iterator{ this->cbegin() };
402  }
403 
404  const_reverse_iterator
405  rend() const
406  {
407  return this->crend();
408  }
409 
411  SizeType
412  Size() const;
413 
414  constexpr SizeType
415  size() const
416  {
417  return VLength;
418  }
419 
421  void
422  Fill(const ValueType &);
423 
424  void
425  swap(FixedArray & other)
426  {
427  std::swap(m_InternalArray, other.m_InternalArray);
428  }
429 
430 private:
433 
434 public:
436  static constexpr FixedArray
437  Filled(const ValueType & value)
438  {
439  return MakeFilled<FixedArray>(value);
440  }
441 };
444 template <typename TValue, unsigned int VLength>
445 std::ostream &
446 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
447 
448 
449 template <typename TValue, unsigned int VLength>
450 inline void
452 {
453  a.swap(b);
454 }
455 
456 } // namespace itk
457 
458 #ifndef ITK_MANUAL_INSTANTIATION
459 # include "itkFixedArray.hxx"
460 #endif
461 
463 
464 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:405
itk::FixedArray::cbegin
constexpr const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:339
itk::FixedArray::ConstReverseIterator::m_Iterator
ConstIterator m_Iterator
Definition: itkFixedArray.h:165
itk::FixedArray::FixedArray
FixedArray(const TScalarValue *r)
Definition: itkFixedArray.h:226
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:305
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--(int)
Definition: itkFixedArray.h:102
itk::FixedArray::size
constexpr SizeType size() const
Definition: itkFixedArray.h:415
itk::FixedArray< TOutput, VPointDimension >::const_reference
const ValueType & const_reference
Definition: itkFixedArray.h:178
itk::FixedArray::begin
constexpr const_iterator begin() const noexcept
Definition: itkFixedArray.h:351
itk::FixedArray::ConstReverseIterator
A const reverse iterator through an array.
Definition: itkFixedArray.h:126
itk::FixedArray::FixedArray
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:215
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++(int)
Definition: itkFixedArray.h:139
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:274
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::FixedArray::m_InternalArray
CArray m_InternalArray
Definition: itkFixedArray.h:432
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(const ReverseIterator &rit)
Definition: itkFixedArray.h:132
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:118
itk::FixedArray::GetElement
const_reference GetElement(unsigned int index) const
Definition: itkFixedArray.h:279
itk::FixedArray< TOutput, VPointDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:169
itk::FixedArray::end
constexpr iterator end() noexcept
Definition: itkFixedArray.h:363
itk::FixedArray::operator=
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:239
itk::FixedArray::operator[]
constexpr const_reference operator[](unsigned int index) const
Definition: itkFixedArray.h:268
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:287
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:149
itkMacro.h
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(ConstIterator i)
Definition: itkFixedArray.h:129
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:293
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--()
Definition: itkFixedArray.h:97
itk::FixedArray::ConstReverseIterator::operator->
ConstIterator operator->() const
Definition: itkFixedArray.h:153
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++(int)
Definition: itkFixedArray.h:92
itk::FixedArray< TOutput, VPointDimension >::const_pointer
const ValueType * const_pointer
Definition: itkFixedArray.h:172
itk::FixedArray::cend
constexpr const_iterator cend() const noexcept
Definition: itkFixedArray.h:357
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:543
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--()
Definition: itkFixedArray.h:144
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:53
itk::FixedArray< TOutput, VPointDimension >::iterator
ValueType * iterator
Definition: itkFixedArray.h:181
itk::FixedArray< TOutput, VPointDimension >::SizeType
unsigned int SizeType
Definition: itkFixedArray.h:190
itk::FixedArray::ReverseIterator::operator*
ValueType & operator*() const
Definition: itkFixedArray.h:107
itk::FixedArray::data
ValueType * data()
Definition: itkFixedArray.h:299
itk::FixedArray< TOutput, VPointDimension >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkFixedArray.h:186
itk::swap
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b)
Definition: itkFixedArray.h:451
itk::FixedArray::end
constexpr const_iterator end() const noexcept
Definition: itkFixedArray.h:369
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:381
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:134
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:375
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:106
itk::FixedArray::begin
constexpr iterator begin() noexcept
Definition: itkFixedArray.h:345
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:425
itk::FixedArray::operator[]
ITK_GCC_PRAGMA_PUSH constexpr ITK_GCC_SUPPRESS_Warray_bounds reference operator[](unsigned int index)
Definition: itkFixedArray.h:267
itk::FixedArray::ConstReverseIterator::operator*
const ValueType & operator*() const
Definition: itkFixedArray.h:154
itk::FixedArray::Filled
static constexpr FixedArray Filled(const ValueType &value)
Definition: itkFixedArray.h:437
itk::FixedArray::ReverseIterator::operator==
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:110
itk::FixedArray< TOutput, VPointDimension >::reference
ValueType & reference
Definition: itkFixedArray.h:175
itk::FixedArray::ConstReverseIterator::operator==
bool operator==(const ConstReverseIterator &rit) const
Definition: itkFixedArray.h:157
itk::FixedArray::rend
reverse_iterator rend()
Definition: itkFixedArray.h:393
itk::FixedArray< TOutput, VPointDimension >::const_iterator
const ValueType * const_iterator
Definition: itkFixedArray.h:184
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray::crend
const_reverse_iterator crend() const
Definition: itkFixedArray.h:399
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:387
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:188
itk::FixedArray::ValueType
TValue ValueType
Definition: itkFixedArray.h:63
itk::FixedArray::FixedArray
FixedArray(const std::array< ValueType, VLength > &stdArray)
Definition: itkFixedArray.h:208