ITK  4.4.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 TValueType, unsigned int VLength = 3 >
51 {
52 public:
54  itkStaticConstMacro(Length, unsigned int, VLength);
55 
57  itkStaticConstMacro(Dimension, unsigned int, VLength);
58 
60  typedef TValueType 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:
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:
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< class TFixedArrayValueType >
141  {
143  Iterator i = this->Begin();
144  while ( i != this->End() )
145  {
146  *i++ = static_cast< TValueType >( *input++ );
147  }
148  }
150 
165  template< class TFixedArrayValueType >
167  {
168  if ( (void *)r.Begin() != (void *)m_InternalArray )
169  {
171  Iterator i = this->Begin();
172  while ( i != this->End() )
173  {
174  *i++ = static_cast< TValueType >( *input++ );
175  }
176  }
177  return *this;
178  }
180 
181  FixedArray & operator=(const ValueType r[VLength]);
182 
186  bool operator==(const FixedArray & r) const;
187 
188  bool operator!=(const FixedArray & r) const
189  { return !operator==(r); }
190 
194  reference operator[](short index) { return m_InternalArray[index]; }
195  const_reference operator[](short index) const { return m_InternalArray[index]; }
196  reference operator[](unsigned short index) { return m_InternalArray[index]; }
197  const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
198  reference operator[](int index) { return m_InternalArray[index]; }
199  const_reference operator[](int index) const { return m_InternalArray[index]; }
200  reference operator[](unsigned int index) { return m_InternalArray[index]; }
201  const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
202  reference operator[](long index) { return m_InternalArray[index]; }
203  const_reference operator[](long index) const { return m_InternalArray[index]; }
204  reference operator[](unsigned long index) { return m_InternalArray[index]; }
205  const_reference operator[](unsigned long index) const { return m_InternalArray[index]; }
206  reference operator[](long long index) { return m_InternalArray[index]; }
207  const_reference operator[](long long index) const { return m_InternalArray[index]; }
208  reference operator[](unsigned long long index) { return m_InternalArray[index]; }
209  const_reference operator[](unsigned long long index) const { return m_InternalArray[index]; }
211 
213  void SetElement(unsigned short index, const_reference value)
214  { m_InternalArray[index] = value; }
215  const_reference GetElement(unsigned short index) const { return m_InternalArray[index]; }
217 
220  {
221  return m_InternalArray; \
222  }
223 
224  const ValueType * GetDataPointer() const
225  {
226  return m_InternalArray; \
227  }
228 
230  Iterator Begin();
231 
232  ConstIterator Begin() const;
233 
234  Iterator End();
235 
236  ConstIterator End() const;
237 
238  ReverseIterator rBegin();
239 
240  ConstReverseIterator rBegin() const;
241 
242  ReverseIterator rEnd();
243 
244  ConstReverseIterator rEnd() const;
245 
246  SizeType Size() const;
247 
248  void Fill(const ValueType &);
249 
250 private:
253 
254 public:
255 
256  static FixedArray Filled(const ValueType &);
257 };
258 
259 template< typename TValueType, unsigned int VLength >
260 std::ostream & operator<<(std::ostream & os, const FixedArray< TValueType, VLength > & arr);
261 } // namespace itk
262 
263 #ifndef ITK_MANUAL_INSTANTIATION
264 #include "itkFixedArray.hxx"
265 #endif
266 
268 
269 #endif
270