ITK  4.2.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 private:
66 
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 protected:
73 
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) {}
88 public:
90 
93 
95  itkNewMacro(Self);
96 
98  itkTypeMacro(VectorContainer, Object);
99 
101  class Iterator;
102  class ConstIterator;
103 
105  STLContainerType & CastToSTLContainer()
106  {
107  return dynamic_cast< STLContainerType & >( *this );
108  }
109 
111  const STLContainerType & CastToSTLConstContainer() const
112  {
113  return dynamic_cast< const STLContainerType & >( *this );
114  }
115 
116  using STLContainerType::begin;
117  using STLContainerType::end;
118  using STLContainerType::rbegin;
119  using STLContainerType::rend;
120 
121  using STLContainerType::size;
122  using STLContainerType::max_size;
123  using STLContainerType::resize;
124  using STLContainerType::capacity;
125  using STLContainerType::empty;
126  using STLContainerType::reserve;
127 
128  using STLContainerType::operator[];
129  using STLContainerType::at;
130  using STLContainerType::front;
131  using STLContainerType::back;
132 
133  using STLContainerType::assign;
134  using STLContainerType::push_back;
135  using STLContainerType::pop_back;
136  using STLContainerType::insert;
137  using STLContainerType::erase;
138  using STLContainerType::swap;
139  using STLContainerType::clear;
140 
141  using STLContainerType::get_allocator;
142 
143  using typename STLContainerType::reference;
144  using typename STLContainerType::const_reference;
145  using typename STLContainerType::iterator;
146  using typename STLContainerType::const_iterator;
147  // already declared before
148  // using STLContainerType::size_type;
149  using typename STLContainerType::difference_type;
150  using typename STLContainerType::value_type;
151  using typename STLContainerType::allocator_type;
152  using typename STLContainerType::pointer;
153  using typename STLContainerType::const_pointer;
154  using typename STLContainerType::reverse_iterator;
155  using typename STLContainerType::const_reverse_iterator;
156 
158  friend class Iterator;
159  friend class ConstIterator;
160 
166  class Iterator
167  {
168 public:
169  Iterator() {}
170  Iterator(size_type d, const VectorIterator & i):m_Pos(d), m_Iter(i) {}
171  Iterator & operator*() { return *this; }
172  Iterator * operator->() { return this; }
173  Iterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
174  Iterator operator++(int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
175  Iterator & operator--() { --m_Pos; --m_Iter; return *this; }
176  Iterator operator--(int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
177  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
178  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
179  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
180  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
181 
184  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
185 
187  Element & Value(void) const { return *m_Iter; }
188 private:
191  friend class ConstIterator;
192  };
193 
200  {
201 public:
203  ConstIterator(size_type d, const VectorConstIterator & i):m_Pos(d), m_Iter(i) {}
204  ConstIterator(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; }
205  ConstIterator & operator*() { return *this; }
206  ConstIterator * operator->() { return this; }
207  ConstIterator & operator++() { ++m_Pos; ++m_Iter; return *this; }
208  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
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=(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
212  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
213  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
214  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
215  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
216 
219  ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
220 
222  const Element & Value(void) const { return *m_Iter; }
223 private:
226  friend class Iterator;
227  };
228 
229  /* Declare the public interface routines. */
230 
239  Element & ElementAt(ElementIdentifier);
240 
247  const Element & ElementAt(ElementIdentifier) const;
248 
257  Element & CreateElementAt(ElementIdentifier);
258 
263  Element GetElement(ElementIdentifier) const;
264 
269  void SetElement(ElementIdentifier, Element);
270 
276  void InsertElement(ElementIdentifier, Element);
277 
282  bool IndexExists(ElementIdentifier) const;
283 
289  bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
290 
296  void CreateIndex(ElementIdentifier);
297 
303  void DeleteIndex(ElementIdentifier);
304 
308  ConstIterator Begin(void) const;
309 
313  ConstIterator End(void) const;
314 
318  Iterator Begin(void);
319 
323  Iterator End(void);
324 
328  ElementIdentifier Size(void) const;
329 
339  void Reserve(ElementIdentifier);
340 
347  void Squeeze(void);
348 
352  void Initialize(void);
353 };
354 } // end namespace itk
355 
356 // Define instantiation macro for this template.
357 #define ITK_TEMPLATE_VectorContainer(_, EXPORT, TypeX, TypeY) \
358  namespace itk \
359  { \
360  _( 2 ( class EXPORT VectorContainer< ITK_TEMPLATE_2 TypeX > ) ) \
361  namespace Templates \
362  { \
363  typedef VectorContainer< ITK_TEMPLATE_2 TypeX > \
364  VectorContainer##TypeY; \
365  } \
366  }
367 
368 #if ITK_TEMPLATE_EXPLICIT
369 #include "Templates/itkVectorContainer+-.h"
370 #endif
371 
372 #if ITK_TEMPLATE_TXX
373 #include "itkVectorContainer.hxx"
374 #endif
375 
376 #endif
377