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

itkMapContainer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMapContainer.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-02-05 19:05:01 $
00007   Version:   $Revision: 1.49 $
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 __itkMapContainer_h
00018 #define __itkMapContainer_h
00019 
00020 #include "itkObject.h"
00021 #include "itkObjectFactory.h"
00022 
00023 #include <map>
00024 
00025 namespace itk
00026 {
00027 
00045 template <typename TElementIdentifier, typename TElement>
00046 class MapContainer:
00047   public Object,
00048   public std::map< TElementIdentifier , TElement >
00049 {
00050 public:
00052   typedef MapContainer              Self;
00053   typedef Object                    Superclass;
00054   typedef SmartPointer<Self>        Pointer;
00055   typedef SmartPointer<const Self>  ConstPointer;
00056 
00058   itkTypeMacro(MapContainer, Object);
00059 
00061   typedef TElementIdentifier  ElementIdentifier;
00062   typedef TElement            Element;
00063 
00064 private:
00065   MapContainer(const Self&); //purposely not implemented
00066   void operator=(const Self&); //purposely not implemented
00067 
00069   typedef std::map<ElementIdentifier, Element>     MapType;
00070   typedef typename MapType::iterator               MapIterator;
00071   typedef typename MapType::const_iterator         MapConstIterator;
00072   typedef typename MapType::key_compare            MapKeyCompareType;
00073 
00074 public:
00078   MapContainer():MapType() {}
00079   MapContainer(const MapKeyCompareType& comp):MapType(comp) {}
00080   //  MapContainer(const Self& r):MapType(r) {}
00081   template <typename TInputIterator>
00082   MapContainer(TInputIterator first, TInputIterator last):MapType(first, last) {}
00083   template <typename TInputIterator>
00084   MapContainer(TInputIterator first, TInputIterator last,const MapKeyCompareType& comp):
00085     MapType(first, last, comp) {}  
00087 
00089   itkNewMacro(Self);
00090 
00092   typedef MapType STLContainerType;
00093 
00095   STLContainerType & CastToSTLContainer()
00096     { return dynamic_cast<STLContainerType &>(*this); }
00097 
00099   const STLContainerType & CastToSTLConstContainer() const
00100     {
00101     return dynamic_cast<const STLContainerType &>(*this);
00102     }
00103 
00105   class Iterator;
00106   class ConstIterator;
00107   friend class Iterator;
00108   friend class ConstIterator;
00109 
00112   class Iterator
00113   {
00114   public:
00115     Iterator() {}
00116     Iterator( const MapIterator& i ): m_Iter(i) {}
00117     
00118     Iterator& operator* ()    { return *this; }
00119     Iterator* operator-> ()   { return this; }
00120     Iterator& operator++ ()   { ++m_Iter; return *this; }
00121     Iterator operator++ (int) { Iterator temp(*this);  ++m_Iter; return temp; }
00122     Iterator& operator-- ()   { --m_Iter; return *this; }
00123     Iterator operator-- (int) { Iterator temp(*this); --m_Iter; return temp; }
00124 
00125     bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00126     bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00127     bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00128     bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00129  
00131     ElementIdentifier Index(void) const { return m_Iter->first; }
00132 
00134     Element& Value(void) { return m_Iter->second; }
00135   private:
00136     MapIterator      m_Iter;
00137     friend class     ConstIterator;
00138   };
00139 
00142   class ConstIterator {
00143   public:
00144     ConstIterator() {}
00145     ConstIterator(const MapConstIterator& ci): m_Iter(ci) {}
00146     ConstIterator(const Iterator& r) { m_Iter = r.m_Iter; }
00147 
00148     ConstIterator& operator* ()    { return *this; }
00149     ConstIterator* operator-> ()   { return this; }
00150     ConstIterator& operator++ ()   { ++m_Iter; return *this; }
00151     ConstIterator operator++ (int) { ConstIterator temp(*this);  ++m_Iter; return temp; }
00152     ConstIterator& operator-- ()   { --m_Iter; return *this; }
00153     ConstIterator operator-- (int) { ConstIterator temp(*this); --m_Iter; return temp; }
00154 
00155     bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; }
00156     bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; }
00157     bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; }
00158     bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; }
00159     
00161     ElementIdentifier Index(void) const { return m_Iter->first; }
00162 
00164     const Element& Value(void) const { return m_Iter->second; }
00165 
00166   private:
00167     MapConstIterator m_Iter;
00168     friend class Iterator;
00169   };
00170 
00171   /* Declare the public interface routines. */
00172 
00180   Element& ElementAt(ElementIdentifier);
00181 
00186   const Element& ElementAt(ElementIdentifier) const;
00187 
00195   Element& CreateElementAt(ElementIdentifier);
00196 
00201   Element GetElement(ElementIdentifier) const;
00202 
00207   void SetElement(ElementIdentifier, Element);
00208 
00213   void InsertElement(ElementIdentifier, Element);
00214 
00219   bool IndexExists(ElementIdentifier) const;
00220 
00226   bool GetElementIfIndexExists(ElementIdentifier, Element*) const;
00227 
00233   void CreateIndex(ElementIdentifier);
00234 
00239   void DeleteIndex(ElementIdentifier);
00240 
00244   ConstIterator Begin(void) const;
00245 
00249   ConstIterator End(void) const;  
00250 
00254   Iterator Begin(void);
00255 
00259   Iterator End(void);  
00260 
00264   unsigned long Size(void) const;
00265 
00272   void Reserve(ElementIdentifier);
00273 
00279   void Squeeze(void);
00280 
00285   void Initialize(void);
00286 
00287 };
00288 
00289 } // end namespace itk
00290 
00291 // Define instantiation macro for this template.
00292 #define ITK_TEMPLATE_MapContainer(_, EXPORT, x, y) namespace itk { \
00293   _(2(class EXPORT MapContainer< ITK_TEMPLATE_2 x >)) \
00294   namespace Templates { typedef MapContainer< ITK_TEMPLATE_2 x > \
00295                                                   MapContainer##y; } \
00296   }
00297 
00298 #if ITK_TEMPLATE_EXPLICIT
00299 # include "Templates/itkMapContainer+-.h"
00300 #endif
00301 
00302 #if ITK_TEMPLATE_TXX
00303 # include "itkMapContainer.txx"
00304 #endif
00305 
00306 #endif
00307 

Generated at Tue Sep 15 03:53:17 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000