ITK  6.0.0
Insight Toolkit
itkFixedArray.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 itkFixedArray_h
19#define itkFixedArray_h
20
21#include "itkMacro.h"
22#include "itkMakeFilled.h"
23#include <algorithm>
24#include <array>
25
26namespace itk
27{
28
52template <typename TValue, unsigned int VLength = 3>
53class ITK_TEMPLATE_EXPORT FixedArray
54{
55public:
57 static constexpr unsigned int Length = VLength;
58
60 static constexpr unsigned int Dimension = VLength;
61
63 using ValueType = TValue;
64
66 using CArray = ValueType[VLength];
67
70
72 using ConstIterator = const ValueType *;
73
75
81 {
82 public:
84 : m_Iterator(i)
85 {}
88 {
89 return ReverseIterator(--m_Iterator);
90 }
93 {
94 return ReverseIterator(m_Iterator--);
95 }
98 {
99 return ReverseIterator(++m_Iterator);
100 }
103 {
104 return ReverseIterator(m_Iterator++);
105 }
108 {
109 return (m_Iterator - 1);
110 }
111 ValueType &
112 operator*() const
113 {
114 return *(m_Iterator - 1);
115 }
116
117 bool
118 operator==(const ReverseIterator & rit) const
119 {
120 return m_Iterator == rit.m_Iterator;
121 }
122
124
125 private:
128 };
129
135 {
136 public:
138 : m_Iterator(i)
139 {}
140 ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
143 {
144 return ConstReverseIterator(--m_Iterator);
145 }
148 {
149 return ConstReverseIterator(m_Iterator--);
150 }
153 {
154 return ConstReverseIterator(++m_Iterator);
155 }
158 {
159 return ConstReverseIterator(m_Iterator++);
160 }
163 {
164 return (m_Iterator - 1);
165 }
166 const ValueType &
167 operator*() const
168 {
169 return *(m_Iterator - 1);
170 }
171
172 bool
174 {
175 return m_Iterator == rit.m_Iterator;
176 }
177
179
180 private:
182 };
183
186
188 using const_pointer = const ValueType *;
189
192
194 using const_reference = const ValueType &;
195
198
200 using const_iterator = const ValueType *;
201
202 using reverse_iterator = std::reverse_iterator<iterator>;
203
204 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
205
206 using SizeType = unsigned int;
207
208public:
211 FixedArray() = default;
212
219 FixedArray(const ValueType r[VLength]);
224 explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
225 {
226 std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
227 }
228
230 template <typename TFixedArrayValueType>
232 {
233 auto input = r.cbegin();
234
235 for (auto & element : m_InternalArray)
236 {
237 element = static_cast<TValue>(*input++);
238 }
239 }
240
241 template <typename TScalarValue>
242 FixedArray(const TScalarValue * r)
243 {
244 std::copy_n(r, VLength, m_InternalArray);
245 }
246
253 template <typename TFixedArrayValueType>
254 FixedArray &
256 {
257 auto input = r.cbegin();
258
259 for (auto & element : m_InternalArray)
260 {
261 element = static_cast<TValue>(*input++);
262 }
263 return *this;
264 }
265
266 FixedArray &
267 operator=(const ValueType r[VLength]);
268
272 bool
273 operator==(const FixedArray & r) const;
274
276
277
280 // false positive warnings with GCC
281 ITK_GCC_PRAGMA_PUSH
282 ITK_GCC_SUPPRESS_Warray_bounds
283 constexpr reference
284 operator[](unsigned int index)
285 {
286 return m_InternalArray[index];
287 }
288 constexpr const_reference
289 operator[](unsigned int index) const
290 {
291 return m_InternalArray[index];
292 }
293 ITK_GCC_PRAGMA_POP
297 void
298 SetElement(unsigned int index, const_reference value)
299 {
300 m_InternalArray[index] = value;
301 }
302 const_reference
303 GetElement(unsigned int index) const
304 {
305 return m_InternalArray[index];
306 }
310 ValueType *
312 {
313 return m_InternalArray;
314 }
315
316 const ValueType *
318 {
319 return m_InternalArray;
320 }
321
322 ValueType *
324 {
325 return m_InternalArray;
326 }
327
328 const ValueType *
329 data() const
330 {
331 return m_InternalArray;
332 }
333
335 Iterator
337
340 Begin() const;
341
345
348 End() const;
349
352
354 itkLegacyMacro(ConstReverseIterator rBegin() const;)
355
358
360 itkLegacyMacro(ConstReverseIterator rEnd() const;)
361
362 constexpr const_iterator
363 cbegin() const noexcept
364 {
365 return m_InternalArray;
366 }
367
368 constexpr iterator
369 begin() noexcept
370 {
371 return m_InternalArray;
372 }
373
374 constexpr const_iterator
375 begin() const noexcept
376 {
377 return this->cbegin();
378 }
379
380 constexpr const_iterator
381 cend() const noexcept
382 {
383 return m_InternalArray + VLength;
384 }
385
386 constexpr iterator
387 end() noexcept
388 {
389 return m_InternalArray + VLength;
390 }
391
392 constexpr const_iterator
393 end() const noexcept
394 {
395 return this->cend();
396 }
397
398 reverse_iterator
400 {
401 return reverse_iterator{ this->end() };
402 }
403
404 const_reverse_iterator
405 crbegin() const
406 {
407 return const_reverse_iterator{ this->cend() };
408 }
409
410 const_reverse_iterator
411 rbegin() const
412 {
413 return this->crbegin();
414 }
415
416 reverse_iterator
418 {
419 return reverse_iterator{ this->begin() };
420 }
421
422 const_reverse_iterator
423 crend() const
424 {
425 return const_reverse_iterator{ this->cbegin() };
426 }
427
428 const_reverse_iterator
429 rend() const
430 {
431 return this->crend();
432 }
433
436 Size() const;
437
438 constexpr SizeType
439 size() const
440 {
441 return VLength;
442 }
443
445 void
446 Fill(const ValueType &);
447
448 void
449 swap(FixedArray & other) noexcept
450 {
451 std::swap(m_InternalArray, other.m_InternalArray);
452 }
453
454private:
457
458public:
460 static constexpr FixedArray
461 Filled(const ValueType & value)
462 {
463 return MakeFilled<FixedArray>(value);
464 }
465};
468template <typename TValue, unsigned int VLength>
469std::ostream &
470operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
471
472
473template <typename TValue, unsigned int VLength>
474inline void
476{
477 a.swap(b);
478}
479
480} // namespace itk
481
482#ifndef ITK_MANUAL_INSTANTIATION
483# include "itkFixedArray.hxx"
484#endif
485
487
488#endif
A const reverse iterator through an array.
ConstReverseIterator operator--(int)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstReverseIterator)
const ValueType & operator*() const
ConstReverseIterator(const ReverseIterator &rit)
ConstReverseIterator operator++(int)
bool operator==(const ConstReverseIterator &rit) const
A reverse iterator through an array.
Definition: itkFixedArray.h:81
bool operator==(const ReverseIterator &rit) const
ReverseIterator operator--(int)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ReverseIterator)
ReverseIterator operator++(int)
Definition: itkFixedArray.h:92
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
ConstIterator End() const
Iterator End()
bool operator==(const FixedArray &r) const
constexpr SizeType size() const
void swap(FixedArray &other) noexcept
const ValueType * const_iterator
std::reverse_iterator< iterator > reverse_iterator
constexpr const_reference operator[](unsigned int index) const
const_reverse_iterator crend() const
FixedArray(const ValueType r[VLength])
ValueType * pointer
const ValueType * GetDataPointer() const
constexpr iterator begin() noexcept
FixedArray(const ValueType &)
constexpr const_iterator cbegin() const noexcept
const ValueType * data() const
unsigned int SizeType
ValueType * GetDataPointer()
FixedArray(const std::array< ValueType, VLength > &stdArray)
const ValueType * ConstIterator
Definition: itkFixedArray.h:72
ValueType[VLength] CArray
Definition: itkFixedArray.h:66
const_reverse_iterator crbegin() const
ValueType & reference
FixedArray()=default
Iterator Begin()
constexpr iterator end() noexcept
reverse_iterator rbegin()
itkLegacyMacro(ReverseIterator rEnd();) itkLegacyMacro(ConstReverseIterator rEnd() const
FixedArray(const TScalarValue *r)
static constexpr FixedArray Filled(const ValueType &value)
SizeType Size() const
const_reverse_iterator rend() const
const_reference GetElement(unsigned int index) const
constexpr const_iterator cend() const noexcept
reverse_iterator rend()
FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > &r)
std::reverse_iterator< const_iterator > const_reverse_iterator
const ValueType & const_reference
FixedArray & operator=(const ValueType r[VLength])
ValueType * iterator
constexpr const_iterator begin() const noexcept
ValueType * Iterator
Definition: itkFixedArray.h:69
CArray m_InternalArray
ITK_GCC_PRAGMA_PUSH ITK_GCC_SUPPRESS_Warray_bounds constexpr reference operator[](unsigned int index)
constexpr const_iterator end() const noexcept
ConstIterator Begin() const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray)
const ValueType * const_pointer
const_reverse_iterator rbegin() const
ValueType * data()
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
ITK_GCC_PRAGMA_POP void SetElement(unsigned int index, const_reference value)
void Fill(const ValueType &)
itkLegacyMacro(ReverseIterator rBegin();) itkLegacyMacro(ConstReverseIterator rBegin() const
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b) noexcept
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)