ITK  4.3.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_EXPORT VectorContainer:
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 
106  STLContainerType & CastToSTLContainer()
107  {
108  return dynamic_cast< STLContainerType & >( *this );
109  }
110 
112  const STLContainerType & CastToSTLConstContainer() const
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;
139  using STLContainerType::swap;
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:
205  ConstIterator(size_type d, const VectorConstIterator & i):m_Pos(d), m_Iter(i) {}
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 
242  Element & ElementAt(ElementIdentifier);
243 
250  const Element & ElementAt(ElementIdentifier) const;
251 
260  Element & CreateElementAt(ElementIdentifier);
261 
266  Element GetElement(ElementIdentifier) const;
267 
272  void SetElement(ElementIdentifier, Element);
273 
279  void InsertElement(ElementIdentifier, Element);
280 
285  bool IndexExists(ElementIdentifier) const;
286 
292  bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
293 
299  void CreateIndex(ElementIdentifier);
300 
306  void DeleteIndex(ElementIdentifier);
307 
311  ConstIterator Begin(void) const;
312 
316  ConstIterator End(void) const;
317 
321  Iterator Begin(void);
322 
326  Iterator End(void);
327 
331  ElementIdentifier Size(void) const;
332 
342  void Reserve(ElementIdentifier);
343 
350  void Squeeze(void);
351 
355  void Initialize(void);
356 };
357 } // end namespace itk
358 
359 #ifndef ITK_MANUAL_INSTANTIATION
360 #include "itkVectorContainer.hxx"
361 #endif
362 
363 #endif
364