ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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{
55 static_assert(VLength > 0);
56
57public:
59 static constexpr unsigned int Length = VLength;
60
62 static constexpr unsigned int Dimension = VLength;
63
65 using ValueType = TValue;
66
68 using CArray = ValueType[VLength];
69
72
74 using ConstIterator = const ValueType *;
75
76 class ConstReverseIterator;
77
83 {
84 public:
86 : m_Iterator(i)
87 {}
90 {
92 }
95 {
97 }
100 {
101 return ReverseIterator(++m_Iterator);
102 }
105 {
106 return ReverseIterator(m_Iterator++);
107 }
110 {
111 return m_Iterator - 1;
112 }
113 ValueType &
114 operator*() const
115 {
116 return *(m_Iterator - 1);
117 }
118
119 bool
120 operator==(const ReverseIterator & rit) const
121 {
122 return m_Iterator == rit.m_Iterator;
123 }
124
126
127 private:
130 };
131
137 {
138 public:
140 : m_Iterator(i)
141 {}
147 {
149 }
152 {
154 }
157 {
159 }
162 {
164 }
167 {
168 return m_Iterator - 1;
169 }
170 const ValueType &
171 operator*() const
172 {
173 return *(m_Iterator - 1);
174 }
175
176 bool
178 {
179 return m_Iterator == rit.m_Iterator;
180 }
181
183
184 private:
186 };
187
189 using value_type = TValue;
190
193
195 using const_pointer = const ValueType *;
196
199
201 using const_reference = const ValueType &;
202
205
207 using const_iterator = const ValueType *;
208
209 using reverse_iterator = std::reverse_iterator<iterator>;
210
211 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
212
213 using SizeType = unsigned int;
214
215public:
218 FixedArray() = default;
219
227 FixedArray(const ValueType r[VLength]);
231 explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
232 {
233 std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
234 }
235
237 template <typename TFixedArrayValueType>
239 {
240 auto input = r.cbegin();
241
242 for (auto & element : m_InternalArray)
243 {
244 element = static_cast<TValue>(*input++);
245 }
246 }
247
248 template <typename TScalarValue>
249 FixedArray(const TScalarValue * r)
250 {
251 std::copy_n(r, VLength, m_InternalArray);
252 }
253
260 template <typename TFixedArrayValueType>
261 FixedArray &
263 {
264 auto input = r.cbegin();
265
266 for (auto & element : m_InternalArray)
267 {
268 element = static_cast<TValue>(*input++);
269 }
270 return *this;
271 }
272
273 FixedArray &
274 operator=(const ValueType r[VLength]);
275
279 bool
280 operator==(const FixedArray & r) const;
281
283
284
288 // false positive warnings with GCC
289 ITK_GCC_PRAGMA_PUSH
290 ITK_GCC_SUPPRESS_Warray_bounds
291 constexpr reference
292 operator[](unsigned int index)
293 {
294 return m_InternalArray[index];
295 }
296 constexpr const_reference
297 operator[](unsigned int index) const
298 {
299 return m_InternalArray[index];
300 }
301 ITK_GCC_PRAGMA_POP
305 void
306 SetElement(unsigned int index, const_reference value)
307 {
308 m_InternalArray[index] = value;
309 }
310 [[nodiscard]] const_reference
311 GetElement(unsigned int index) const
312 {
313 return m_InternalArray[index];
314 }
315
316
317 [[nodiscard]] constexpr reference
319 {
320 return m_InternalArray[0];
321 }
322
323 [[nodiscard]] constexpr const_reference
324 front() const
325 {
326 return m_InternalArray[0];
327 }
328
329 [[nodiscard]] constexpr reference
331 {
332 return m_InternalArray[VLength - 1];
333 }
334
335 [[nodiscard]] constexpr const_reference
336 back() const
337 {
338 return m_InternalArray[VLength - 1];
339 }
340
342 ValueType *
344 {
345 return m_InternalArray;
346 }
347
348 [[nodiscard]] const ValueType *
350 {
351 return m_InternalArray;
352 }
353
354 [[nodiscard]] ValueType *
356 {
357 return m_InternalArray;
358 }
359
360 [[nodiscard]] const ValueType *
361 data() const
362 {
363 return m_InternalArray;
364 }
365
367 Iterator
369
371 [[nodiscard]] ConstIterator
372 Begin() const;
373
377
379 [[nodiscard]] ConstIterator
380 End() const;
381
383 itkLegacyMacro(ReverseIterator rBegin();)
384
386 itkLegacyMacro(ConstReverseIterator rBegin() const;)
387
389 itkLegacyMacro(ReverseIterator rEnd();)
390
392 itkLegacyMacro(ConstReverseIterator rEnd() const;)
393
394 [[nodiscard]] constexpr const_iterator
395 cbegin() const noexcept
396 {
397 return m_InternalArray;
398 }
399
400 [[nodiscard]] constexpr iterator
401 begin() noexcept
402 {
403 return m_InternalArray;
404 }
405
406 [[nodiscard]] constexpr const_iterator
407 begin() const noexcept
408 {
409 return this->cbegin();
410 }
411
412 [[nodiscard]] constexpr const_iterator
413 cend() const noexcept
414 {
415 return m_InternalArray + VLength;
416 }
417
418 [[nodiscard]] constexpr iterator
419 end() noexcept
420 {
421 return m_InternalArray + VLength;
422 }
423
424 [[nodiscard]] constexpr const_iterator
425 end() const noexcept
426 {
427 return this->cend();
428 }
429
430 [[nodiscard]] reverse_iterator
432 {
433 return reverse_iterator{ this->end() };
434 }
435
436 [[nodiscard]] const_reverse_iterator
437 crbegin() const
438 {
439 return const_reverse_iterator{ this->cend() };
440 }
441
442 [[nodiscard]] const_reverse_iterator
443 rbegin() const
444 {
445 return this->crbegin();
446 }
447
448 [[nodiscard]] reverse_iterator
450 {
451 return reverse_iterator{ this->begin() };
452 }
453
454 [[nodiscard]] const_reverse_iterator
455 crend() const
456 {
457 return const_reverse_iterator{ this->cbegin() };
458 }
459
460 [[nodiscard]] const_reverse_iterator
461 rend() const
462 {
463 return this->crend();
464 }
465
467 [[nodiscard]] SizeType
468 Size() const;
469
470 [[nodiscard]] constexpr SizeType
471 size() const
472 {
473 return VLength;
474 }
475
476 [[nodiscard]] constexpr bool
477 empty() const noexcept
478 {
479 return false;
480 }
481
483 void
484 Fill(const ValueType &);
485
486 void
487 swap(FixedArray & other) noexcept
488 {
489 std::swap(m_InternalArray, other.m_InternalArray);
490 }
491
492private:
495
496public:
498 static constexpr FixedArray
499 Filled(const ValueType & value)
500 {
501 return MakeFilled<FixedArray>(value);
502 }
503};
504
505template <typename TValue, unsigned int VLength>
506std::ostream &
507operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
508
509
510template <typename TValue, unsigned int VLength>
511inline void
513{
514 a.swap(b);
515}
516
517} // namespace itk
518
519#ifndef ITK_MANUAL_INSTANTIATION
520# include "itkFixedArray.hxx"
521#endif
522
524
525#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.
bool operator==(const ReverseIterator &rit) const
ReverseIterator operator--(int)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ReverseIterator)
ReverseIterator operator++(int)
Simulate a standard C array with copy semantics.
ConstIterator End() const
Iterator End()
bool operator==(const FixedArray &r) const
constexpr SizeType size() const
void swap(FixedArray &other) noexcept
std::reverse_iterator< iterator > reverse_iterator
constexpr const_reference operator[](unsigned int index) const
const_reverse_iterator crend() const
FixedArray(const ValueType r[VLength])
const ValueType * GetDataPointer() const
constexpr iterator begin() noexcept
FixedArray(const ValueType &)
constexpr const_iterator cbegin() const noexcept
const ValueType * data() const
ValueType * GetDataPointer()
constexpr reference back()
FixedArray(const std::array< ValueType, VLength > &stdArray)
const_reverse_iterator crbegin() const
FixedArray()=default
Iterator Begin()
constexpr iterator end() noexcept
reverse_iterator rbegin()
FixedArray(const TScalarValue *r)
static constexpr FixedArray Filled(const ValueType &value)
SizeType Size() const
const_reverse_iterator rend() const
ConstReverseIterator rBegin() 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
ConstReverseIterator rEnd() const
FixedArray & operator=(const ValueType r[VLength])
constexpr const_iterator begin() const noexcept
ITK_GCC_PRAGMA_PUSH ITK_GCC_SUPPRESS_Warray_bounds constexpr reference operator[](unsigned int index)
constexpr reference front()
constexpr bool empty() const noexcept
constexpr const_iterator end() const noexcept
ConstIterator Begin() const
ReverseIterator rBegin()
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(FixedArray)
const_reverse_iterator rbegin() const
ValueType * data()
FixedArray(const FixedArray< TFixedArrayValueType, VLength > &r)
constexpr const_reference back() const
ITK_GCC_PRAGMA_POP void SetElement(unsigned int index, const_reference value)
constexpr const_reference front() const
void Fill(const ValueType &)
ReverseIterator rEnd()
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
constexpr TContainer MakeFilled(typename TContainer::const_reference value)
void swap(Array< T > &a, Array< T > &b) noexcept
Definition itkArray.h:247
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)