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{
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
74 class ConstReverseIterator;
75
81 {
82 public:
84 : m_Iterator(i)
85 {}
88 {
90 }
93 {
95 }
98 {
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 {}
145 {
147 }
150 {
152 }
155 {
157 }
160 {
162 }
165 {
166 return (m_Iterator - 1);
167 }
168 const ValueType &
169 operator*() const
170 {
171 return *(m_Iterator - 1);
172 }
173
174 bool
176 {
177 return m_Iterator == rit.m_Iterator;
178 }
179
181
182 private:
184 };
185
187 using value_type = TValue;
188
191
193 using const_pointer = const ValueType *;
194
197
199 using const_reference = const ValueType &;
200
203
205 using const_iterator = const ValueType *;
206
207 using reverse_iterator = std::reverse_iterator<iterator>;
208
209 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
210
211 using SizeType = unsigned int;
212
213public:
216 FixedArray() = default;
217
225 FixedArray(const ValueType r[VLength]);
229 explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
230 {
231 std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
232 }
233
235 template <typename TFixedArrayValueType>
237 {
238 auto input = r.cbegin();
239
240 for (auto & element : m_InternalArray)
241 {
242 element = static_cast<TValue>(*input++);
243 }
244 }
245
246 template <typename TScalarValue>
247 FixedArray(const TScalarValue * r)
248 {
249 std::copy_n(r, VLength, m_InternalArray);
250 }
251
258 template <typename TFixedArrayValueType>
259 FixedArray &
261 {
262 auto input = r.cbegin();
263
264 for (auto & element : m_InternalArray)
265 {
266 element = static_cast<TValue>(*input++);
267 }
268 return *this;
269 }
270
271 FixedArray &
272 operator=(const ValueType r[VLength]);
273
277 bool
278 operator==(const FixedArray & r) const;
279
281
282
286 // false positive warnings with GCC
287 ITK_GCC_PRAGMA_PUSH
288 ITK_GCC_SUPPRESS_Warray_bounds
289 constexpr reference
290 operator[](unsigned int index)
291 {
292 return m_InternalArray[index];
293 }
294 constexpr const_reference
295 operator[](unsigned int index) const
296 {
297 return m_InternalArray[index];
298 }
299 ITK_GCC_PRAGMA_POP
303 void
304 SetElement(unsigned int index, const_reference value)
305 {
306 m_InternalArray[index] = value;
307 }
308 [[nodiscard]] const_reference
309 GetElement(unsigned int index) const
310 {
311 return m_InternalArray[index];
312 }
313
315 ValueType *
317 {
318 return m_InternalArray;
319 }
320
321 [[nodiscard]] const ValueType *
323 {
324 return m_InternalArray;
325 }
326
327 ValueType *
329 {
330 return m_InternalArray;
331 }
332
333 [[nodiscard]] const ValueType *
334 data() const
335 {
336 return m_InternalArray;
337 }
338
340 Iterator
342
344 [[nodiscard]] ConstIterator
345 Begin() const;
346
350
352 [[nodiscard]] ConstIterator
353 End() const;
354
356 itkLegacyMacro(ReverseIterator rBegin();)
357
359 itkLegacyMacro(ConstReverseIterator rBegin() const;)
360
362 itkLegacyMacro(ReverseIterator rEnd();)
363
365 itkLegacyMacro(ConstReverseIterator rEnd() const;)
366
367 [[nodiscard]] constexpr const_iterator
368 cbegin() const noexcept
369 {
370 return m_InternalArray;
371 }
372
373 constexpr iterator
374 begin() noexcept
375 {
376 return m_InternalArray;
377 }
378
379 [[nodiscard]] constexpr const_iterator
380 begin() const noexcept
381 {
382 return this->cbegin();
383 }
384
385 [[nodiscard]] constexpr const_iterator
386 cend() const noexcept
387 {
388 return m_InternalArray + VLength;
389 }
390
391 constexpr iterator
392 end() noexcept
393 {
394 return m_InternalArray + VLength;
395 }
396
397 [[nodiscard]] constexpr const_iterator
398 end() const noexcept
399 {
400 return this->cend();
401 }
402
403 reverse_iterator
405 {
406 return reverse_iterator{ this->end() };
407 }
408
409 [[nodiscard]] const_reverse_iterator
410 crbegin() const
411 {
412 return const_reverse_iterator{ this->cend() };
413 }
414
415 [[nodiscard]] const_reverse_iterator
416 rbegin() const
417 {
418 return this->crbegin();
419 }
420
421 reverse_iterator
423 {
424 return reverse_iterator{ this->begin() };
425 }
426
427 [[nodiscard]] const_reverse_iterator
428 crend() const
429 {
430 return const_reverse_iterator{ this->cbegin() };
431 }
432
433 [[nodiscard]] const_reverse_iterator
434 rend() const
435 {
436 return this->crend();
437 }
438
440 [[nodiscard]] SizeType
441 Size() const;
442
443 [[nodiscard]] constexpr SizeType
444 size() const
445 {
446 return VLength;
447 }
448
450 void
451 Fill(const ValueType &);
452
453 void
454 swap(FixedArray & other) noexcept
455 {
456 std::swap(m_InternalArray, other.m_InternalArray);
457 }
458
459private:
462
463public:
465 static constexpr FixedArray
466 Filled(const ValueType & value)
467 {
468 return MakeFilled<FixedArray>(value);
469 }
470};
471
472template <typename TValue, unsigned int VLength>
473std::ostream &
474operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
475
476
477template <typename TValue, unsigned int VLength>
478inline void
480{
481 a.swap(b);
482}
483
484} // namespace itk
485
486#ifndef ITK_MANUAL_INSTANTIATION
487# include "itkFixedArray.hxx"
488#endif
489
491
492#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()
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 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)
ITK_GCC_PRAGMA_POP void SetElement(unsigned int index, const_reference value)
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)