ITK  5.4.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 #if defined(__GNUC__)
266 # if (__GNUC__ >= 7)
267 # pragma GCC diagnostic push
268 # pragma GCC diagnostic ignored "-Warray-bounds"
269 # endif
270 #endif
271  constexpr reference operator[](unsigned int index) { return m_InternalArray[index]; }
272  constexpr const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
273 #if defined(__GNUC__)
274 # if (__GNUC__ >= 7)
275 # pragma GCC diagnostic pop
276 # endif
277 #endif
278 
281  void
282  SetElement(unsigned int index, const_reference value)
283  {
284  m_InternalArray[index] = value;
285  }
286  const_reference
287  GetElement(unsigned int index) const
288  {
289  return m_InternalArray[index];
290  }
294  ValueType *
296  {
297  return m_InternalArray;
298  }
299 
300  const ValueType *
302  {
303  return m_InternalArray;
304  }
305 
306  ValueType *
308  {
309  return m_InternalArray;
310  }
311 
312  const ValueType *
313  data() const
314  {
315  return m_InternalArray;
316  }
317 
319  Iterator
320  Begin();
321 
323  ConstIterator
324  Begin() const;
325 
327  Iterator
328  End();
329 
331  ConstIterator
332  End() const;
333 
335  itkLegacyMacro(ReverseIterator rBegin());
336 
338  itkLegacyMacro(ConstReverseIterator rBegin() const);
339 
341  itkLegacyMacro(ReverseIterator rEnd());
342 
344  itkLegacyMacro(ConstReverseIterator rEnd() const);
345 
346  constexpr const_iterator
347  cbegin() const noexcept
348  {
349  return m_InternalArray;
350  }
351 
352  constexpr iterator
353  begin() noexcept
354  {
355  return m_InternalArray;
356  }
357 
358  constexpr const_iterator
359  begin() const noexcept
360  {
361  return this->cbegin();
362  }
363 
364  constexpr const_iterator
365  cend() const noexcept
366  {
367  return m_InternalArray + VLength;
368  }
369 
370  constexpr iterator
371  end() noexcept
372  {
373  return m_InternalArray + VLength;
374  }
375 
376  constexpr const_iterator
377  end() const noexcept
378  {
379  return this->cend();
380  }
381 
382  reverse_iterator
384  {
385  return reverse_iterator{ this->end() };
386  }
387 
388  const_reverse_iterator
389  crbegin() const
390  {
391  return const_reverse_iterator{ this->cend() };
392  }
393 
394  const_reverse_iterator
395  rbegin() const
396  {
397  return this->crbegin();
398  }
399 
400  reverse_iterator
402  {
403  return reverse_iterator{ this->begin() };
404  }
405 
406  const_reverse_iterator
407  crend() const
408  {
409  return const_reverse_iterator{ this->cbegin() };
410  }
411 
412  const_reverse_iterator
413  rend() const
414  {
415  return this->crend();
416  }
417 
419  SizeType
420  Size() const;
421 
422  constexpr SizeType
423  size() const
424  {
425  return VLength;
426  }
427 
429  void
430  Fill(const ValueType &);
431 
432  void
433  swap(FixedArray & other)
434  {
435  std::swap(m_InternalArray, other.m_InternalArray);
436  }
437 
438 private:
441 
442 public:
444  static constexpr FixedArray
445  Filled(const ValueType & value)
446  {
447  return MakeFilled<FixedArray>(value);
448  }
449 };
452 template <typename TValue, unsigned int VLength>
453 std::ostream &
454 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
455 
456 
457 template <typename TValue, unsigned int VLength>
458 inline void
460 {
461  a.swap(b);
462 }
463 
464 } // namespace itk
465 
466 #ifndef ITK_MANUAL_INSTANTIATION
467 # include "itkFixedArray.hxx"
468 #endif
469 
471 
472 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:413
itk::FixedArray::cbegin
constexpr const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:347
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:71
itk::FixedArray< TOutput, VPointDimension >::CArray
ValueType[VLength] CArray
Definition: itkFixedArray.h:66
itk::FixedArray::data
const ValueType * data() const
Definition: itkFixedArray.h:313
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--(int)
Definition: itkFixedArray.h:102
itk::FixedArray::size
constexpr SizeType size() const
Definition: itkFixedArray.h:423
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:359
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::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::FixedArray::m_InternalArray
CArray m_InternalArray
Definition: itkFixedArray.h:440
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(const ReverseIterator &rit)
Definition: itkFixedArray.h:132
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:287
itk::FixedArray< TOutput, VPointDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:169
itk::FixedArray::end
constexpr iterator end() noexcept
Definition: itkFixedArray.h:371
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:272
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:295
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::operator[]
constexpr reference operator[](unsigned int index)
Definition: itkFixedArray.h:271
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:301
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:365
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:545
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:307
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:459
itk::FixedArray::end
constexpr const_iterator end() const noexcept
Definition: itkFixedArray.h:377
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::FixedArray::crbegin
const_reverse_iterator crbegin() const
Definition: itkFixedArray.h:389
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:134
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:383
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:106
itk::FixedArray::begin
constexpr iterator begin() noexcept
Definition: itkFixedArray.h:353
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:433
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:445
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:401
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:407
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:395
itk::FixedArray< TOutput, VPointDimension >::Iterator
ValueType * Iterator
Definition: itkFixedArray.h:69
itk::FixedArray::SetElement
void SetElement(unsigned int index, const_reference value)
Definition: itkFixedArray.h:282
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