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 
70  explicit Array(SizeValueType dimension);
71 
78  Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
79 
80 #if defined(ITK_LEGACY_REMOVE)
81 
85  Array(const ValueType * datain, SizeValueType sz);
86 
87 #else // defined ( ITK_LEGACY_REMOVE )
88 
94  Array(const ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
95 #endif
96 
98  template <typename TArrayValue>
100  {
101  this->m_LetArrayManageMemory = true;
102  this->SetSize(r.GetSize());
103  for (SizeValueType i = 0; i < r.GetSize(); i++)
104  {
105  this->operator[](i) = static_cast<TValue>(r[i]);
106  }
107  }
109 
111  void
112  Fill(TValue const & v)
113  {
114  this->fill(v);
115  }
116 
118  const Self &
119  operator=(const Self & rhs);
120 
121  const Self &
122  operator=(const VnlVectorType & rhs);
123 
126  Size() const
127  {
128  return static_cast<SizeValueType>(this->size());
129  }
130  unsigned int
132  {
133  return static_cast<SizeValueType>(this->size());
134  }
136 
138  const TValue &
140  {
141  return this->operator[](i);
142  }
143 
145  void
146  SetElement(SizeValueType i, const TValue & value)
147  {
148  this->operator[](i) = value;
149  }
150 
152  void
153  SetSize(SizeValueType sz);
154 
156  GetSize() const
157  {
158  return static_cast<SizeValueType>(this->size());
159  }
160 
169  void
170  SetDataSameSize(TValue * datain, bool LetArrayManageMemory = false);
171 
181  void
182  SetData(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
183 
184 #ifdef __INTEL_COMPILER
185 # pragma warning disable 444 // destructor for base class "itk::Array<>" is not virtual
186 #endif
187 
189  ~Array() override;
190 
191 #if !defined(ITK_LEGACY_REMOVE)
192  void
193  swap(Array & other)
194  {
195  this->Swap(other);
196  }
197 #endif
198 
199  void
200  Swap(Array & other)
201  {
202  using std::swap;
203  this->VnlVectorType::swap(other);
204  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
205  }
206 
207 private:
209 };
210 
211 template <typename TValue>
212 std::ostream &
213 operator<<(std::ostream & os, const Array<TValue> & arr)
214 {
215  os << "[";
216  const unsigned int length = arr.size();
217  if (length >= 1)
218  {
219  const unsigned int last = length - 1;
220  for (unsigned int i = 0; i < last; ++i)
221  {
222  os << arr[i] << ", ";
223  }
224  os << arr[last];
225  }
226  os << "]";
227  return os;
228 }
229 
230 // declaration of specialization
231 template <>
232 ITKCommon_EXPORT std::ostream & operator<<<double>(std::ostream & os, const Array<double> & arr);
233 template <>
234 ITKCommon_EXPORT std::ostream & operator<<<float>(std::ostream & os, const Array<float> & arr);
235 
236 
237 template <typename T>
238 inline void
240 {
241  a.Swap(b);
242 }
243 
244 } // namespace itk
245 
246 #ifndef ITK_MANUAL_INSTANTIATION
247 # include "itkArray.hxx"
248 #endif
249 
250 #endif
itk::Array::SetElement
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:146
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk::Array::GetElement
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:139
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:239
itk::Array::Array
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:99
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:208
itkMacro.h
itk::Array::GetSize
SizeValueType GetSize() const
Definition: itkArray.h:156
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:200
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:126
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array::GetNumberOfElements
unsigned int GetNumberOfElements() const
Definition: itkArray.h:131
itk::Array::Fill
void Fill(TValue const &v)
Definition: itkArray.h:112