ITK  4.13.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 ITK_TEMPLATE_EXPORT 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 
152  void SetDataSameSize(TValue *data, bool LetArrayManageMemory = false);
153 
163  void SetData(TValue *data, SizeValueType sz, bool LetArrayManageMemory = false);
164 
165 #ifdef __INTEL_COMPILER
166 #pragma warning disable 444 //destructor for base class "itk::Array<>" is not virtual
167 #endif
168 
170  ~Array();
171 
172 #if ! defined ( ITK_FUTURE_LEGACY_REMOVE )
173  void swap(Array &other)
174  {
175  this->Swap(other);
176  }
177 #endif
178 
179  void Swap(Array &other)
180  {
181  using std::swap;
182  this->VnlVectorType::swap(other);
183  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
184  }
185 
186 private:
187 
189 };
190 
191 template< typename TValue >
192 std::ostream & operator<<(std::ostream & os, const Array< TValue > & arr)
193 {
194  os << "[";
195  const unsigned int length = arr.size();
196  if ( length >= 1 )
197  {
198  const unsigned int last = length - 1;
199  for ( unsigned int i = 0; i < last; ++i )
200  {
201  os << arr[i] << ", ";
202  }
203  os << arr[last];
204  }
205  os << "]";
206  return os;
207 }
208 
209 // declaration of specialization
210 template<> ITKCommon_EXPORT std::ostream & operator<< <double> (std::ostream & os, const Array< double > & arr);
211 template<> ITKCommon_EXPORT std::ostream & operator<< <float> (std::ostream & os, const Array< float > & arr);
212 
213 
214 template<typename T>
215 inline void swap( Array<T> &a, Array<T> &b )
216 {
217  a.Swap(b);
218 }
219 
220 } // namespace itk
221 
222 #ifndef ITK_MANUAL_INSTANTIATION
223 #include "itkArray.hxx"
224 #endif
225 
226 #endif
Array class with size defined at construction time.
Definition: itkArray.h:50
TValue ValueType
Definition: itkArray.h:55
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:188
void Fill(TValue const &v)
Definition: itkArray.h:113
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:215
unsigned int GetNumberOfElements(void) const
Definition: itkArray.h:126
SizeValueType GetSize(void) const
Definition: itkArray.h:141
void swap(Array &other)
Definition: itkArray.h:173
void Swap(Array &other)
Definition: itkArray.h:179
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