ITK  5.1.0
Insight Toolkit
itkFixedArray.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 
325  Iterator
326  Begin();
327 
328  ConstIterator
329  Begin() const;
330 
331  Iterator
332  End();
333 
334  ConstIterator
335  End() const;
336 
337  itkLegacyMacro(ReverseIterator rBegin());
338 
339  itkLegacyMacro(ConstReverseIterator rBegin() const);
340 
341  itkLegacyMacro(ReverseIterator rEnd());
342 
343  itkLegacyMacro(ConstReverseIterator rEnd() const);
344 
345  const_iterator
346  cbegin() const noexcept
347  {
348  return m_InternalArray;
349  }
350 
351  iterator
352  begin() noexcept
353  {
354  return m_InternalArray;
355  }
356 
357  const_iterator
358  begin() const noexcept
359  {
360  return this->cbegin();
361  }
362 
363  const_iterator
364  cend() const noexcept
365  {
366  return m_InternalArray + VLength;
367  }
368 
369  iterator
370  end() noexcept
371  {
372  return m_InternalArray + VLength;
373  }
374 
375  const_iterator
376  end() const noexcept
377  {
378  return this->cend();
379  }
380 
381  reverse_iterator
383  {
384  return reverse_iterator{ this->end() };
385  }
386 
387  const_reverse_iterator
388  crbegin() const
389  {
390  return const_reverse_iterator{ this->cend() };
391  }
392 
393  const_reverse_iterator
394  rbegin() const
395  {
396  return this->crbegin();
397  }
398 
399  reverse_iterator
401  {
402  return reverse_iterator{ this->begin() };
403  }
404 
405  const_reverse_iterator
406  crend() const
407  {
408  return const_reverse_iterator{ this->cbegin() };
409  }
410 
411  const_reverse_iterator
412  rend() const
413  {
414  return this->crend();
415  }
416 
417  SizeType
418  Size() const;
419 
420  void
421  Fill(const ValueType &);
422 
423  void
424  swap(FixedArray & other)
425  {
426  std::swap(m_InternalArray, other.m_InternalArray);
427  }
428 
429 private:
432 
433 public:
434  static FixedArray
435  Filled(const ValueType &);
436 };
437 
438 template <typename TValue, unsigned int VLength>
439 std::ostream &
440 operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
441 
442 
443 template <typename TValue, unsigned int VLength>
444 inline void
446 {
447  a.swap(b);
448 }
449 
450 } // namespace itk
451 
452 #ifndef ITK_MANUAL_INSTANTIATION
453 # include "itkFixedArray.hxx"
454 #endif
455 
457 
458 #endif
itk::FixedArray::rend
const_reverse_iterator rend() const
Definition: itkFixedArray.h:412
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::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
itk::FixedArray::ReverseIterator::operator--
ReverseIterator operator--(int)
Definition: itkFixedArray.h:101
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:237
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:431
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:376
itk::FixedArray::cend
const_iterator cend() const noexcept
Definition: itkFixedArray.h:364
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:370
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< 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:445
itk::FixedArray::begin
const_iterator begin() const noexcept
Definition: itkFixedArray.h:358
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: itkArray.h:26
itk::FixedArray::crbegin
const_reverse_iterator crbegin() const
Definition: itkFixedArray.h:388
itk::FixedArray::ConstReverseIterator::operator++
ConstReverseIterator operator++()
Definition: itkFixedArray.h:135
itk::FixedArray::rbegin
reverse_iterator rbegin()
Definition: itkFixedArray.h:382
itk::FixedArray::ReverseIterator::operator->
Iterator operator->() const
Definition: itkFixedArray.h:105
itk::FixedArray::swap
void swap(FixedArray &other)
Definition: itkFixedArray.h:424
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:400
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:406
itk::FixedArray::operator[]
const_reference operator[](short index) const
Definition: itkFixedArray.h:269
itk::FixedArray::rbegin
const_reverse_iterator rbegin() const
Definition: itkFixedArray.h:394
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:352
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:346
itk::FixedArray::FixedArray
FixedArray(const std::array< ValueType, VLength > &stdArray)
Definition: itkFixedArray.h:213