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 &
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  }
185  Iterator
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  }
200  Iterator
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 
221  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
222 
223  bool
224  operator==(const ConstIterator & r) const
225  {
226  return m_Iter == r.m_Iter;
227  }
228 
229  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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{};
277  VectorIterator m_Iter{};
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  {}
304  ConstIterator &
306  {
307  return *this;
308  }
309  ConstIterator *
311  {
312  return this;
313  }
314  ConstIterator &
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  }
329  ConstIterator &
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  }
344  ConstIterator &
345  operator=(const Iterator & r)
346  {
347  m_Pos = r.m_Pos;
348  m_Iter = r.m_Iter;
349  return *this;
350  }
351  ConstIterator &
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 
371  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
372 
373  bool
374  operator==(const ConstIterator & r) const
375  {
376  return m_Iter == r.m_Iter;
377  }
378 
379  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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 
434  reference ElementAt(ElementIdentifier);
435 
442  const_reference ElementAt(ElementIdentifier) const;
443 
452  reference CreateElementAt(ElementIdentifier);
453 
458  Element GetElement(ElementIdentifier) const;
459 
464  void SetElement(ElementIdentifier, Element);
465 
471  void InsertElement(ElementIdentifier, Element);
472 
477  bool IndexExists(ElementIdentifier) const;
478 
484  bool
485  GetElementIfIndexExists(ElementIdentifier, Element *) const;
486 
492  void CreateIndex(ElementIdentifier);
493 
499  void DeleteIndex(ElementIdentifier);
500 
505  Begin() const;
506 
511  End() const;
512 
516  Iterator
517  Begin();
518 
522  Iterator
523  End();
524 
529  Size() const;
530 
542  void Reserve(ElementIdentifier);
543 
552  void
553  Squeeze();
554 
558  void
559  Initialize();
560 
561 protected:
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
598 template <typename T1, typename T2 = void>
599 using VectorContainer = detail::VectorContainer<std::conditional_t<std::is_void_v<T2>, SizeValueType, T1>,
600  std::conditional_t<std::is_void_v<T2>, T1, T2>>;
601 
602 
604 template <typename TElement>
605 auto
606 MakeVectorContainer(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
itk::detail::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:194
itk::detail::VectorContainer::STLContainerType
VectorType STLContainerType
Definition: itkVectorContainer.h:74
itk::detail::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:179
itk::detail::VectorContainer::Iterator
Definition: itkVectorContainer.h:154
itk::detail::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:186
itk::detail::VectorContainer::ConstIterator::iterator_category
typename VectorConstIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:289
itkObjectFactory.h
itk::detail::VectorContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:366
itk::detail::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:352
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:566
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:580
itk::detail::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:216
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:290
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:600
itk::detail::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:305
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:276
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:253
itk::detail::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:330
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:566
itk::detail::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:286
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:374
itk::detail::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:224
itk::detail::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:345
itk::detail::VectorContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:63
itk::detail::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:270
itk::detail::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:201
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:413
itk::detail::VectorContainer::Iterator::operator->
Iterator * operator->()
Definition: itkVectorContainer.h:174
itk::MakeVectorContainer
auto MakeVectorContainer(std::vector< TElement > stdVector)
Definition: itkVectorContainer.h:606
itk::detail::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:293
itk::detail::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:210
itk::detail::VectorContainer::ConstIterator::operator>
bool operator>(const ConstIterator &r) const
Definition: itkVectorContainer.h:387
itk::detail::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:337
itk::detail::VectorContainer::ConstIterator::difference_type
typename VectorConstIterator::difference_type difference_type
Definition: itkVectorContainer.h:291
itk::detail::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:237
itk::detail::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:360
itk::detail::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:263
itk::detail::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:420
itkObject.h
itk::detail::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:169
itk::detail::VectorContainer::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:68
itk::detail::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:277
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:310
itk::detail::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:300
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:296
itk::detail::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:292
itk::detail::VectorContainer::VectorContainer
VectorContainer(TInputIterator first, TInputIterator last)
Definition: itkVectorContainer.h:579
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:242
itk::detail::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:419
itk::detail::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:315
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:570
itk::detail::VectorContainer::Element
TElement Element
Definition: itkVectorContainer.h:64
itk::detail::VectorContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:406
itk::detail::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:322
itk::detail::VectorContainer::VectorContainer
VectorContainer(const Self &r)
Definition: itkVectorContainer.h:574
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:397
itk::detail::VectorContainer::Iterator::pointer
typename VectorIterator::pointer pointer
Definition: itkVectorContainer.h:160