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  * 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 
108  bool
109  operator==(const ReverseIterator & rit) const
110  {
111  return m_Iterator == rit.m_Iterator;
112  }
113 
114  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ReverseIterator);
115 
116  private:
118  friend class ConstReverseIterator;
119  };
120 
126  {
127  public:
129  : m_Iterator(i)
130  {}
131  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
134  {
135  return ConstReverseIterator(--m_Iterator);
136  }
139  {
140  return ConstReverseIterator(m_Iterator--);
141  }
144  {
145  return ConstReverseIterator(++m_Iterator);
146  }
149  {
150  return ConstReverseIterator(m_Iterator++);
151  }
152  ConstIterator operator->() const { return (m_Iterator - 1); }
153  const ValueType & operator*() const { return *(m_Iterator - 1); }
154 
155  bool
156  operator==(const ConstReverseIterator & rit) const
157  {
158  return m_Iterator == rit.m_Iterator;
159  }
160 
161  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstReverseIterator);
162 
163  private:
165  };
166 
168  using pointer = ValueType *;
169 
171  using const_pointer = const ValueType *;
172 
174  using reference = ValueType &;
175 
177  using const_reference = const ValueType &;
178 
180  using iterator = ValueType *;
181 
183  using const_iterator = const ValueType *;
184 
185  using reverse_iterator = std::reverse_iterator<iterator>;
186 
187  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
188 
189  using SizeType = unsigned int;
190 
191 public:
193  FixedArray() = default;
194  FixedArray(const FixedArray &) = default;
195  FixedArray &
196  operator=(const FixedArray &) = default;
197  FixedArray(FixedArray &&) = default;
198  FixedArray &
199  operator=(FixedArray &&) = default;
200  ~FixedArray() = default;
202 
204  FixedArray(const ValueType r[VLength]);
205  FixedArray(const ValueType &);
207 
209  explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
210  {
211  std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
212  }
213 
215  template <typename TFixedArrayValueType>
217  {
218  auto input = r.cbegin();
219 
220  for (auto & element : m_InternalArray)
221  {
222  element = static_cast<TValue>(*input++);
223  }
224  }
225 
226  template <typename TScalarValue>
227  FixedArray(const TScalarValue * r)
228  {
229  std::copy_n(r, VLength, m_InternalArray);
230  }
231 
233  template <typename TFixedArrayValueType>
234  FixedArray &
236  {
237  auto input = r.cbegin();
238 
239  for (auto & element : m_InternalArray)
240  {
241  element = static_cast<TValue>(*input++);
242  }
243  return *this;
244  }
245 
246  FixedArray &
247  operator=(const ValueType r[VLength]);
248 
252  bool
253  operator==(const FixedArray & r) const;
254 
255  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray);
256 
257 
260 // false positive warnings with GCC
261 #if defined(__GNUC__)
262 # if (__GNUC__ == 4) && (__GNUC_MINOR__ == 9) || (__GNUC__ >= 7)
263 # pragma GCC diagnostic push
264 # pragma GCC diagnostic ignored "-Warray-bounds"
265 # endif
266 #endif
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 #if defined(__GNUC__)
270 # if (__GNUC__ == 4) && (__GNUC_MINOR__ == 9) || (__GNUC__ >= 7)
271 # pragma GCC diagnostic pop
272 # endif
273 #endif
274 
275 
277  void
278  SetElement(unsigned int index, const_reference value)
279  {
280  m_InternalArray[index] = value;
281  }
282  const_reference
283  GetElement(unsigned int index) const
284  {
285  return m_InternalArray[index];
286  }
288 
290  ValueType *
292  {
293  return m_InternalArray;
294  }
295 
296  const ValueType *
298  {
299  return m_InternalArray;
300  }
301 
302  ValueType *
304  {
305  return m_InternalArray;
306  }
307 
308  const ValueType *
309  data() const
310  {
311  return m_InternalArray;
312  }
313 
315  Iterator
316  Begin();
317 
318  ConstIterator
319  Begin() const;
320 
321  Iterator
322  End();
323 
324  ConstIterator
325  End() const;
326 
327  itkLegacyMacro(ReverseIterator rBegin());
328 
329  itkLegacyMacro(ConstReverseIterator rBegin() const);
330 
331  itkLegacyMacro(ReverseIterator rEnd());
332 
333  itkLegacyMacro(ConstReverseIterator rEnd() const);
334 
335  const_iterator
336  cbegin() const noexcept
337  {
338  return m_InternalArray;
339  }
340 
341  iterator
342  begin() noexcept
343  {
344  return m_InternalArray;
345  }
346 
347  const_iterator
348  begin() const noexcept
349  {
350  return this->cbegin();
351  }
352 
353  const_iterator
354  cend() const noexcept
355  {
356  return m_InternalArray + VLength;
357  }
358 
359  iterator
360  end() noexcept
361  {
362  return m_InternalArray + VLength;
363  }
364 
365  const_iterator
366  end() const noexcept
367  {
368  return this->cend();
369  }
370 
371  reverse_iterator
373  {
374  return reverse_iterator{ this->end() };
375  }
376 
377  const_reverse_iterator
378  crbegin() const
379  {
380  return const_reverse_iterator{ this->cend() };
381  }
382 
383  const_reverse_iterator
384  rbegin() const
385  {
386  return this->crbegin();
387  }
388 
389  reverse_iterator
391  {
392  return reverse_iterator{ this->begin() };
393  }
394 
395  const_reverse_iterator
396  crend() const
397  {
398  return const_reverse_iterator{ this->cbegin() };
399  }
400 
401  const_reverse_iterator
402  rend() const
403  {
404  return this->crend();
405  }
406 
408  SizeType
409  Size() const;
410 
411  constexpr SizeType
412  size() const
413  {
414  return VLength;
415  }
416 
418  void
419  Fill(const ValueType &);
420 
421  void
422  swap(FixedArray & other)
423  {
424  std::swap(m_InternalArray, other.m_InternalArray);
425  }
426 
427 private:
430 
431 public:
433  static constexpr FixedArray
434  Filled(const ValueType & value)
435  {
436  FixedArray result{};
437  for (ValueType & element : result.m_InternalArray)
438  {
439  element = value;
440  }
441  return result;
442  }
443 };
445 
446 template <typename TValue, unsigned int VLength>
447 std::ostream &
448 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
449 
450 
451 template <typename TValue, unsigned int VLength>
452 inline void
454 {
455  a.swap(b);
456 }
457 
458 } // namespace itk
459 
460 #ifndef ITK_MANUAL_INSTANTIATION
461 # include "itkFixedArray.hxx"
462 #endif
463 
465 
466 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:402
itk::FixedArray::ConstReverseIterator::m_Iterator
ConstIterator m_Iterator
Definition: itkFixedArray.h:164
itk::FixedArray::FixedArray
FixedArray(const TScalarValue *r)
Definition: itkFixedArray.h:227
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:309
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:101
itk::FixedArray::size
constexpr SizeType size() const
Definition: itkFixedArray.h:412
itk::FixedArray< float, Self::ImageDimension >::const_reference
const ValueType & const_reference
Definition: itkFixedArray.h:177
itk::FixedArray::ConstReverseIterator
A const reverse iterator through an array.
Definition: itkFixedArray.h:125
itk::FixedArray::FixedArray
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:216
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++(int)
Definition: itkFixedArray.h:138
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:242
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::FixedArray::m_InternalArray
CArray m_InternalArray
Definition: itkFixedArray.h:429
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(const ReverseIterator &rit)
Definition: itkFixedArray.h:131
itk::FixedArray::ReverseIterator::m_Iterator
Iterator m_Iterator
Definition: itkFixedArray.h:117
itk::FixedArray::GetElement
const_reference GetElement(unsigned int index) const
Definition: itkFixedArray.h:283
itk::FixedArray< float, Self::ImageDimension >::pointer
ValueType * pointer
Definition: itkFixedArray.h:168
itk::FixedArray::operator=
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
Definition: itkFixedArray.h:235
itk::FixedArray::operator[]
constexpr const_reference operator[](unsigned int index) const
Definition: itkFixedArray.h:268
itk::FixedArray::GetDataPointer
ValueType * GetDataPointer()
Definition: itkFixedArray.h:291
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:148
itk::FixedArray::end
const_iterator end() const noexcept
Definition: itkFixedArray.h:366
itk::FixedArray::cend
const_iterator cend() const noexcept
Definition: itkFixedArray.h:354
itkMacro.h
itk::FixedArray::ConstReverseIterator::ConstReverseIterator
ConstReverseIterator(ConstIterator i)
Definition: itkFixedArray.h:128
itk::FixedArray::operator[]
constexpr reference operator[](unsigned int index)
Definition: itkFixedArray.h:267
itk::FixedArray::GetDataPointer
const ValueType * GetDataPointer() const
Definition: itkFixedArray.h:297
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--()
Definition: itkFixedArray.h:96
itk::FixedArray::ConstReverseIterator::operator->
ConstIterator operator->() const
Definition: itkFixedArray.h:152
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++(int)
Definition: itkFixedArray.h:91
itk::FixedArray< float, Self::ImageDimension >::const_pointer
const ValueType * const_pointer
Definition: itkFixedArray.h:171
itkNumericTraitsFixedArrayPixel.h
itk::FixedArray::end
iterator end() noexcept
Definition: itkFixedArray.h:360
itk::FixedArray::ReverseIterator::operator++
ReverseIterator operator++()
Definition: itkFixedArray.h:86
itk::operator==
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:536
itk::FixedArray::ConstReverseIterator::operator--
ConstReverseIterator operator--()
Definition: itkFixedArray.h:143
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:180
itk::FixedArray< float, Self::ImageDimension >::SizeType
unsigned int SizeType
Definition: itkFixedArray.h:189
itk::FixedArray::ReverseIterator::operator*
ValueType & operator*() const
Definition: itkFixedArray.h:106
itk::FixedArray::data
ValueType * data()
Definition: itkFixedArray.h:303
itk::FixedArray< float, Self::ImageDimension >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: itkFixedArray.h:185
itk::swap
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b)
Definition: itkFixedArray.h:453
itk::FixedArray::begin
const_iterator begin() const noexcept
Definition: itkFixedArray.h:348
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:378
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:133
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:372
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:105
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:422
itk::FixedArray::ConstReverseIterator::operator*
const ValueType & operator*() const
Definition: itkFixedArray.h:153
itk::FixedArray::Filled
static constexpr FixedArray Filled(const ValueType &value)
Definition: itkFixedArray.h:434
itk::FixedArray::ReverseIterator::operator==
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:109
itk::FixedArray< float, Self::ImageDimension >::reference
ValueType & reference
Definition: itkFixedArray.h:174
itk::FixedArray::ConstReverseIterator::operator==
bool operator==(const ConstReverseIterator &rit) const
Definition: itkFixedArray.h:156
itk::FixedArray::rend
reverse_iterator rend()
Definition: itkFixedArray.h:390
itk::FixedArray< float, Self::ImageDimension >::const_iterator
const ValueType * const_iterator
Definition: itkFixedArray.h:183
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::FixedArray::crend
const_reverse_iterator crend() const
Definition: itkFixedArray.h:396
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:384
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:278
itk::FixedArray::begin
iterator begin() noexcept
Definition: itkFixedArray.h:342
itk::FixedArray< float, Self::ImageDimension >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: itkFixedArray.h:187
itk::FixedArray::ValueType
TValue ValueType
Definition: itkFixedArray.h:62
itk::FixedArray::cbegin
const_iterator cbegin() const noexcept
Definition: itkFixedArray.h:336
itk::FixedArray::FixedArray
FixedArray(const std::array< ValueType, VLength > &stdArray)
Definition: itkFixedArray.h:209