ITK  5.4.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 & operator*() { return *this; }
186  Iterator * operator->() { return this; }
187  Iterator &
189  {
190  ++m_Iter;
191  return *this;
192  }
193  Iterator
195  {
196  Iterator temp(*this);
197  ++m_Iter;
198  return temp;
199  }
200  Iterator &
202  {
203  --m_Iter;
204  return *this;
205  }
206  Iterator
208  {
209  Iterator temp(*this);
210  --m_Iter;
211  return temp;
212  }
213 
214  bool
215  operator==(const Iterator & r) const
216  {
217  return m_Iter == r.m_Iter;
218  }
219 
220  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
221 
222  bool
223  operator==(const ConstIterator & r) const
224  {
225  return m_Iter == r.m_Iter;
226  }
227 
228  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
229 
232  Index() const
233  {
234  return m_Iter->first;
235  }
236 
238  Element &
240  {
241  return m_Iter->second;
242  }
243 
244  private:
246  friend class ConstIterator;
247  };
248 
254  {
255  public:
256  using iterator_category = typename MapConstIterator::iterator_category;
257  using value_type = typename MapConstIterator::value_type;
258  using difference_type = typename MapConstIterator::difference_type;
259  using pointer = typename MapConstIterator::pointer;
260  using reference = typename MapConstIterator::reference;
261 
262  ConstIterator() = default;
264  : m_Iter(ci)
265  {}
267  : m_Iter(r.m_Iter)
268  {}
269 
270  ConstIterator & operator*() { return *this; }
271  ConstIterator * operator->() { return this; }
272  ConstIterator &
274  {
275  ++m_Iter;
276  return *this;
277  }
280  {
281  ConstIterator temp(*this);
282  ++m_Iter;
283  return temp;
284  }
285  ConstIterator &
287  {
288  --m_Iter;
289  return *this;
290  }
293  {
294  ConstIterator temp(*this);
295  --m_Iter;
296  return temp;
297  }
298 
299  bool
300  operator==(const Iterator & r) const
301  {
302  return m_Iter == r.m_Iter;
303  }
304 
305  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
306 
307  bool
308  operator==(const ConstIterator & r) const
309  {
310  return m_Iter == r.m_Iter;
311  }
312 
313  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
314 
317  Index() const
318  {
319  return m_Iter->first;
320  }
321 
323  const Element &
324  Value() const
325  {
326  return m_Iter->second;
327  }
328 
329  private:
331  friend class Iterator;
332  };
333 
334  /* Declare the public interface routines. */
335 
343  Element & ElementAt(ElementIdentifier);
344 
349  const Element & ElementAt(ElementIdentifier) const;
350 
358  Element & CreateElementAt(ElementIdentifier);
359 
364  Element GetElement(ElementIdentifier) const;
365 
370  void SetElement(ElementIdentifier, Element);
371 
376  void InsertElement(ElementIdentifier, Element);
377 
382  bool IndexExists(ElementIdentifier) const;
383 
389  bool
390  GetElementIfIndexExists(ElementIdentifier, Element *) const;
391 
397  void CreateIndex(ElementIdentifier);
398 
403  void DeleteIndex(ElementIdentifier);
404 
409  Begin() const;
410 
415  End() const;
416 
420  Iterator
421  Begin();
422 
426  Iterator
427  End();
428 
433  Size() const;
434 
441  void Reserve(ElementIdentifier);
442 
448  void
449  Squeeze();
450 
455  void
456  Initialize();
457 };
458 } // end namespace itk
459 
460 #ifndef ITK_MANUAL_INSTANTIATION
461 # include "itkMapContainer.hxx"
462 #endif
463 
464 #endif
itk::MapContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkMapContainer.h:286
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const MapConstIterator &ci)
Definition: itkMapContainer.h:263
itk::MapContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:300
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:71
itk::MapContainer::MapIterator
typename MapType::iterator MapIterator
Definition: itkMapContainer.h:68
itk::MapContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkMapContainer.h:273
itk::MapContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkMapContainer.h:194
itk::MapContainer::MapType
std::map< ElementIdentifier, Element > MapType
Definition: itkMapContainer.h:67
itk::MapContainer::ConstIterator::operator++
ConstIterator operator++(int)
Definition: itkMapContainer.h:279
itk::MapContainer::CastToSTLContainer
STLContainerType & CastToSTLContainer() noexcept
Definition: itkMapContainer.h:101
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:242
itk::MapContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkMapContainer.h:271
itk::MapContainer::Iterator::Value
Element & Value()
Definition: itkMapContainer.h:239
itk::MapContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkMapContainer.h:270
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:253
itk::SmartPointer< Self >
itk::MapContainer::Iterator::operator->
Iterator * operator->()
Definition: itkMapContainer.h:186
itk::MapContainer::MapContainer
MapContainer(TInputIterator first, TInputIterator last)
Definition: itkMapContainer.h:84
itk::MapContainer::Iterator::m_Iter
MapIterator m_Iter
Definition: itkMapContainer.h:245
itk::MapContainer::ConstIterator::iterator_category
typename MapConstIterator::iterator_category iterator_category
Definition: itkMapContainer.h:256
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:207
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:232
itk::MapContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:223
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:324
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:308
itk::MapContainer::Iterator::operator--
Iterator & operator--()
Definition: itkMapContainer.h:201
itk::MapContainer::Iterator::operator*
Iterator & operator*()
Definition: itkMapContainer.h:185
itk::MapContainer::ConstIterator::m_Iter
MapConstIterator m_Iter
Definition: itkMapContainer.h:330
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:175
itk::MapContainer::ConstIterator::pointer
typename MapConstIterator::pointer pointer
Definition: itkMapContainer.h:259
itk::MapContainer::Iterator::pointer
typename MapIterator::pointer pointer
Definition: itkMapContainer.h:177
itk::MapContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:317
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:257
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::MapContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkMapContainer.h:292
itk::MapContainer::ConstIterator::difference_type
typename MapConstIterator::difference_type difference_type
Definition: itkMapContainer.h:258
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:266
itk::MapContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkMapContainer.h:62
itk::MapContainer::Iterator::operator++
Iterator & operator++()
Definition: itkMapContainer.h:188
itk::MapContainer::ConstIterator::reference
typename MapConstIterator::reference reference
Definition: itkMapContainer.h:260
itk::MapContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:215