ITK  5.1.0
Insight Toolkit
itkVectorContainer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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(const Iterator & r)
197  : m_Pos(r.m_Pos)
198  , m_Iter(r.m_Iter)
199  {}
200  Iterator & operator*() { return *this; }
201  Iterator * operator->() { return this; }
202  Iterator &
204  {
205  ++m_Pos;
206  ++m_Iter;
207  return *this;
208  }
209  Iterator
211  {
212  Iterator temp(*this);
213  ++m_Pos;
214  ++m_Iter;
215  return temp;
216  }
217  Iterator &
219  {
220  --m_Pos;
221  --m_Iter;
222  return *this;
223  }
224  Iterator
226  {
227  Iterator temp(*this);
228  --m_Pos;
229  --m_Iter;
230  return temp;
231  }
232 
233  difference_type
234  operator-(const Iterator & r) const
235  {
236  return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
237  }
238 
239  bool
240  operator==(const Iterator & r) const
241  {
242  return m_Iter == r.m_Iter;
243  }
244  bool
245  operator!=(const Iterator & r) const
246  {
247  return m_Iter != r.m_Iter;
248  }
249  bool
250  operator==(const ConstIterator & r) const
251  {
252  return m_Iter == r.m_Iter;
253  }
254  bool
255  operator!=(const ConstIterator & r) const
256  {
257  return m_Iter != r.m_Iter;
258  }
259  bool
260  operator<(const Iterator & r) const
261  {
262  return (this->operator-(r)) < 0;
263  }
264  bool
265  operator>(const Iterator & r) const
266  {
267  return (r < *this);
268  }
269  bool
270  operator>=(const Iterator & r) const
271  {
272  return !(*this < r);
273  }
274  bool
275  operator<=(const Iterator & r) const
276  {
277  return !(*this < r);
278  }
279 
280  Iterator &
282  {
283  m_Pos += n;
284  m_Iter += n;
285  return *this;
286  };
287 
291  Index() const
292  {
293  return static_cast<ElementIdentifier>(m_Pos);
294  }
295 
297  reference
298  Value() const
299  {
300  return *m_Iter;
301  }
302 
303  private:
306  friend class ConstIterator;
307  };
308 
315  {
316  public:
317  using iterator_category = typename VectorConstIterator::iterator_category;
318  using value_type = typename VectorConstIterator::value_type;
319  using difference_type = typename VectorConstIterator::difference_type;
320  using pointer = typename VectorConstIterator::pointer;
321  using reference = typename VectorConstIterator::reference;
322 
324  : m_Pos(0)
325  {}
327  : m_Pos(d)
328  , m_Iter(i)
329  {}
331  : m_Pos(r.m_Pos)
332  , m_Iter(r.m_Iter)
333  {}
334  ConstIterator & operator*() { return *this; }
335  ConstIterator * operator->() { return this; }
336  ConstIterator &
338  {
339  ++m_Pos;
340  ++m_Iter;
341  return *this;
342  }
345  {
346  ConstIterator temp(*this);
347  ++m_Pos;
348  ++m_Iter;
349  return temp;
350  }
351  ConstIterator &
353  {
354  --m_Pos;
355  --m_Iter;
356  return *this;
357  }
360  {
361  ConstIterator temp(*this);
362  --m_Pos;
363  --m_Iter;
364  return temp;
365  }
366  ConstIterator &
367  operator=(const Iterator & r)
368  {
369  m_Pos = r.m_Pos;
370  m_Iter = r.m_Iter;
371  return *this;
372  }
373  ConstIterator &
375  {
376  m_Pos += n;
377  m_Iter += n;
378  return *this;
379  };
380 
381  difference_type
382  operator-(const ConstIterator & r) const
383  {
384  return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
385  }
386 
387  bool
388  operator==(const Iterator & r) const
389  {
390  return m_Iter == r.m_Iter;
391  }
392  bool
393  operator!=(const Iterator & r) const
394  {
395  return m_Iter != r.m_Iter;
396  }
397  bool
398  operator==(const ConstIterator & r) const
399  {
400  return m_Iter == r.m_Iter;
401  }
402  bool
403  operator!=(const ConstIterator & r) const
404  {
405  return m_Iter != r.m_Iter;
406  }
407  bool
408  operator<(const ConstIterator & r) const
409  {
410  return (this->operator-(r) < 0);
411  }
412  bool
413  operator>(const ConstIterator & r) const
414  {
415  return (r < *this);
416  }
417  bool
418  operator<=(const ConstIterator & r) const
419  {
420  return !(*this > r);
421  }
422  bool
423  operator>=(const ConstIterator & r) const
424  {
425  return !(*this < r);
426  }
427 
428 
432  Index() const
433  {
434  return static_cast<ElementIdentifier>(m_Pos);
435  }
436 
438  const_reference
439  Value() const
440  {
441  return *m_Iter;
442  }
443 
444  private:
447  friend class Iterator;
448  };
449 
450  /* Declare the public interface routines. */
451 
460  reference ElementAt(ElementIdentifier);
461 
468  const_reference ElementAt(ElementIdentifier) const;
469 
478  reference CreateElementAt(ElementIdentifier);
479 
484  Element GetElement(ElementIdentifier) const;
485 
490  void SetElement(ElementIdentifier, Element);
491 
497  void InsertElement(ElementIdentifier, Element);
498 
503  bool IndexExists(ElementIdentifier) const;
504 
510  bool
511  GetElementIfIndexExists(ElementIdentifier, Element *) const;
512 
518  void CreateIndex(ElementIdentifier);
519 
525  void DeleteIndex(ElementIdentifier);
526 
531  Begin() const;
532 
537  End() const;
538 
542  Iterator
543  Begin();
544 
548  Iterator
549  End();
550 
555  Size() const;
556 
566  void Reserve(ElementIdentifier);
567 
574  void
575  Squeeze();
576 
580  void
581  Initialize();
582 };
583 } // end namespace itk
584 
585 #ifndef ITK_MANUAL_INSTANTIATION
586 # include "itkVectorContainer.hxx"
587 #endif
588 
589 #endif
itk::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:344
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:201
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:318
itk::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:234
itk::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:321
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:281
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:237
itk::VectorContainer::Iterator::Iterator
Iterator()
Definition: itkVectorContainer.h:189
itk::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:352
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:432
itk::SmartPointer< Self >
itk::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:240
itk::VectorContainer< TElementIdentifier, TElementWrapper >::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:60
itk::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:374
itk::VectorContainer::ConstIterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkVectorContainer.h:403
itk::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:120
itk::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:305
itk::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:334
itk::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:439
itk::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:445
itk::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:265
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:317
itk::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:270
itk::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:250
itk::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:367
itk::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:200
itk::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:304
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:388
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:326
itk::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:218
itk::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:291
itk::VectorContainer::ConstIterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkVectorContainer.h:393
itk::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:335
itk::VectorContainer::Iterator::Iterator
Iterator(const Iterator &r)
Definition: itkVectorContainer.h:196
itk::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:446
itk::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:423
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:398
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::VectorContainer::ConstIterator::difference_type
typename VectorConstIterator::difference_type difference_type
Definition: itkVectorContainer.h:319
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator()
Definition: itkVectorContainer.h:323
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:65
itk::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:298
itk::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:382
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:60
itk::VectorContainer::ConstIterator::operator>
bool operator>(const ConstIterator &r) const
Definition: itkVectorContainer.h:413
itk::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:203
itk::VectorContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkVectorContainer.h:113
itk::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:337
itk::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:225
itk::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:314
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:330
itk::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:359
itk::VectorContainer::Iterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkVectorContainer.h:255
itk::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:210
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:245
itk::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:320
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