ITK  6.0.0
Insight Toolkit
itkVectorContainer.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 itkVectorContainer_h
19#define itkVectorContainer_h
20
21#include "itkObject.h"
22#include "itkObjectFactory.h"
23
24#include <type_traits> // For is_void_v.
25#include <utility>
26#include <vector>
27
28namespace itk
29{
30namespace detail
31{
50template <typename TElementIdentifier, typename TElement>
51class ITK_TEMPLATE_EXPORT VectorContainer
52 : public Object
53 , private std::vector<TElement>
54{
55public:
61
63 using ElementIdentifier = TElementIdentifier;
64 using Element = TElement;
65
66private:
68 using VectorType = std::vector<Element>;
69 using VectorIterator = typename VectorType::iterator;
70 using VectorConstIterator = typename VectorType::const_iterator;
71
72public:
75
77 itkNewMacro(Self);
78
80 itkOverrideGetNameOfClassMacro(VectorContainer);
81
83 class Iterator;
84 class ConstIterator;
85
89 {
90 return *this;
91 }
92
94 const STLContainerType &
95 CastToSTLConstContainer() const noexcept
96 {
97 return *this;
98 }
99
100 using STLContainerType::begin;
101 using STLContainerType::end;
102 using STLContainerType::rbegin;
103 using STLContainerType::rend;
104 using STLContainerType::cbegin;
105 using STLContainerType::cend;
106 using STLContainerType::crbegin;
107 using STLContainerType::crend;
108
109 using STLContainerType::size;
110 using STLContainerType::max_size;
111 using STLContainerType::resize;
112 using STLContainerType::capacity;
113 using STLContainerType::empty;
114 using STLContainerType::reserve;
115 using STLContainerType::shrink_to_fit;
116
117 using STLContainerType::operator[];
118 using STLContainerType::at;
119 using STLContainerType::front;
120 using STLContainerType::back;
121
122 using STLContainerType::assign;
123 using STLContainerType::push_back;
124 using STLContainerType::pop_back;
125 using STLContainerType::insert;
126 using STLContainerType::erase;
128 using STLContainerType::clear;
129
130 using STLContainerType::get_allocator;
131
132 using typename STLContainerType::reference;
133 using typename STLContainerType::const_reference;
134 using typename STLContainerType::iterator;
135 using typename STLContainerType::const_iterator;
136 using typename STLContainerType::size_type;
137 using typename STLContainerType::difference_type;
138 using typename STLContainerType::value_type;
139 using typename STLContainerType::allocator_type;
140 using typename STLContainerType::pointer;
141 using typename STLContainerType::const_pointer;
142 using typename STLContainerType::reverse_iterator;
143 using typename STLContainerType::const_reverse_iterator;
144
146 friend class Iterator;
147 friend class ConstIterator;
148
155 {
156 public:
157 using iterator_category = typename VectorIterator::iterator_category;
158 using value_type = typename VectorIterator::value_type;
159 using difference_type = typename VectorIterator::difference_type;
160 using pointer = typename VectorIterator::pointer;
161 using reference = typename VectorIterator::reference;
162
163 Iterator() = default;
164 Iterator(size_type d, const VectorIterator & i)
165 : m_Pos(d)
166 , m_Iter(i)
167 {}
168 Iterator & operator*() { return *this; }
169 Iterator * operator->() { return this; }
170 Iterator &
172 {
173 ++m_Pos;
174 ++m_Iter;
175 return *this;
176 }
179 {
180 Iterator temp(*this);
181 ++m_Pos;
182 ++m_Iter;
183 return temp;
184 }
185 Iterator &
187 {
188 --m_Pos;
189 --m_Iter;
190 return *this;
191 }
194 {
195 Iterator temp(*this);
196 --m_Pos;
197 --m_Iter;
198 return temp;
199 }
200
201 difference_type
202 operator-(const Iterator & r) const
203 {
204 return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
205 }
206
207 bool
208 operator==(const Iterator & r) const
209 {
210 return m_Iter == r.m_Iter;
211 }
212
214
215 bool
216 operator==(const ConstIterator & r) const
217 {
218 return m_Iter == r.m_Iter;
219 }
220
222
223 bool
224 operator<(const Iterator & r) const
225 {
226 return (this->operator-(r)) < 0;
227 }
228 bool
229 operator>(const Iterator & r) const
230 {
231 return (r < *this);
232 }
233 bool
234 operator>=(const Iterator & r) const
235 {
236 return !(*this < r);
237 }
238 bool
239 operator<=(const Iterator & r) const
240 {
241 return !(*this < r);
242 }
243
244 Iterator &
246 {
247 m_Pos += n;
248 m_Iter += n;
249 return *this;
250 }
251
255 Index() const
256 {
257 return static_cast<ElementIdentifier>(m_Pos);
258 }
259
261 reference
262 Value() const
263 {
264 return *m_Iter;
265 }
266
267 private:
268 size_type m_Pos{};
270 friend class ConstIterator;
271 };
272
279 {
280 public:
281 using iterator_category = typename VectorConstIterator::iterator_category;
282 using value_type = typename VectorConstIterator::value_type;
283 using difference_type = typename VectorConstIterator::difference_type;
284 using pointer = typename VectorConstIterator::pointer;
285 using reference = typename VectorConstIterator::reference;
286
287 ConstIterator() = default;
288 ConstIterator(size_type d, const VectorConstIterator & i)
289 : m_Pos(d)
290 , m_Iter(i)
291 {}
293 : m_Pos(r.m_Pos)
294 , m_Iter(r.m_Iter)
295 {}
296 ConstIterator & operator*() { return *this; }
297 ConstIterator * operator->() { return this; }
300 {
301 ++m_Pos;
302 ++m_Iter;
303 return *this;
304 }
307 {
308 ConstIterator temp(*this);
309 ++m_Pos;
310 ++m_Iter;
311 return temp;
312 }
315 {
316 --m_Pos;
317 --m_Iter;
318 return *this;
319 }
322 {
323 ConstIterator temp(*this);
324 --m_Pos;
325 --m_Iter;
326 return temp;
327 }
330 {
331 m_Pos = r.m_Pos;
332 m_Iter = r.m_Iter;
333 return *this;
334 }
337 {
338 m_Pos += n;
339 m_Iter += n;
340 return *this;
341 }
342
343 difference_type
344 operator-(const ConstIterator & r) const
345 {
346 return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
347 }
348
349 bool
350 operator==(const Iterator & r) const
351 {
352 return m_Iter == r.m_Iter;
353 }
354
356
357 bool
358 operator==(const ConstIterator & r) const
359 {
360 return m_Iter == r.m_Iter;
361 }
362
364
365 bool
366 operator<(const ConstIterator & r) const
367 {
368 return (this->operator-(r) < 0);
369 }
370 bool
371 operator>(const ConstIterator & r) const
372 {
373 return (r < *this);
374 }
375 bool
376 operator<=(const ConstIterator & r) const
377 {
378 return !(*this > r);
379 }
380 bool
381 operator>=(const ConstIterator & r) const
382 {
383 return !(*this < r);
384 }
385
386
390 Index() const
391 {
392 return static_cast<ElementIdentifier>(m_Pos);
393 }
394
396 const_reference
397 Value() const
398 {
399 return *m_Iter;
400 }
401
402 private:
403 size_type m_Pos{};
405 friend class Iterator;
406 };
407
408 /* Declare the public interface routines. */
409
419
426 const_reference ElementAt(ElementIdentifier) const;
427
437
443
449
456
462
468 bool
470
477
484
489 Begin() const;
490
495 End() const;
496
502
508
513 Size() const;
514
527
536 void
538
542 void
544
545protected:
549 VectorContainer() = default;
550 VectorContainer(size_type n)
551 : Object()
552 , VectorType(n)
553 {}
554 VectorContainer(size_type n, const Element & x)
555 : Object()
556 , VectorType(n, x)
557 {}
559 : Object()
560 , VectorType(r.CastToSTLConstContainer())
561 {}
562 template <typename TInputIterator>
563 VectorContainer(TInputIterator first, TInputIterator last)
564 : Object()
565 , VectorType(first, last)
566 {}
567};
568} // namespace detail
582template <typename T1, typename T2 = void>
584 std::conditional_t<std::is_void_v<T2>, T1, T2>>;
585
586
588template <typename TElement>
589auto
590MakeVectorContainer(std::vector<TElement> stdVector)
591{
592 auto vectorContainer = VectorContainer<TElement>::New();
593 vectorContainer->CastToSTLContainer() = std::move(stdVector);
594 return vectorContainer;
595}
598} // end namespace itk
599
600#ifndef ITK_MANUAL_INSTANTIATION
601# include "itkVectorContainer.hxx"
602#endif
603
604#endif
Light weight base class for most itk classes.
Base class for most ITK classes.
Definition: itkObject.h:62
typename VectorConstIterator::pointer pointer
bool operator==(const ConstIterator &r) const
bool operator>=(const ConstIterator &r) const
ConstIterator & operator=(const Iterator &r)
bool operator>(const ConstIterator &r) const
difference_type operator-(const ConstIterator &r) const
ConstIterator(size_type d, const VectorConstIterator &i)
typename VectorConstIterator::reference reference
typename VectorConstIterator::difference_type difference_type
ConstIterator & operator+=(difference_type n)
typename VectorConstIterator::value_type value_type
typename VectorConstIterator::iterator_category iterator_category
difference_type operator-(const Iterator &r) const
typename VectorIterator::difference_type difference_type
typename VectorIterator::pointer pointer
bool operator>(const Iterator &r) const
typename VectorIterator::iterator_category iterator_category
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
Iterator & operator+=(difference_type n)
bool operator==(const Iterator &r) const
typename VectorIterator::value_type value_type
bool operator==(const ConstIterator &r) const
bool operator>=(const Iterator &r) const
typename VectorIterator::reference reference
Iterator(size_type d, const VectorIterator &i)
Define a front-end to the STL "vector" container that conforms to the IndexedContainerInterface.
void InsertElement(ElementIdentifier, Element)
void CreateIndex(ElementIdentifier)
bool IndexExists(ElementIdentifier) const
Element GetElement(ElementIdentifier) const
ConstIterator Begin() const
reference CreateElementAt(ElementIdentifier)
TElementIdentifier ElementIdentifier
STLContainerType & CastToSTLContainer() noexcept
void DeleteIndex(ElementIdentifier)
VectorContainer(size_type n, const Element &x)
ConstIterator End() const
typename VectorType::iterator VectorIterator
reference ElementAt(ElementIdentifier)
ElementIdentifier Size() const
std::vector< Element > VectorType
bool GetElementIfIndexExists(ElementIdentifier, Element *) const
typename VectorType::const_iterator VectorConstIterator
void Reserve(ElementIdentifier)
const_reference ElementAt(ElementIdentifier) const
VectorContainer(TInputIterator first, TInputIterator last)
const STLContainerType & CastToSTLConstContainer() const noexcept
void SetElement(ElementIdentifier, Element)
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
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:571
unsigned long SizeValueType
Definition: itkIntTypes.h:86
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:557
auto MakeVectorContainer(std::vector< TElement > stdVector)
detail::VectorContainer< std::conditional_t< std::is_void_v< T2 >, SizeValueType, T1 >, std::conditional_t< std::is_void_v< T2 >, T1, T2 > > VectorContainer