ITK  5.1.0
Insight 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:
50  using ValueType = TValue;
51  using Self = Array;
52  using VnlVectorType = vnl_vector<TValue>;
53  using SizeValueType = typename vnl_vector<TValue>::size_type;
54 
55 public:
58  Array();
59 
62  Array(const Array &);
63 
65  explicit Array(const VnlVectorType &);
66 
68  explicit Array(SizeValueType dimension);
69 
76  Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
77 
78 #if defined(ITK_LEGACY_REMOVE)
79 
83  Array(const ValueType * datain, SizeValueType sz);
84 
85 #else // defined ( ITK_LEGACY_REMOVE )
86 
92  Array(const ValueType * datain, SizeValueType sz, 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
110  Fill(TValue const & v)
111  {
112  this->fill(v);
113  }
114 
116  const Self &
117  operator=(const Self & rhs);
118 
119  const Self &
120  operator=(const VnlVectorType & rhs);
121 
124  Size() const
125  {
126  return static_cast<SizeValueType>(this->size());
127  }
128  unsigned int
130  {
131  return static_cast<SizeValueType>(this->size());
132  }
134 
136  const TValue &
138  {
139  return this->operator[](i);
140  }
141 
143  void
144  SetElement(SizeValueType i, const TValue & value)
145  {
146  this->operator[](i) = value;
147  }
148 
150  void
151  SetSize(SizeValueType sz);
152 
154  GetSize() const
155  {
156  return static_cast<SizeValueType>(this->size());
157  }
158 
167  void
168  SetDataSameSize(TValue * datain, bool LetArrayManageMemory = false);
169 
179  void
180  SetData(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
181 
182 #ifdef __INTEL_COMPILER
183 # pragma warning disable 444 // destructor for base class "itk::Array<>" is not virtual
184 #endif
185 
187  ~Array();
188 
189 #if !defined(ITK_LEGACY_REMOVE)
190  void
191  swap(Array & other)
192  {
193  this->Swap(other);
194  }
195 #endif
196 
197  void
198  Swap(Array & other)
199  {
200  using std::swap;
201  this->VnlVectorType::swap(other);
202  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
203  }
204 
205 private:
207 };
208 
209 template <typename TValue>
210 std::ostream &
211 operator<<(std::ostream & os, const Array<TValue> & arr)
212 {
213  os << "[";
214  const unsigned int length = arr.size();
215  if (length >= 1)
216  {
217  const unsigned int last = length - 1;
218  for (unsigned int i = 0; i < last; ++i)
219  {
220  os << arr[i] << ", ";
221  }
222  os << arr[last];
223  }
224  os << "]";
225  return os;
226 }
227 
228 // declaration of specialization
229 template <>
230 ITKCommon_EXPORT std::ostream & operator<<<double>(std::ostream & os, const Array<double> & arr);
231 template <>
232 ITKCommon_EXPORT std::ostream & operator<<<float>(std::ostream & os, const Array<float> & arr);
233 
234 
235 template <typename T>
236 inline void
238 {
239  a.Swap(b);
240 }
241 
242 } // namespace itk
243 
244 #ifndef ITK_MANUAL_INSTANTIATION
245 # include "itkArray.hxx"
246 #endif
247 
248 #endif
itk::Array::SetElement
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:144
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:211
itk::Array::GetElement
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:137
itk::operator<<<double >
ITKCommon_EXPORT std::ostream & operator<<<double >(std::ostream &os, const Array< double > &arr)
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:237
itk::Array::Array
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:97
itk::Array< AccumulateType >::SizeValueType
typename vnl_vector< AccumulateType >::size_type SizeValueType
Definition: itkArray.h:53
itk::Array< AccumulateType >::VnlVectorType
vnl_vector< AccumulateType > VnlVectorType
Definition: itkArray.h:52
itk::Array::m_LetArrayManageMemory
bool m_LetArrayManageMemory
Definition: itkArray.h:206
itkMacro.h
itk::Array::GetSize
SizeValueType GetSize() const
Definition: itkArray.h:154
itk::operator<<<float >
ITKCommon_EXPORT std::ostream & operator<<<float >(std::ostream &os, const Array< float > &arr)
itk::Array::Swap
void Swap(Array &other)
Definition: itkArray.h:198
itk::Array< AccumulateType >::ValueType
AccumulateType ValueType
Definition: itkArray.h:50
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:46
itk::Array::Size
SizeValueType Size() const
Definition: itkArray.h:124
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array::GetNumberOfElements
unsigned int GetNumberOfElements() const
Definition: itkArray.h:129
itk::Array::Fill
void Fill(TValue const &v)
Definition: itkArray.h:110