ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkMapContainer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMapContainer_h
19 #define itkMapContainer_h
20 
21 #include "itkObject.h"
22 #include "itkObjectFactory.h"
23 
24 #include <map>
25 
26 namespace itk
27 {
44 template< typename TElementIdentifier, typename TElement >
46  public Object,
47  private std::map< TElementIdentifier, TElement >
48 {
49 public:
51  typedef MapContainer Self;
52  typedef Object Superclass;
55 
57  itkTypeMacro(MapContainer, Object);
58 
60  typedef TElementIdentifier ElementIdentifier;
61  typedef TElement Element;
62 
63 private:
64  MapContainer(const Self &) ITK_DELETE_FUNCTION;
65  void operator=(const Self &) ITK_DELETE_FUNCTION;
66 
68  typedef std::map< ElementIdentifier, Element > MapType;
69  typedef typename MapType::iterator MapIterator;
70  typedef typename MapType::const_iterator MapConstIterator;
71  typedef typename MapType::key_compare MapKeyCompareType;
72 
73 public:
77  MapContainer():MapType() {}
78  MapContainer(const MapKeyCompareType & comp):MapType(comp) {}
79  // MapContainer(const Self& r):MapType(r) {}
80  template< typename TInputIterator >
81  MapContainer(TInputIterator first, TInputIterator last):MapType(first, last) {}
82  template< typename TInputIterator >
83  MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType & comp):
84  MapType(first, last, comp) {}
86 
88  itkNewMacro(Self);
89 
91  typedef MapType STLContainerType;
92 
95  { return dynamic_cast< STLContainerType & >( *this ); }
96 
99  {
100  return dynamic_cast< const STLContainerType & >( *this );
101  }
102 
103  using STLContainerType::begin;
104  using STLContainerType::end;
105  using STLContainerType::rbegin;
106  using STLContainerType::rend;
107 
108  using STLContainerType::empty;
109  using STLContainerType::size;
110  using STLContainerType::max_size;
111 
112  using STLContainerType::operator[];
113 
114  using STLContainerType::insert;
115  using STLContainerType::erase;
117  using STLContainerType::clear;
118 
119  using STLContainerType::key_comp;
120  using STLContainerType::value_comp;
121 
122  using STLContainerType::find;
123  using STLContainerType::count;
124  using STLContainerType::lower_bound;
125  using STLContainerType::upper_bound;
126  using STLContainerType::equal_range;
127 
128  using STLContainerType::get_allocator;
129 
130  using typename STLContainerType::key_type;
131  using typename STLContainerType::mapped_type;
132  using typename STLContainerType::value_type;
133  using typename STLContainerType::key_compare;
134  using typename STLContainerType::value_compare;
135  using typename STLContainerType::allocator_type;
136  using typename STLContainerType::reference;
137  using typename STLContainerType::const_reference;
138  using typename STLContainerType::iterator;
139  using typename STLContainerType::const_iterator;
140  using typename STLContainerType::size_type;
141  using typename STLContainerType::difference_type;
142  using typename STLContainerType::pointer;
143  using typename STLContainerType::const_pointer;
144  using typename STLContainerType::reverse_iterator;
145  using typename STLContainerType::const_reverse_iterator;
146 
148  class Iterator;
150  friend class Iterator;
151  friend class ConstIterator;
152 
157  class Iterator
158  {
159 public:
160  typedef typename MapIterator::iterator_category iterator_category;
161  typedef typename MapIterator::value_type value_type;
162  typedef typename MapIterator::difference_type difference_type;
163  typedef typename MapIterator::pointer pointer;
164  typedef typename MapIterator::reference reference;
165 
166  Iterator() {}
167  Iterator(const Iterator & i):m_Iter(i.m_Iter) {}
168  Iterator(const MapIterator & i):m_Iter(i) {}
169  Iterator & operator=(const Iterator & r ) { m_Iter = r.m_Iter; return *this; }
170 
171  Iterator & operator*() { return *this; }
172  Iterator * operator->() { return this; }
173  Iterator & operator++() { ++m_Iter; return *this; }
174  Iterator operator++(int) { Iterator temp(*this); ++m_Iter; return temp; }
175  Iterator & operator--() { --m_Iter; return *this; }
176  Iterator operator--(int) { Iterator temp(*this); --m_Iter; return temp; }
177 
178  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
179  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
180  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
181  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
182 
184  ElementIdentifier Index(void) const { return m_Iter->first; }
185 
187  Element & Value(void) { return m_Iter->second; }
188 
189 private:
191  friend class ConstIterator;
192  };
193 
199  {
200 public:
201  typedef typename MapConstIterator::iterator_category iterator_category;
202  typedef typename MapConstIterator::value_type value_type;
203  typedef typename MapConstIterator::difference_type difference_type;
204  typedef typename MapConstIterator::pointer pointer;
205  typedef typename MapConstIterator::reference reference;
206 
209  ConstIterator(const Iterator & r) : m_Iter( r.m_Iter ) {}
210  ConstIterator & operator=(const ConstIterator & r ) { m_Iter = r.m_Iter; return *this; }
211 
212  ConstIterator & operator*() { return *this; }
213  ConstIterator * operator->() { return this; }
214  ConstIterator & operator++() { ++m_Iter; return *this; }
215  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Iter; return temp; }
216  ConstIterator & operator--() { --m_Iter; return *this; }
217  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
218 
219  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
220  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
221  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
222  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
223 
225  ElementIdentifier Index(void) const { return m_Iter->first; }
226 
228  const Element & Value(void) const { return m_Iter->second; }
229 
230 private:
232  friend class Iterator;
233  };
234 
235  /* Declare the public interface routines. */
236 
245 
250  const Element & ElementAt(ElementIdentifier) const;
251 
260 
266 
272 
278 
283  bool IndexExists(ElementIdentifier) const;
284 
291 
298 
304 
308  ConstIterator Begin() const;
309 
313  ConstIterator End() const;
314 
318  Iterator Begin();
319 
323  Iterator End();
324 
328  ElementIdentifier Size() const;
329 
337 
343  void Squeeze();
344 
349  void Initialize();
350 };
351 } // end namespace itk
352 
353 #ifndef ITK_MANUAL_INSTANTIATION
354 #include "itkMapContainer.hxx"
355 #endif
356 
357 #endif
bool operator==(const ConstIterator &r) const
MapConstIterator::difference_type difference_type
bool operator==(const Iterator &r) const
Light weight base class for most itk classes.
MapConstIterator::pointer pointer
MapIterator::value_type value_type
bool operator==(const ConstIterator &r) const
MapContainer(const MapKeyCompareType &comp)
MapConstIterator::iterator_category iterator_category
A wrapper of the STL &quot;map&quot; container.
const Element & Value(void) const
ConstIterator & operator=(const ConstIterator &r)
void InsertElement(ElementIdentifier, Element)
bool operator!=(const ConstIterator &r) const
bool IndexExists(ElementIdentifier) const
SmartPointer< const Self > ConstPointer
ElementIdentifier Index(void) const
bool operator==(const Iterator &r) const
MapIterator::iterator_category iterator_category
bool operator!=(const ConstIterator &r) const
Element & CreateElementAt(ElementIdentifier)
SmartPointer< Self > Pointer
Iterator(const Iterator &i)
ConstIterator End() const
MapType::iterator MapIterator
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:214
void DeleteIndex(ElementIdentifier)
The non-const iterator type for the map.
Element & ElementAt(ElementIdentifier)
const STLContainerType & CastToSTLConstContainer() const
bool operator!=(const Iterator &r) const
ElementIdentifier Index(void) const
The const iterator type for the map.
MapIterator::difference_type difference_type
void CreateIndex(ElementIdentifier)
bool GetElementIfIndexExists(ElementIdentifier, Element *) const
ConstIterator Begin() const
MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType &comp)
bool operator!=(const Iterator &r) const
MapContainer Self
MapType::key_compare MapKeyCompareType
ConstIterator(const MapConstIterator &ci)
MapContainer(TInputIterator first, TInputIterator last)
MapType::const_iterator MapConstIterator
Iterator(const MapIterator &i)
MapIterator::reference reference
Element GetElement(ElementIdentifier) const
ElementIdentifier Size() const
MapIterator::pointer pointer
void SetElement(ElementIdentifier, Element)
std::map< ElementIdentifier, Element > MapType
STLContainerType & CastToSTLContainer()
TElementIdentifier ElementIdentifier
MapConstIterator::value_type value_type
Iterator & operator=(const Iterator &r)
Base class for most ITK classes.
Definition: itkObject.h:57
void Reserve(ElementIdentifier)
friend class Iterator
MapConstIterator::reference reference