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 size_type = typename VectorType::size_type;
67  using VectorIterator = typename VectorType::iterator;
68  using VectorConstIterator = typename VectorType::const_iterator;
69 
70 protected:
74  VectorContainer() = default;
76  : Object()
77  , VectorType(n)
78  {}
80  : Object()
81  , VectorType(n, x)
82  {}
83  VectorContainer(const Self & r)
84  : Object()
85  , VectorType(r.CastToSTLConstContainer())
86  {}
87  template <typename TInputIterator>
88  VectorContainer(TInputIterator first, TInputIterator last)
89  : Object()
90  , VectorType(first, last)
91  {}
94 public:
97 
99  itkNewMacro(Self);
100 
102  itkTypeMacro(VectorContainer, Object);
103 
105  class Iterator;
106  class ConstIterator;
107 
111  {
112  return *this;
113  }
114 
116  const STLContainerType &
117  CastToSTLConstContainer() const noexcept
118  {
119  return *this;
120  }
121 
122  using STLContainerType::begin;
123  using STLContainerType::end;
124  using STLContainerType::rbegin;
125  using STLContainerType::rend;
126  using STLContainerType::cbegin;
127  using STLContainerType::cend;
128  using STLContainerType::crbegin;
129  using STLContainerType::crend;
130 
131  using STLContainerType::size;
132  using STLContainerType::max_size;
133  using STLContainerType::resize;
134  using STLContainerType::capacity;
135  using STLContainerType::empty;
136  using STLContainerType::reserve;
137  using STLContainerType::shrink_to_fit;
138 
139  using STLContainerType::operator[];
140  using STLContainerType::at;
141  using STLContainerType::front;
142  using STLContainerType::back;
143 
144  using STLContainerType::assign;
145  using STLContainerType::push_back;
146  using STLContainerType::pop_back;
147  using STLContainerType::insert;
148  using STLContainerType::erase;
150  using STLContainerType::clear;
151 
152  using STLContainerType::get_allocator;
153 
154  using typename STLContainerType::reference;
155  using typename STLContainerType::const_reference;
156  using typename STLContainerType::iterator;
157  using typename STLContainerType::const_iterator;
158  // already declared before
159  // using STLContainerType::size_type;
160  using typename STLContainerType::difference_type;
161  using typename STLContainerType::value_type;
162  using typename STLContainerType::allocator_type;
163  using typename STLContainerType::pointer;
164  using typename STLContainerType::const_pointer;
165  using typename STLContainerType::reverse_iterator;
166  using typename STLContainerType::const_reverse_iterator;
167 
169  friend class Iterator;
170  friend class ConstIterator;
171 
177  class Iterator
178  {
179  public:
180  using iterator_category = typename VectorIterator::iterator_category;
181  using value_type = typename VectorIterator::value_type;
182  using difference_type = typename VectorIterator::difference_type;
183  using pointer = typename VectorIterator::pointer;
184  using reference = typename VectorIterator::reference;
185 
186  Iterator() = default;
188  : m_Pos(d)
189  , m_Iter(i)
190  {}
191  Iterator & operator*() { return *this; }
192  Iterator * operator->() { return this; }
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  Iterator &
210  {
211  --m_Pos;
212  --m_Iter;
213  return *this;
214  }
215  Iterator
217  {
218  Iterator temp(*this);
219  --m_Pos;
220  --m_Iter;
221  return temp;
222  }
223 
224  difference_type
225  operator-(const Iterator & r) const
226  {
227  return static_cast<difference_type>(this->m_Pos) - static_cast<difference_type>(r.m_Pos);
228  }
229 
230  bool
231  operator==(const Iterator & r) const
232  {
233  return m_Iter == r.m_Iter;
234  }
235 
236  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
237 
238  bool
239  operator==(const ConstIterator & r) const
240  {
241  return m_Iter == r.m_Iter;
242  }
243 
244  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
245 
246  bool
247  operator<(const Iterator & r) const
248  {
249  return (this->operator-(r)) < 0;
250  }
251  bool
252  operator>(const Iterator & r) const
253  {
254  return (r < *this);
255  }
256  bool
257  operator>=(const Iterator & r) const
258  {
259  return !(*this < r);
260  }
261  bool
262  operator<=(const Iterator & r) const
263  {
264  return !(*this < r);
265  }
266 
267  Iterator &
269  {
270  m_Pos += n;
271  m_Iter += n;
272  return *this;
273  };
274 
278  Index() const
279  {
280  return static_cast<ElementIdentifier>(m_Pos);
281  }
282 
284  reference
285  Value() const
286  {
287  return *m_Iter;
288  }
289 
290  private:
291  size_type m_Pos{};
292  VectorIterator m_Iter{};
293  friend class ConstIterator;
294  };
295 
302  {
303  public:
304  using iterator_category = typename VectorConstIterator::iterator_category;
305  using value_type = typename VectorConstIterator::value_type;
306  using difference_type = typename VectorConstIterator::difference_type;
307  using pointer = typename VectorConstIterator::pointer;
308  using reference = typename VectorConstIterator::reference;
309 
310  ConstIterator() = default;
312  : m_Pos(d)
313  , m_Iter(i)
314  {}
316  : m_Pos(r.m_Pos)
317  , m_Iter(r.m_Iter)
318  {}
319  ConstIterator & operator*() { return *this; }
320  ConstIterator * operator->() { return this; }
321  ConstIterator &
323  {
324  ++m_Pos;
325  ++m_Iter;
326  return *this;
327  }
330  {
331  ConstIterator temp(*this);
332  ++m_Pos;
333  ++m_Iter;
334  return temp;
335  }
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 &
352  operator=(const Iterator & r)
353  {
354  m_Pos = r.m_Pos;
355  m_Iter = r.m_Iter;
356  return *this;
357  }
358  ConstIterator &
360  {
361  m_Pos += n;
362  m_Iter += n;
363  return *this;
364  };
365 
366  difference_type
367  operator-(const ConstIterator & r) const
368  {
369  return static_cast<difference_type>(m_Pos) - static_cast<difference_type>(r.m_Pos);
370  }
371 
372  bool
373  operator==(const Iterator & r) const
374  {
375  return m_Iter == r.m_Iter;
376  }
377 
378  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
379 
380  bool
381  operator==(const ConstIterator & r) const
382  {
383  return m_Iter == r.m_Iter;
384  }
385 
386  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
387 
388  bool
389  operator<(const ConstIterator & r) const
390  {
391  return (this->operator-(r) < 0);
392  }
393  bool
394  operator>(const ConstIterator & r) const
395  {
396  return (r < *this);
397  }
398  bool
399  operator<=(const ConstIterator & r) const
400  {
401  return !(*this > r);
402  }
403  bool
404  operator>=(const ConstIterator & r) const
405  {
406  return !(*this < r);
407  }
408 
409 
413  Index() const
414  {
415  return static_cast<ElementIdentifier>(m_Pos);
416  }
417 
419  const_reference
420  Value() const
421  {
422  return *m_Iter;
423  }
424 
425  private:
426  size_type m_Pos{};
428  friend class Iterator;
429  };
430 
431  /* Declare the public interface routines. */
432 
441  reference ElementAt(ElementIdentifier);
442 
449  const_reference ElementAt(ElementIdentifier) const;
450 
459  reference CreateElementAt(ElementIdentifier);
460 
465  Element GetElement(ElementIdentifier) const;
466 
471  void SetElement(ElementIdentifier, Element);
472 
478  void InsertElement(ElementIdentifier, Element);
479 
484  bool IndexExists(ElementIdentifier) const;
485 
491  bool
492  GetElementIfIndexExists(ElementIdentifier, Element *) const;
493 
499  void CreateIndex(ElementIdentifier);
500 
506  void DeleteIndex(ElementIdentifier);
507 
512  Begin() const;
513 
518  End() const;
519 
523  Iterator
524  Begin();
525 
529  Iterator
530  End();
531 
536  Size() const;
537 
549  void Reserve(ElementIdentifier);
550 
559  void
560  Squeeze();
561 
565  void
566  Initialize();
567 };
568 } // end namespace itk
569 
570 #ifndef ITK_MANUAL_INSTANTIATION
571 # include "itkVectorContainer.hxx"
572 #endif
573 
574 #endif
itk::VectorContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkVectorContainer.h:329
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:184
itk::VectorContainer::VectorContainer
VectorContainer(size_type n)
Definition: itkVectorContainer.h:75
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:192
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:88
itk::VectorContainer::ConstIterator::value_type
typename VectorConstIterator::value_type value_type
Definition: itkVectorContainer.h:305
itk::VectorContainer::Iterator::operator-
difference_type operator-(const Iterator &r) const
Definition: itkVectorContainer.h:225
itk::VectorContainer::ConstIterator::reference
typename VectorConstIterator::reference reference
Definition: itkVectorContainer.h:308
itk::VectorContainer< TElementIdentifier, TElementWrapper >::STLContainerType
VectorType STLContainerType
Definition: itkVectorContainer.h:96
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::VectorContainer::Iterator::operator+=
Iterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:268
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::VectorContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkVectorContainer.h:337
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:182
itk::VectorContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:413
itk::SmartPointer< Self >
itk::VectorContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:231
itk::VectorContainer< TElementIdentifier, TElementWrapper >::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkVectorContainer.h:60
itk::VectorContainer::ConstIterator::operator+=
ConstIterator & operator+=(difference_type n)
Definition: itkVectorContainer.h:359
itk::VectorContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkVectorContainer.h:117
itk::VectorContainer::Iterator::m_Iter
VectorIterator m_Iter
Definition: itkVectorContainer.h:292
itk::VectorContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkVectorContainer.h:319
itk::VectorContainer::ConstIterator::Value
const_reference Value() const
Definition: itkVectorContainer.h:420
itk::VectorContainer::ConstIterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:426
itk::VectorContainer::Iterator::operator>
bool operator>(const Iterator &r) const
Definition: itkVectorContainer.h:252
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:304
itk::VectorContainer::Iterator::operator>=
bool operator>=(const Iterator &r) const
Definition: itkVectorContainer.h:257
itk::VectorContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:239
itk::VectorContainer::ConstIterator::operator=
ConstIterator & operator=(const Iterator &r)
Definition: itkVectorContainer.h:352
itk::VectorContainer::Iterator::operator*
Iterator & operator*()
Definition: itkVectorContainer.h:191
itk::VectorContainer::Iterator::m_Pos
size_type m_Pos
Definition: itkVectorContainer.h:291
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:187
itk::VectorContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkVectorContainer.h:373
itk::VectorContainer::Iterator::iterator_category
typename VectorIterator::iterator_category iterator_category
Definition: itkVectorContainer.h:180
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(size_type d, const VectorConstIterator &i)
Definition: itkVectorContainer.h:311
itk::VectorContainer::Iterator::operator--
Iterator & operator--()
Definition: itkVectorContainer.h:209
itk::VectorContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkVectorContainer.h:278
itk::VectorContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkVectorContainer.h:320
itk::VectorContainer::ConstIterator::m_Iter
VectorConstIterator m_Iter
Definition: itkVectorContainer.h:427
itk::VectorContainer::ConstIterator::operator>=
bool operator>=(const ConstIterator &r) const
Definition: itkVectorContainer.h:404
itk::VectorContainer::Iterator::value_type
typename VectorIterator::value_type value_type
Definition: itkVectorContainer.h:181
itkObject.h
itk::VectorContainer::VectorContainer
VectorContainer(const Self &r)
Definition: itkVectorContainer.h:83
itk::VectorContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkVectorContainer.h:381
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:306
itk::VectorContainer< TElementIdentifier, TElementWrapper >::VectorType
std::vector< Element > VectorType
Definition: itkVectorContainer.h:65
itk::VectorContainer::Iterator::Value
reference Value() const
Definition: itkVectorContainer.h:285
itk::VectorContainer::ConstIterator::operator-
difference_type operator-(const ConstIterator &r) const
Definition: itkVectorContainer.h:367
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:394
itk::VectorContainer::Iterator::operator++
Iterator & operator++()
Definition: itkVectorContainer.h:194
itk::VectorContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkVectorContainer.h:110
itk::VectorContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkVectorContainer.h:322
itk::VectorContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkVectorContainer.h:216
itk::VectorContainer::ConstIterator
Definition: itkVectorContainer.h:301
itk::VectorContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkVectorContainer.h:315
itk::VectorContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkVectorContainer.h:344
itk::VectorContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkVectorContainer.h:201
itk::VectorContainer::Iterator::pointer
typename VectorIterator::pointer pointer
Definition: itkVectorContainer.h:183
itk::VectorContainer::ConstIterator::pointer
typename VectorConstIterator::pointer pointer
Definition: itkVectorContainer.h:307
itk::VectorContainer::Iterator
Definition: itkVectorContainer.h:177
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:79