ITK  5.3.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 size_type = typename VectorType::size_type;
67  using VectorIterator = typename VectorType::iterator;
68  using VectorConstIterator = typename VectorType::const_iterator;
69 
70 protected:
75  : Object()
76  , VectorType()
77  {}
79  : Object()
80  , VectorType(n)
81  {}
83  : Object()
84  , VectorType(n, x)
85  {}
86  VectorContainer(const Self & r)
87  : Object()
88  , VectorType(r.CastToSTLConstContainer())
89  {}
90  template <typename TInputIterator>
91  VectorContainer(TInputIterator first, TInputIterator last)
92  : Object()
93  , VectorType(first, last)
94  {}
97 public:
100 
102  itkNewMacro(Self);
103 
105  itkTypeMacro(VectorContainer, Object);
106 
108  class Iterator;
109  class ConstIterator;
110 
114  {
115  return *this;
116  }
117 
119  const STLContainerType &
120  CastToSTLConstContainer() const noexcept
121  {
122  return *this;
123  }
124 
125  using STLContainerType::begin;
126  using STLContainerType::end;
127  using STLContainerType::rbegin;
128  using STLContainerType::rend;
129  using STLContainerType::cbegin;
130  using STLContainerType::cend;
131  using STLContainerType::crbegin;
132  using STLContainerType::crend;
133 
134  using STLContainerType::size;
135  using STLContainerType::max_size;
136  using STLContainerType::resize;
137  using STLContainerType::capacity;
138  using STLContainerType::empty;
139  using STLContainerType::reserve;
140  using STLContainerType::shrink_to_fit;
141 
142  using STLContainerType::operator[];
143  using STLContainerType::at;
144  using STLContainerType::front;
145  using STLContainerType::back;
146 
147  using STLContainerType::assign;
148  using STLContainerType::push_back;
149  using STLContainerType::pop_back;
150  using STLContainerType::insert;
151  using STLContainerType::erase;
153  using STLContainerType::clear;
154 
155  using STLContainerType::get_allocator;
156 
157  using typename STLContainerType::reference;
158  using typename STLContainerType::const_reference;
159  using typename STLContainerType::iterator;
160  using typename STLContainerType::const_iterator;
161  // already declared before
162  // using STLContainerType::size_type;
163  using typename STLContainerType::difference_type;
164  using typename STLContainerType::value_type;
165  using typename STLContainerType::allocator_type;
166  using typename STLContainerType::pointer;
167  using typename STLContainerType::const_pointer;
168  using typename STLContainerType::reverse_iterator;
169  using typename STLContainerType::const_reverse_iterator;
170 
172  friend class Iterator;
173  friend class ConstIterator;
174 
180  class Iterator
181  {
182  public:
183  using iterator_category = typename VectorIterator::iterator_category;
184  using value_type = typename VectorIterator::value_type;
185  using difference_type = typename VectorIterator::difference_type;
186  using pointer = typename VectorIterator::pointer;
187  using reference = typename VectorIterator::reference;
188 
190  : m_Pos(0)
191  {}
193  : m_Pos(d)
194  , m_Iter(i)
195  {}
196  Iterator & operator*() { return *this; }
197  Iterator * operator->() { return this; }
198  Iterator &
200  {
201  ++m_Pos;
202  ++m_Iter;
203  return *this;
204  }
205  Iterator
207  {
208  Iterator temp(*this);
209  ++m_Pos;
210  ++m_Iter;
211  return temp;
212  }
213  Iterator &
215  {
216  --m_Pos;
217  --m_Iter;
218  return *this;
219  }
220  Iterator
222  {
223  Iterator temp(*this);
224  --m_Pos;
225  --m_Iter;
226  return temp;
227  }
228 
229  difference_type
230  operator-(const Iterator & r) const
231  {
232  return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
233  }
234 
235  bool
236  operator==(const Iterator & r) const
237  {
238  return m_Iter == r.m_Iter;
239  }
240 
241  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
242 
243  bool
244  operator==(const ConstIterator & r) const
245  {
246  return m_Iter == r.m_Iter;
247  }
248 
249  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
250 
251  bool
252  operator<(const Iterator & r) const
253  {
254  return (this->operator-(r)) < 0;
255  }
256  bool
257  operator>(const Iterator & r) const
258  {
259  return (r < *this);
260  }
261  bool
262  operator>=(const Iterator & r) const
263  {
264  return !(*this < r);
265  }
266  bool
267  operator<=(const Iterator & r) const
268  {
269  return !(*this < r);
270  }
271 
272  Iterator &
274  {
275  m_Pos += n;
276  m_Iter += n;
277  return *this;
278  };
279 
283  Index() const
284  {
285  return static_cast<ElementIdentifier>(m_Pos);
286  }
287 
289  reference
290  Value() const
291  {
292  return *m_Iter;
293  }
294 
295  private:
298  friend class ConstIterator;
299  };
300 
307  {
308  public:
309  using iterator_category = typename VectorConstIterator::iterator_category;
310  using value_type = typename VectorConstIterator::value_type;
311  using difference_type = typename VectorConstIterator::difference_type;
312  using pointer = typename VectorConstIterator::pointer;
313  using reference = typename VectorConstIterator::reference;
314 
316  : m_Pos(0)
317  {}
319  : m_Pos(d)
320  , m_Iter(i)
321  {}
323  : m_Pos(r.m_Pos)
324  , m_Iter(r.m_Iter)
325  {}
326  ConstIterator & operator*() { return *this; }
327  ConstIterator * operator->() { return this; }
328  ConstIterator &
330  {
331  ++m_Pos;
332  ++m_Iter;
333  return *this;
334  }
337  {
338  ConstIterator temp(*this);
339  ++m_Pos;
340  ++m_Iter;
341  return temp;
342  }
343  ConstIterator &
345  {
346  --m_Pos;
347  --m_Iter;
348  return *this;
349  }
352  {
353  ConstIterator temp(*this);
354  --m_Pos;
355  --m_Iter;
356  return temp;
357  }
358  ConstIterator &
359  operator=(const Iterator & r)
360  {
361  m_Pos = r.m_Pos;
362  m_Iter = r.m_Iter;
363  return *this;
364  }
365  ConstIterator &
367  {
368  m_Pos += n;
369  m_Iter += n;
370  return *this;
371  };
372 
373  difference_type
374  operator-(const ConstIterator & r) const
375  {
376  return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
377  }
378 
379  bool
380  operator==(const Iterator & r) const
381  {
382  return m_Iter == r.m_Iter;
383  }
384 
385  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
386 
387  bool
388  operator==(const ConstIterator & r) const
389  {
390  return m_Iter == r.m_Iter;
391  }
392 
393  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
394 
395  bool
396  operator<(const ConstIterator & r) const
397  {
398  return (this->operator-(r) < 0);
399  }
400  bool
401  operator>(const ConstIterator & r) const
402  {
403  return (r < *this);
404  }
405  bool
406  operator<=(const ConstIterator & r) const
407  {
408  return !(*this > r);
409  }
410  bool
411  operator>=(const ConstIterator & r) const
412  {
413  return !(*this < r);
414  }
415 
416 
420  Index() const
421  {
422  return static_cast<ElementIdentifier>(m_Pos);
423  }
424 
426  const_reference
427  Value() const
428  {
429  return *m_Iter;
430  }
431 
432  private:
435  friend class Iterator;
436  };
437 
438  /* Declare the public interface routines. */
439 
448  reference ElementAt(ElementIdentifier);
449 
456  const_reference ElementAt(ElementIdentifier) const;
457 
466  reference CreateElementAt(ElementIdentifier);
467 
472  Element GetElement(ElementIdentifier) const;
473 
478  void SetElement(ElementIdentifier, Element);
479 
485  void InsertElement(ElementIdentifier, Element);
486 
491  bool IndexExists(ElementIdentifier) const;
492 
498  bool
499  GetElementIfIndexExists(ElementIdentifier, Element *) const;
500 
506  void CreateIndex(ElementIdentifier);
507 
513  void DeleteIndex(ElementIdentifier);
514 
519  Begin() const;
520 
525  End() const;
526 
530  Iterator
531  Begin();
532 
536  Iterator
537  End();
538 
543  Size() const;
544 
554  void Reserve(ElementIdentifier);
555 
562  void
563  Squeeze();
564 
568  void
569  Initialize();
570 };
571 } // end namespace itk
572 
573 #ifndef ITK_MANUAL_INSTANTIATION
574 # include "itkVectorContainer.hxx"
575 #endif
576 
577 #endif
itk::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:336
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorConstIterator
typename VectorType::const_iterator VectorConstIterator
Definition: itkVectorContainer.h:68
itkObjectFactory.h
itk::VectorContainer::Iterator::reference
typename VectorIterator::reference reference
Definition: itkVectorContainer.h:187
itk::VectorContainer::VectorContainer
VectorContainer(size_type n)
Definition: itkVectorContainer.h:78
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:197
itk::VectorContainer< TElementIdentifier, TElementWrapper >::size_type
typename VectorType::size_type size_type
Definition: itkVectorContainer.h:66
itk::VectorContainer::VectorContainer
VectorContainer(TInputIterator first, TInputIterator last)
Definition: itkVectorContainer.h:91
itk::VectorContainer::ConstIterator::value_type
typename VectorConstIterator::value_type value_type
Definition: itkVectorContainer.h:310
itk::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:230
itk::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:313
itk::VectorContainer< TElementIdentifier, TElementWrapper >::STLContainerType
VectorType STLContainerType
Definition: itkVectorContainer.h:99
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::VectorContainer::Iterator::operator+=
Iterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:273
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::VectorContainer::Iterator::Iterator
Iterator()
Definition: itkVectorContainer.h:189
itk::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:344
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorIterator
typename VectorType::iterator VectorIterator
Definition: itkVectorContainer.h:67
itk::VectorContainer::Iterator::difference_type
typename VectorIterator::difference_type difference_type
Definition: itkVectorContainer.h:185
itk::VectorContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:420
itk::SmartPointer< Self >
itk::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:236
itk::VectorContainer< TElementIdentifier, TElementWrapper >::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:60
itk::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:366
itk::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:120
itk::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:297
itk::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:326
itk::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:427
itk::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:433
itk::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:257
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:309
itk::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:262
itk::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:244
itk::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:359
itk::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:196
itk::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:296
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:192
itk::VectorContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:380
itk::VectorContainer::Iterator::iterator_category
typename VectorIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:183
itk::VectorContainer::VectorContainer
VectorContainer()
Definition: itkVectorContainer.h:74
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(size_type d, const VectorConstIterator &i)
Definition: itkVectorContainer.h:318
itk::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:214
itk::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:283
itk::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:327
itk::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:434
itk::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:411
itk::VectorContainer::Iterator::value_type
typename VectorIterator::value_type value_type
Definition: itkVectorContainer.h:184
itkObject.h
itk::VectorContainer::VectorContainer
VectorContainer(const Self &r)
Definition: itkVectorContainer.h:86
itk::VectorContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:388
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:311
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator()
Definition: itkVectorContainer.h:315
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:65
itk::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:290
itk::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:374
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:401
itk::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:199
itk::VectorContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkVectorContainer.h:113
itk::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:329
itk::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:221
itk::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:306
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:322
itk::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:351
itk::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:206
itk::VectorContainer::Iterator::pointer
typename VectorIterator::pointer pointer
Definition: itkVectorContainer.h:186
itk::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:312
itk::VectorContainer::Iterator
Definition: itkVectorContainer.h:180
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:82