ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVectorContainer.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkVectorContainer_h
00019 #define __itkVectorContainer_h
00020 
00021 #include "itkObject.h"
00022 #include "itkObjectFactory.h"
00023 
00024 #include <utility>
00025 #include <vector>
00026 
00027 namespace itk
00028 {
00047 template<
00048   typename TElementIdentifier,
00049   typename TElement
00050   >
00051 class ITK_EXPORT VectorContainer:
00052   public Object,
00053   private std::vector< TElement >
00054 {
00055 public:
00057   typedef VectorContainer            Self;
00058   typedef Object                     Superclass;
00059   typedef SmartPointer< Self >       Pointer;
00060   typedef SmartPointer< const Self > ConstPointer;
00061 
00063   typedef TElementIdentifier ElementIdentifier;
00064   typedef TElement           Element;
00065 private:
00066 
00068   typedef std::vector< Element >              VectorType;
00069   typedef typename VectorType::size_type      size_type;
00070   typedef typename VectorType::iterator       VectorIterator;
00071   typedef typename VectorType::const_iterator VectorConstIterator;
00072 protected:
00073 
00077   VectorContainer():
00078     Object(), VectorType() {}
00079   VectorContainer(size_type n):
00080     Object(), VectorType(n) {}
00081   VectorContainer(size_type n, const Element & x):
00082     Object(), VectorType(n, x) {}
00083   VectorContainer(const Self & r):
00084     Object(), VectorType(r) {}
00085   template< typename TInputIterator >
00086   VectorContainer(TInputIterator first, TInputIterator last):
00087     Object(), VectorType(first, last) {}
00088 public:
00090 
00092   typedef VectorType                     STLContainerType;
00093 
00095   itkNewMacro(Self);
00096 
00098   itkTypeMacro(VectorContainer, Object);
00099 
00101   class Iterator;
00102   class ConstIterator;
00103 
00105   STLContainerType & CastToSTLContainer()
00106   {
00107     return dynamic_cast< STLContainerType & >( *this );
00108   }
00109 
00111   const STLContainerType & CastToSTLConstContainer() const
00112   {
00113     return dynamic_cast< const STLContainerType & >( *this );
00114   }
00115 
00116   using STLContainerType::begin;
00117   using STLContainerType::end;
00118   using STLContainerType::rbegin;
00119   using STLContainerType::rend;
00120 
00121   using STLContainerType::size;
00122   using STLContainerType::max_size;
00123   using STLContainerType::resize;
00124   using STLContainerType::capacity;
00125   using STLContainerType::empty;
00126   using STLContainerType::reserve;
00127 
00128   using STLContainerType::operator[];
00129   using STLContainerType::at;
00130   using STLContainerType::front;
00131   using STLContainerType::back;
00132 
00133   using STLContainerType::assign;
00134   using STLContainerType::push_back;
00135   using STLContainerType::pop_back;
00136   using STLContainerType::insert;
00137   using STLContainerType::erase;
00138   using STLContainerType::swap;
00139   using STLContainerType::clear;
00140 
00141   using STLContainerType::get_allocator;
00142 
00143   using typename STLContainerType::reference;
00144   using typename STLContainerType::const_reference;
00145   using typename STLContainerType::iterator;
00146   using typename STLContainerType::const_iterator;
00147   // already declared before
00148   // using STLContainerType::size_type;
00149   using typename STLContainerType::difference_type;
00150   using typename STLContainerType::value_type;
00151   using typename STLContainerType::allocator_type;
00152   using typename STLContainerType::pointer;
00153   using typename STLContainerType::const_pointer;
00154   using typename STLContainerType::reverse_iterator;
00155   using typename STLContainerType::const_reverse_iterator;
00156 
00158   friend class Iterator;
00159   friend class ConstIterator;
00160 
00166   class Iterator
00167   {
00168 public:
00169     Iterator() {}
00170     Iterator(size_type d, const VectorIterator & i):m_Pos(d), m_Iter(i) {}
00171     Iterator & operator*()    { return *this; }
00172     Iterator * operator->()   { return this; }
00173     Iterator & operator++()   { ++m_Pos; ++m_Iter; return *this; }
00174     Iterator operator++(int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00175     Iterator & operator--()   { --m_Pos; --m_Iter; return *this; }
00176     Iterator operator--(int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
00177     bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
00178     bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
00179     bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
00180     bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
00181 
00184     ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
00185 
00187     Element & Value(void) const { return *m_Iter; }
00188 private:
00189     size_type      m_Pos;
00190     VectorIterator m_Iter;
00191     friend class ConstIterator;
00192   };
00193 
00199   class ConstIterator
00200   {
00201 public:
00202     ConstIterator() {}
00203     ConstIterator(size_type d, const VectorConstIterator & i):m_Pos(d), m_Iter(i) {}
00204     ConstIterator(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; }
00205     ConstIterator & operator*()    { return *this; }
00206     ConstIterator * operator->()   { return this; }
00207     ConstIterator & operator++()   { ++m_Pos; ++m_Iter; return *this; }
00208     ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00209     ConstIterator & operator--()   { --m_Pos; --m_Iter; return *this; }
00210     ConstIterator operator--(int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
00211     ConstIterator & operator=(const Iterator & r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
00212     bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
00213     bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
00214     bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
00215     bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
00216 
00219     ElementIdentifier Index(void) const { return static_cast< ElementIdentifier >( m_Pos ); }
00220 
00222     const Element & Value(void) const { return *m_Iter; }
00223 private:
00224     size_type           m_Pos;
00225     VectorConstIterator m_Iter;
00226     friend class Iterator;
00227   };
00228 
00229   /* Declare the public interface routines. */
00230 
00239   Element & ElementAt(ElementIdentifier);
00240 
00247   const Element & ElementAt(ElementIdentifier) const;
00248 
00257   Element & CreateElementAt(ElementIdentifier);
00258 
00263   Element GetElement(ElementIdentifier) const;
00264 
00269   void SetElement(ElementIdentifier, Element);
00270 
00276   void InsertElement(ElementIdentifier, Element);
00277 
00282   bool IndexExists(ElementIdentifier) const;
00283 
00289   bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
00290 
00296   void CreateIndex(ElementIdentifier);
00297 
00303   void DeleteIndex(ElementIdentifier);
00304 
00308   ConstIterator Begin(void) const;
00309 
00313   ConstIterator End(void) const;
00314 
00318   Iterator Begin(void);
00319 
00323   Iterator End(void);
00324 
00328   ElementIdentifier Size(void) const;
00329 
00339   void Reserve(ElementIdentifier);
00340 
00347   void Squeeze(void);
00348 
00352   void Initialize(void);
00353 };
00354 } // end namespace itk
00355 
00356 // Define instantiation macro for this template.
00357 #define ITK_TEMPLATE_VectorContainer(_, EXPORT, TypeX, TypeY)     \
00358   namespace itk                                                   \
00359   {                                                               \
00360   _( 2 ( class EXPORT VectorContainer< ITK_TEMPLATE_2 TypeX > ) ) \
00361   namespace Templates                                             \
00362   {                                                               \
00363   typedef VectorContainer< ITK_TEMPLATE_2 TypeX >                 \
00364   VectorContainer##TypeY;                                       \
00365   }                                                               \
00366   }
00367 
00368 #if ITK_TEMPLATE_EXPLICIT
00369 #include "Templates/itkVectorContainer+-.h"
00370 #endif
00371 
00372 #if ITK_TEMPLATE_TXX
00373 #include "itkVectorContainer.hxx"
00374 #endif
00375 
00376 #endif
00377