ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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
26namespace itk
27{
46template <typename TValue>
47class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
48{
49public:
51 using ValueType = TValue;
52 using Self = Array;
53 using VnlVectorType = vnl_vector<TValue>;
54 using SizeValueType = typename vnl_vector<TValue>::size_type;
55
56public:
59 Array() = default;
60
63 Array(const Array &);
64
66 explicit Array(const VnlVectorType &);
67
71 explicit Array(SizeValueType dimension);
72
74 explicit Array(SizeValueType dimension, const ValueType & value);
75
83 Array(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
84
85#if defined(ITK_LEGACY_REMOVE)
91 Array(const TValue * datain, SizeValueType sz);
92
93#else // defined ( ITK_LEGACY_REMOVE )
99 Array(const TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
100#endif
101
103 template <typename TArrayValue>
105 {
106 this->SetSize(r.GetSize());
107 for (SizeValueType i = 0; i < r.GetSize(); ++i)
108 {
109 this->operator[](i) = static_cast<TValue>(r[i]);
110 }
111 }
112
114 void
115 Fill(const TValue & v)
116 {
117 this->fill(v);
118 }
119
121 Self &
122 operator=(const Self & rhs);
123
124 Self &
126
129 [[nodiscard]] SizeValueType
130 Size() const
131 {
132 return static_cast<SizeValueType>(this->size());
133 }
134 [[nodiscard]] unsigned int
136 {
137 return static_cast<SizeValueType>(this->size());
138 }
139
141 [[nodiscard]] const TValue &
143 {
144 return this->operator[](i);
145 }
146
148 void
149 SetElement(SizeValueType i, const TValue & value)
150 {
151 this->operator[](i) = value;
152 }
153
155 void
157
158 [[nodiscard]] SizeValueType
159 GetSize() const
160 {
161 return static_cast<SizeValueType>(this->size());
162 }
163
172 void
173 SetDataSameSize(TValue * datain, bool LetArrayManageMemory = false);
174
184 void
185 SetData(TValue * datain, SizeValueType sz, bool LetArrayManageMemory = false);
186
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) noexcept
201 {
202 using std::swap;
203 this->VnlVectorType::swap(other);
204 swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
205 }
206
207private:
210};
211
212// Deduction guide to avoid compiler warnings (-wctad-maybe-unsupported) when using class template argument deduction.
213template <typename TValue>
214Array(TValue *, typename vnl_vector<TValue>::size_type, bool) -> Array<TValue>;
215
216template <typename TValue>
217std::ostream &
218operator<<(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
236template <>
237ITKCommon_EXPORT std::ostream & operator<< <double>(std::ostream & os, const Array<double> & arr);
238template <>
239ITKCommon_EXPORT std::ostream & operator<< <float>(std::ostream & os, const Array<float> & arr);
240
241
242template <typename T>
243inline void
244swap(Array<T> & a, Array<T> & b) noexcept
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
Array class with size defined at construction time.
Definition itkArray.h:48
Array()=default
void SetData(TValue *datain, SizeValueType sz, bool LetArrayManageMemory=false)
SizeValueType Size() const
Definition itkArray.h:130
void SetElement(SizeValueType i, const TValue &value)
Definition itkArray.h:149
SizeValueType GetSize() const
Definition itkArray.h:159
Array(const TValue *datain, SizeValueType sz)
Array(const Array< TArrayValue > &r)
Definition itkArray.h:104
vnl_vector< TValue > VnlVectorType
Definition itkArray.h:53
void Swap(Array &other) noexcept
Definition itkArray.h:200
unsigned int GetNumberOfElements() const
Definition itkArray.h:135
Self & operator=(const VnlVectorType &rhs)
~Array() override
Array(TValue *datain, SizeValueType sz, bool LetArrayManageMemory=false)
Array(SizeValueType dimension)
Array Self
Definition itkArray.h:52
const TValue & GetElement(SizeValueType i) const
Definition itkArray.h:142
TValue ValueType
Definition itkArray.h:51
Array(const Array &)
Array(SizeValueType dimension, const ValueType &value)
void Fill(const TValue &v)
Definition itkArray.h:115
void SetSize(SizeValueType sz)
typename vnl_vector< TValue >::size_type SizeValueType
Definition itkArray.h:54
Self & operator=(const Self &rhs)
Array(const VnlVectorType &)
void SetDataSameSize(TValue *datain, bool LetArrayManageMemory=false)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:244
ITKCommon_EXPORT std::ostream & operator<<< float >(std::ostream &os, const Array< float > &arr)
ITKCommon_EXPORT std::ostream & operator<<< double >(std::ostream &os, const Array< double > &arr)
Array(TValue *, typename vnl_vector< TValue >::size_type, bool) -> Array< TValue >
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, AnatomicalOrientation::CoordinateEnum value)