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 
28 namespace itk
29 {
30 namespace detail
31 {
50 template <typename TElementIdentifier, typename TElement>
51 class ITK_TEMPLATE_EXPORT VectorContainer
52  : public Object
53  , private std::vector<TElement>
54 {
55 public:
58  using Superclass = Object;
61 
63  using ElementIdentifier = TElementIdentifier;
64  using Element = TElement;
65 
66 private:
68  using VectorType = std::vector<Element>;
69  using VectorIterator = typename VectorType::iterator;
70  using VectorConstIterator = typename VectorType::const_iterator;
71 
72 public:
75 
77  itkNewMacro(Self);
78 
80  itkOverrideGetNameOfClassMacro(VectorContainer);
81 
83  class Iterator;
84  class ConstIterator;
85 
88  CastToSTLContainer() noexcept
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 
154  class Iterator
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  }
177  Iterator
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  }
192  Iterator
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 
213  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
214 
215  bool
216  operator==(const ConstIterator & r) const
217  {
218  return m_Iter == r.m_Iter;
219  }
220 
221  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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{};
269  VectorIterator m_Iter{};
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; }
298  ConstIterator &
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  }
313  ConstIterator &
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  }
328  ConstIterator &
329  operator=(const Iterator & r)
330  {
331  m_Pos = r.m_Pos;
332  m_Iter = r.m_Iter;
333  return *this;
334  }
335  ConstIterator &
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 
355  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
356 
357  bool
358  operator==(const ConstIterator & r) const
359  {
360  return m_Iter == r.m_Iter;
361  }
362 
363  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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 
418  reference ElementAt(ElementIdentifier);
419 
426  const_reference ElementAt(ElementIdentifier) const;
427 
436  reference CreateElementAt(ElementIdentifier);
437 
442  Element GetElement(ElementIdentifier) const;
443 
448  void SetElement(ElementIdentifier, Element);
449 
455  void InsertElement(ElementIdentifier, Element);
456 
461  bool IndexExists(ElementIdentifier) const;
462 
468  bool
469  GetElementIfIndexExists(ElementIdentifier, Element *) const;
470 
476  void CreateIndex(ElementIdentifier);
477 
483  void DeleteIndex(ElementIdentifier);
484 
489  Begin() const;
490 
495  End() const;
496 
500  Iterator
501  Begin();
502 
506  Iterator
507  End();
508 
513  Size() const;
514 
526  void Reserve(ElementIdentifier);
527 
536  void
537  Squeeze();
538 
542  void
543  Initialize();
544 
545 protected:
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
582 template <typename T1, typename T2 = void>
583 using VectorContainer = detail::VectorContainer<std::conditional_t<std::is_void_v<T2>, SizeValueType, T1>,
584  std::conditional_t<std::is_void_v<T2>, T1, T2>>;
585 
586 
588 template <typename TElement>
589 auto
590 MakeVectorContainer(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
itk::detail::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:186
itk::detail::VectorContainer::STLContainerType
VectorType STLContainerType
Definition: itkVectorContainer.h:74
itk::detail::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:171
itk::detail::VectorContainer::Iterator
Definition: itkVectorContainer.h:154
itk::detail::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:178
itk::detail::VectorContainer::ConstIterator::iterator_category
typename VectorConstIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:281
itkObjectFactory.h
itk::detail::VectorContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:350
itk::detail::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:336
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:557
itk::detail::VectorContainer
Define a front-end to the STL "vector" container that conforms to the IndexedContainerInterface.
Definition: itkVectorContainer.h:51
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:571
itk::detail::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:208
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::detail::VectorContainer::Iterator::reference
typename VectorIterator::reference reference
Definition: itkVectorContainer.h:161
itk::detail::VectorContainer::ConstIterator::value_type
typename VectorConstIterator::value_type value_type
Definition: itkVectorContainer.h:282
itk::VectorContainer
detail::VectorContainer< std::conditional_t< std::is_void_v< T2 >, SizeValueType, T1 >, std::conditional_t< std::is_void_v< T2 >, T1, T2 > > VectorContainer
Definition: itkVectorContainer.h:584
itk::detail::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:296
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::detail::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:268
itk::detail::VectorContainer::Iterator::iterator_category
typename VectorIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:157
itk::detail::VectorContainer::New
static Pointer New()
itk::SmartPointer< Self >
itk::detail::VectorContainer::Iterator::operator+=
Iterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:245
itk::detail::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:314
itk::detail::VectorContainer::Iterator::difference_type
typename VectorIterator::difference_type difference_type
Definition: itkVectorContainer.h:159
itk::detail::VectorContainer::VectorIterator
typename VectorType::iterator VectorIterator
Definition: itkVectorContainer.h:69
itk::detail::VectorContainer::Iterator::value_type
typename VectorIterator::value_type value_type
Definition: itkVectorContainer.h:158
itk::detail::VectorContainer::VectorContainer
VectorContainer(size_type n)
Definition: itkVectorContainer.h:550
itk::detail::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:278
itk::detail::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:95
itk::detail::VectorContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:358
itk::detail::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:216
itk::detail::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:329
itk::detail::VectorContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:63
itk::detail::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:262
itk::detail::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:193
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::detail::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:397
itk::detail::VectorContainer::Iterator::operator->
Iterator * operator->()
Definition: itkVectorContainer.h:169
itk::MakeVectorContainer
auto MakeVectorContainer(std::vector< TElement > stdVector)
Definition: itkVectorContainer.h:590
itk::detail::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:285
itk::detail::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:202
itk::detail::VectorContainer::ConstIterator::operator>
bool operator>(const ConstIterator &r) const
Definition: itkVectorContainer.h:371
itk::detail::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:321
itk::detail::VectorContainer::ConstIterator::difference_type
typename VectorConstIterator::difference_type difference_type
Definition: itkVectorContainer.h:283
itk::detail::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:229
itk::detail::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:344
itk::detail::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:255
itk::detail::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:404
itkObject.h
itk::detail::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:168
itk::detail::VectorContainer::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:68
itk::detail::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:269
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::detail::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:297
itk::detail::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:292
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::detail::VectorContainer::ConstIterator::ConstIterator
ConstIterator(size_type d, const VectorConstIterator &i)
Definition: itkVectorContainer.h:288
itk::detail::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:284
itk::detail::VectorContainer::VectorContainer
VectorContainer(TInputIterator first, TInputIterator last)
Definition: itkVectorContainer.h:563
itk::detail::VectorContainer::VectorConstIterator
typename VectorType::const_iterator VectorConstIterator
Definition: itkVectorContainer.h:70
itk::detail::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:234
itk::detail::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:403
itk::detail::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:299
itk::detail::VectorContainer::Iterator::Iterator
Iterator(size_type d, const VectorIterator &i)
Definition: itkVectorContainer.h:164
itk::detail::VectorContainer::VectorContainer
VectorContainer(size_type n, const Element &x)
Definition: itkVectorContainer.h:554
itk::detail::VectorContainer::Element
TElement Element
Definition: itkVectorContainer.h:64
itk::detail::VectorContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:390
itk::detail::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:306
itk::detail::VectorContainer::VectorContainer
VectorContainer(const Self &r)
Definition: itkVectorContainer.h:558
itk::detail::VectorContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkVectorContainer.h:88
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itk::detail::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:381
itk::detail::VectorContainer::Iterator::pointer
typename VectorIterator::pointer pointer
Definition: itkVectorContainer.h:160