ITK  4.3.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 &); //purposely not implemented
65  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 
73 public:
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 
181 private:
183  friend class ConstIterator;
184  };
185 
191  {
192 public:
195  ConstIterator(const Iterator & r) { m_Iter = r.m_Iter; }
196 
197  ConstIterator & operator*() { return *this; }
198  ConstIterator * operator->() { return this; }
199  ConstIterator & operator++() { ++m_Iter; return *this; }
200  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Iter; return temp; }
201  ConstIterator & operator--() { --m_Iter; return *this; }
202  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
203 
204  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
205  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
206  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
207  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
208 
210  ElementIdentifier Index(void) const { return m_Iter->first; }
211 
213  const Element & Value(void) const { return m_Iter->second; }
214 
215 private:
217  friend class Iterator;
218  };
219 
220  /* Declare the public interface routines. */
221 
230 
235  const Element & ElementAt(ElementIdentifier) const;
236 
245 
251 
257 
263 
268  bool IndexExists(ElementIdentifier) const;
269 
276 
283 
289 
293  ConstIterator Begin(void) const;
294 
298  ConstIterator End(void) const;
299 
303  Iterator Begin(void);
304 
308  Iterator End(void);
309 
313  ElementIdentifier Size(void) const;
314 
322 
328  void Squeeze(void);
329 
334  void Initialize(void);
335 };
336 } // end namespace itk
337 
338 #ifndef ITK_MANUAL_INSTANTIATION
339 #include "itkMapContainer.hxx"
340 #endif
341 
342 #endif
343