ITK  6.0.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  * https://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() = default;
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 
97  Array(const ValueType * datain, SizeValueType sz, bool LetArrayManageMemory = false);
98 #endif
99 
101  template <typename TArrayValue>
103  {
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  }
113  void
114  Fill(TValue const & v)
115  {
116  this->fill(v);
117  }
118 
120  Self &
121  operator=(const Self & rhs);
122 
123  Self &
124  operator=(const VnlVectorType & rhs);
125 
128  Size() const
129  {
130  return static_cast<SizeValueType>(this->size());
131  }
132  unsigned int
134  {
135  return static_cast<SizeValueType>(this->size());
136  }
140  const TValue &
142  {
143  return this->operator[](i);
144  }
145 
147  void
148  SetElement(SizeValueType i, const TValue & value)
149  {
150  this->operator[](i) = value;
151  }
152 
154  void
155  SetSize(SizeValueType sz);
156 
158  GetSize() const
159  {
160  return static_cast<SizeValueType>(this->size());
161  }
162 
171  void
172  SetDataSameSize(TValue * datain, bool LetArrayManageMemory = false);
173 
183  void
184  SetData(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
185 
186 #ifdef __INTEL_COMPILER
187 # pragma warning disable 444 // destructor for base class "itk::Array<>" is not virtual
188 #endif
189 
191  ~Array() override;
192 
193 #if !defined(ITK_LEGACY_REMOVE)
194  void
195  swap(Array & other)
196  {
197  this->Swap(other);
198  }
199 #endif
200 
201  void
202  Swap(Array & other) noexcept
203  {
204  using std::swap;
205  this->VnlVectorType::swap(other);
206  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
207  }
208 
209 private:
211  bool m_LetArrayManageMemory{ true };
212 };
213 
214 template <typename TValue>
215 std::ostream &
216 operator<<(std::ostream & os, const Array<TValue> & arr)
217 {
218  os << '[';
219  const unsigned int length = arr.size();
220  if (length >= 1)
221  {
222  const unsigned int last = length - 1;
223  for (unsigned int i = 0; i < last; ++i)
224  {
225  os << arr[i] << ", ";
226  }
227  os << arr[last];
228  }
229  os << ']';
230  return os;
231 }
232 
233 // declaration of specialization
234 template <>
235 ITKCommon_EXPORT std::ostream & operator<<<double>(std::ostream & os, const Array<double> & arr);
236 template <>
237 ITKCommon_EXPORT std::ostream & operator<<<float>(std::ostream & os, const Array<float> & arr);
238 
239 
240 template <typename T>
241 inline void
242 swap(Array<T> & a, Array<T> & b) noexcept
243 {
244  a.Swap(b);
245 }
246 
247 } // namespace itk
248 
249 #ifndef ITK_MANUAL_INSTANTIATION
250 # include "itkArray.hxx"
251 #endif
252 
253 #endif
itk::Array::SetElement
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:148
itk::Array::GetElement
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:141
itk::operator<<<double >
ITKCommon_EXPORT std::ostream & operator<<<double >(std::ostream &os, const Array< double > &arr)
itk::Array::Array
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:102
itk::Array< TParametersValueType >::SizeValueType
typename vnl_vector< TParametersValueType >::size_type SizeValueType
Definition: itkArray.h:55
itk::operator<<
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
itk::Array< TParametersValueType >::VnlVectorType
vnl_vector< TParametersValueType > VnlVectorType
Definition: itkArray.h:54
itkMacro.h
itk::Array::GetSize
SizeValueType GetSize() const
Definition: itkArray.h:158
itk::operator<<<float >
ITKCommon_EXPORT std::ostream & operator<<<float >(std::ostream &os, const Array< float > &arr)
itk::Array< TParametersValueType >::ValueType
TParametersValueType ValueType
Definition: itkArray.h:52
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::Array
Array class with size defined at construction time.
Definition: itkArray.h:47
itk::Array::Size
SizeValueType Size() const
Definition: itkArray.h:128
itk::Array::Swap
void Swap(Array &other) noexcept
Definition: itkArray.h:202
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itk::Array::GetNumberOfElements
unsigned int GetNumberOfElements() const
Definition: itkArray.h:133
itk::Array::Fill
void Fill(TValue const &v)
Definition: itkArray.h:114