Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkVectorContainer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkVectorContainer.h,v $
00005   Language:  C++
00006   Date:      $Date: 2007/01/30 20:56:09 $
00007   Version:   $Revision: 1.57 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
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 
00093   itkNewMacro(Self);
00094 
00096   itkTypeMacro(VectorContainer, Object);
00097 
00099   class Iterator;
00100   class ConstIterator;
00101 
00103   STLContainerType & CastToSTLContainer() {
00104      return dynamic_cast<STLContainerType &>(*this); }
00105 
00107   const STLContainerType & CastToSTLConstContainer() const {
00108      return dynamic_cast<const STLContainerType &>(*this); }
00109 
00111   friend class Iterator;
00112   friend class ConstIterator;
00113 
00116   class Iterator
00117   {
00118   public:
00119     Iterator() {}
00120     Iterator(size_type d, const VectorIterator& i): m_Pos(d), m_Iter(i) {}
00122 
00123     Iterator& operator* ()    { return *this; }
00124     Iterator* operator-> ()   { return this; }
00125     Iterator& operator++ ()   { ++m_Pos; ++m_Iter; return *this; }
00126     Iterator operator++ (int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00127     Iterator& operator-- ()   { --m_Pos; --m_Iter; return *this; }
00128     Iterator operator-- (int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; }
00129 
00130     bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00131     bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00132     bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00133     bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00134     
00136     ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00137 
00139     Element& Value(void) const { return *m_Iter; }
00140 
00141   private:
00142     size_type m_Pos;
00143     VectorIterator m_Iter;
00144     friend class ConstIterator;
00145   };
00146   
00149   class ConstIterator
00150   {
00151   public:
00152     ConstIterator() {}
00153     ConstIterator(size_type d, const VectorConstIterator& i): m_Pos(d), m_Iter(i) {}
00154     ConstIterator(const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; }
00156 
00157     ConstIterator& operator* ()    { return *this; }
00158     ConstIterator* operator-> ()   { return this; }
00159     ConstIterator& operator++ ()   { ++m_Pos; ++m_Iter; return *this; }
00160     ConstIterator operator++ (int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; }
00161     ConstIterator& operator-- ()   { --m_Pos; --m_Iter; return *this; }
00162     ConstIterator operator-- (int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; }
00163 
00164     ConstIterator& operator = (const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; }
00165     
00166     bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00167     bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00168     bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00169     bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00170     
00172     ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); }
00173 
00175     const Element& Value(void) const { return *m_Iter; }
00176 
00177   private:
00178     size_type m_Pos;
00179     VectorConstIterator m_Iter;
00180     friend class Iterator;
00181   };  
00182   
00183   /* Declare the public interface routines. */
00184   
00193   Element& ElementAt(ElementIdentifier);
00194 
00201   const Element& ElementAt(ElementIdentifier) const;
00202 
00211   Element& CreateElementAt(ElementIdentifier);
00212 
00217   Element GetElement(ElementIdentifier) const;
00218 
00223   void SetElement(ElementIdentifier, Element );
00224 
00230   void InsertElement(ElementIdentifier, Element );
00231 
00236   bool IndexExists(ElementIdentifier) const;
00237 
00243   bool GetElementIfIndexExists(ElementIdentifier, Element*) const;
00244 
00250   void CreateIndex(ElementIdentifier);
00251 
00257   void DeleteIndex(ElementIdentifier);
00258 
00262   ConstIterator Begin(void) const;
00263 
00267   ConstIterator End(void) const;  
00268 
00272   Iterator Begin(void);
00273 
00277   Iterator End(void);  
00278 
00282   unsigned long Size(void) const;
00283 
00293   void Reserve( ElementIdentifier );
00294 
00301   void Squeeze(void);
00302 
00306   void Initialize(void);
00307 
00308 };
00309 
00310 } // end namespace itk
00311 
00312 // Define instantiation macro for this template.
00313 #define ITK_TEMPLATE_VectorContainer(_, EXPORT, x, y) namespace itk { \
00314   _(2(class EXPORT VectorContainer< ITK_TEMPLATE_2 x >)) \
00315   namespace Templates { typedef VectorContainer< ITK_TEMPLATE_2 x > \
00316                                                   VectorContainer##y; } \
00317   }
00318 
00319 #if ITK_TEMPLATE_EXPLICIT
00320 # include "Templates/itkVectorContainer+-.h"
00321 #endif
00322 
00323 #if ITK_TEMPLATE_TXX
00324 # include "itkVectorContainer.txx"
00325 #endif
00326 
00327 #endif
00328 

Generated at Sun Sep 23 14:31:57 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000