ITK  5.2.0
Insight Toolkit
itkMapContainer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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:
50  ITK_DISALLOW_COPY_AND_MOVE(MapContainer);
51 
53  using Self = MapContainer;
54  using Superclass = Object;
57 
59  itkTypeMacro(MapContainer, Object);
60 
62  using ElementIdentifier = TElementIdentifier;
63  using Element = TElement;
64 
65 private:
67  using MapType = std::map<ElementIdentifier, Element>;
68  using MapIterator = typename MapType::iterator;
69  using MapConstIterator = typename MapType::const_iterator;
70  using MapKeyCompareType = typename MapType::key_compare;
71 
72 public:
77  : MapType()
78  {}
80  : MapType(comp)
81  {}
82  // MapContainer(const Self& r):MapType(r) {}
83  template <typename TInputIterator>
84  MapContainer(TInputIterator first, TInputIterator last)
85  : MapType(first, last)
86  {}
87  template <typename TInputIterator>
88  MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType & comp)
89  : MapType(first, last, comp)
90  {}
92 
94  itkNewMacro(Self);
95 
98 
101  CastToSTLContainer() ITK_NOEXCEPT
102  {
103  return *this;
104  }
105 
107  const STLContainerType &
108  CastToSTLConstContainer() const ITK_NOEXCEPT
109  {
110  return *this;
111  }
112 
113  using STLContainerType::begin;
114  using STLContainerType::end;
115  using STLContainerType::rbegin;
116  using STLContainerType::rend;
117 
118  using STLContainerType::empty;
119  using STLContainerType::size;
120  using STLContainerType::max_size;
121 
122  using STLContainerType::operator[];
123 
124  using STLContainerType::insert;
125  using STLContainerType::erase;
127  using STLContainerType::clear;
128 
129  using STLContainerType::key_comp;
130  using STLContainerType::value_comp;
131 
132  using STLContainerType::find;
133  using STLContainerType::count;
134  using STLContainerType::lower_bound;
135  using STLContainerType::upper_bound;
136  using STLContainerType::equal_range;
137 
138  using STLContainerType::get_allocator;
139 
140  using typename STLContainerType::key_type;
141  using typename STLContainerType::mapped_type;
142  using typename STLContainerType::value_type;
143  using typename STLContainerType::key_compare;
144  using typename STLContainerType::value_compare;
145  using typename STLContainerType::allocator_type;
146  using typename STLContainerType::reference;
147  using typename STLContainerType::const_reference;
148  using typename STLContainerType::iterator;
149  using typename STLContainerType::const_iterator;
150  using typename STLContainerType::size_type;
151  using typename STLContainerType::difference_type;
152  using typename STLContainerType::pointer;
153  using typename STLContainerType::const_pointer;
154  using typename STLContainerType::reverse_iterator;
155  using typename STLContainerType::const_reverse_iterator;
156 
158  class Iterator;
160  friend class Iterator;
161  friend class ConstIterator;
162 
167  class Iterator
168  {
169  public:
170  using iterator_category = typename MapIterator::iterator_category;
171  using value_type = typename MapIterator::value_type;
172  using difference_type = typename MapIterator::difference_type;
173  using pointer = typename MapIterator::pointer;
174  using reference = typename MapIterator::reference;
175 
176  Iterator() = default;
178  : m_Iter(i)
179  {}
180 
181  Iterator & operator*() { return *this; }
182  Iterator * operator->() { return this; }
183  Iterator &
185  {
186  ++m_Iter;
187  return *this;
188  }
189  Iterator
191  {
192  Iterator temp(*this);
193  ++m_Iter;
194  return temp;
195  }
196  Iterator &
198  {
199  --m_Iter;
200  return *this;
201  }
202  Iterator
204  {
205  Iterator temp(*this);
206  --m_Iter;
207  return temp;
208  }
209 
210  bool
211  operator==(const Iterator & r) const
212  {
213  return m_Iter == r.m_Iter;
214  }
215  bool
216  operator!=(const Iterator & r) const
217  {
218  return m_Iter != r.m_Iter;
219  }
220  bool
221  operator==(const ConstIterator & r) const
222  {
223  return m_Iter == r.m_Iter;
224  }
225  bool
226  operator!=(const ConstIterator & r) const
227  {
228  return m_Iter != r.m_Iter;
229  }
230 
233  Index() const
234  {
235  return m_Iter->first;
236  }
237 
239  Element &
241  {
242  return m_Iter->second;
243  }
244 
245  private:
247  friend class ConstIterator;
248  };
249 
255  {
256  public:
257  using iterator_category = typename MapConstIterator::iterator_category;
258  using value_type = typename MapConstIterator::value_type;
259  using difference_type = typename MapConstIterator::difference_type;
260  using pointer = typename MapConstIterator::pointer;
261  using reference = typename MapConstIterator::reference;
262 
263  ConstIterator() = default;
265  : m_Iter(ci)
266  {}
268  : m_Iter(r.m_Iter)
269  {}
270 
271  ConstIterator & operator*() { return *this; }
272  ConstIterator * operator->() { return this; }
273  ConstIterator &
275  {
276  ++m_Iter;
277  return *this;
278  }
281  {
282  ConstIterator temp(*this);
283  ++m_Iter;
284  return temp;
285  }
286  ConstIterator &
288  {
289  --m_Iter;
290  return *this;
291  }
294  {
295  ConstIterator temp(*this);
296  --m_Iter;
297  return temp;
298  }
299 
300  bool
301  operator==(const Iterator & r) const
302  {
303  return m_Iter == r.m_Iter;
304  }
305  bool
306  operator!=(const Iterator & r) const
307  {
308  return m_Iter != r.m_Iter;
309  }
310  bool
311  operator==(const ConstIterator & r) const
312  {
313  return m_Iter == r.m_Iter;
314  }
315  bool
316  operator!=(const ConstIterator & r) const
317  {
318  return m_Iter != r.m_Iter;
319  }
320 
323  Index() const
324  {
325  return m_Iter->first;
326  }
327 
329  const Element &
330  Value() const
331  {
332  return m_Iter->second;
333  }
334 
335  private:
337  friend class Iterator;
338  };
339 
340  /* Declare the public interface routines. */
341 
349  Element & ElementAt(ElementIdentifier);
350 
355  const Element & ElementAt(ElementIdentifier) const;
356 
364  Element & CreateElementAt(ElementIdentifier);
365 
370  Element GetElement(ElementIdentifier) const;
371 
376  void SetElement(ElementIdentifier, Element);
377 
382  void InsertElement(ElementIdentifier, Element);
383 
388  bool IndexExists(ElementIdentifier) const;
389 
395  bool
396  GetElementIfIndexExists(ElementIdentifier, Element *) const;
397 
403  void CreateIndex(ElementIdentifier);
404 
409  void DeleteIndex(ElementIdentifier);
410 
415  Begin() const;
416 
421  End() const;
422 
426  Iterator
427  Begin();
428 
432  Iterator
433  End();
434 
439  Size() const;
440 
447  void Reserve(ElementIdentifier);
448 
454  void
455  Squeeze();
456 
461  void
462  Initialize();
463 };
464 } // end namespace itk
465 
466 #ifndef ITK_MANUAL_INSTANTIATION
467 # include "itkMapContainer.hxx"
468 #endif
469 
470 #endif
itk::MapContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkMapContainer.h:287
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const MapConstIterator &ci)
Definition: itkMapContainer.h:264
itk::MapContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:301
itkObjectFactory.h
itk::MapContainer::Iterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkMapContainer.h:216
itk::MapContainer::CastToSTLConstContainer
const STLContainerType & CastToSTLConstContainer() const noexcept
Definition: itkMapContainer.h:108
itk::MapContainer::MapConstIterator
typename MapType::const_iterator MapConstIterator
Definition: itkMapContainer.h:69
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::MapContainer::MapIterator
typename MapType::iterator MapIterator
Definition: itkMapContainer.h:68
itk::MapContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkMapContainer.h:274
itk::MapContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkMapContainer.h:190
itk::MapContainer::MapType
std::map< ElementIdentifier, Element > MapType
Definition: itkMapContainer.h:67
itk::MapContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkMapContainer.h:280
itk::MapContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkMapContainer.h:101
itk::MapContainer::ConstIterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkMapContainer.h:306
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:244
itk::MapContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkMapContainer.h:272
itk::MapContainer::Iterator::Value
Element & Value()
Definition: itkMapContainer.h:240
itk::MapContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkMapContainer.h:271
itk::MapContainer::Iterator
The non-const iterator type for the map.
Definition: itkMapContainer.h:167
itk::MapContainer::ConstIterator
The const iterator type for the map.
Definition: itkMapContainer.h:254
itk::SmartPointer< Self >
itk::MapContainer::Iterator::operator->
Iterator * operator->()
Definition: itkMapContainer.h:182
itk::MapContainer::MapContainer
MapContainer(TInputIterator first, TInputIterator last)
Definition: itkMapContainer.h:84
itk::MapContainer::Iterator::m_Iter
MapIterator m_Iter
Definition: itkMapContainer.h:246
itk::MapContainer::ConstIterator::iterator_category
typename MapConstIterator::iterator_category iterator_category
Definition: itkMapContainer.h:257
itk::MapContainer
A wrapper of the STL "map" container.
Definition: itkMapContainer.h:45
itk::MapContainer::Element
TElement Element
Definition: itkMapContainer.h:63
itk::MapContainer::Iterator::operator--
Iterator operator--(int)
Definition: itkMapContainer.h:203
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::MapContainer::ConstIterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkMapContainer.h:316
itk::MapContainer::Iterator::reference
typename MapIterator::reference reference
Definition: itkMapContainer.h:174
itk::MapContainer::MapKeyCompareType
typename MapType::key_compare MapKeyCompareType
Definition: itkMapContainer.h:70
itk::MapContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:233
itk::MapContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:221
itk::MapContainer::MapContainer
MapContainer(const MapKeyCompareType &comp)
Definition: itkMapContainer.h:79
itk::MapContainer::MapContainer
MapContainer()
Definition: itkMapContainer.h:76
itk::MapContainer::STLContainerType
MapType STLContainerType
Definition: itkMapContainer.h:97
itk::MapContainer::ConstIterator::Value
const Element & Value() const
Definition: itkMapContainer.h:330
itk::MapContainer::Iterator::difference_type
typename MapIterator::difference_type difference_type
Definition: itkMapContainer.h:172
itkObject.h
itk::MapContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:311
itk::MapContainer::Iterator::operator--
Iterator & operator--()
Definition: itkMapContainer.h:197
itk::MapContainer::Iterator::operator*
Iterator & operator*()
Definition: itkMapContainer.h:181
itk::MapContainer::ConstIterator::m_Iter
MapConstIterator m_Iter
Definition: itkMapContainer.h:336
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::MapContainer::Iterator::value_type
typename MapIterator::value_type value_type
Definition: itkMapContainer.h:171
itk::MapContainer::ConstIterator::pointer
typename MapConstIterator::pointer pointer
Definition: itkMapContainer.h:260
itk::MapContainer::Iterator::pointer
typename MapIterator::pointer pointer
Definition: itkMapContainer.h:173
itk::MapContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:323
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::MapContainer::MapContainer
MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType &comp)
Definition: itkMapContainer.h:88
itk::MapContainer::ConstIterator::value_type
typename MapConstIterator::value_type value_type
Definition: itkMapContainer.h:258
itk::MapContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkMapContainer.h:293
itk::MapContainer::ConstIterator::difference_type
typename MapConstIterator::difference_type difference_type
Definition: itkMapContainer.h:259
itk::MapContainer::Iterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkMapContainer.h:226
itk::MapContainer::Iterator::Iterator
Iterator(const MapIterator &i)
Definition: itkMapContainer.h:177
itk::MapContainer::Iterator::iterator_category
typename MapIterator::iterator_category iterator_category
Definition: itkMapContainer.h:170
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkMapContainer.h:267
itk::MapContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkMapContainer.h:62
itk::MapContainer::Iterator::operator++
Iterator & operator++()
Definition: itkMapContainer.h:184
itk::MapContainer::ConstIterator::reference
typename MapConstIterator::reference reference
Definition: itkMapContainer.h:261
itk::MapContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:211