ITK  4.8.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  void swap(Array &other)
172  {
173  using std::swap;
174  this->VnlVectorType::swap(other);
176  }
177 
178 private:
179 
181 };
182 
183 template< typename TValue >
184 std::ostream & operator<<(std::ostream & os, const Array< TValue > & arr)
185 {
186  os << "[";
187  const unsigned int length = arr.size();
188  if ( length >= 1 )
189  {
190  const unsigned int last = length - 1;
191  for ( unsigned int i = 0; i < last; ++i )
192  {
193  os << arr[i] << ", ";
194  }
195  os << arr[last];
196  }
197  os << "]";
198  return os;
199 }
200 
201 // declaration of specialization
202 template<> ITKCommon_EXPORT std::ostream & operator<< <double> (std::ostream & os, const Array< double > & arr);
203 template<> ITKCommon_EXPORT std::ostream & operator<< <float> (std::ostream & os, const Array< float > & arr);
204 
205 
206 template<typename T>
207 inline void swap( Array<T> &a, Array<T> &b )
208 {
209  a.swap(b);
210 }
211 
212 } // namespace itk
213 
214 #ifndef ITK_MANUAL_INSTANTIATION
215 #include "itkArray.hxx"
216 #endif
217 
218 #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:180
void Fill(TValue const &v)
Definition: itkArray.h:113
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:207
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:171
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