ITK  5.4.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#if defined(__GNUC__)
266# if (__GNUC__ >= 7)
267# pragma GCC diagnostic push
268# pragma GCC diagnostic ignored "-Warray-bounds"
269# endif
270#endif
271 constexpr reference operator[](unsigned int index) { return m_InternalArray[index]; }
272 constexpr const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
273#if defined(__GNUC__)
274# if (__GNUC__ >= 7)
275# pragma GCC diagnostic pop
276# endif
277#endif
281 void
282 SetElement(unsigned int index, const_reference value)
283 {
284 m_InternalArray[index] = value;
285 }
286 const_reference
287 GetElement(unsigned int index) const
288 {
289 return m_InternalArray[index];
290 }
294 ValueType *
296 {
297 return m_InternalArray;
298 }
299
300 const ValueType *
302 {
303 return m_InternalArray;
304 }
305
306 ValueType *
308 {
309 return m_InternalArray;
310 }
311
312 const ValueType *
313 data() const
314 {
315 return m_InternalArray;
316 }
317
319 Iterator
321
324 Begin() const;
325
329
332 End() const;
333
336
339
342
345
346 constexpr const_iterator
347 cbegin() const noexcept
348 {
349 return m_InternalArray;
350 }
351
352 constexpr iterator
353 begin() noexcept
354 {
355 return m_InternalArray;
356 }
357
358 constexpr const_iterator
359 begin() const noexcept
360 {
361 return this->cbegin();
362 }
363
364 constexpr const_iterator
365 cend() const noexcept
366 {
367 return m_InternalArray + VLength;
368 }
369
370 constexpr iterator
371 end() noexcept
372 {
373 return m_InternalArray + VLength;
374 }
375
376 constexpr const_iterator
377 end() const noexcept
378 {
379 return this->cend();
380 }
381
382 reverse_iterator
384 {
385 return reverse_iterator{ this->end() };
386 }
387
388 const_reverse_iterator
389 crbegin() const
390 {
391 return const_reverse_iterator{ this->cend() };
392 }
393
394 const_reverse_iterator
395 rbegin() const
396 {
397 return this->crbegin();
398 }
399
400 reverse_iterator
402 {
403 return reverse_iterator{ this->begin() };
404 }
405
406 const_reverse_iterator
407 crend() const
408 {
409 return const_reverse_iterator{ this->cbegin() };
410 }
411
412 const_reverse_iterator
413 rend() const
414 {
415 return this->crend();
416 }
417
420 Size() const;
421
422 constexpr SizeType
423 size() const
424 {
425 return VLength;
426 }
427
429 void
430 Fill(const ValueType &);
431
432 void
434 {
435 std::swap(m_InternalArray, other.m_InternalArray);
436 }
437
438private:
441
442public:
444 static constexpr FixedArray
445 Filled(const ValueType & value)
446 {
447 return MakeFilled<FixedArray>(value);
448 }
449};
452template <typename TValue, unsigned int VLength>
453std::ostream &
454operator<<(std::ostream & os, const FixedArray<TValue, VLength> & arr);
455
456
457template <typename TValue, unsigned int VLength>
458inline void
460{
461 a.swap(b);
462}
463
464} // namespace itk
465
466#ifndef ITK_MANUAL_INSTANTIATION
467# include "itkFixedArray.hxx"
468#endif
469
471
472#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
itkLegacyMacro(ConstReverseIterator rBegin() 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
itkLegacyMacro(ConstReverseIterator rEnd() const)
const_reverse_iterator crbegin() const
ValueType & reference
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
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
constexpr reference operator[](unsigned int index)
const ValueType & const_reference
FixedArray & operator=(const ValueType r[VLength])
ValueType * iterator
itkLegacyMacro(ReverseIterator rBegin())
constexpr const_iterator begin() const noexcept
ValueType * Iterator
Definition: itkFixedArray.h:69
CArray m_InternalArray
void SetElement(unsigned int index, const_reference value)
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)
void Fill(const ValueType &)
itkLegacyMacro(ReverseIterator rEnd())
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
void swap(FixedArray< TValue, VLength > &a, FixedArray< TValue, VLength > &b)