ITK  5.4.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 
27 namespace itk
28 {
47 template <typename TElementIdentifier, typename TElement>
48 class ITK_TEMPLATE_EXPORT VectorContainer
49  : public Object
50  , private std::vector<TElement>
51 {
52 public:
55  using Superclass = Object;
58 
60  using ElementIdentifier = TElementIdentifier;
61  using Element = TElement;
62 
63 private:
65  using VectorType = std::vector<Element>;
66  using VectorIterator = typename VectorType::iterator;
67  using VectorConstIterator = typename VectorType::const_iterator;
68 
69 public:
72 
74  itkNewMacro(Self);
75 
77  itkOverrideGetNameOfClassMacro(VectorContainer);
78 
80  class Iterator;
81  class ConstIterator;
82 
85  CastToSTLContainer() noexcept
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 
151  class Iterator
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  }
174  Iterator
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  }
189  Iterator
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 
210  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
211 
212  bool
213  operator==(const ConstIterator & r) const
214  {
215  return m_Iter == r.m_Iter;
216  }
217 
218  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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{};
266  VectorIterator m_Iter{};
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; }
295  ConstIterator &
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  }
310  ConstIterator &
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  }
325  ConstIterator &
326  operator=(const Iterator & r)
327  {
328  m_Pos = r.m_Pos;
329  m_Iter = r.m_Iter;
330  return *this;
331  }
332  ConstIterator &
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 
352  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
353 
354  bool
355  operator==(const ConstIterator & r) const
356  {
357  return m_Iter == r.m_Iter;
358  }
359 
360  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
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 
415  reference ElementAt(ElementIdentifier);
416 
423  const_reference ElementAt(ElementIdentifier) const;
424 
433  reference CreateElementAt(ElementIdentifier);
434 
439  Element GetElement(ElementIdentifier) const;
440 
445  void SetElement(ElementIdentifier, Element);
446 
452  void InsertElement(ElementIdentifier, Element);
453 
458  bool IndexExists(ElementIdentifier) const;
459 
465  bool
466  GetElementIfIndexExists(ElementIdentifier, Element *) const;
467 
473  void CreateIndex(ElementIdentifier);
474 
480  void DeleteIndex(ElementIdentifier);
481 
486  Begin() const;
487 
492  End() const;
493 
497  Iterator
498  Begin();
499 
503  Iterator
504  End();
505 
510  Size() const;
511 
523  void Reserve(ElementIdentifier);
524 
533  void
534  Squeeze();
535 
539  void
540  Initialize();
541 
542 protected:
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
itk::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:303
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorConstIterator
typename VectorType::const_iterator VectorConstIterator
Definition: itkVectorContainer.h:67
itkObjectFactory.h
itk::VectorContainer::Iterator::reference
typename VectorIterator::reference reference
Definition: itkVectorContainer.h:158
itk::VectorContainer::VectorContainer
VectorContainer(size_type n)
Definition: itkVectorContainer.h:547
itk::operator<
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:559
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:573
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::VectorContainer::Iterator::operator->
Iterator * operator->()
Definition: itkVectorContainer.h:166
itk::VectorContainer::VectorContainer
VectorContainer(TInputIterator first, TInputIterator last)
Definition: itkVectorContainer.h:560
itk::VectorContainer::ConstIterator::value_type
typename VectorConstIterator::value_type value_type
Definition: itkVectorContainer.h:279
itk::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:199
itk::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:282
itk::VectorContainer< TElementIdentifier, TElementWrapper >::STLContainerType
VectorType STLContainerType
Definition: itkVectorContainer.h:71
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::VectorContainer::Iterator::operator+=
Iterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:242
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:311
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorIterator
typename VectorType::iterator VectorIterator
Definition: itkVectorContainer.h:66
itk::VectorContainer::Iterator::difference_type
typename VectorIterator::difference_type difference_type
Definition: itkVectorContainer.h:156
itk::VectorContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:387
itk::SmartPointer< Self >
itk::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:205
itk::VectorContainer< TElementIdentifier, TElementWrapper >::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:60
itk::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:333
itk::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:92
itk::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:266
itk::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:293
itk::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:394
itk::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:400
itk::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:226
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::VectorContainer::ConstIterator::iterator_category
typename VectorConstIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:278
itk::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:231
itk::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:213
itk::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:326
itk::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:165
itk::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:265
itk::VectorContainer< TElementIdentifier, TElementWrapper >::Element
TElementWrapper Element
Definition: itkVectorContainer.h:61
itk::VectorContainer::Iterator::Iterator
Iterator(size_type d, const VectorIterator &i)
Definition: itkVectorContainer.h:161
itk::VectorContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:347
itk::VectorContainer::Iterator::iterator_category
typename VectorIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:154
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(size_type d, const VectorConstIterator &i)
Definition: itkVectorContainer.h:285
itk::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:183
itk::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:252
itk::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:294
itk::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:401
itk::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:378
itk::VectorContainer::Iterator::value_type
typename VectorIterator::value_type value_type
Definition: itkVectorContainer.h:155
itkObject.h
itk::VectorContainer::VectorContainer
VectorContainer(const Self &r)
Definition: itkVectorContainer.h:555
itk::VectorContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:355
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::VectorContainer::ConstIterator::difference_type
typename VectorConstIterator::difference_type difference_type
Definition: itkVectorContainer.h:280
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:65
itk::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:259
itk::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:341
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::VectorContainer::ConstIterator::operator>
bool operator>(const ConstIterator &r) const
Definition: itkVectorContainer.h:368
itk::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:168
itk::VectorContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkVectorContainer.h:85
itk::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:296
itk::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:190
itk::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:275
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:289
itk::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:318
itk::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:175
itk::VectorContainer::Iterator::pointer
typename VectorIterator::pointer pointer
Definition: itkVectorContainer.h:157
itk::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:281
itk::VectorContainer::Iterator
Definition: itkVectorContainer.h:151
itk::VectorContainer
Define a front-end to the STL "vector" container that conforms to the IndexedContainerInterface.
Definition: itkVectorContainer.h:48
itk::VectorContainer::VectorContainer
VectorContainer(size_type n, const Element &x)
Definition: itkVectorContainer.h:551