ITK  5.1.0
Insight Segmentation and Registration 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(SizeValueType dimension);
66 
73  Array(ValueType * data, SizeValueType sz, bool LetArrayManageMemory = false);
74 
75 #if defined(ITK_LEGACY_REMOVE)
76 
80  Array(const ValueType * datain, SizeValueType sz);
81 
82 #else // defined ( ITK_LEGACY_REMOVE )
83 
89  Array(const ValueType * data, SizeValueType sz, bool LetArrayManageMemory = false);
90 #endif
91 
93  template <typename TArrayValue>
95  {
96  this->m_LetArrayManageMemory = true;
97  this->SetSize(r.GetSize());
98  for (SizeValueType i = 0; i < r.GetSize(); i++)
99  {
100  this->operator[](i) = static_cast<TValue>(r[i]);
101  }
102  }
104 
106  void
107  Fill(TValue const & v)
108  {
109  this->fill(v);
110  }
111 
113  const Self &
114  operator=(const Self & rhs);
115 
116  const Self &
117  operator=(const VnlVectorType & rhs);
118 
121  Size() const
122  {
123  return static_cast<SizeValueType>(this->size());
124  }
125  unsigned int
127  {
128  return static_cast<SizeValueType>(this->size());
129  }
131 
133  const TValue &
135  {
136  return this->operator[](i);
137  }
138 
140  void
141  SetElement(SizeValueType i, const TValue & value)
142  {
143  this->operator[](i) = value;
144  }
145 
147  void
148  SetSize(SizeValueType sz);
149 
151  GetSize() const
152  {
153  return static_cast<SizeValueType>(this->size());
154  }
155 
164  void
165  SetDataSameSize(TValue * data, bool LetArrayManageMemory = false);
166 
176  void
177  SetData(TValue * data, SizeValueType sz, bool LetArrayManageMemory = false);
178 
179 #ifdef __INTEL_COMPILER
180 # pragma warning disable 444 // destructor for base class "itk::Array<>" is not virtual
181 #endif
182 
184  ~Array();
185 
186 #if !defined(ITK_LEGACY_REMOVE)
187  void
188  swap(Array & other)
189  {
190  this->Swap(other);
191  }
192 #endif
193 
194  void
195  Swap(Array & other)
196  {
197  using std::swap;
198  this->VnlVectorType::swap(other);
199  swap(this->m_LetArrayManageMemory, other.m_LetArrayManageMemory);
200  }
201 
202 private:
204 };
205 
206 template <typename TValue>
207 std::ostream &
208 operator<<(std::ostream & os, const Array<TValue> & arr)
209 {
210  os << "[";
211  const unsigned int length = arr.size();
212  if (length >= 1)
213  {
214  const unsigned int last = length - 1;
215  for (unsigned int i = 0; i < last; ++i)
216  {
217  os << arr[i] << ", ";
218  }
219  os << arr[last];
220  }
221  os << "]";
222  return os;
223 }
224 
225 // declaration of specialization
226 template <>
227 ITKCommon_EXPORT std::ostream & operator<<<double>(std::ostream & os, const Array<double> & arr);
228 template <>
229 ITKCommon_EXPORT std::ostream & operator<<<float>(std::ostream & os, const Array<float> & arr);
230 
231 
232 template <typename T>
233 inline void
235 {
236  a.Swap(b);
237 }
238 
239 } // namespace itk
240 
241 #ifndef ITK_MANUAL_INSTANTIATION
242 # include "itkArray.hxx"
243 #endif
244 
245 #endif
itk::Array::SetElement
void SetElement(SizeValueType i, const TValue &value)
Definition: itkArray.h:141
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:208
itk::Array::GetElement
const TValue & GetElement(SizeValueType i) const
Definition: itkArray.h:134
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:234
itk::Array::Array
Array(const Array< TArrayValue > &r)
Definition: itkArray.h:94
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:203
itkMacro.h
itk::Array::GetSize
SizeValueType GetSize() const
Definition: itkArray.h:151
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:195
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:121
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::Array::GetNumberOfElements
unsigned int GetNumberOfElements() const
Definition: itkArray.h:126
itk::Array::Fill
void Fill(TValue const &v)
Definition: itkArray.h:107