ITK  6.0.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  * https://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  itkOverrideGetNameOfClassMacro(MapContainer);
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  {}
94  itkNewMacro(Self);
95 
98 
102  {
103  return *this;
104  }
105 
107  const STLContainerType &
108  CastToSTLConstContainer() const noexcept
109  {
110  return *this;
111  }
112 
113  using STLContainerType::begin;
114  using STLContainerType::cbegin;
115  using STLContainerType::end;
116  using STLContainerType::cend;
117  using STLContainerType::rbegin;
118  using STLContainerType::crbegin;
119  using STLContainerType::rend;
120  using STLContainerType::crend;
121 
122  using STLContainerType::empty;
123  using STLContainerType::size;
124  using STLContainerType::max_size;
125 
126  using STLContainerType::operator[];
127 
128  using STLContainerType::insert;
129  using STLContainerType::erase;
131  using STLContainerType::clear;
132 
133  using STLContainerType::key_comp;
134  using STLContainerType::value_comp;
135 
136  using STLContainerType::find;
137  using STLContainerType::count;
138  using STLContainerType::lower_bound;
139  using STLContainerType::upper_bound;
140  using STLContainerType::equal_range;
141 
142  using STLContainerType::get_allocator;
143 
144  using typename STLContainerType::key_type;
145  using typename STLContainerType::mapped_type;
146  using typename STLContainerType::value_type;
147  using typename STLContainerType::key_compare;
148  using typename STLContainerType::value_compare;
149  using typename STLContainerType::allocator_type;
150  using typename STLContainerType::reference;
151  using typename STLContainerType::const_reference;
152  using typename STLContainerType::iterator;
153  using typename STLContainerType::const_iterator;
154  using typename STLContainerType::size_type;
155  using typename STLContainerType::difference_type;
156  using typename STLContainerType::pointer;
157  using typename STLContainerType::const_pointer;
158  using typename STLContainerType::reverse_iterator;
159  using typename STLContainerType::const_reverse_iterator;
160 
162  class Iterator;
164  friend class Iterator;
165  friend class ConstIterator;
166 
171  class Iterator
172  {
173  public:
174  using iterator_category = typename MapIterator::iterator_category;
175  using value_type = typename MapIterator::value_type;
176  using difference_type = typename MapIterator::difference_type;
177  using pointer = typename MapIterator::pointer;
178  using reference = typename MapIterator::reference;
179 
180  Iterator() = default;
182  : m_Iter(i)
183  {}
184 
185  Iterator &
187  {
188  return *this;
189  }
190  Iterator *
192  {
193  return this;
194  }
195  Iterator &
197  {
198  ++m_Iter;
199  return *this;
200  }
201  Iterator
203  {
204  Iterator temp(*this);
205  ++m_Iter;
206  return temp;
207  }
208  Iterator &
210  {
211  --m_Iter;
212  return *this;
213  }
214  Iterator
216  {
217  Iterator temp(*this);
218  --m_Iter;
219  return temp;
220  }
221 
222  bool
223  operator==(const Iterator & r) const
224  {
225  return m_Iter == r.m_Iter;
226  }
227 
228  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
229 
230  bool
231  operator==(const ConstIterator & r) const
232  {
233  return m_Iter == r.m_Iter;
234  }
235 
236  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
237 
240  Index() const
241  {
242  return m_Iter->first;
243  }
244 
246  Element &
248  {
249  return m_Iter->second;
250  }
251 
252  private:
254  friend class ConstIterator;
255  };
256 
262  {
263  public:
264  using iterator_category = typename MapConstIterator::iterator_category;
265  using value_type = typename MapConstIterator::value_type;
266  using difference_type = typename MapConstIterator::difference_type;
267  using pointer = typename MapConstIterator::pointer;
268  using reference = typename MapConstIterator::reference;
269 
270  ConstIterator() = default;
272  : m_Iter(ci)
273  {}
275  : m_Iter(r.m_Iter)
276  {}
277 
278  ConstIterator &
280  {
281  return *this;
282  }
283  ConstIterator *
285  {
286  return this;
287  }
288  ConstIterator &
290  {
291  ++m_Iter;
292  return *this;
293  }
296  {
297  ConstIterator temp(*this);
298  ++m_Iter;
299  return temp;
300  }
301  ConstIterator &
303  {
304  --m_Iter;
305  return *this;
306  }
309  {
310  ConstIterator temp(*this);
311  --m_Iter;
312  return temp;
313  }
314 
315  bool
316  operator==(const Iterator & r) const
317  {
318  return m_Iter == r.m_Iter;
319  }
320 
321  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
322 
323  bool
324  operator==(const ConstIterator & r) const
325  {
326  return m_Iter == r.m_Iter;
327  }
328 
329  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
330 
333  Index() const
334  {
335  return m_Iter->first;
336  }
337 
339  const Element &
340  Value() const
341  {
342  return m_Iter->second;
343  }
344 
345  private:
347  friend class Iterator;
348  };
349 
350  /* Declare the public interface routines. */
351 
359  Element & ElementAt(ElementIdentifier);
360 
365  const Element & ElementAt(ElementIdentifier) const;
366 
374  Element & CreateElementAt(ElementIdentifier);
375 
380  Element GetElement(ElementIdentifier) const;
381 
386  void SetElement(ElementIdentifier, Element);
387 
392  void InsertElement(ElementIdentifier, Element);
393 
398  bool IndexExists(ElementIdentifier) const;
399 
405  bool
406  GetElementIfIndexExists(ElementIdentifier, Element *) const;
407 
413  void CreateIndex(ElementIdentifier);
414 
419  void DeleteIndex(ElementIdentifier);
420 
425  Begin() const;
426 
431  End() const;
432 
436  Iterator
437  Begin();
438 
442  Iterator
443  End();
444 
449  Size() const;
450 
457  void Reserve(ElementIdentifier);
458 
464  void
465  Squeeze();
466 
471  void
472  Initialize();
473 };
474 } // end namespace itk
475 
476 #ifndef ITK_MANUAL_INSTANTIATION
477 # include "itkMapContainer.hxx"
478 #endif
479 
480 #endif
itk::MapContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkMapContainer.h:302
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const MapConstIterator &ci)
Definition: itkMapContainer.h:271
itk::MapContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:316
itkObjectFactory.h
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:289
itk::MapContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkMapContainer.h:202
itk::MapContainer::MapType
std::map< ElementIdentifier, Element > MapType
Definition: itkMapContainer.h:67
itk::MapContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkMapContainer.h:295
itk::MapContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkMapContainer.h:101
itk::MapContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkMapContainer.h:284
itk::MapContainer::Iterator::Value
Element & Value()
Definition: itkMapContainer.h:247
itk::MapContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkMapContainer.h:279
itk::MapContainer::Iterator
The non-const iterator type for the map.
Definition: itkMapContainer.h:171
itk::MapContainer::ConstIterator
The const iterator type for the map.
Definition: itkMapContainer.h:261
itk::SmartPointer< Self >
itk::MapContainer::Iterator::operator->
Iterator * operator->()
Definition: itkMapContainer.h:191
itk::MapContainer::MapContainer
MapContainer(TInputIterator first, TInputIterator last)
Definition: itkMapContainer.h:84
itk::MapContainer::Iterator::m_Iter
MapIterator m_Iter
Definition: itkMapContainer.h:253
itk::MapContainer::ConstIterator::iterator_category
typename MapConstIterator::iterator_category iterator_category
Definition: itkMapContainer.h:264
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:215
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::MapContainer::Iterator::reference
typename MapIterator::reference reference
Definition: itkMapContainer.h:178
itk::MapContainer::MapKeyCompareType
typename MapType::key_compare MapKeyCompareType
Definition: itkMapContainer.h:70
itk::MapContainer::Iterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:240
itk::MapContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:231
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:340
itk::MapContainer::Iterator::difference_type
typename MapIterator::difference_type difference_type
Definition: itkMapContainer.h:176
itkObject.h
itk::MapContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:324
itk::MapContainer::Iterator::operator--
Iterator & operator--()
Definition: itkMapContainer.h:209
itk::MapContainer::Iterator::operator*
Iterator & operator*()
Definition: itkMapContainer.h:186
itk::MapContainer::ConstIterator::m_Iter
MapConstIterator m_Iter
Definition: itkMapContainer.h:346
itk::swap
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itk::MapContainer::Iterator::value_type
typename MapIterator::value_type value_type
Definition: itkMapContainer.h:175
itk::MapContainer::ConstIterator::pointer
typename MapConstIterator::pointer pointer
Definition: itkMapContainer.h:267
itk::MapContainer::Iterator::pointer
typename MapIterator::pointer pointer
Definition: itkMapContainer.h:177
itk::MapContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:333
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
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:265
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::MapContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkMapContainer.h:308
itk::MapContainer::ConstIterator::difference_type
typename MapConstIterator::difference_type difference_type
Definition: itkMapContainer.h:266
itk::MapContainer::Iterator::Iterator
Iterator(const MapIterator &i)
Definition: itkMapContainer.h:181
itk::MapContainer::Iterator::iterator_category
typename MapIterator::iterator_category iterator_category
Definition: itkMapContainer.h:174
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const Iterator &r)
Definition: itkMapContainer.h:274
itk::MapContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkMapContainer.h:62
itk::MapContainer::Iterator::operator++
Iterator & operator++()
Definition: itkMapContainer.h:196
itk::MapContainer::ConstIterator::reference
typename MapConstIterator::reference reference
Definition: itkMapContainer.h:268
itk::MapContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:223