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 }
106 Iterator operator->() const { return (m_Iterator - 1); }
107 ValueType & operator*() const { return *(m_Iterator - 1); }
108
109 bool
110 operator==(const ReverseIterator & rit) const
111 {
112 return m_Iterator == rit.m_Iterator;
113 }
114
116
117 private:
120 };
121
127 {
128 public:
130 : m_Iterator(i)
131 {}
132 ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
135 {
136 return ConstReverseIterator(--m_Iterator);
137 }
140 {
141 return ConstReverseIterator(m_Iterator--);
142 }
145 {
146 return ConstReverseIterator(++m_Iterator);
147 }
150 {
151 return ConstReverseIterator(m_Iterator++);
152 }
153 ConstIterator operator->() const { return (m_Iterator - 1); }
154 const ValueType & operator*() const { return *(m_Iterator - 1); }
155
156 bool
158 {
159 return m_Iterator == rit.m_Iterator;
160 }
161
163
164 private:
166 };
167
170
172 using const_pointer = const ValueType *;
173
176
178 using const_reference = const ValueType &;
179
182
184 using const_iterator = const ValueType *;
185
186 using reverse_iterator = std::reverse_iterator<iterator>;
187
188 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
189
190 using SizeType = unsigned int;
191
192public:
195 FixedArray() = default;
196
203 FixedArray(const ValueType r[VLength]);
208 explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
209 {
210 std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
211 }
212
214 template <typename TFixedArrayValueType>
216 {
217 auto input = r.cbegin();
218
219 for (auto & element : m_InternalArray)
220 {
221 element = static_cast<TValue>(*input++);
222 }
223 }
224
225 template <typename TScalarValue>
226 FixedArray(const TScalarValue * r)
227 {
228 std::copy_n(r, VLength, m_InternalArray);
229 }
230
237 template <typename TFixedArrayValueType>
238 FixedArray &
240 {
241 auto input = r.cbegin();
242
243 for (auto & element : m_InternalArray)
244 {
245 element = static_cast<TValue>(*input++);
246 }
247 return *this;
248 }
249
250 FixedArray &
251 operator=(const ValueType r[VLength]);
252
256 bool
257 operator==(const FixedArray & r) const;
258
260
261
264 // false positive warnings with GCC
265 ITK_GCC_PRAGMA_PUSH
266 ITK_GCC_SUPPRESS_Warray_bounds
267 constexpr reference operator[](unsigned int index) { return m_InternalArray[index]; }
268 constexpr const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
269 ITK_GCC_PRAGMA_POP
273 void
274 SetElement(unsigned int index, const_reference value)
275 {
276 m_InternalArray[index] = value;
277 }
278 const_reference
279 GetElement(unsigned int index) const
280 {
281 return m_InternalArray[index];
282 }
286 ValueType *
288 {
289 return m_InternalArray;
290 }
291
292 const ValueType *
294 {
295 return m_InternalArray;
296 }
297
298 ValueType *
300 {
301 return m_InternalArray;
302 }
303
304 const ValueType *
305 data() const
306 {
307 return m_InternalArray;
308 }
309
311 Iterator
313
316 Begin() const;
317
321
324 End() const;
325
328
330 itkLegacyMacro(ConstReverseIterator rBegin() const;)
331
334
336 itkLegacyMacro(ConstReverseIterator rEnd() const;)
337
338 constexpr const_iterator
339 cbegin() const noexcept
340 {
341 return m_InternalArray;
342 }
343
344 constexpr iterator
345 begin() noexcept
346 {
347 return m_InternalArray;
348 }
349
350 constexpr const_iterator
351 begin() const noexcept
352 {
353 return this->cbegin();
354 }
355
356 constexpr const_iterator
357 cend() const noexcept
358 {
359 return m_InternalArray + VLength;
360 }
361
362 constexpr iterator
363 end() noexcept
364 {
365 return m_InternalArray + VLength;
366 }
367
368 constexpr const_iterator
369 end() const noexcept
370 {
371 return this->cend();
372 }
373
374 reverse_iterator
376 {
377 return reverse_iterator{ this->end() };
378 }
379
380 const_reverse_iterator
381 crbegin() const
382 {
383 return const_reverse_iterator{ this->cend() };
384 }
385
386 const_reverse_iterator
387 rbegin() const
388 {
389 return this->crbegin();
390 }
391
392 reverse_iterator
394 {
395 return reverse_iterator{ this->begin() };
396 }
397
398 const_reverse_iterator
399 crend() const
400 {
401 return const_reverse_iterator{ this->cbegin() };
402 }
403
404 const_reverse_iterator
405 rend() const
406 {
407 return this->crend();
408 }
409
412 Size() const;
413
414 constexpr SizeType
415 size() const
416 {
417 return VLength;
418 }
419
421 void
422 Fill(const ValueType &);
423
424 void
426 {
427 std::swap(m_InternalArray, other.m_InternalArray);
428 }
429
430private:
433
434public:
436 static constexpr FixedArray
437 Filled(const ValueType & value)
438 {
439 return MakeFilled<FixedArray>(value);
440 }
441};
444template <typename TValue, unsigned int VLength>
445std::ostream &
446operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
447
448
449template <typename TValue, unsigned int VLength>
450inline void
452{
453 a.swap(b);
454}
455
456} // namespace itk
457
458#ifndef ITK_MANUAL_INSTANTIATION
459# include "itkFixedArray.hxx"
460#endif
461
463
464#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
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
void swap(FixedArray &other)
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
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b)