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 &
170 {
171 return *this;
172 }
173 Iterator *
175 {
176 return this;
177 }
178 Iterator &
180 {
181 ++m_Pos;
182 ++m_Iter;
183 return *this;
184 }
187 {
188 Iterator temp(*this);
189 ++m_Pos;
190 ++m_Iter;
191 return temp;
192 }
193 Iterator &
195 {
196 --m_Pos;
197 --m_Iter;
198 return *this;
199 }
202 {
203 Iterator temp(*this);
204 --m_Pos;
205 --m_Iter;
206 return temp;
207 }
208
209 difference_type
210 operator-(const Iterator & r) const
211 {
212 return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
213 }
214
215 bool
216 operator==(const Iterator & r) const
217 {
218 return m_Iter == r.m_Iter;
219 }
220
222
223 bool
224 operator==(const ConstIterator & r) const
225 {
226 return m_Iter == r.m_Iter;
227 }
228
230
231 bool
232 operator<(const Iterator & r) const
233 {
234 return (this->operator-(r)) < 0;
235 }
236 bool
237 operator>(const Iterator & r) const
238 {
239 return (r < *this);
240 }
241 bool
242 operator>=(const Iterator & r) const
243 {
244 return !(*this < r);
245 }
246 bool
247 operator<=(const Iterator & r) const
248 {
249 return !(*this < r);
250 }
251
252 Iterator &
254 {
255 m_Pos += n;
256 m_Iter += n;
257 return *this;
258 }
259
263 Index() const
264 {
265 return static_cast<ElementIdentifier>(m_Pos);
266 }
267
269 reference
270 Value() const
271 {
272 return *m_Iter;
273 }
274
275 private:
276 size_type m_Pos{};
278 friend class ConstIterator;
279 };
280
287 {
288 public:
289 using iterator_category = typename VectorConstIterator::iterator_category;
290 using value_type = typename VectorConstIterator::value_type;
291 using difference_type = typename VectorConstIterator::difference_type;
292 using pointer = typename VectorConstIterator::pointer;
293 using reference = typename VectorConstIterator::reference;
294
295 ConstIterator() = default;
296 ConstIterator(size_type d, const VectorConstIterator & i)
297 : m_Pos(d)
298 , m_Iter(i)
299 {}
301 : m_Pos(r.m_Pos)
302 , m_Iter(r.m_Iter)
303 {}
306 {
307 return *this;
308 }
311 {
312 return this;
313 }
316 {
317 ++m_Pos;
318 ++m_Iter;
319 return *this;
320 }
323 {
324 ConstIterator temp(*this);
325 ++m_Pos;
326 ++m_Iter;
327 return temp;
328 }
331 {
332 --m_Pos;
333 --m_Iter;
334 return *this;
335 }
338 {
339 ConstIterator temp(*this);
340 --m_Pos;
341 --m_Iter;
342 return temp;
343 }
346 {
347 m_Pos = r.m_Pos;
348 m_Iter = r.m_Iter;
349 return *this;
350 }
353 {
354 m_Pos += n;
355 m_Iter += n;
356 return *this;
357 }
358
359 difference_type
360 operator-(const ConstIterator & r) const
361 {
362 return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
363 }
364
365 bool
366 operator==(const Iterator & r) const
367 {
368 return m_Iter == r.m_Iter;
369 }
370
372
373 bool
374 operator==(const ConstIterator & r) const
375 {
376 return m_Iter == r.m_Iter;
377 }
378
380
381 bool
382 operator<(const ConstIterator & r) const
383 {
384 return (this->operator-(r) < 0);
385 }
386 bool
387 operator>(const ConstIterator & r) const
388 {
389 return (r < *this);
390 }
391 bool
392 operator<=(const ConstIterator & r) const
393 {
394 return !(*this > r);
395 }
396 bool
397 operator>=(const ConstIterator & r) const
398 {
399 return !(*this < r);
400 }
401
402
406 Index() const
407 {
408 return static_cast<ElementIdentifier>(m_Pos);
409 }
410
412 const_reference
413 Value() const
414 {
415 return *m_Iter;
416 }
417
418 private:
419 size_type m_Pos{};
421 friend class Iterator;
422 };
423
424 /* Declare the public interface routines. */
425
435
442 const_reference ElementAt(ElementIdentifier) const;
443
453
459
465
472
478
484 bool
486
493
500
505 Begin() const;
506
511 End() const;
512
518
524
529 Size() const;
530
543
552 void
554
558 void
560
561protected:
565 VectorContainer() = default;
566 VectorContainer(size_type n)
567 : Object()
568 , VectorType(n)
569 {}
570 VectorContainer(size_type n, const Element & x)
571 : Object()
572 , VectorType(n, x)
573 {}
575 : Object()
576 , VectorType(r.CastToSTLConstContainer())
577 {}
578 template <typename TInputIterator>
579 VectorContainer(TInputIterator first, TInputIterator last)
580 : Object()
581 , VectorType(first, last)
582 {}
583};
584} // namespace detail
598template <typename T1, typename T2 = void>
600 std::conditional_t<std::is_void_v<T2>, T1, T2>>;
601
602
604template <typename TElement>
605auto
606MakeVectorContainer(std::vector<TElement> stdVector)
607{
608 auto vectorContainer = VectorContainer<TElement>::New();
609 vectorContainer->CastToSTLContainer() = std::move(stdVector);
610 return vectorContainer;
611}
614} // end namespace itk
615
616#ifndef ITK_MANUAL_INSTANTIATION
617# include "itkVectorContainer.hxx"
618#endif
619
620#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:580
unsigned long SizeValueType
Definition: itkIntTypes.h:86
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:566
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