ITK  4.13.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 >
45 class ITK_TEMPLATE_EXPORT MapContainer:
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  ITK_DISALLOW_COPY_AND_ASSIGN(MapContainer);
65 
67  typedef std::map< ElementIdentifier, Element > MapType;
68  typedef typename MapType::iterator MapIterator;
69  typedef typename MapType::const_iterator MapConstIterator;
70  typedef typename MapType::key_compare MapKeyCompareType;
71 
72 public:
77  MapContainer(const MapKeyCompareType & comp):MapType(comp) {}
78  // MapContainer(const Self& r):MapType(r) {}
79  template< typename TInputIterator >
80  MapContainer(TInputIterator first, TInputIterator last):MapType(first, last) {}
81  template< typename TInputIterator >
82  MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType & comp):
83  MapType(first, last, comp) {}
85 
87  itkNewMacro(Self);
88 
90  typedef MapType STLContainerType;
91 
94  { return dynamic_cast< STLContainerType & >( *this ); }
95 
98  {
99  return dynamic_cast< const STLContainerType & >( *this );
100  }
101 
102  using STLContainerType::begin;
103  using STLContainerType::end;
104  using STLContainerType::rbegin;
105  using STLContainerType::rend;
106 
107  using STLContainerType::empty;
108  using STLContainerType::size;
109  using STLContainerType::max_size;
110 
111  using STLContainerType::operator[];
112 
113  using STLContainerType::insert;
114  using STLContainerType::erase;
116  using STLContainerType::clear;
117 
118  using STLContainerType::key_comp;
119  using STLContainerType::value_comp;
120 
121  using STLContainerType::find;
122  using STLContainerType::count;
123  using STLContainerType::lower_bound;
124  using STLContainerType::upper_bound;
125  using STLContainerType::equal_range;
126 
127  using STLContainerType::get_allocator;
128 
129  using typename STLContainerType::key_type;
130  using typename STLContainerType::mapped_type;
131  using typename STLContainerType::value_type;
132  using typename STLContainerType::key_compare;
133  using typename STLContainerType::value_compare;
134  using typename STLContainerType::allocator_type;
135  using typename STLContainerType::reference;
136  using typename STLContainerType::const_reference;
137  using typename STLContainerType::iterator;
138  using typename STLContainerType::const_iterator;
139  using typename STLContainerType::size_type;
140  using typename STLContainerType::difference_type;
141  using typename STLContainerType::pointer;
142  using typename STLContainerType::const_pointer;
143  using typename STLContainerType::reverse_iterator;
144  using typename STLContainerType::const_reverse_iterator;
145 
147  class Iterator;
149  friend class Iterator;
150  friend class ConstIterator;
151 
156  class Iterator
157  {
158 public:
159  typedef typename MapIterator::iterator_category iterator_category;
160  typedef typename MapIterator::value_type value_type;
161  typedef typename MapIterator::difference_type difference_type;
162  typedef typename MapIterator::pointer pointer;
163  typedef typename MapIterator::reference reference;
164 
165  Iterator() {}
166  Iterator(const Iterator & i):m_Iter(i.m_Iter) {}
167  Iterator(const MapIterator & i):m_Iter(i) {}
168  Iterator & operator=(const Iterator & r ) { m_Iter = r.m_Iter; return *this; }
169 
170  Iterator & operator*() { return *this; }
171  Iterator * operator->() { return this; }
172  Iterator & operator++() { ++m_Iter; return *this; }
173  Iterator operator++(int) { Iterator temp(*this); ++m_Iter; return temp; }
174  Iterator & operator--() { --m_Iter; return *this; }
175  Iterator operator--(int) { Iterator temp(*this); --m_Iter; return temp; }
176 
177  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
178  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
179  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
180  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
181 
183  ElementIdentifier Index(void) const { return m_Iter->first; }
184 
186  Element & Value(void) { return m_Iter->second; }
187 
188 private:
190  friend class ConstIterator;
191  };
192 
198  {
199 public:
200  typedef typename MapConstIterator::iterator_category iterator_category;
201  typedef typename MapConstIterator::value_type value_type;
202  typedef typename MapConstIterator::difference_type difference_type;
203  typedef typename MapConstIterator::pointer pointer;
204  typedef typename MapConstIterator::reference reference;
205 
207  ConstIterator(const MapConstIterator & ci):m_Iter(ci) {}
208  ConstIterator(const Iterator & r) : m_Iter( r.m_Iter ) {}
209  ConstIterator & operator=(const ConstIterator & r ) { m_Iter = r.m_Iter; return *this; }
210 
211  ConstIterator & operator*() { return *this; }
212  ConstIterator * operator->() { return this; }
213  ConstIterator & operator++() { ++m_Iter; return *this; }
214  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Iter; return temp; }
215  ConstIterator & operator--() { --m_Iter; return *this; }
216  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
217 
218  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
219  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
220  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
221  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
222 
224  ElementIdentifier Index(void) const { return m_Iter->first; }
225 
227  const Element & Value(void) const { return m_Iter->second; }
228 
229 private:
231  friend class Iterator;
232  };
233 
234  /* Declare the public interface routines. */
235 
243  Element & ElementAt(ElementIdentifier);
244 
249  const Element & ElementAt(ElementIdentifier) const;
250 
258  Element & CreateElementAt(ElementIdentifier);
259 
264  Element GetElement(ElementIdentifier) const;
265 
270  void SetElement(ElementIdentifier, Element);
271 
276  void InsertElement(ElementIdentifier, Element);
277 
282  bool IndexExists(ElementIdentifier) const;
283 
289  bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
290 
296  void CreateIndex(ElementIdentifier);
297 
302  void DeleteIndex(ElementIdentifier);
303 
307  ConstIterator Begin() const;
308 
312  ConstIterator End() const;
313 
317  Iterator Begin();
318 
322  Iterator End();
323 
327  ElementIdentifier Size() const;
328 
335  void Reserve(ElementIdentifier);
336 
342  void Squeeze();
343 
348  void Initialize();
349 };
350 } // end namespace itk
351 
352 #ifndef ITK_MANUAL_INSTANTIATION
353 #include "itkMapContainer.hxx"
354 #endif
355 
356 #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
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
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)
bool operator!=(const ConstIterator &r) 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
SmartPointer< Self > Pointer
Iterator(const Iterator &i)
MapType::iterator MapIterator
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:215
The non-const iterator type for the map.
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
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
MapIterator::pointer pointer
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:59
MapConstIterator::reference reference