| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkMapContainer.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:41 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkMapContainer_h |
| 18 |
|
#define __itkMapContainer_h |
| 19 |
|
|
| 20 |
|
#include "itkObject.h" |
| 21 |
|
#include "itkObjectFactory.h" |
| 22 |
|
|
| 23 |
|
#include <map> |
| 24 |
|
|
| 25 |
|
namespace itk |
| 26 |
|
{ |
| 27 |
|
|
| 28 |
|
/** \class MapContainer |
| 29 |
|
* Define a front-end to the STL "map" container that conforms to the |
| 30 |
|
* IndexedContainerInterface. This is a full-fleged Object, so |
| 31 |
|
* there are events, modification time, debug, and reference count |
| 32 |
|
* information. |
| 33 |
|
* |
| 34 |
|
* Template parameters for MapContainer: |
| 35 |
|
* |
| 36 |
|
* TElementIdentifier = |
| 37 |
|
* A type that shall be used to index the container. |
| 38 |
|
* It must have a < operator defined for ordering. |
| 39 |
|
* |
| 40 |
|
* TElement = |
| 41 |
|
* The element type stored in the container. |
| 42 |
|
* |
| 43 |
|
* \ingroup DataRepresentation |
| 44 |
|
*/ |
| 45 |
|
template <typename TElementIdentifier, typename TElement> |
| 46 |
|
class MapContainer: |
| 47 |
IND |
**public Object, |
| 48 |
IND |
**public std::map< TElementIdentifier , TElement > |
| 49 |
|
{ |
| 50 |
|
public: |
| 51 |
|
/** Standard class typedefs. */ |
| 52 |
|
typedef MapContainer Self; |
| 53 |
TDA |
typedef Object Superclass; |
| 54 |
|
typedef SmartPointer<Self> Pointer; |
| 55 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 56 |
|
|
| 57 |
|
/** Standard part of every itk Object. */ |
| 58 |
|
itkTypeMacro(MapContainer, Object); |
| 59 |
|
|
| 60 |
|
/** Save the template parameters. */ |
| 61 |
|
typedef TElementIdentifier ElementIdentifier; |
| 62 |
|
typedef TElement Element; |
| 63 |
|
|
| 64 |
|
private: |
| 65 |
|
MapContainer(const Self&); //purposely not implemented |
| 66 |
|
void operator=(const Self&); //purposely not implemented |
| 67 |
|
|
| 68 |
|
/** Quick access to the STL map type that was inherited. */ |
| 69 |
|
typedef std::map<ElementIdentifier, Element> MapType; |
| 70 |
|
typedef typename MapType::iterator MapIterator; |
| 71 |
|
typedef typename MapType::const_iterator MapConstIterator; |
| 72 |
|
typedef typename MapType::key_compare MapKeyCompareType; |
| 73 |
|
|
| 74 |
|
public: |
| 75 |
|
/** Provide pass-through constructors corresponding to all the STL |
| 76 |
|
* map constructors. These are for internal use only since this is also |
| 77 |
|
* an Object which must be constructed through the "New()" routine. */ |
| 78 |
|
MapContainer():MapType() {} |
| 79 |
|
MapContainer(const MapKeyCompareType& comp):MapType(comp) {} |
| 80 |
|
// MapContainer(const Self& r):MapType(r) {} |
| 81 |
|
template <typename InputIterator> |
| 82 |
|
MapContainer(InputIterator first, InputIterator last):MapType(first, last) {} |
| 83 |
|
template <typename InputIterator> |
| 84 |
LEN |
MapContainer(InputIterator first, InputIterator last,const MapKeyCompareType& comp): |
| 85 |
IND |
****MapType(first, last, comp) {} |
| 86 |
|
|
| 87 |
|
/** Method for creation through the object factory. */ |
| 88 |
|
itkNewMacro(Self); |
| 89 |
|
|
| 90 |
|
/** This type is provided to Adapt this container as an STL container */ |
| 91 |
|
typedef MapType STLContainerType; |
| 92 |
|
|
| 93 |
|
/** Cast the container to a STL container type */ |
| 94 |
|
STLContainerType & CastToSTLContainer() { |
| 95 |
IND |
*****return dynamic_cast<STLContainerType &>(*this); } |
| 96 |
|
|
| 97 |
|
/** Cast the container to a const STL container type */ |
| 98 |
|
const STLContainerType & CastToSTLConstContainer() const { |
| 99 |
IND |
*****return dynamic_cast<const STLContainerType &>(*this); } |
| 100 |
|
|
| 101 |
|
/** Declare iterators to container. */ |
| 102 |
|
class Iterator; |
| 103 |
|
class ConstIterator; |
| 104 |
|
friend class Iterator; |
| 105 |
|
friend class ConstIterator; |
| 106 |
|
|
| 107 |
|
/** \brief The non-const iterator type for the map. */ |
| 108 |
|
class Iterator |
| 109 |
MCM,IND |
**{ |
| 110 |
|
public: |
| 111 |
|
Iterator() {} |
| 112 |
|
Iterator( const MapIterator& i ): m_Iter(i) {} |
| 113 |
|
|
| 114 |
|
Iterator& operator* () { return *this; } |
| 115 |
|
Iterator* operator-> () { return this; } |
| 116 |
|
Iterator& operator++ () { ++m_Iter; return *this; } |
| 117 |
|
Iterator operator++ (int) { Iterator temp(*this); ++m_Iter; return temp; } |
| 118 |
|
Iterator& operator-- () { --m_Iter; return *this; } |
| 119 |
|
Iterator operator-- (int) { Iterator temp(*this); --m_Iter; return temp; } |
| 120 |
|
|
| 121 |
|
bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; } |
| 122 |
|
bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; } |
| 123 |
LEN |
bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; } |
| 124 |
LEN |
bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; } |
| 125 |
|
|
| 126 |
|
/** Get the index into the MapContainer associated with this iterator. */ |
| 127 |
|
ElementIdentifier Index(void) const { return m_Iter->first; } |
| 128 |
|
|
| 129 |
|
/** Get the value at this iterator's location in the MapContainer. */ |
| 130 |
|
Element& Value(void) { return m_Iter->second; } |
| 131 |
|
private: |
| 132 |
|
MapIterator m_Iter; |
| 133 |
|
friend class ConstIterator; |
| 134 |
IND |
**}; |
| 135 |
|
|
| 136 |
|
/** \brief The const iterator type for the map. */ |
| 137 |
|
class ConstIterator |
| 138 |
MCM,IND |
**{ |
| 139 |
|
public: |
| 140 |
|
ConstIterator() {} |
| 141 |
|
ConstIterator(const MapConstIterator& ci): m_Iter(ci) {} |
| 142 |
|
ConstIterator(const Iterator& r) { m_Iter = r.m_Iter; } |
| 143 |
|
|
| 144 |
|
ConstIterator& operator* () { return *this; } |
| 145 |
|
ConstIterator* operator-> () { return this; } |
| 146 |
|
ConstIterator& operator++ () { ++m_Iter; return *this; } |
| 147 |
LEN |
ConstIterator operator++ (int) { ConstIterator temp(*this); ++m_Iter; return temp; } |
| 148 |
|
ConstIterator& operator-- () { --m_Iter; return *this; } |
| 149 |
LEN |
ConstIterator operator-- (int) { ConstIterator temp(*this); --m_Iter; return temp; } |
| 150 |
|
|
| 151 |
|
bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; } |
| 152 |
|
bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; } |
| 153 |
LEN |
bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; } |
| 154 |
LEN |
bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; } |
| 155 |
|
|
| 156 |
|
/** Get the index into the MapContainer associated with this iterator. */ |
| 157 |
|
ElementIdentifier Index(void) const { return m_Iter->first; } |
| 158 |
|
|
| 159 |
|
/** Get the value at this iterator's location in the MapContainer. */ |
| 160 |
|
const Element& Value(void) const { return m_Iter->second; } |
| 161 |
|
|
| 162 |
|
private: |
| 163 |
|
MapConstIterator m_Iter; |
| 164 |
|
friend class Iterator; |
| 165 |
IND |
**}; |
| 166 |
|
|
| 167 |
|
/** Declare the public interface routines. */ |
| 168 |
|
Element& ElementAt(ElementIdentifier); |
| 169 |
|
const Element& ElementAt(ElementIdentifier) const; |
| 170 |
|
Element& CreateElementAt(ElementIdentifier); |
| 171 |
|
Element GetElement(ElementIdentifier) const; |
| 172 |
|
void SetElement(ElementIdentifier, Element); |
| 173 |
|
void InsertElement(ElementIdentifier, Element); |
| 174 |
|
bool IndexExists(ElementIdentifier) const; |
| 175 |
|
bool GetElementIfIndexExists(ElementIdentifier, Element*) const; |
| 176 |
|
void CreateIndex(ElementIdentifier); |
| 177 |
|
void DeleteIndex(ElementIdentifier); |
| 178 |
|
ConstIterator Begin(void) const; |
| 179 |
|
ConstIterator End(void) const; |
| 180 |
|
Iterator Begin(void); |
| 181 |
|
Iterator End(void); |
| 182 |
|
unsigned long Size(void) const; |
| 183 |
|
void Reserve(ElementIdentifier); |
| 184 |
|
void Squeeze(void); |
| 185 |
|
void Initialize(void); |
| 186 |
|
|
| 187 |
|
}; |
| 188 |
|
|
| 189 |
|
} // end namespace itk |
| 190 |
|
|
| 191 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 192 |
|
#include "itkMapContainer.txx" |
| 193 |
|
#endif |
| 194 |
|
|
| 195 |
|
#endif |
| 196 |
|
|