ITK  5.2.0
Insight Toolkit
itkArray.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 {
46 template <typename TValue>
47 class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
48 {
49 public:
50 
52  using ValueType = TValue;
53  using Self = Array;
54  using VnlVectorType = vnl_vector<TValue>;
55  using SizeValueType = typename vnl_vector<TValue>::size_type;
56 
57 public:
60  Array();
61 
64  Array(const Array &);
65 
67  explicit Array(const VnlVectorType &);
68 
72  explicit Array(SizeValueType dimension);
73 
75  explicit Array(SizeValueType dimension, const ValueType & value);
76 
83  Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
84 
85 #if defined(ITK_LEGACY_REMOVE)
86 
90  Array(const ValueType * datain, SizeValueType sz);
91 
92 #else // defined ( ITK_LEGACY_REMOVE )
93 
99  Array(const ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
100 #endif
101 
103  template <typename TArrayValue>
105  {
106  this->m_LetArrayManageMemory = true;
107  this->SetSize(r.GetSize());
108  for (SizeValueType i = 0; i < r.GetSize(); i++)
109  {
110  this->operator[](i) = static_cast<TValue>(r[i]);
111  }
112  }
114 
116  void
117  Fill(TValue const & v)
118  {
119  this->fill(v);
120  }
121 
123  const Self &
124  operator=(const Self & rhs);
125 
126  const Self &
127  operator=(const VnlVectorType & rhs);
128 
131  Size() const
132  {
133  return static_cast<SizeValueType>(this->size());
134  }
135  unsigned int
137  {
138  return static_cast<SizeValueType>(this->size());
139  }
141 
143  const TValue &
145  {
146  return this->operator[](i);
147  }
148 
150  void
151  SetElement(SizeValueType i, const TValue & value)
152  {
153  this->operator[](i) = value;
154  }
155 
157  void
158  SetSize(SizeValueType sz);
159 
161  GetSize() const
162  {
163  return static_cast<SizeValueType>(this->size());
164  }
165 
174  void
175  SetDataSameSize(TValue * datain, bool LetArrayManageMemory = false);
176 
186  void
187  SetData(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
188 
189 #ifdef __INTEL_COMPILER
190 # pragma warning disable 444 // destructor for base class "itk::Array<>" is not virtual
191 #endif
192 
194  ~Array() override;
195 
196 #if !defined(ITK_LEGACY_REMOVE)
197  void
198  swap(Array & other)
199  {
200  this->Swap(other);
201  }
202 #endif
203 
204  void
205  Swap(Array & other)
206  {
207  using std::swap;
208  this->VnlVectorType::swap(other);
209  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
210  }
211 
212 private:
214 };
215 
216 template <typename TValue>
217 std::ostream &
218 operator<<(std::ostream & os, const Array<TValue> & arr)
219 {
220  os << "[";
221  const unsigned int length = arr.size();
222  if (length >= 1)
223  {
224  const unsigned int last = length - 1;
225  for (unsigned int i = 0; i < last; ++i)
226  {
227  os << arr[i] << ", ";
228  }
229  os << arr[last];
230  }
231  os << "]";
232  return os;
233 }
234 
235 // declaration of specialization
236 template <>
237 ITKCommon_EXPORT std::ostream & operator<<<double>(std::ostream & os, const Array<double> & arr);
238 template <>
239 ITKCommon_EXPORT std::ostream & operator<<<float>(std::ostream & os, const Array<float> & arr);
240 
241 
242 template <typename T>
243 inline void
245 {
246  a.Swap(b);
247 }
248 
249 } // namespace itk
250 
251 #ifndef ITK_MANUAL_INSTANTIATION
252 # include "itkArray.hxx"
253 #endif
254 
255 #endif
itk::Array::SetElement
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:151
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:218
itk::Array::GetElement
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:144
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:244
itk::Array::Array
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:104
itk::Array< AccumulateType >::SizeValueType
typename vnl_vector< AccumulateType >::size_type SizeValueType
Definition: itkArray.h:55
itk::Array< AccumulateType >::VnlVectorType
vnl_vector< AccumulateType > VnlVectorType
Definition: itkArray.h:54
itk::Array::m_LetArrayManageMemory
bool m_LetArrayManageMemory
Definition: itkArray.h:213
itkMacro.h
itk::Array::GetSize
SizeValueType GetSize() const
Definition: itkArray.h:161
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:205
itk::Array< AccumulateType >::ValueType
AccumulateType ValueType
Definition: itkArray.h:52
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::Array::Size
SizeValueType Size() const
Definition: itkArray.h:131
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array::GetNumberOfElements
unsigned int GetNumberOfElements() const
Definition: itkArray.h:136
itk::Array::Fill
void Fill(TValue const &v)
Definition: itkArray.h:117