ITK  5.2.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  * http://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 <algorithm>
23 #include <array>
24 
25 namespace itk
26 {
27 
51 template <typename TValue, unsigned int VLength = 3>
52 class ITK_TEMPLATE_EXPORT FixedArray
53 {
54 public:
56  static constexpr unsigned int Length = VLength;
57 
59  static constexpr unsigned int Dimension = VLength;
60 
62  using ValueType = TValue;
63 
65  using CArray = ValueType[VLength];
66 
68  using Iterator = ValueType *;
69 
71  using ConstIterator = const ValueType *;
72 
73  class ConstReverseIterator;
74 
80  {
81  public:
83  : m_Iterator(i)
84  {}
87  {
88  return ReverseIterator(--m_Iterator);
89  }
92  {
93  return ReverseIterator(m_Iterator--);
94  }
97  {
98  return ReverseIterator(++m_Iterator);
99  }
102  {
103  return ReverseIterator(m_Iterator++);
104  }
105  Iterator operator->() const { return (m_Iterator - 1); }
106  ValueType & operator*() const { return *(m_Iterator - 1); }
107  bool
108  operator!=(const ReverseIterator & rit) const
109  {
110  return m_Iterator != rit.m_Iterator;
111  }
112  bool
113  operator==(const ReverseIterator & rit) const
114  {
115  return m_Iterator == rit.m_Iterator;
116  }
117 
118  private:
120  friend class ConstReverseIterator;
121  };
122 
128  {
129  public:
131  : m_Iterator(i)
132  {}
133  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
136  {
137  return ConstReverseIterator(--m_Iterator);
138  }
141  {
142  return ConstReverseIterator(m_Iterator--);
143  }
146  {
147  return ConstReverseIterator(++m_Iterator);
148  }
151  {
152  return ConstReverseIterator(m_Iterator++);
153  }
154  ConstIterator operator->() const { return (m_Iterator - 1); }
155  const ValueType & operator*() const { return *(m_Iterator - 1); }
156  bool
157  operator!=(const ConstReverseIterator & rit) const
158  {
159  return m_Iterator != rit.m_Iterator;
160  }
161  bool
162  operator==(const ConstReverseIterator & rit) const
163  {
164  return m_Iterator == rit.m_Iterator;
165  }
166 
167  private:
169  };
170 
172  using pointer = ValueType *;
173 
175  using const_pointer = const ValueType *;
176 
178  using reference = ValueType &;
179 
181  using const_reference = const ValueType &;
182 
184  using iterator = ValueType *;
185 
187  using const_iterator = const ValueType *;
188 
189  using reverse_iterator = std::reverse_iterator<iterator>;
190 
191  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
192 
193  using SizeType = unsigned int;
194 
195 public:
197  FixedArray() = default;
198  FixedArray(const FixedArray &) = default;
199  FixedArray &
200  operator=(const FixedArray &) = default;
201  FixedArray(FixedArray &&) = default;
202  FixedArray &
203  operator=(FixedArray &&) = default;
204  ~FixedArray() = default;
206 
208  FixedArray(const ValueType r[VLength]);
209  FixedArray(const ValueType &);
211 
213  explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
214  {
215  std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
216  }
217 
219  template <typename TFixedArrayValueType>
221  {
222  auto input = r.cbegin();
223 
224  for (auto & element : m_InternalArray)
225  {
226  element = static_cast<TValue>(*input++);
227  }
228  }
229 
230  template <typename TScalarValue>
231  FixedArray(const TScalarValue * r)
232  {
233  std::copy_n(r, VLength, m_InternalArray);
234  }
235 
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  bool
260  operator!=(const FixedArray & r) const
261  {
262  return !operator==(r);
263  }
264 
268  reference operator[](short index) { return m_InternalArray[index]; }
269  const_reference operator[](short index) const { return m_InternalArray[index]; }
270  reference operator[](unsigned short index) { return m_InternalArray[index]; }
271  const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
272  reference operator[](int index) { return m_InternalArray[index]; }
273  const_reference operator[](int index) const { return m_InternalArray[index]; }
274 // false positive warnings with GCC
275 #if defined(__GNUC__)
276 # if (__GNUC__ == 4) && (__GNUC_MINOR__ == 9) || (__GNUC__ >= 7)
277 # pragma GCC diagnostic push
278 # pragma GCC diagnostic ignored "-Warray-bounds"
279 # endif
280 #endif
281  reference operator[](unsigned int index) { return m_InternalArray[index]; }
282  const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
283 #if defined(__GNUC__)
284 # if (__GNUC__ == 4) && (__GNUC_MINOR__ == 9) || (__GNUC__ >= 7)
285 # pragma GCC diagnostic pop
286 # endif
287 #endif
288  reference operator[](long index) { return m_InternalArray[index]; }
289  const_reference operator[](long index) const { return m_InternalArray[index]; }
290  reference operator[](unsigned long index) { return m_InternalArray[index]; }
291  const_reference operator[](unsigned long index) const { return m_InternalArray[index]; }
292  reference operator[](long long index) { return m_InternalArray[index]; }
293  const_reference operator[](long long index) const { return m_InternalArray[index]; }
294  reference operator[](unsigned long long index) { return m_InternalArray[index]; }
295  const_reference operator[](unsigned long long index) const { return m_InternalArray[index]; }
297 
299  void
300  SetElement(unsigned int index, const_reference value)
301  {
302  m_InternalArray[index] = value;
303  }
304  const_reference
305  GetElement(unsigned int index) const
306  {
307  return m_InternalArray[index];
308  }
310 
312  ValueType *
314  {
315  return m_InternalArray;
316  }
317 
318  const ValueType *
320  {
321  return m_InternalArray;
322  }
323 
324  ValueType *
326  {
327  return m_InternalArray;
328  }
329 
330  const ValueType *
331  data() const
332  {
333  return m_InternalArray;
334  }
335 
337  Iterator
338  Begin();
339 
340  ConstIterator
341  Begin() const;
342 
343  Iterator
344  End();
345 
346  ConstIterator
347  End() const;
348 
349  itkLegacyMacro(ReverseIterator rBegin());
350 
351  itkLegacyMacro(ConstReverseIterator rBegin() const);
352 
353  itkLegacyMacro(ReverseIterator rEnd());
354 
355  itkLegacyMacro(ConstReverseIterator rEnd() const);
356 
357  const_iterator
358  cbegin() const noexcept
359  {
360  return m_InternalArray;
361  }
362 
363  iterator
364  begin() noexcept
365  {
366  return m_InternalArray;
367  }
368 
369  const_iterator
370  begin() const noexcept
371  {
372  return this->cbegin();
373  }
374 
375  const_iterator
376  cend() const noexcept
377  {
378  return m_InternalArray + VLength;
379  }
380 
381  iterator
382  end() noexcept
383  {
384  return m_InternalArray + VLength;
385  }
386 
387  const_iterator
388  end() const noexcept
389  {
390  return this->cend();
391  }
392 
393  reverse_iterator
395  {
396  return reverse_iterator{ this->end() };
397  }
398 
399  const_reverse_iterator
400  crbegin() const
401  {
402  return const_reverse_iterator{ this->cend() };
403  }
404 
405  const_reverse_iterator
406  rbegin() const
407  {
408  return this->crbegin();
409  }
410 
411  reverse_iterator
413  {
414  return reverse_iterator{ this->begin() };
415  }
416 
417  const_reverse_iterator
418  crend() const
419  {
420  return const_reverse_iterator{ this->cbegin() };
421  }
422 
423  const_reverse_iterator
424  rend() const
425  {
426  return this->crend();
427  }
428 
430  SizeType
431  Size() const;
432 
433  constexpr SizeType
434  size() const
435  {
436  return VLength;
437  }
438 
440  void
441  Fill(const ValueType &);
442 
443  void
444  swap(FixedArray & other)
445  {
446  std::swap(m_InternalArray, other.m_InternalArray);
447  }
448 
449 private:
452 
453 public:
454  static FixedArray
455  Filled(const ValueType &);
456 };
457 
458 template <typename TValue, unsigned int VLength>
459 std::ostream &
460 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
461 
462 
463 template <typename TValue, unsigned int VLength>
464 inline void
466 {
467  a.swap(b);
468 }
469 
470 } // namespace itk
471 
472 #ifndef ITK_MANUAL_INSTANTIATION
473 # include "itkFixedArray.hxx"
474 #endif
475 
477 
478 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:424
itk::FixedArray::operator[]
const_reference operator[](int index) const
Definition: itkFixedArray.h:273
itk::FixedArray::ConstReverseIterator::m_Iterator
ConstIterator m_Iterator
Definition: itkFixedArray.h:168
itk::FixedArray::operator[]
reference operator[](long index)
Definition: itkFixedArray.h:288
itk::FixedArray::FixedArray
FixedArray(const TScalarValue *r)
Definition: itkFixedArray.h:231
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::FixedArray< float, Self::ImageDimension >::CArray
ValueType[VLength] CArray
Definition: itkFixedArray.h:65
itk::FixedArray::data
const ValueType * data() const
Definition: itkFixedArray.h:331
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--(int)
Definition: itkFixedArray.h:101
itk::FixedArray::size
constexpr SizeType size() const
Definition: itkFixedArray.h:434
itk::FixedArray< float, Self::ImageDimension >::const_reference
const ValueType & const_reference
Definition: itkFixedArray.h:181
itk::FixedArray::ConstReverseIterator
A const reverse iterator through an array.
Definition: itkFixedArray.h:127
itk::FixedArray::FixedArray
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:220
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++(int)
Definition: itkFixedArray.h:140
itk::FixedArray::ReverseIterator
A reverse iterator through an array.
Definition: itkFixedArray.h:79
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
itk::FixedArray::operator[]
reference operator[](unsigned long index)
Definition: itkFixedArray.h:290
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::FixedArray::m_InternalArray
CArray m_InternalArray
Definition: itkFixedArray.h:451
itk::FixedArray::operator[]
reference operator[](unsigned int index)
Definition: itkFixedArray.h:281
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(const ReverseIterator &rit)
Definition: itkFixedArray.h:133
itk::FixedArray::ReverseIterator::m_Iterator
Iterator m_Iterator
Definition: itkFixedArray.h:119
itk::FixedArray::GetElement
const_reference GetElement(unsigned int index) const
Definition: itkFixedArray.h:305
itk::FixedArray< float, Self::ImageDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:172
itk::FixedArray::operator[]
const_reference operator[](long long index) const
Definition: itkFixedArray.h:293
itk::FixedArray::operator=
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:239
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:313
itk::FixedArray::operator[]
reference operator[](int index)
Definition: itkFixedArray.h:272
itk::FixedArray::operator[]
reference operator[](unsigned short index)
Definition: itkFixedArray.h:270
itk::FixedArray::ReverseIterator::ReverseIterator
ReverseIterator(Iterator i)
Definition: itkFixedArray.h:82
itk::FixedArray< float, Self::ImageDimension >::ConstIterator
const ValueType * ConstIterator
Definition: itkFixedArray.h:71
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--(int)
Definition: itkFixedArray.h:150
itk::FixedArray::end
const_iterator end() const noexcept
Definition: itkFixedArray.h:388
itk::FixedArray::cend
const_iterator cend() const noexcept
Definition: itkFixedArray.h:376
itkMacro.h
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(ConstIterator i)
Definition: itkFixedArray.h:130
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:319
itk::FixedArray::ReverseIterator::operator!=
bool operator!=(const ReverseIterator &rit) const
Definition: itkFixedArray.h:108
itk::FixedArray::operator[]
reference operator[](unsigned long long index)
Definition: itkFixedArray.h:294
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--()
Definition: itkFixedArray.h:96
itk::FixedArray::ConstReverseIterator::operator->
ConstIterator operator->() const
Definition: itkFixedArray.h:154
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++(int)
Definition: itkFixedArray.h:91
itk::FixedArray::operator[]
const_reference operator[](unsigned int index) const
Definition: itkFixedArray.h:282
itk::FixedArray::operator[]
const_reference operator[](unsigned long index) const
Definition: itkFixedArray.h:291
itk::FixedArray< float, Self::ImageDimension >::const_pointer
const ValueType * const_pointer
Definition: itkFixedArray.h:175
itkNumericTraitsFixedArrayPixel.h
itk::FixedArray::end
iterator end() noexcept
Definition: itkFixedArray.h:382
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++()
Definition: itkFixedArray.h:86
itk::FixedArray::operator[]
reference operator[](short index)
Definition: itkFixedArray.h:268
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:532
itk::FixedArray::operator[]
const_reference operator[](unsigned short index) const
Definition: itkFixedArray.h:271
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--()
Definition: itkFixedArray.h:145
itk::FixedArray
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:52
itk::FixedArray< float, Self::ImageDimension >::iterator
ValueType * iterator
Definition: itkFixedArray.h:184
itk::FixedArray::operator!=
bool operator!=(const FixedArray &r) const
Definition: itkFixedArray.h:260
itk::FixedArray< float, Self::ImageDimension >::SizeType
unsigned int SizeType
Definition: itkFixedArray.h:193
itk::FixedArray::ReverseIterator::operator*
ValueType & operator*() const
Definition: itkFixedArray.h:106
itk::FixedArray::data
ValueType * data()
Definition: itkFixedArray.h:325
itk::FixedArray< float, Self::ImageDimension >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkFixedArray.h:189
itk::FixedArray::ConstReverseIterator::operator!=
bool operator!=(const ConstReverseIterator &rit) const
Definition: itkFixedArray.h:157
itk::swap
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b)
Definition: itkFixedArray.h:465
itk::FixedArray::begin
const_iterator begin() const noexcept
Definition: itkFixedArray.h:370
itk::FixedArray::operator[]
const_reference operator[](long index) const
Definition: itkFixedArray.h:289
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:400
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:135
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:394
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:105
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:444
itk::FixedArray::ConstReverseIterator::operator*
const ValueType & operator*() const
Definition: itkFixedArray.h:155
itk::FixedArray::operator[]
reference operator[](long long index)
Definition: itkFixedArray.h:292
itk::FixedArray::ReverseIterator::operator==
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:113
itk::FixedArray< float, Self::ImageDimension >::reference
ValueType & reference
Definition: itkFixedArray.h:178
itk::FixedArray::ConstReverseIterator::operator==
bool operator==(const ConstReverseIterator &rit) const
Definition: itkFixedArray.h:162
itk::FixedArray::rend
reverse_iterator rend()
Definition: itkFixedArray.h:412
itk::FixedArray< float, Self::ImageDimension >::const_iterator
const ValueType * const_iterator
Definition: itkFixedArray.h:187
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray::crend
const_reverse_iterator crend() const
Definition: itkFixedArray.h:418
itk::FixedArray::operator[]
const_reference operator[](short index) const
Definition: itkFixedArray.h:269
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:406
itk::FixedArray< float, Self::ImageDimension >::Iterator
ValueType * Iterator
Definition: itkFixedArray.h:68
itk::FixedArray::SetElement
void SetElement(unsigned int index, const_reference value)
Definition: itkFixedArray.h:300
itk::FixedArray::begin
iterator begin() noexcept
Definition: itkFixedArray.h:364
itk::FixedArray< float, Self::ImageDimension >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkFixedArray.h:191
itk::FixedArray::ValueType
TValue ValueType
Definition: itkFixedArray.h:62
itk::FixedArray::operator[]
const_reference operator[](unsigned long long index) const
Definition: itkFixedArray.h:295
itk::FixedArray::cbegin
const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:358
itk::FixedArray::FixedArray
FixedArray(const std::array< ValueType, VLength > &stdArray)
Definition: itkFixedArray.h:213