ITK  5.3.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 
198  FixedArray(const ValueType r[VLength]);
199  FixedArray(const ValueType &);
203  explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
204  {
205  std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
206  }
207 
209  template <typename TFixedArrayValueType>
211  {
212  auto input = r.cbegin();
213 
214  for (auto & element : m_InternalArray)
215  {
216  element = static_cast<TValue>(*input++);
217  }
218  }
219 
220  template <typename TScalarValue>
221  FixedArray(const TScalarValue * r)
222  {
223  std::copy_n(r, VLength, m_InternalArray);
224  }
225 
227  template <typename TFixedArrayValueType>
228  FixedArray &
230  {
231  auto input = r.cbegin();
232 
233  for (auto & element : m_InternalArray)
234  {
235  element = static_cast<TValue>(*input++);
236  }
237  return *this;
238  }
239 
240  FixedArray &
241  operator=(const ValueType r[VLength]);
242 
246  bool
247  operator==(const FixedArray & r) const;
248 
249  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray);
250 
251 
254 // false positive warnings with GCC
255 #if defined(__GNUC__)
256 # if (__GNUC__ >= 7)
257 # pragma GCC diagnostic push
258 # pragma GCC diagnostic ignored "-Warray-bounds"
259 # endif
260 #endif
261  constexpr reference operator[](unsigned int index) { return m_InternalArray[index]; }
262  constexpr const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
263 #if defined(__GNUC__)
264 # if (__GNUC__ >= 7)
265 # pragma GCC diagnostic pop
266 # endif
267 #endif
268 
271  void
272  SetElement(unsigned int index, const_reference value)
273  {
274  m_InternalArray[index] = value;
275  }
276  const_reference
277  GetElement(unsigned int index) const
278  {
279  return m_InternalArray[index];
280  }
284  ValueType *
286  {
287  return m_InternalArray;
288  }
289 
290  const ValueType *
292  {
293  return m_InternalArray;
294  }
295 
296  ValueType *
298  {
299  return m_InternalArray;
300  }
301 
302  const ValueType *
303  data() const
304  {
305  return m_InternalArray;
306  }
307 
309  Iterator
310  Begin();
311 
312  ConstIterator
313  Begin() const;
314 
315  Iterator
316  End();
317 
318  ConstIterator
319  End() const;
320 
321  itkLegacyMacro(ReverseIterator rBegin());
322 
323  itkLegacyMacro(ConstReverseIterator rBegin() const);
324 
325  itkLegacyMacro(ReverseIterator rEnd());
326 
327  itkLegacyMacro(ConstReverseIterator rEnd() const);
328 
329  constexpr const_iterator
330  cbegin() const noexcept
331  {
332  return m_InternalArray;
333  }
334 
335  constexpr iterator
336  begin() noexcept
337  {
338  return m_InternalArray;
339  }
340 
341  constexpr const_iterator
342  begin() const noexcept
343  {
344  return this->cbegin();
345  }
346 
347  constexpr const_iterator
348  cend() const noexcept
349  {
350  return m_InternalArray + VLength;
351  }
352 
353  constexpr iterator
354  end() noexcept
355  {
356  return m_InternalArray + VLength;
357  }
358 
359  constexpr const_iterator
360  end() const noexcept
361  {
362  return this->cend();
363  }
364 
365  reverse_iterator
367  {
368  return reverse_iterator{ this->end() };
369  }
370 
371  const_reverse_iterator
372  crbegin() const
373  {
374  return const_reverse_iterator{ this->cend() };
375  }
376 
377  const_reverse_iterator
378  rbegin() const
379  {
380  return this->crbegin();
381  }
382 
383  reverse_iterator
385  {
386  return reverse_iterator{ this->begin() };
387  }
388 
389  const_reverse_iterator
390  crend() const
391  {
392  return const_reverse_iterator{ this->cbegin() };
393  }
394 
395  const_reverse_iterator
396  rend() const
397  {
398  return this->crend();
399  }
400 
402  SizeType
403  Size() const;
404 
405  constexpr SizeType
406  size() const
407  {
408  return VLength;
409  }
410 
412  void
413  Fill(const ValueType &);
414 
415  void
416  swap(FixedArray & other)
417  {
418  std::swap(m_InternalArray, other.m_InternalArray);
419  }
420 
421 private:
424 
425 public:
427  static constexpr FixedArray
428  Filled(const ValueType & value)
429  {
430  return MakeFilled<FixedArray>(value);
431  }
432 };
435 template <typename TValue, unsigned int VLength>
436 std::ostream &
437 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
438 
439 
440 template <typename TValue, unsigned int VLength>
441 inline void
443 {
444  a.swap(b);
445 }
446 
447 } // namespace itk
448 
449 #ifndef ITK_MANUAL_INSTANTIATION
450 # include "itkFixedArray.hxx"
451 #endif
452 
454 
455 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:396
itk::FixedArray::cbegin
constexpr const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:330
itk::FixedArray::ConstReverseIterator::m_Iterator
ConstIterator m_Iterator
Definition: itkFixedArray.h:165
itk::FixedArray::FixedArray
FixedArray(const TScalarValue *r)
Definition: itkFixedArray.h:221
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:303
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:406
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:342
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:210
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:423
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:277
itk::FixedArray< TOutput, VPointDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:169
itk::FixedArray::end
constexpr iterator end() noexcept
Definition: itkFixedArray.h:354
itk::FixedArray::operator=
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:229
itk::FixedArray::operator[]
constexpr const_reference operator[](unsigned int index) const
Definition: itkFixedArray.h:262
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:285
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:261
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:291
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:348
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:297
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:442
itk::FixedArray::end
constexpr const_iterator end() const noexcept
Definition: itkFixedArray.h:360
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:372
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:134
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:366
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:106
itk::FixedArray::begin
constexpr iterator begin() noexcept
Definition: itkFixedArray.h:336
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:416
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:428
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:384
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:390
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:378
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:272
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:203