ITK  4.8.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  Iterator() {}
171  Iterator(size_type d, const VectorIterator & i):m_Pos(d), m_Iter(i) {}
172  Iterator & operator*() { return *this; }
173  Iterator * operator->() { return this; }
174  Iterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
175  Iterator operator++(int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
176  Iterator & operator--() { --m_Pos; --m_Iter; return *this; }
177  Iterator operator--(int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
178  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
179  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
180  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
181  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
182 
185  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
186 
188  Element & Value(void) const { return *m_Iter; }
189 
190 private:
193  friend class ConstIterator;
194  };
195 
202  {
203 public:
206  ConstIterator(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; }
207  ConstIterator & operator*() { return *this; }
208  ConstIterator * operator->() { return this; }
209  ConstIterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
210  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
211  ConstIterator & operator--() { --m_Pos; --m_Iter; return *this; }
212  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
213  ConstIterator & operator=(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
214  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
215  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
216  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
217  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
218 
221  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
222 
224  const Element & Value(void) const { return *m_Iter; }
225 
226 private:
229  friend class Iterator;
230  };
231 
232  /* Declare the public interface routines. */
233 
243 
250  const Element & ElementAt(ElementIdentifier) const;
251 
261 
267 
273 
280 
285  bool IndexExists(ElementIdentifier) const;
286 
293 
300 
307 
311  ConstIterator Begin() const;
312 
316  ConstIterator End() const;
317 
321  Iterator Begin();
322 
326  Iterator End();
327 
331  ElementIdentifier Size() const;
332 
343 
350  void Squeeze();
351 
355  void Initialize();
356 };
357 } // end namespace itk
358 
359 #ifndef ITK_MANUAL_INSTANTIATION
360 #include "itkVectorContainer.hxx"
361 #endif
362 
363 #endif
bool operator!=(const Iterator &r) const
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
ConstIterator(size_type d, const VectorConstIterator &i)
VectorType::const_iterator VectorConstIterator
Iterator(size_type d, const VectorIterator &i)
bool operator==(const ConstIterator &r) const
bool operator==(const Iterator &r) const
const Element & Value(void) const
SmartPointer< const Self > ConstPointer
ConstIterator End() const
void DeleteIndex(ElementIdentifier)
STLContainerType & CastToSTLContainer()
ElementIdentifier Size() const
const STLContainerType & CastToSTLConstContainer() const
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:207
bool IndexExists(ElementIdentifier) const
bool operator!=(const ConstIterator &r) const
void InsertElement(ElementIdentifier, Element)
void Reserve(ElementIdentifier)
bool operator==(const ConstIterator &r) const
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.
VectorContainer(const Self &r)
void CreateIndex(ElementIdentifier)
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
ElementIdentifier Index(void) const
VectorContainer(size_type n, const Element &x)