ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMapContainer.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 __itkMapContainer_h
00019 #define __itkMapContainer_h
00020 
00021 #include "itkObject.h"
00022 #include "itkObjectFactory.h"
00023 
00024 #include <map>
00025 
00026 namespace itk
00027 {
00044 template< typename TElementIdentifier, typename TElement >
00045 class MapContainer:
00046   public Object,
00047   private std::map< TElementIdentifier, TElement >
00048 {
00049 public:
00051   typedef MapContainer               Self;
00052   typedef Object                     Superclass;
00053   typedef SmartPointer< Self >       Pointer;
00054   typedef SmartPointer< const Self > ConstPointer;
00055 
00057   itkTypeMacro(MapContainer, Object);
00058 
00060   typedef TElementIdentifier ElementIdentifier;
00061   typedef TElement           Element;
00062 private:
00063   MapContainer(const Self &);   //purposely not implemented
00064   void operator=(const Self &); //purposely not implemented
00066 
00068   typedef std::map< ElementIdentifier, Element > MapType;
00069   typedef typename MapType::iterator             MapIterator;
00070   typedef typename MapType::const_iterator       MapConstIterator;
00071   typedef typename MapType::key_compare          MapKeyCompareType;
00072 public:
00073 
00077   MapContainer():MapType() {}
00078   MapContainer(const MapKeyCompareType & comp):MapType(comp) {}
00079   //  MapContainer(const Self& r):MapType(r) {}
00080   template< typename TInputIterator >
00081   MapContainer(TInputIterator first, TInputIterator last):MapType(first, last) {}
00082   template< typename TInputIterator >
00083   MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType & comp):
00084     MapType(first, last, comp) {}
00086 
00088   itkNewMacro(Self);
00089 
00091   typedef MapType STLContainerType;
00092 
00094   STLContainerType & CastToSTLContainer()
00095   { return dynamic_cast< STLContainerType & >( *this ); }
00096 
00098   const STLContainerType & CastToSTLConstContainer() const
00099   {
00100     return dynamic_cast< const STLContainerType & >( *this );
00101   }
00102 
00103   using STLContainerType::begin;
00104   using STLContainerType::end;
00105   using STLContainerType::rbegin;
00106   using STLContainerType::rend;
00107 
00108   using STLContainerType::empty;
00109   using STLContainerType::size;
00110   using STLContainerType::max_size;
00111 
00112   using STLContainerType::operator[];
00113 
00114   using STLContainerType::insert;
00115   using STLContainerType::erase;
00116   using STLContainerType::swap;
00117   using STLContainerType::clear;
00118 
00119   using STLContainerType::key_comp;
00120   using STLContainerType::value_comp;
00121 
00122   using STLContainerType::find;
00123   using STLContainerType::count;
00124   using STLContainerType::lower_bound;
00125   using STLContainerType::upper_bound;
00126   using STLContainerType::equal_range;
00127 
00128   using STLContainerType::get_allocator;
00129 
00130   using typename STLContainerType::key_type;
00131   using typename STLContainerType::mapped_type;
00132   using typename STLContainerType::value_type;
00133   using typename STLContainerType::key_compare;
00134   using typename STLContainerType::value_compare;
00135   using typename STLContainerType::allocator_type;
00136   using typename STLContainerType::reference;
00137   using typename STLContainerType::const_reference;
00138   using typename STLContainerType::iterator;
00139   using typename STLContainerType::const_iterator;
00140   using typename STLContainerType::size_type;
00141   using typename STLContainerType::difference_type;
00142   using typename STLContainerType::pointer;
00143   using typename STLContainerType::const_pointer;
00144   using typename STLContainerType::reverse_iterator;
00145   using typename STLContainerType::const_reverse_iterator;
00146 
00148   class Iterator;
00149   class ConstIterator;
00150   friend class Iterator;
00151   friend class ConstIterator;
00152 
00157   class Iterator
00158   {
00159 public:
00160     Iterator() {}
00161     Iterator(const MapIterator & i):m_Iter(i) {}
00162 
00163     Iterator & operator*()    { return *this; }
00164     Iterator * operator->()   { return this; }
00165     Iterator & operator++()   { ++m_Iter; return *this; }
00166     Iterator operator++(int) { Iterator temp(*this);  ++m_Iter; return temp; }
00167     Iterator & operator--()   { --m_Iter; return *this; }
00168     Iterator operator--(int) { Iterator temp(*this); --m_Iter; return temp; }
00169 
00170     bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
00171     bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
00172     bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
00173     bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
00174 
00176     ElementIdentifier Index(void) const { return m_Iter->first; }
00177 
00179     Element & Value(void) { return m_Iter->second; }
00180 private:
00181     MapIterator m_Iter;
00182     friend class ConstIterator;
00183   };
00184 
00189   class ConstIterator
00190   {
00191 public:
00192     ConstIterator() {}
00193     ConstIterator(const MapConstIterator & ci):m_Iter(ci) {}
00194     ConstIterator(const Iterator & r) { m_Iter = r.m_Iter; }
00195 
00196     ConstIterator & operator*()    { return *this; }
00197     ConstIterator * operator->()   { return this; }
00198     ConstIterator & operator++()   { ++m_Iter; return *this; }
00199     ConstIterator operator++(int) { ConstIterator temp(*this);  ++m_Iter; return temp; }
00200     ConstIterator & operator--()   { --m_Iter; return *this; }
00201     ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
00202 
00203     bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
00204     bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
00205     bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
00206     bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
00207 
00209     ElementIdentifier Index(void) const { return m_Iter->first; }
00210 
00212     const Element & Value(void) const { return m_Iter->second; }
00213 private:
00214     MapConstIterator m_Iter;
00215     friend class Iterator;
00216   };
00217 
00218   /* Declare the public interface routines. */
00219 
00227   Element & ElementAt(ElementIdentifier);
00228 
00233   const Element & ElementAt(ElementIdentifier) const;
00234 
00242   Element & CreateElementAt(ElementIdentifier);
00243 
00248   Element GetElement(ElementIdentifier) const;
00249 
00254   void SetElement(ElementIdentifier, Element);
00255 
00260   void InsertElement(ElementIdentifier, Element);
00261 
00266   bool IndexExists(ElementIdentifier) const;
00267 
00273   bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
00274 
00280   void CreateIndex(ElementIdentifier);
00281 
00286   void DeleteIndex(ElementIdentifier);
00287 
00291   ConstIterator Begin(void) const;
00292 
00296   ConstIterator End(void) const;
00297 
00301   Iterator Begin(void);
00302 
00306   Iterator End(void);
00307 
00311   ElementIdentifier Size(void) const;
00312 
00319   void Reserve(ElementIdentifier);
00320 
00326   void Squeeze(void);
00327 
00332   void Initialize(void);
00333 };
00334 } // end namespace itk
00335 
00336 // Define instantiation macro for this template.
00337 #define ITK_TEMPLATE_MapContainer(_, EXPORT, TypeX, TypeY)     \
00338   namespace itk                                                \
00339   {                                                            \
00340   _( 2 ( class EXPORT MapContainer< ITK_TEMPLATE_2 TypeX > ) ) \
00341   namespace Templates                                          \
00342   {                                                            \
00343   typedef MapContainer< ITK_TEMPLATE_2 TypeX >                 \
00344   MapContainer##TypeY;                                       \
00345   }                                                            \
00346   }
00347 
00348 #if ITK_TEMPLATE_EXPLICIT
00349 #include "Templates/itkMapContainer+-.h"
00350 #endif
00351 
00352 #if ITK_TEMPLATE_TXX
00353 #include "itkMapContainer.hxx"
00354 #endif
00355 
00356 #endif
00357