ITK  5.0.0
Insight Segmentation and Registration 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 
24 namespace itk
25 {
26 
50 template< typename TValue, unsigned int VLength = 3 >
51 class ITK_TEMPLATE_EXPORT FixedArray
52 {
53 public:
55  static constexpr unsigned int Length = VLength;
56 
58  static constexpr unsigned int Dimension = VLength;
59 
61  using ValueType = TValue;
62 
64  typedef ValueType CArray[VLength];
65 
67  using Iterator = ValueType *;
68 
70  using ConstIterator = const ValueType *;
71 
72  class ConstReverseIterator;
73 
79  {
80  public:
81  explicit ReverseIterator(Iterator i):m_Iterator(i) {}
82  Iterator operator++() { return --m_Iterator; }
83  Iterator operator++(int) { return m_Iterator--; }
84  Iterator operator--() { return ++m_Iterator; }
85  Iterator operator--(int) { return m_Iterator++; }
86  Iterator operator->() const { return ( m_Iterator - 1 ); }
87  ValueType & operator*() const { return *( m_Iterator - 1 ); }
88  bool operator!=(const ReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
89  bool operator==(const ReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
90 
91  private:
93  friend class ConstReverseIterator;
94  };
95 
101  {
102  public:
103  explicit ConstReverseIterator(ConstIterator i):m_Iterator(i) {}
104  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
105  ConstIterator operator++() { return --m_Iterator; }
106  ConstIterator operator++(int) { return m_Iterator--; }
107  ConstIterator operator--() { return ++m_Iterator; }
108  ConstIterator operator--(int) { return m_Iterator++; }
109  ConstIterator operator->() const { return ( m_Iterator - 1 ); }
110  const ValueType & operator*() const { return *( m_Iterator - 1 ); }
111  bool operator!=(const ConstReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
112  bool operator==(const ConstReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
113 
114  private:
116  };
117 
119  using pointer = ValueType *;
120 
122  using const_pointer = const ValueType *;
123 
125  using reference = ValueType &;
126 
128  using const_reference = const ValueType &;
129 
130  using SizeType = unsigned int;
131 
132 public:
134  FixedArray() = default;
135  FixedArray(const FixedArray &) = default;
136  FixedArray & operator=(const FixedArray & ) = default;
137  FixedArray(FixedArray &&) = default;
138  FixedArray & operator=(FixedArray && ) = default;
139  ~FixedArray() = default;
141 
143  FixedArray(const ValueType r[VLength]);
144  FixedArray(const ValueType & );
146 
148  template< typename TFixedArrayValueType >
150  {
152  Iterator i = this->Begin();
153  while ( i != this->End() )
154  {
155  *i++ = static_cast< TValue >( *input++ );
156  }
157  }
159 
160  template< typename TScalarValue >
161  FixedArray(const TScalarValue *r)
162  {
163  std::copy(r, r + this->Size(), this->GetDataPointer());
164  }
165 
167  template< typename TFixedArrayValueType >
169  {
170  if ( (const void *)r.Begin() != (const void *)m_InternalArray )
171  {
173  Iterator i = this->Begin();
174  while ( i != this->End() )
175  {
176  *i++ = static_cast< TValue >( *input++ );
177  }
178  }
179  return *this;
180  }
182 
183  FixedArray & operator=(const ValueType r[VLength]);
184 
188  bool operator==(const FixedArray & r) const;
189 
190  bool operator!=(const FixedArray & r) const
191  { return !operator==(r); }
192 
196  reference operator[](short index) { return m_InternalArray[index]; }
197  const_reference operator[](short index) const { return m_InternalArray[index]; }
198  reference operator[](unsigned short index) { return m_InternalArray[index]; }
199  const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
200  reference operator[](int index) { return m_InternalArray[index]; }
201  const_reference operator[](int index) const { return m_InternalArray[index]; }
202 // false positive warnings with GCC
203 #if defined( __GNUC__ )
204 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) || ( __GNUC__ >= 7 )
205 #pragma GCC diagnostic push
206 #pragma GCC diagnostic ignored "-Warray-bounds"
207 #endif
208 #endif
209  reference operator[](unsigned int index) { return m_InternalArray[index]; }
210  const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
211 #if defined( __GNUC__ )
212 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) || ( __GNUC__ >= 7 )
213 #pragma GCC diagnostic pop
214 #endif
215 #endif
216  reference operator[](long index) { return m_InternalArray[index]; }
217  const_reference operator[](long index) const { return m_InternalArray[index]; }
218  reference operator[](unsigned long index) { return m_InternalArray[index]; }
219  const_reference operator[](unsigned long index) const { return m_InternalArray[index]; }
220  reference operator[](long long index) { return m_InternalArray[index]; }
221  const_reference operator[](long long index) const { return m_InternalArray[index]; }
222  reference operator[](unsigned long long index) { return m_InternalArray[index]; }
223  const_reference operator[](unsigned long long index) const { return m_InternalArray[index]; }
225 
227  void SetElement(unsigned short index, const_reference value)
228  { m_InternalArray[index] = value; }
229  const_reference GetElement(unsigned short index) const { return m_InternalArray[index]; }
231 
234  {
235  return m_InternalArray; \
236  }
237 
238  const ValueType * GetDataPointer() const
239  {
240  return m_InternalArray; \
241  }
242 
244  Iterator Begin();
245 
246  ConstIterator Begin() const;
247 
248  Iterator End();
249 
250  ConstIterator End() const;
251 
252  ReverseIterator rBegin();
253 
254  ConstReverseIterator rBegin() const;
255 
256  ReverseIterator rEnd();
257 
258  ConstReverseIterator rEnd() const;
259 
260  SizeType Size() const;
261 
262  void Fill(const ValueType &);
263 
264  void swap(FixedArray &other)
265  {
266  std::swap_ranges(this->Begin(), this->End(), other.Begin());
267  }
268 
269 private:
272 
273 public:
274 
275  static FixedArray Filled(const ValueType &);
276 };
277 
278 template< typename TValue, unsigned int VLength >
279 std::ostream & operator<<(std::ostream & os, const FixedArray< TValue, VLength > & arr);
280 
281 
282 template< typename TValue, unsigned int VLength >
284 {
285  a.swap(b);
286 }
287 
288 } // namespace itk
289 
290 #ifndef ITK_MANUAL_INSTANTIATION
291 #include "itkFixedArray.hxx"
292 #endif
293 
295 
296 #endif
void swap(FixedArray &other)
reference operator[](long index)
bool operator==(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:485
const_reference operator[](unsigned short index) const
A const reverse iterator through an array.
bool operator==(const ConstReverseIterator &rit) const
ConstReverseIterator(const ReverseIterator &rit)
reference operator[](short index)
const_reference GetElement(unsigned short index) const
reference operator[](int index)
bool operator!=(const FixedArray &r) const
reference operator[](unsigned int index)
FixedArray(const TScalarValue *r)
const_reference operator[](long long index) const
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:89
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:51
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
reference operator[](unsigned short index)
reference operator[](long long index)
CArray m_InternalArray
bool operator!=(const ReverseIterator &rit) const
Definition: itkFixedArray.h:88
const_reference operator[](short index) const
void SetElement(unsigned short index, const_reference value)
A reverse iterator through an array.
Definition: itkFixedArray.h:78
const_reference operator[](long index) const
const_reference operator[](unsigned long index) const
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
bool operator!=(const ConstReverseIterator &rit) const
const_reference operator[](unsigned long long index) const
Iterator Begin()
const_reference operator[](unsigned int index) const
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
const_reference operator[](int index) const
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
reference operator[](unsigned long long index)
ValueType * GetDataPointer()
const ValueType & operator*() const
const ValueType * GetDataPointer() const
reference operator[](unsigned long index)
ValueType & operator*() const
Definition: itkFixedArray.h:87