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 <utility>
25#include <vector>
26
27namespace itk
28{
47template <typename TElementIdentifier, typename TElement>
48class ITK_TEMPLATE_EXPORT VectorContainer
49 : public Object
50 , private std::vector<TElement>
51{
52public:
58
60 using ElementIdentifier = TElementIdentifier;
61 using Element = TElement;
62
63private:
65 using VectorType = std::vector<Element>;
66 using VectorIterator = typename VectorType::iterator;
67 using VectorConstIterator = typename VectorType::const_iterator;
68
69public:
72
74 itkNewMacro(Self);
75
77 itkOverrideGetNameOfClassMacro(VectorContainer);
78
80 class Iterator;
81 class ConstIterator;
82
86 {
87 return *this;
88 }
89
91 const STLContainerType &
92 CastToSTLConstContainer() const noexcept
93 {
94 return *this;
95 }
96
97 using STLContainerType::begin;
98 using STLContainerType::end;
99 using STLContainerType::rbegin;
100 using STLContainerType::rend;
101 using STLContainerType::cbegin;
102 using STLContainerType::cend;
103 using STLContainerType::crbegin;
104 using STLContainerType::crend;
105
106 using STLContainerType::size;
107 using STLContainerType::max_size;
108 using STLContainerType::resize;
109 using STLContainerType::capacity;
110 using STLContainerType::empty;
111 using STLContainerType::reserve;
112 using STLContainerType::shrink_to_fit;
113
114 using STLContainerType::operator[];
115 using STLContainerType::at;
116 using STLContainerType::front;
117 using STLContainerType::back;
118
119 using STLContainerType::assign;
120 using STLContainerType::push_back;
121 using STLContainerType::pop_back;
122 using STLContainerType::insert;
123 using STLContainerType::erase;
125 using STLContainerType::clear;
126
127 using STLContainerType::get_allocator;
128
129 using typename STLContainerType::reference;
130 using typename STLContainerType::const_reference;
131 using typename STLContainerType::iterator;
132 using typename STLContainerType::const_iterator;
133 using typename STLContainerType::size_type;
134 using typename STLContainerType::difference_type;
135 using typename STLContainerType::value_type;
136 using typename STLContainerType::allocator_type;
137 using typename STLContainerType::pointer;
138 using typename STLContainerType::const_pointer;
139 using typename STLContainerType::reverse_iterator;
140 using typename STLContainerType::const_reverse_iterator;
141
143 friend class Iterator;
144 friend class ConstIterator;
145
152 {
153 public:
154 using iterator_category = typename VectorIterator::iterator_category;
155 using value_type = typename VectorIterator::value_type;
156 using difference_type = typename VectorIterator::difference_type;
157 using pointer = typename VectorIterator::pointer;
158 using reference = typename VectorIterator::reference;
159
160 Iterator() = default;
161 Iterator(size_type d, const VectorIterator & i)
162 : m_Pos(d)
163 , m_Iter(i)
164 {}
165 Iterator & operator*() { return *this; }
166 Iterator * operator->() { return this; }
167 Iterator &
169 {
170 ++m_Pos;
171 ++m_Iter;
172 return *this;
173 }
176 {
177 Iterator temp(*this);
178 ++m_Pos;
179 ++m_Iter;
180 return temp;
181 }
182 Iterator &
184 {
185 --m_Pos;
186 --m_Iter;
187 return *this;
188 }
191 {
192 Iterator temp(*this);
193 --m_Pos;
194 --m_Iter;
195 return temp;
196 }
197
198 difference_type
199 operator-(const Iterator & r) const
200 {
201 return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
202 }
203
204 bool
205 operator==(const Iterator & r) const
206 {
207 return m_Iter == r.m_Iter;
208 }
209
211
212 bool
213 operator==(const ConstIterator & r) const
214 {
215 return m_Iter == r.m_Iter;
216 }
217
219
220 bool
221 operator<(const Iterator & r) const
222 {
223 return (this->operator-(r)) < 0;
224 }
225 bool
226 operator>(const Iterator & r) const
227 {
228 return (r < *this);
229 }
230 bool
231 operator>=(const Iterator & r) const
232 {
233 return !(*this < r);
234 }
235 bool
236 operator<=(const Iterator & r) const
237 {
238 return !(*this < r);
239 }
240
241 Iterator &
243 {
244 m_Pos += n;
245 m_Iter += n;
246 return *this;
247 }
248
252 Index() const
253 {
254 return static_cast<ElementIdentifier>(m_Pos);
255 }
256
258 reference
259 Value() const
260 {
261 return *m_Iter;
262 }
263
264 private:
265 size_type m_Pos{};
267 friend class ConstIterator;
268 };
269
276 {
277 public:
278 using iterator_category = typename VectorConstIterator::iterator_category;
279 using value_type = typename VectorConstIterator::value_type;
280 using difference_type = typename VectorConstIterator::difference_type;
281 using pointer = typename VectorConstIterator::pointer;
282 using reference = typename VectorConstIterator::reference;
283
284 ConstIterator() = default;
285 ConstIterator(size_type d, const VectorConstIterator & i)
286 : m_Pos(d)
287 , m_Iter(i)
288 {}
290 : m_Pos(r.m_Pos)
291 , m_Iter(r.m_Iter)
292 {}
293 ConstIterator & operator*() { return *this; }
294 ConstIterator * operator->() { return this; }
297 {
298 ++m_Pos;
299 ++m_Iter;
300 return *this;
301 }
304 {
305 ConstIterator temp(*this);
306 ++m_Pos;
307 ++m_Iter;
308 return temp;
309 }
312 {
313 --m_Pos;
314 --m_Iter;
315 return *this;
316 }
319 {
320 ConstIterator temp(*this);
321 --m_Pos;
322 --m_Iter;
323 return temp;
324 }
327 {
328 m_Pos = r.m_Pos;
329 m_Iter = r.m_Iter;
330 return *this;
331 }
334 {
335 m_Pos += n;
336 m_Iter += n;
337 return *this;
338 }
339
340 difference_type
341 operator-(const ConstIterator & r) const
342 {
343 return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
344 }
345
346 bool
347 operator==(const Iterator & r) const
348 {
349 return m_Iter == r.m_Iter;
350 }
351
353
354 bool
355 operator==(const ConstIterator & r) const
356 {
357 return m_Iter == r.m_Iter;
358 }
359
361
362 bool
363 operator<(const ConstIterator & r) const
364 {
365 return (this->operator-(r) < 0);
366 }
367 bool
368 operator>(const ConstIterator & r) const
369 {
370 return (r < *this);
371 }
372 bool
373 operator<=(const ConstIterator & r) const
374 {
375 return !(*this > r);
376 }
377 bool
378 operator>=(const ConstIterator & r) const
379 {
380 return !(*this < r);
381 }
382
383
387 Index() const
388 {
389 return static_cast<ElementIdentifier>(m_Pos);
390 }
391
393 const_reference
394 Value() const
395 {
396 return *m_Iter;
397 }
398
399 private:
400 size_type m_Pos{};
402 friend class Iterator;
403 };
404
405 /* Declare the public interface routines. */
406
416
423 const_reference ElementAt(ElementIdentifier) const;
424
434
440
446
453
459
465 bool
467
474
481
486 Begin() const;
487
492 End() const;
493
499
505
510 Size() const;
511
524
533 void
535
539 void
541
542protected:
546 VectorContainer() = default;
547 VectorContainer(size_type n)
548 : Object()
549 , VectorType(n)
550 {}
551 VectorContainer(size_type n, const Element & x)
552 : Object()
553 , VectorType(n, x)
554 {}
556 : Object()
557 , VectorType(r.CastToSTLConstContainer())
558 {}
559 template <typename TInputIterator>
560 VectorContainer(TInputIterator first, TInputIterator last)
561 : Object()
562 , VectorType(first, last)
563 {}
564};
565} // end namespace itk
568#ifndef ITK_MANUAL_INSTANTIATION
569# include "itkVectorContainer.hxx"
570#endif
571
572#endif
Light weight base class for most itk classes.
Base class for most ITK classes.
Definition: itkObject.h:62
bool operator>=(const ConstIterator &r) const
typename VectorConstIterator::value_type value_type
ConstIterator(size_type d, const VectorConstIterator &i)
typename VectorConstIterator::pointer pointer
typename VectorConstIterator::iterator_category iterator_category
typename VectorConstIterator::difference_type difference_type
ConstIterator & operator+=(difference_type n)
bool operator>(const ConstIterator &r) const
bool operator==(const Iterator &r) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
bool operator==(const ConstIterator &r) const
difference_type operator-(const ConstIterator &r) const
ConstIterator & operator=(const Iterator &r)
typename VectorConstIterator::reference reference
Iterator & operator+=(difference_type n)
typename VectorIterator::pointer pointer
typename VectorIterator::difference_type difference_type
Iterator(size_type d, const VectorIterator &i)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator)
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
typename VectorIterator::reference reference
typename VectorIterator::iterator_category iterator_category
bool operator==(const Iterator &r) const
bool operator==(const ConstIterator &r) const
difference_type operator-(const Iterator &r) const
ElementIdentifier Index() const
bool operator>=(const Iterator &r) const
bool operator>(const Iterator &r) const
typename VectorIterator::value_type value_type
Define a front-end to the STL "vector" container that conforms to the IndexedContainerInterface.
void CreateIndex(ElementIdentifier)
void Reserve(ElementIdentifier)
typename VectorType::const_iterator VectorConstIterator
VectorContainer(const Self &r)
void InsertElement(ElementIdentifier, Element)
VectorContainer(size_type n, const Element &x)
STLContainerType & CastToSTLContainer() noexcept
typename VectorType::iterator VectorIterator
Element GetElement(ElementIdentifier) const
reference ElementAt(ElementIdentifier)
void DeleteIndex(ElementIdentifier)
ConstIterator Begin() const
const STLContainerType & CastToSTLConstContainer() const noexcept
VectorContainer()=default
TElementIdentifier ElementIdentifier
bool IndexExists(ElementIdentifier) const
VectorContainer(TInputIterator first, TInputIterator last)
reference CreateElementAt(ElementIdentifier)
const_reference ElementAt(ElementIdentifier) const
ConstIterator End() const
bool GetElementIfIndexExists(ElementIdentifier, Element *) const
void SetElement(ElementIdentifier, Element)
std::vector< Element > VectorType
ElementIdentifier Size() 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
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:571
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:557