ITK  5.0.0
Insight Segmentation and Registration 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<
48  typename TElementIdentifier,
49  typename TElement
50  >
51 class ITK_TEMPLATE_EXPORT VectorContainer:
52  public Object,
53  private std::vector< TElement >
54 {
55 public:
58  using Superclass = Object;
61 
63  using ElementIdentifier = TElementIdentifier;
64  using Element = TElement;
65 
66 private:
68  using VectorType = std::vector< Element >;
69  using size_type = typename VectorType::size_type;
70  using VectorIterator = typename VectorType::iterator;
71  using VectorConstIterator = typename VectorType::const_iterator;
72 
73 protected:
78  Object(), VectorType() {}
80  Object(), VectorType(n) {}
82  Object(), VectorType(n, x) {}
83  VectorContainer(const Self & r):
84  Object(), VectorType(r.CastToSTLConstContainer()) {}
85  template< typename TInputIterator >
86  VectorContainer(TInputIterator first, TInputIterator last):
87  Object(), VectorType(first, last) {}
89 
90 public:
91 
94 
96  itkNewMacro(Self);
97 
99  itkTypeMacro(VectorContainer, Object);
100 
102  class Iterator;
103  class ConstIterator;
104 
107  {
108  return *this;
109  }
110 
112  const STLContainerType & CastToSTLConstContainer() const ITK_NOEXCEPT
113  {
114  return *this;
115  }
116 
117  using STLContainerType::begin;
118  using STLContainerType::end;
119  using STLContainerType::rbegin;
120  using STLContainerType::rend;
121  using STLContainerType::cbegin;
122  using STLContainerType::cend;
123  using STLContainerType::crbegin;
124  using STLContainerType::crend;
125 
126  using STLContainerType::size;
127  using STLContainerType::max_size;
128  using STLContainerType::resize;
129  using STLContainerType::capacity;
130  using STLContainerType::empty;
131  using STLContainerType::reserve;
132  using STLContainerType::shrink_to_fit;
133 
134  using STLContainerType::operator[];
135  using STLContainerType::at;
136  using STLContainerType::front;
137  using STLContainerType::back;
138 
139  using STLContainerType::assign;
140  using STLContainerType::push_back;
141  using STLContainerType::pop_back;
142  using STLContainerType::insert;
143  using STLContainerType::erase;
145  using STLContainerType::clear;
146 
147  using STLContainerType::get_allocator;
148 
149  using typename STLContainerType::reference;
150  using typename STLContainerType::const_reference;
151  using typename STLContainerType::iterator;
152  using typename STLContainerType::const_iterator;
153  // already declared before
154  // using STLContainerType::size_type;
155  using typename STLContainerType::difference_type;
156  using typename STLContainerType::value_type;
157  using typename STLContainerType::allocator_type;
158  using typename STLContainerType::pointer;
159  using typename STLContainerType::const_pointer;
160  using typename STLContainerType::reverse_iterator;
161  using typename STLContainerType::const_reverse_iterator;
162 
164  friend class Iterator;
165  friend class ConstIterator;
166 
172  class Iterator
173  {
174 public:
175  using iterator_category = typename VectorIterator::iterator_category;
176  using value_type = typename VectorIterator::value_type;
177  using difference_type = typename VectorIterator::difference_type;
178  using pointer = typename VectorIterator::pointer;
179  using reference = typename VectorIterator::reference;
180 
181  Iterator() : m_Pos(0) {}
182  Iterator(size_type d, const VectorIterator & i):m_Pos(d), m_Iter(i) {}
183  Iterator(const Iterator & r): m_Pos(r.m_Pos), m_Iter(r.m_Iter) {}
184  Iterator & operator*() { return *this; }
185  Iterator * operator->() { return this; }
186  Iterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
187  Iterator operator++(int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
188  Iterator & operator--() { --m_Pos; --m_Iter; return *this; }
189  Iterator operator--(int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
190 
191  difference_type operator-(const Iterator & r) const { return static_cast< difference_type >( this->m_Pos ) - static_cast< difference_type >( r.m_Pos ); }
192 
193  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
194  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
195  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
196  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
197  bool operator<(const Iterator & r) const { return ( this->operator-( r ) ) < 0; }
198  bool operator>(const Iterator & r) const { return ( r < *this ); }
199  bool operator>=(const Iterator & r) const { return !( *this < r ); }
200  bool operator<=(const Iterator & r) const { return !( *this < r ); }
201 
202  Iterator & operator+=(difference_type n) { m_Pos += n; m_Iter += n; return *this; };
203 
206  ElementIdentifier Index() const { return static_cast< ElementIdentifier >( m_Pos ); }
207 
209  reference Value() const { return *m_Iter; }
210 
211 private:
214  friend class ConstIterator;
215  };
216 
223  {
224 public:
225  using iterator_category = typename VectorConstIterator::iterator_category;
226  using value_type = typename VectorConstIterator::value_type;
227  using difference_type = typename VectorConstIterator::difference_type;
228  using pointer = typename VectorConstIterator::pointer;
229  using reference = typename VectorConstIterator::reference;
230 
231  ConstIterator():m_Pos(0) {}
232  ConstIterator(size_type d, const VectorConstIterator & i):m_Pos(d), m_Iter(i) {}
233  ConstIterator(const Iterator & r) : m_Pos( r.m_Pos ), m_Iter( r.m_Iter ) { }
234  ConstIterator & operator*() { return *this; }
235  ConstIterator * operator->() { return this; }
236  ConstIterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
237  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
238  ConstIterator & operator--() { --m_Pos; --m_Iter; return *this; }
239  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
240  ConstIterator & operator=(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
241  ConstIterator & operator+=(difference_type n) { m_Pos += n; m_Iter += n; return *this; };
242 
243  difference_type operator-(const ConstIterator & r) const { return static_cast< difference_type >( m_Pos ) - static_cast< difference_type >( r.m_Pos ); }
244 
245  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
246  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
247  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
248  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
249  bool operator<(const ConstIterator & r) const { return ( this->operator-(r) < 0 ); }
250  bool operator>(const ConstIterator & r) const { return ( r < *this ); }
251  bool operator<=(const ConstIterator & r) const { return !( *this > r ); }
252  bool operator>=(const ConstIterator & r) const { return !( *this < r ); }
253 
254 
257  ElementIdentifier Index() const { return static_cast< ElementIdentifier >( m_Pos ); }
258 
260  const_reference Value() const { return *m_Iter; }
261 
262 private:
265  friend class Iterator;
266  };
267 
268  /* Declare the public interface routines. */
269 
278  reference ElementAt(ElementIdentifier);
279 
286  const_reference ElementAt(ElementIdentifier) const;
287 
296  reference CreateElementAt(ElementIdentifier);
297 
302  Element GetElement(ElementIdentifier) const;
303 
308  void SetElement(ElementIdentifier, Element);
309 
315  void InsertElement(ElementIdentifier, Element);
316 
321  bool IndexExists(ElementIdentifier) const;
322 
328  bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
329 
335  void CreateIndex(ElementIdentifier);
336 
342  void DeleteIndex(ElementIdentifier);
343 
347  ConstIterator Begin() const;
348 
352  ConstIterator End() const;
353 
357  Iterator Begin();
358 
362  Iterator End();
363 
367  ElementIdentifier Size() const;
368 
378  void Reserve(ElementIdentifier);
379 
386  void Squeeze();
387 
391  void Initialize();
392 };
393 } // end namespace itk
394 
395 #ifndef ITK_MANUAL_INSTANTIATION
396 #include "itkVectorContainer.hxx"
397 #endif
398 
399 #endif
bool operator!=(const Iterator &r) const
typename VectorIterator::difference_type difference_type
Light weight base class for most itk classes.
ConstIterator & operator=(const Iterator &r)
typename VectorConstIterator::pointer pointer
bool operator>(const Iterator &r) const
bool operator!=(const Iterator &r) const
ConstIterator(size_type d, const VectorConstIterator &i)
Iterator(size_type d, const VectorIterator &i)
typename VectorConstIterator::iterator_category iterator_category
bool operator==(const ConstIterator &r) const
difference_type operator-(const ConstIterator &r) const
typename VectorConstIterator::value_type value_type
bool operator==(const Iterator &r) const
Iterator & operator+=(difference_type n)
ConstIterator & operator+=(difference_type n)
ElementIdentifier Index() const
bool operator>=(const Iterator &r) const
bool operator<(const ConstIterator &r) const
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
typename VectorConstIterator::difference_type difference_type
bool operator<(const Iterator &r) const
typename VectorIterator::reference reference
bool operator!=(const ConstIterator &r) const
difference_type operator-(const Iterator &r) const
bool operator>=(const ConstIterator &r) const
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
bool operator==(const ConstIterator &r) const
typename VectorConstIterator::reference reference
bool operator!=(const ConstIterator &r) const
VectorContainer(size_type n)
Define a front-end to the STL &quot;vector&quot; container that conforms to the IndexedContainerInterface.
typename VectorIterator::pointer pointer
bool operator<=(const ConstIterator &r) const
VectorContainer(const Self &r)
bool operator>(const ConstIterator &r) const
typename VectorIterator::iterator_category iterator_category
ElementIdentifier Index() const
Base class for most ITK classes.
Definition: itkObject.h:60
const STLContainerType & CastToSTLConstContainer() const noexcept
VectorContainer(TInputIterator first, TInputIterator last)
bool operator==(const Iterator &r) const
typename VectorIterator::value_type value_type
bool operator<=(const Iterator &r) const
VectorContainer(size_type n, const Element &x)
STLContainerType & CastToSTLContainer() noexcept