itkVectorContainer.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkVectorContainer_h
00018 #define __itkVectorContainer_h
00019
00020 #include "itkObject.h"
00021 #include "itkObjectFactory.h"
00022
00023 #include <utility>
00024 #include <vector>
00025
00026 namespace itk
00027 {
00028
00044 template <
00045 typename TElementIdentifier,
00046 typename TElement
00047 >
00048 class ITK_EXPORT VectorContainer:
00049 public Object,
00050 public std::vector<TElement>
00051 {
00052 public:
00054 typedef VectorContainer Self;
00055 typedef Object Superclass;
00056 typedef SmartPointer<Self> Pointer;
00057 typedef SmartPointer<const Self> ConstPointer;
00058
00060 typedef TElementIdentifier ElementIdentifier;
00061 typedef TElement Element;
00062
00063 private:
00065 typedef std::vector<Element> VectorType;
00066 typedef typename VectorType::size_type size_type;
00067 typedef typename VectorType::iterator VectorIterator;
00068 typedef typename VectorType::const_iterator VectorConstIterator;
00069
00070 protected:
00074 VectorContainer():
00075 Object(), VectorType() {}
00076 VectorContainer(size_type n):
00077 Object(), VectorType(n) {}
00078 VectorContainer(size_type n, const Element& x):
00079 Object(), VectorType(n, x) {}
00080 VectorContainer(const Self& r):
00081 Object(), VectorType(r) {}
00082 template <typename InputIterator>
00083 VectorContainer(InputIterator first, InputIterator last):
00084 Object(), VectorType(first, last) {}
00086
00087 public:
00088
00090 typedef VectorType STLContainerType;
00091 typedef typename VectorType::size_type VectorContainerSizeType;
00092
00094 itkNewMacro(Self);
00095
00097 itkTypeMacro(VectorContainer, Object);
00098
00100 class Iterator;
00101 class ConstIterator;
00102
00104 STLContainerType & CastToSTLContainer() {
00105 return dynamic_cast<STLContainerType &>(*this); }
00106
00108 const STLContainerType & CastToSTLConstContainer() const {
00109 return dynamic_cast<const STLContainerType &>(*this); }
00110
00112 friend class Iterator;
00113 friend class ConstIterator;
00114
00117 class Iterator
00118 {
00119 public:
00120 Iterator() {}
00121 Iterator(size_type d, const VectorIterator& i): m_Pos(d), m_Iter(i) {}
00123
00124 Iterator& operator* () { return *this; }
00125 Iterator* operator-> () { return this; }
00126 Iterator& operator++ () { ++m_Pos; ++m_Iter; return *this; }
00127 Iterator operator++ (int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00128 Iterator& operator-- () { --m_Pos; --m_Iter; return *this; }
00129 Iterator operator-- (int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
00130
00131 bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00132 bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00133 bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00134 bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00135
00137 ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00138
00140 Element& Value(void) const { return *m_Iter; }
00141
00142 private:
00143 size_type m_Pos;
00144 VectorIterator m_Iter;
00145 friend class ConstIterator;
00146 };
00147
00150 class ConstIterator
00151 {
00152 public:
00153 ConstIterator() {}
00154 ConstIterator(size_type d, const VectorConstIterator& i): m_Pos(d), m_Iter(i) {}
00155 ConstIterator(const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; }
00157
00158 ConstIterator& operator* () { return *this; }
00159 ConstIterator* operator-> () { return this; }
00160 ConstIterator& operator++ () { ++m_Pos; ++m_Iter; return *this; }
00161 ConstIterator operator++ (int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00162 ConstIterator& operator-- () { --m_Pos; --m_Iter; return *this; }
00163 ConstIterator operator-- (int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
00164
00165 ConstIterator& operator = (const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
00166
00167 bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00168 bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00169 bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00170 bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00171
00173 ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00174
00176 const Element& Value(void) const { return *m_Iter; }
00177
00178 private:
00179 size_type m_Pos;
00180 VectorConstIterator m_Iter;
00181 friend class Iterator;
00182 };
00183
00184
00185
00194 Element& ElementAt(ElementIdentifier);
00195
00202 const Element& ElementAt(ElementIdentifier) const;
00203
00212 Element& CreateElementAt(ElementIdentifier);
00213
00218 Element GetElement(ElementIdentifier) const;
00219
00224 void SetElement(ElementIdentifier, Element );
00225
00231 void InsertElement(ElementIdentifier, Element );
00232
00237 bool IndexExists(ElementIdentifier) const;
00238
00244 bool GetElementIfIndexExists(ElementIdentifier, Element*) const;
00245
00251 void CreateIndex(ElementIdentifier);
00252
00258 void DeleteIndex(ElementIdentifier);
00259
00263 ConstIterator Begin(void) const;
00264
00268 ConstIterator End(void) const;
00269
00273 Iterator Begin(void);
00274
00278 Iterator End(void);
00279
00283 unsigned long Size(void) const;
00284
00294 void Reserve( ElementIdentifier );
00295
00302 void Squeeze(void);
00303
00307 void Initialize(void);
00308
00309 };
00310
00311 }
00312
00313
00314 #define ITK_TEMPLATE_VectorContainer(_, EXPORT, x, y) namespace itk { \
00315 _(2(class EXPORT VectorContainer< ITK_TEMPLATE_2 x >)) \
00316 namespace Templates { typedef VectorContainer< ITK_TEMPLATE_2 x > \
00317 VectorContainer##y; } \
00318 }
00319
00320 #if ITK_TEMPLATE_EXPLICIT
00321 # include "Templates/itkVectorContainer+-.h"
00322 #endif
00323
00324 #if ITK_TEMPLATE_TXX
00325 # include "itkVectorContainer.txx"
00326 #endif
00327
00328 #endif
00329