ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkArray.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 itkArray_h
19 #define itkArray_h
20 
21 #include "itkMacro.h"
22 
23 #include "vxl_version.h"
24 #if VXL_VERSION_DATE_FULL < 20110428
25 #error "System installed VXL version is too old. Please make sure the version date is later than 2011-04-28."
26 #endif
27 
28 #include "vnl/vnl_vector.h"
29 
30 namespace itk
31 {
49 template< typename TValue >
50 class Array : public vnl_vector< TValue >
51 {
52 public:
53 
55  typedef TValue ValueType;
56  typedef Array Self;
57  typedef vnl_vector< TValue > VnlVectorType;
58  typedef typename vnl_vector< TValue>::size_type SizeValueType;
59 
60 public:
61 
64  Array();
65 
68  Array(const Array&);
69 
71  explicit Array(SizeValueType dimension);
72 
79  Array(ValueType *data, SizeValueType sz, bool LetArrayManageMemory = false);
80 
81 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
82 
86  Array(const ValueType *datain, SizeValueType sz);
87 
88 #else // defined ( ITK_FUTURE_LEGACY_REMOVE )
89 
95  Array(const ValueType *data, SizeValueType sz,
96  bool LetArrayManageMemory = false);
97 #endif
98 
100  template< typename TArrayValue >
102  {
103  this->m_LetArrayManageMemory = true;
104  this->SetSize( r.GetSize() );
105  for( SizeValueType i=0; i<r.GetSize(); i++ )
106  {
107  this->operator[](i) = static_cast< TValue >( r[i] );
108  }
109  }
111 
113  void Fill(TValue const & v)
114  {
115  this->fill(v);
116  }
117 
119  const Self & operator=(const Self & rhs);
120 
121  const Self & operator=(const VnlVectorType & rhs);
122 
124  SizeValueType Size(void) const
125  { return static_cast<SizeValueType >( this->size() ); }
126  unsigned int GetNumberOfElements(void) const
127  { return static_cast<SizeValueType >( this->size() ); }
129 
131  const TValue & GetElement(SizeValueType i) const
132  { return this->operator[](i); }
133 
135  void SetElement(SizeValueType i, const TValue & value)
136  { this->operator[](i) = value; }
137 
139  void SetSize(SizeValueType sz);
140 
141  SizeValueType GetSize(void) const
142  { return static_cast< SizeValueType >( this->size() ); }
143 
149  void SetData(TValue *data, bool LetArrayManageMemory = false);
150 
160  void SetData(TValue *data, SizeValueType sz,
161  bool LetArrayManageMemory = false);
162 
163 
164 #ifdef __INTEL_COMPILER
165 #pragma warning disable 444 //destructor for base class "itk::Array<>" is not virtual
166 #endif
167 
169  ~Array();
170 
171 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
172  void swap(Array &other)
173  {
174  this->Swap(other);
175  }
176 #endif
177 
178  void Swap(Array &other)
179  {
180  using std::swap;
181  this->VnlVectorType::swap(other);
183  }
184 
185 private:
186 
188 };
189 
190 template< typename TValue >
191 std::ostream & operator<<(std::ostream & os, const Array< TValue > & arr)
192 {
193  os << "[";
194  const unsigned int length = arr.size();
195  if ( length >= 1 )
196  {
197  const unsigned int last = length - 1;
198  for ( unsigned int i = 0; i < last; ++i )
199  {
200  os << arr[i] << ", ";
201  }
202  os << arr[last];
203  }
204  os << "]";
205  return os;
206 }
207 
208 // declaration of specialization
209 template<> ITKCommon_EXPORT std::ostream & operator<< <double> (std::ostream & os, const Array< double > & arr);
210 template<> ITKCommon_EXPORT std::ostream & operator<< <float> (std::ostream & os, const Array< float > & arr);
211 
212 
213 template<typename T>
214 inline void swap( Array<T> &a, Array<T> &b )
215 {
216  a.Swap(b);
217 }
218 
219 } // namespace itk
220 
221 #ifndef ITK_MANUAL_INSTANTIATION
222 #include "itkArray.hxx"
223 #endif
224 
225 #endif
Array class with size defined at construction time.
Definition: itkArray.h:50
const Self & operator=(const Self &rhs)
TValue ValueType
Definition: itkArray.h:55
void SetData(TValue *data, bool LetArrayManageMemory=false)
vnl_vector< TValue > VnlVectorType
Definition: itkArray.h:57
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:135
unsigned long SizeValueType
Definition: itkIntTypes.h:143
Array Self
Definition: itkArray.h:56
bool m_LetArrayManageMemory
Definition: itkArray.h:187
void Fill(TValue const &v)
Definition: itkArray.h:113
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:214
unsigned int GetNumberOfElements(void) const
Definition: itkArray.h:126
void SetSize(SizeValueType sz)
SizeValueType GetSize(void) const
Definition: itkArray.h:141
void swap(Array &other)
Definition: itkArray.h:172
void Swap(Array &other)
Definition: itkArray.h:178
ITKCommon_EXPORT std::ostream & operator<< < double >(std::ostream &os, const Array< double > &arr)
ITKCommon_EXPORT std::ostream & operator<< < float >(std::ostream &os, const Array< float > &arr)
SizeValueType Size(void) const
Definition: itkArray.h:124
vnl_vector< TValue >::size_type SizeValueType
Definition: itkArray.h:58
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:131
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:101