ITK  4.9.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  >
52  public Object,
53  private std::vector< TElement >
54 {
55 public:
58  typedef Object Superclass;
61 
63  typedef TElementIdentifier ElementIdentifier;
64  typedef TElement Element;
65 
66 private:
68  typedef std::vector< Element > VectorType;
69  typedef typename VectorType::size_type size_type;
70  typedef typename VectorType::iterator VectorIterator;
71  typedef typename VectorType::const_iterator VectorConstIterator;
72 
73 protected:
78  Object(), VectorType() {}
80  Object(), VectorType(n) {}
82  Object(), VectorType(n, x) {}
83  VectorContainer(const Self & r):
84  Object(), VectorType(r) {}
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 dynamic_cast< STLContainerType & >( *this );
109  }
110 
113  {
114  return dynamic_cast< const STLContainerType & >( *this );
115  }
116 
117  using STLContainerType::begin;
118  using STLContainerType::end;
119  using STLContainerType::rbegin;
120  using STLContainerType::rend;
121 
122  using STLContainerType::size;
123  using STLContainerType::max_size;
124  using STLContainerType::resize;
125  using STLContainerType::capacity;
126  using STLContainerType::empty;
127  using STLContainerType::reserve;
128 
129  using STLContainerType::operator[];
130  using STLContainerType::at;
131  using STLContainerType::front;
132  using STLContainerType::back;
133 
134  using STLContainerType::assign;
135  using STLContainerType::push_back;
136  using STLContainerType::pop_back;
137  using STLContainerType::insert;
138  using STLContainerType::erase;
140  using STLContainerType::clear;
141 
142  using STLContainerType::get_allocator;
143 
144  using typename STLContainerType::reference;
145  using typename STLContainerType::const_reference;
146  using typename STLContainerType::iterator;
147  using typename STLContainerType::const_iterator;
148  // already declared before
149  // using STLContainerType::size_type;
150  using typename STLContainerType::difference_type;
151  using typename STLContainerType::value_type;
152  using typename STLContainerType::allocator_type;
153  using typename STLContainerType::pointer;
154  using typename STLContainerType::const_pointer;
155  using typename STLContainerType::reverse_iterator;
156  using typename STLContainerType::const_reverse_iterator;
157 
159  friend class Iterator;
160  friend class ConstIterator;
161 
167  class Iterator
168  {
169 public:
170  typedef typename VectorIterator::iterator_category iterator_category;
171  typedef typename VectorIterator::value_type value_type;
172  typedef typename VectorIterator::difference_type difference_type;
173  typedef typename VectorIterator::pointer pointer;
174  typedef typename VectorIterator::reference reference;
175 
176  Iterator() : m_Pos(0) {}
177  Iterator(size_type d, const VectorIterator & i):m_Pos(d), m_Iter(i) {}
178  Iterator(const Iterator & r): m_Pos(r.m_Pos), m_Iter(r.m_Iter) {}
179  Iterator & operator*() { return *this; }
180  Iterator * operator->() { return this; }
181  Iterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
182  Iterator operator++(int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
183  Iterator & operator--() { --m_Pos; --m_Iter; return *this; }
184  Iterator operator--(int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
185 
186  difference_type operator-(const Iterator & r) const { return static_cast< difference_type >( this->m_Pos ) - static_cast< difference_type >( r.m_Pos ); }
187 
188  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
189  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
190  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
191  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
192  bool operator<(const Iterator & r) const { return ( this->operator-( r ) ) < 0; }
193  bool operator>(const Iterator & r) const { return ( r < *this ); }
194  bool operator>=(const Iterator & r) const { return !( *this < r ); }
195  bool operator<=(const Iterator & r) const { return !( *this < r ); }
196 
197  Iterator & operator+=(difference_type n) { m_Pos += n; m_Iter += n; return *this; };
198 
201  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
202 
204  Element & Value(void) const { return *m_Iter; }
205 
206 private:
209  friend class ConstIterator;
210  };
211 
218  {
219 public:
220  typedef typename VectorConstIterator::iterator_category iterator_category;
221  typedef typename VectorConstIterator::value_type value_type;
222  typedef typename VectorConstIterator::difference_type difference_type;
223  typedef typename VectorConstIterator::pointer pointer;
224  typedef typename VectorConstIterator::reference reference;
225 
228  ConstIterator(const Iterator & r) : m_Pos( r.m_Pos ), m_Iter( r.m_Iter ) { }
229  ConstIterator & operator*() { return *this; }
230  ConstIterator * operator->() { return this; }
231  ConstIterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
232  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
233  ConstIterator & operator--() { --m_Pos; --m_Iter; return *this; }
234  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
235  ConstIterator & operator=(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
236  ConstIterator & operator+=(difference_type n) { m_Pos += n; m_Iter += n; return *this; };
237 
238  difference_type operator-(const ConstIterator & r) const { return static_cast< difference_type >( m_Pos ) - static_cast< difference_type >( r.m_Pos ); }
239 
240  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
241  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
242  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
243  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
244  bool operator<(const ConstIterator & r) const { return ( this->operator-(r) < 0 ); }
245  bool operator>(const ConstIterator & r) const { return ( r < *this ); }
246  bool operator<=(const ConstIterator & r) const { return !( *this > r ); }
247  bool operator>=(const ConstIterator & r) const { return !( *this < r ); }
248 
249 
252  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
253 
255  const Element & Value(void) const { return *m_Iter; }
256 
257 private:
260  friend class Iterator;
261  };
262 
263  /* Declare the public interface routines. */
264 
274 
281  const Element & ElementAt(ElementIdentifier) const;
282 
292 
298 
304 
311 
316  bool IndexExists(ElementIdentifier) const;
317 
324 
331 
338 
342  ConstIterator Begin() const;
343 
347  ConstIterator End() const;
348 
352  Iterator Begin();
353 
357  Iterator End();
358 
362  ElementIdentifier Size() const;
363 
374 
381  void Squeeze();
382 
386  void Initialize();
387 };
388 } // end namespace itk
389 
390 #ifndef ITK_MANUAL_INSTANTIATION
391 #include "itkVectorContainer.hxx"
392 #endif
393 
394 #endif
bool operator!=(const Iterator &r) const
VectorConstIterator::value_type value_type
bool GetElementIfIndexExists(ElementIdentifier, Element *) const
Light weight base class for most itk classes.
Element & CreateElementAt(ElementIdentifier)
ConstIterator & operator=(const Iterator &r)
ElementIdentifier Index(void) const
TElementIdentifier ElementIdentifier
bool operator>(const Iterator &r) const
bool operator!=(const Iterator &r) const
ConstIterator(size_type d, const VectorConstIterator &i)
VectorType::const_iterator VectorConstIterator
Iterator(size_type d, const VectorIterator &i)
VectorIterator::pointer pointer
bool operator==(const ConstIterator &r) const
difference_type operator-(const ConstIterator &r) const
VectorIterator::iterator_category iterator_category
bool operator==(const Iterator &r) const
VectorConstIterator::iterator_category iterator_category
Iterator & operator+=(difference_type n)
const Element & Value(void) const
SmartPointer< const Self > ConstPointer
ConstIterator End() const
void DeleteIndex(ElementIdentifier)
STLContainerType & CastToSTLContainer()
ConstIterator & operator+=(difference_type n)
bool operator>=(const Iterator &r) const
ElementIdentifier Size() const
const STLContainerType & CastToSTLConstContainer() const
bool operator<(const ConstIterator &r) const
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:214
bool IndexExists(ElementIdentifier) const
bool operator<(const Iterator &r) const
bool operator!=(const ConstIterator &r) const
VectorIterator::value_type value_type
difference_type operator-(const Iterator &r) const
bool operator>=(const ConstIterator &r) const
void InsertElement(ElementIdentifier, Element)
void Reserve(ElementIdentifier)
bool operator==(const ConstIterator &r) const
VectorConstIterator::pointer pointer
bool operator!=(const ConstIterator &r) const
VectorContainer(size_type n)
VectorType::iterator VectorIterator
SmartPointer< Self > Pointer
Define a front-end to the STL &quot;vector&quot; container that conforms to the IndexedContainerInterface.
VectorIterator::reference reference
VectorIterator::difference_type difference_type
bool operator<=(const ConstIterator &r) const
VectorContainer(const Self &r)
void CreateIndex(ElementIdentifier)
bool operator>(const ConstIterator &r) const
Element & ElementAt(ElementIdentifier)
void SetElement(ElementIdentifier, Element)
Base class for most ITK classes.
Definition: itkObject.h:57
std::vector< Element > VectorType
Element GetElement(ElementIdentifier) const
VectorContainer(TInputIterator first, TInputIterator last)
ConstIterator Begin() const
bool operator==(const Iterator &r) const
VectorType::size_type size_type
bool operator<=(const Iterator &r) const
ElementIdentifier Index(void) const
VectorConstIterator::difference_type difference_type
VectorConstIterator::reference reference
VectorContainer(size_type n, const Element &x)