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 {}
143 {
145 }
148 {
150 }
153 {
155 }
158 {
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
185 using value_type = TValue;
186
189
191 using const_pointer = const ValueType *;
192
195
197 using const_reference = const ValueType &;
198
201
203 using const_iterator = const ValueType *;
204
205 using reverse_iterator = std::reverse_iterator<iterator>;
206
207 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
208
209 using SizeType = unsigned int;
210
211public:
214 FixedArray() = default;
215
222 FixedArray(const ValueType r[VLength]);
225
227 explicit FixedArray(const std::array<ValueType, VLength> & stdArray)
228 {
229 std::copy_n(stdArray.cbegin(), VLength, m_InternalArray);
230 }
231
233 template <typename TFixedArrayValueType>
235 {
236 auto input = r.cbegin();
237
238 for (auto & element : m_InternalArray)
239 {
240 element = static_cast<TValue>(*input++);
241 }
242 }
243
244 template <typename TScalarValue>
245 FixedArray(const TScalarValue * r)
246 {
247 std::copy_n(r, VLength, m_InternalArray);
248 }
249
256 template <typename TFixedArrayValueType>
257 FixedArray &
259 {
260 auto input = r.cbegin();
261
262 for (auto & element : m_InternalArray)
263 {
264 element = static_cast<TValue>(*input++);
265 }
266 return *this;
267 }
268
269 FixedArray &
270 operator=(const ValueType r[VLength]);
271
275 bool
276 operator==(const FixedArray & r) const;
277
279
280
283 // false positive warnings with GCC
284 ITK_GCC_PRAGMA_PUSH
285 ITK_GCC_SUPPRESS_Warray_bounds
286 constexpr reference
287 operator[](unsigned int index)
288 {
289 return m_InternalArray[index];
290 }
291 constexpr const_reference
292 operator[](unsigned int index) const
293 {
294 return m_InternalArray[index];
295 }
296 ITK_GCC_PRAGMA_POP
298
300 void
301 SetElement(unsigned int index, const_reference value)
302 {
303 m_InternalArray[index] = value;
304 }
305 const_reference
306 GetElement(unsigned int index) const
307 {
308 return m_InternalArray[index];
309 }
310
311
313 ValueType *
315 {
316 return m_InternalArray;
317 }
318
319 const ValueType *
321 {
322 return m_InternalArray;
323 }
324
325 ValueType *
327 {
328 return m_InternalArray;
329 }
330
331 const ValueType *
332 data() const
333 {
334 return m_InternalArray;
335 }
336
338 Iterator
340
343 Begin() const;
344
348
351 End() const;
352
354 itkLegacyMacro(ReverseIterator rBegin();)
355
357 itkLegacyMacro(ConstReverseIterator rBegin() const;)
358
360 itkLegacyMacro(ReverseIterator rEnd();)
361
363 itkLegacyMacro(ConstReverseIterator rEnd() const;)
364
365 constexpr const_iterator
366 cbegin() const noexcept
367 {
368 return m_InternalArray;
369 }
370
371 constexpr iterator
372 begin() noexcept
373 {
374 return m_InternalArray;
375 }
376
377 constexpr const_iterator
378 begin() const noexcept
379 {
380 return this->cbegin();
381 }
382
383 constexpr const_iterator
384 cend() const noexcept
385 {
386 return m_InternalArray + VLength;
387 }
388
389 constexpr iterator
390 end() noexcept
391 {
392 return m_InternalArray + VLength;
393 }
394
395 constexpr const_iterator
396 end() const noexcept
397 {
398 return this->cend();
399 }
400
401 reverse_iterator
403 {
404 return reverse_iterator{ this->end() };
405 }
406
407 const_reverse_iterator
408 crbegin() const
409 {
410 return const_reverse_iterator{ this->cend() };
411 }
412
413 const_reverse_iterator
414 rbegin() const
415 {
416 return this->crbegin();
417 }
418
419 reverse_iterator
421 {
422 return reverse_iterator{ this->begin() };
423 }
424
425 const_reverse_iterator
426 crend() const
427 {
428 return const_reverse_iterator{ this->cbegin() };
429 }
430
431 const_reverse_iterator
432 rend() const
433 {
434 return this->crend();
435 }
436
438 SizeType
439 Size() const;
440
441 constexpr SizeType
442 size() const
443 {
444 return VLength;
445 }
446
448 void
449 Fill(const ValueType &);
450
451 void
452 swap(FixedArray & other) noexcept
453 {
454 std::swap(m_InternalArray, other.m_InternalArray);
455 }
456
457private:
460
461public:
463 static constexpr FixedArray
464 Filled(const ValueType & value)
465 {
466 return MakeFilled<FixedArray>(value);
467 }
468};
469
470
471template <typename TValue, unsigned int VLength>
472std::ostream &
473operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
474
475
476template <typename TValue, unsigned int VLength>
477inline void
479{
480 a.swap(b);
481}
482
483} // namespace itk
484
485#ifndef ITK_MANUAL_INSTANTIATION
486# include "itkFixedArray.hxx"
487#endif
488
490
491#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:249
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)