ITK  5.0.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 #include "vnl/vnl_vector.h"
25 
26 namespace itk
27 {
45 template< typename TValue >
46 class ITK_TEMPLATE_EXPORT Array : public vnl_vector< TValue >
47 {
48 public:
49 
51  using ValueType = TValue;
52  using Self = Array;
53  using VnlVectorType = vnl_vector< TValue >;
54  using SizeValueType = typename vnl_vector< TValue>::size_type;
55 
56 public:
57 
60  Array();
61 
64  Array(const Array&);
65 
67  explicit Array(SizeValueType dimension);
68 
75  Array(ValueType *data, SizeValueType sz, bool LetArrayManageMemory = false);
76 
77 #if defined ( ITK_LEGACY_REMOVE )
78 
82  Array(const ValueType *datain, SizeValueType sz);
83 
84 #else // defined ( ITK_LEGACY_REMOVE )
85 
91  Array(const ValueType *data, SizeValueType sz,
92  bool LetArrayManageMemory = false);
93 #endif
94 
96  template< typename TArrayValue >
98  {
99  this->m_LetArrayManageMemory = true;
100  this->SetSize( r.GetSize() );
101  for( SizeValueType i=0; i<r.GetSize(); i++ )
102  {
103  this->operator[](i) = static_cast< TValue >( r[i] );
104  }
105  }
107 
109  void Fill(TValue const & v)
110  {
111  this->fill(v);
112  }
113 
115  const Self & operator=(const Self & rhs);
116 
117  const Self & operator=(const VnlVectorType & rhs);
118 
121  { return static_cast<SizeValueType >( this->size() ); }
122  unsigned int GetNumberOfElements() const
123  { return static_cast<SizeValueType >( this->size() ); }
125 
127  const TValue & GetElement(SizeValueType i) const
128  { return this->operator[](i); }
129 
131  void SetElement(SizeValueType i, const TValue & value)
132  { this->operator[](i) = value; }
133 
135  void SetSize(SizeValueType sz);
136 
138  { return static_cast< SizeValueType >( this->size() ); }
139 
148  void SetDataSameSize(TValue *data, bool LetArrayManageMemory = false);
149 
159  void SetData(TValue *data, SizeValueType sz, bool LetArrayManageMemory = false);
160 
161 #ifdef __INTEL_COMPILER
162 #pragma warning disable 444 //destructor for base class "itk::Array<>" is not virtual
163 #endif
164 
166  ~Array();
167 
168 #if ! defined ( ITK_LEGACY_REMOVE )
169  void swap(Array &other)
170  {
171  this->Swap(other);
172  }
173 #endif
174 
175  void Swap(Array &other)
176  {
177  using std::swap;
178  this->VnlVectorType::swap(other);
179  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
180  }
181 
182 private:
183 
185 };
186 
187 template< typename TValue >
188 std::ostream & operator<<(std::ostream & os, const Array< TValue > & arr)
189 {
190  os << "[";
191  const unsigned int length = arr.size();
192  if ( length >= 1 )
193  {
194  const unsigned int last = length - 1;
195  for ( unsigned int i = 0; i < last; ++i )
196  {
197  os << arr[i] << ", ";
198  }
199  os << arr[last];
200  }
201  os << "]";
202  return os;
203 }
204 
205 // declaration of specialization
206 template<> ITKCommon_EXPORT std::ostream & operator<< <double> (std::ostream & os, const Array< double > & arr);
207 template<> ITKCommon_EXPORT std::ostream & operator<< <float> (std::ostream & os, const Array< float > & arr);
208 
209 
210 template<typename T>
211 inline void swap( Array<T> &a, Array<T> &b )
212 {
213  a.Swap(b);
214 }
215 
216 } // namespace itk
217 
218 #ifndef ITK_MANUAL_INSTANTIATION
219 #include "itkArray.hxx"
220 #endif
221 
222 #endif
Array class with size defined at construction time.
Definition: itkArray.h:46
unsigned int GetNumberOfElements() const
Definition: itkArray.h:122
unsigned long SizeValueType
Definition: itkIntTypes.h:83
AccumulateType ValueType
Definition: itkArray.h:51
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:131
bool m_LetArrayManageMemory
Definition: itkArray.h:184
void Fill(TValue const &v)
Definition: itkArray.h:109
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
vnl_vector< AccumulateType > VnlVectorType
Definition: itkArray.h:53
SizeValueType Size() const
Definition: itkArray.h:120
void Swap(Array &other)
Definition: itkArray.h:175
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 GetSize() const
Definition: itkArray.h:137
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:127
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:97
typename vnl_vector< AccumulateType >::size_type SizeValueType
Definition: itkArray.h:54