ITK  4.13.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 
23 namespace itk
24 {
25 
49 template< typename TValue, unsigned int VLength = 3 >
50 class ITK_TEMPLATE_EXPORT FixedArray
51 {
52 public:
54  itkStaticConstMacro(Length, unsigned int, VLength);
55 
57  itkStaticConstMacro(Dimension, unsigned int, VLength);
58 
60  typedef TValue ValueType;
61 
63  typedef ValueType CArray[VLength];
64 
66  typedef ValueType *Iterator;
67 
69  typedef const ValueType *ConstIterator;
70 
71  class ConstReverseIterator;
72 
78  {
79  public:
80  explicit ReverseIterator(Iterator i):m_Iterator(i) {}
81  Iterator operator++() { return --m_Iterator; }
82  Iterator operator++(int) { return m_Iterator--; }
83  Iterator operator--() { return ++m_Iterator; }
84  Iterator operator--(int) { return m_Iterator++; }
85  Iterator operator->() const { return ( m_Iterator - 1 ); }
86  ValueType & operator*() const { return *( m_Iterator - 1 ); }
87  bool operator!=(const ReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
88  bool operator==(const ReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
89 
90  private:
92  friend class ConstReverseIterator;
93  };
94 
100  {
101  public:
102  explicit ConstReverseIterator(ConstIterator i):m_Iterator(i) {}
103  ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
104  ConstIterator operator++() { return --m_Iterator; }
105  ConstIterator operator++(int) { return m_Iterator--; }
106  ConstIterator operator--() { return ++m_Iterator; }
107  ConstIterator operator--(int) { return m_Iterator++; }
108  ConstIterator operator->() const { return ( m_Iterator - 1 ); }
109  const ValueType & operator*() const { return *( m_Iterator - 1 ); }
110  bool operator!=(const ConstReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
111  bool operator==(const ConstReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
112 
113  private:
115  };
116 
118  typedef ValueType *pointer;
119 
121  typedef const ValueType *const_pointer;
122 
124  typedef ValueType & reference;
125 
127  typedef const ValueType & const_reference;
128 
129  typedef unsigned int SizeType;
130 
131 public:
133  FixedArray();
134  FixedArray(const ValueType r[VLength]);
135  FixedArray(const ValueType & r);
137 
139  template< typename TFixedArrayValueType >
141  {
143  Iterator i = this->Begin();
144  while ( i != this->End() )
145  {
146  *i++ = static_cast< TValue >( *input++ );
147  }
148  }
150 
151  template< typename TScalarValue >
152  FixedArray(const TScalarValue *r)
153  {
154  std::copy(r, r + this->Size(), this->GetDataPointer());
155  }
156 
171  template< typename TFixedArrayValueType >
173  {
174  if ( (const void *)r.Begin() != (const void *)m_InternalArray )
175  {
177  Iterator i = this->Begin();
178  while ( i != this->End() )
179  {
180  *i++ = static_cast< TValue >( *input++ );
181  }
182  }
183  return *this;
184  }
186 
187  FixedArray & operator=(const ValueType r[VLength]);
188 
192  bool operator==(const FixedArray & r) const;
193 
194  bool operator!=(const FixedArray & r) const
195  { return !operator==(r); }
196 
200  reference operator[](short index) { return m_InternalArray[index]; }
201  const_reference operator[](short index) const { return m_InternalArray[index]; }
202  reference operator[](unsigned short index) { return m_InternalArray[index]; }
203  const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
204  reference operator[](int index) { return m_InternalArray[index]; }
205  const_reference operator[](int index) const { return m_InternalArray[index]; }
206 // false positive warnings with GCC
207 #if defined( __GNUC__ )
208 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) || ( __GNUC__ >= 7 )
209 #pragma GCC diagnostic push
210 #pragma GCC diagnostic ignored "-Warray-bounds"
211 #endif
212 #endif
213  reference operator[](unsigned int index) { return m_InternalArray[index]; }
214  const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
215 #if defined( __GNUC__ )
216 #if ( __GNUC__ == 4 ) && ( __GNUC_MINOR__ == 9 ) || ( __GNUC__ >= 7 )
217 #pragma GCC diagnostic pop
218 #endif
219 #endif
220  reference operator[](long index) { return m_InternalArray[index]; }
221  const_reference operator[](long index) const { return m_InternalArray[index]; }
222  reference operator[](unsigned long index) { return m_InternalArray[index]; }
223  const_reference operator[](unsigned long index) const { return m_InternalArray[index]; }
224  reference operator[](long long index) { return m_InternalArray[index]; }
225  const_reference operator[](long long index) const { return m_InternalArray[index]; }
226  reference operator[](unsigned long long index) { return m_InternalArray[index]; }
227  const_reference operator[](unsigned long long index) const { return m_InternalArray[index]; }
229 
231  void SetElement(unsigned short index, const_reference value)
232  { m_InternalArray[index] = value; }
233  const_reference GetElement(unsigned short index) const { return m_InternalArray[index]; }
235 
238  {
239  return m_InternalArray; \
240  }
241 
242  const ValueType * GetDataPointer() const
243  {
244  return m_InternalArray; \
245  }
246 
248  Iterator Begin();
249 
250  ConstIterator Begin() const;
251 
252  Iterator End();
253 
254  ConstIterator End() const;
255 
256  ReverseIterator rBegin();
257 
258  ConstReverseIterator rBegin() const;
259 
260  ReverseIterator rEnd();
261 
262  ConstReverseIterator rEnd() const;
263 
264  SizeType Size() const;
265 
266  void Fill(const ValueType &);
267 
268 private:
271 
272 public:
273 
274  static FixedArray Filled(const ValueType &);
275 };
276 
277 template< typename TValue, unsigned int VLength >
278 std::ostream & operator<<(std::ostream & os, const FixedArray< TValue, VLength > & arr);
279 } // namespace itk
280 
281 #ifndef ITK_MANUAL_INSTANTIATION
282 #include "itkFixedArray.hxx"
283 #endif
284 
286 
287 #endif
reference operator[](long index)
const_reference operator[](unsigned short index) const
A const reverse iterator through an array.
Definition: itkFixedArray.h:99
bool operator==(const ConstReverseIterator &rit) const
ConstReverseIterator(const ReverseIterator &rit)
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
reference operator[](short index)
const_reference GetElement(unsigned short index) const
reference operator[](int index)
unsigned int SizeType
ValueType * Iterator
Definition: itkFixedArray.h:66
bool operator!=(const FixedArray &r) const
const ValueType * const_pointer
reference operator[](unsigned int index)
FixedArray(const TScalarValue *r)
const ValueType & const_reference
const_reference operator[](long long index) const
bool operator==(const ReverseIterator &rit) const
Definition: itkFixedArray.h:88
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
ValueType & reference
reference operator[](unsigned short index)
reference operator[](long long index)
CArray m_InternalArray
bool operator!=(const ReverseIterator &rit) const
Definition: itkFixedArray.h:87
const_reference operator[](short index) const
void SetElement(unsigned short index, const_reference value)
A reverse iterator through an array.
Definition: itkFixedArray.h:77
const_reference operator[](long index) const
const_reference operator[](unsigned long index) const
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)
ValueType * pointer
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
bool ITKIOXML_EXPORT operator==(itk::FancyString &s, const std::string &)
reference operator[](unsigned long index)
const ValueType * ConstIterator
Definition: itkFixedArray.h:69
ValueType & operator*() const
Definition: itkFixedArray.h:86