ITK  5.2.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  * http://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  {}
96 
97 public:
100 
102  itkNewMacro(Self);
103 
105  itkTypeMacro(VectorContainer, Object);
106 
108  class Iterator;
109  class ConstIterator;
110 
113  CastToSTLContainer() ITK_NOEXCEPT
114  {
115  return *this;
116  }
117 
119  const STLContainerType &
120  CastToSTLConstContainer() const ITK_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  bool
241  operator!=(const Iterator & r) const
242  {
243  return m_Iter != r.m_Iter;
244  }
245  bool
246  operator==(const ConstIterator & r) const
247  {
248  return m_Iter == r.m_Iter;
249  }
250  bool
251  operator!=(const ConstIterator & r) const
252  {
253  return m_Iter != r.m_Iter;
254  }
255  bool
256  operator<(const Iterator & r) const
257  {
258  return (this->operator-(r)) < 0;
259  }
260  bool
261  operator>(const Iterator & r) const
262  {
263  return (r < *this);
264  }
265  bool
266  operator>=(const Iterator & r) const
267  {
268  return !(*this < r);
269  }
270  bool
271  operator<=(const Iterator & r) const
272  {
273  return !(*this < r);
274  }
275 
276  Iterator &
278  {
279  m_Pos += n;
280  m_Iter += n;
281  return *this;
282  };
283 
287  Index() const
288  {
289  return static_cast<ElementIdentifier>(m_Pos);
290  }
291 
293  reference
294  Value() const
295  {
296  return *m_Iter;
297  }
298 
299  private:
302  friend class ConstIterator;
303  };
304 
311  {
312  public:
313  using iterator_category = typename VectorConstIterator::iterator_category;
314  using value_type = typename VectorConstIterator::value_type;
315  using difference_type = typename VectorConstIterator::difference_type;
316  using pointer = typename VectorConstIterator::pointer;
317  using reference = typename VectorConstIterator::reference;
318 
320  : m_Pos(0)
321  {}
323  : m_Pos(d)
324  , m_Iter(i)
325  {}
327  : m_Pos(r.m_Pos)
328  , m_Iter(r.m_Iter)
329  {}
330  ConstIterator & operator*() { return *this; }
331  ConstIterator * operator->() { return this; }
332  ConstIterator &
334  {
335  ++m_Pos;
336  ++m_Iter;
337  return *this;
338  }
341  {
342  ConstIterator temp(*this);
343  ++m_Pos;
344  ++m_Iter;
345  return temp;
346  }
347  ConstIterator &
349  {
350  --m_Pos;
351  --m_Iter;
352  return *this;
353  }
356  {
357  ConstIterator temp(*this);
358  --m_Pos;
359  --m_Iter;
360  return temp;
361  }
362  ConstIterator &
363  operator=(const Iterator & r)
364  {
365  m_Pos = r.m_Pos;
366  m_Iter = r.m_Iter;
367  return *this;
368  }
369  ConstIterator &
371  {
372  m_Pos += n;
373  m_Iter += n;
374  return *this;
375  };
376 
377  difference_type
378  operator-(const ConstIterator & r) const
379  {
380  return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
381  }
382 
383  bool
384  operator==(const Iterator & r) const
385  {
386  return m_Iter == r.m_Iter;
387  }
388  bool
389  operator!=(const Iterator & r) const
390  {
391  return m_Iter != r.m_Iter;
392  }
393  bool
394  operator==(const ConstIterator & r) const
395  {
396  return m_Iter == r.m_Iter;
397  }
398  bool
399  operator!=(const ConstIterator & r) const
400  {
401  return m_Iter != r.m_Iter;
402  }
403  bool
404  operator<(const ConstIterator & r) const
405  {
406  return (this->operator-(r) < 0);
407  }
408  bool
409  operator>(const ConstIterator & r) const
410  {
411  return (r < *this);
412  }
413  bool
414  operator<=(const ConstIterator & r) const
415  {
416  return !(*this > r);
417  }
418  bool
419  operator>=(const ConstIterator & r) const
420  {
421  return !(*this < r);
422  }
423 
424 
428  Index() const
429  {
430  return static_cast<ElementIdentifier>(m_Pos);
431  }
432 
434  const_reference
435  Value() const
436  {
437  return *m_Iter;
438  }
439 
440  private:
443  friend class Iterator;
444  };
445 
446  /* Declare the public interface routines. */
447 
456  reference ElementAt(ElementIdentifier);
457 
464  const_reference ElementAt(ElementIdentifier) const;
465 
474  reference CreateElementAt(ElementIdentifier);
475 
480  Element GetElement(ElementIdentifier) const;
481 
486  void SetElement(ElementIdentifier, Element);
487 
493  void InsertElement(ElementIdentifier, Element);
494 
499  bool IndexExists(ElementIdentifier) const;
500 
506  bool
507  GetElementIfIndexExists(ElementIdentifier, Element *) const;
508 
514  void CreateIndex(ElementIdentifier);
515 
521  void DeleteIndex(ElementIdentifier);
522 
527  Begin() const;
528 
533  End() const;
534 
538  Iterator
539  Begin();
540 
544  Iterator
545  End();
546 
551  Size() const;
552 
562  void Reserve(ElementIdentifier);
563 
570  void
571  Squeeze();
572 
576  void
577  Initialize();
578 };
579 } // end namespace itk
580 
581 #ifndef ITK_MANUAL_INSTANTIATION
582 # include "itkVectorContainer.hxx"
583 #endif
584 
585 #endif
itk::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:340
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:546
itk::operator<=
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:560
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
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:314
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:317
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:277
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
itk::VectorContainer::Iterator::Iterator
Iterator()
Definition: itkVectorContainer.h:189
itk::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:348
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:428
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:370
itk::VectorContainer::ConstIterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkVectorContainer.h:399
itk::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:120
itk::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:301
itk::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:330
itk::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:435
itk::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:441
itk::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:261
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::VectorContainer::ConstIterator::iterator_category
typename VectorConstIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:313
itk::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:266
itk::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:246
itk::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:363
itk::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:196
itk::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:300
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:384
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:322
itk::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:214
itk::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:287
itk::VectorContainer::ConstIterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkVectorContainer.h:389
itk::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:331
itk::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:442
itk::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:419
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:394
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:315
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator()
Definition: itkVectorContainer.h:319
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:65
itk::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:294
itk::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:378
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::VectorContainer::ConstIterator::operator>
bool operator>(const ConstIterator &r) const
Definition: itkVectorContainer.h:409
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:333
itk::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:221
itk::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:310
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:326
itk::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:355
itk::VectorContainer::Iterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkVectorContainer.h:251
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::Iterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkVectorContainer.h:241
itk::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:316
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