ITK  4.2.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 private:
63  MapContainer(const Self &); //purposely not implemented
64  void operator=(const Self &); //purposely not implemented
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 public:
73 
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;
116  using STLContainerType::swap;
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  Iterator() {}
161  Iterator(const MapIterator & i):m_Iter(i) {}
162 
163  Iterator & operator*() { return *this; }
164  Iterator * operator->() { return this; }
165  Iterator & operator++() { ++m_Iter; return *this; }
166  Iterator operator++(int) { Iterator temp(*this); ++m_Iter; return temp; }
167  Iterator & operator--() { --m_Iter; return *this; }
168  Iterator operator--(int) { Iterator temp(*this); --m_Iter; return temp; }
169 
170  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
171  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
172  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
173  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
174 
176  ElementIdentifier Index(void) const { return m_Iter->first; }
177 
179  Element & Value(void) { return m_Iter->second; }
180 private:
182  friend class ConstIterator;
183  };
184 
190  {
191 public:
194  ConstIterator(const Iterator & r) { m_Iter = r.m_Iter; }
195 
196  ConstIterator & operator*() { return *this; }
197  ConstIterator * operator->() { return this; }
198  ConstIterator & operator++() { ++m_Iter; return *this; }
199  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Iter; return temp; }
200  ConstIterator & operator--() { --m_Iter; return *this; }
201  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
202 
203  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
204  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
205  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
206  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
207 
209  ElementIdentifier Index(void) const { return m_Iter->first; }
210 
212  const Element & Value(void) const { return m_Iter->second; }
213 private:
215  friend class Iterator;
216  };
217 
218  /* Declare the public interface routines. */
219 
228 
233  const Element & ElementAt(ElementIdentifier) const;
234 
243 
249 
255 
261 
266  bool IndexExists(ElementIdentifier) const;
267 
274 
281 
287 
291  ConstIterator Begin(void) const;
292 
296  ConstIterator End(void) const;
297 
301  Iterator Begin(void);
302 
306  Iterator End(void);
307 
311  ElementIdentifier Size(void) const;
312 
320 
326  void Squeeze(void);
327 
332  void Initialize(void);
333 };
334 } // end namespace itk
335 
336 // Define instantiation macro for this template.
337 #define ITK_TEMPLATE_MapContainer(_, EXPORT, TypeX, TypeY) \
338  namespace itk \
339  { \
340  _( 2 ( class EXPORT MapContainer< ITK_TEMPLATE_2 TypeX > ) ) \
341  namespace Templates \
342  { \
343  typedef MapContainer< ITK_TEMPLATE_2 TypeX > \
344  MapContainer##TypeY; \
345  } \
346  }
347 
348 #if ITK_TEMPLATE_EXPLICIT
349 #include "Templates/itkMapContainer+-.h"
350 #endif
351 
352 #if ITK_TEMPLATE_TXX
353 #include "itkMapContainer.hxx"
354 #endif
355 
356 #endif
357