ITK  5.3.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 
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::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 
216  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
217 
218  bool
219  operator==(const ConstIterator & r) const
220  {
221  return m_Iter == r.m_Iter;
222  }
223 
224  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
225 
228  Index() const
229  {
230  return m_Iter->first;
231  }
232 
234  Element &
236  {
237  return m_Iter->second;
238  }
239 
240  private:
242  friend class ConstIterator;
243  };
244 
250  {
251  public:
252  using iterator_category = typename MapConstIterator::iterator_category;
253  using value_type = typename MapConstIterator::value_type;
254  using difference_type = typename MapConstIterator::difference_type;
255  using pointer = typename MapConstIterator::pointer;
256  using reference = typename MapConstIterator::reference;
257 
258  ConstIterator() = default;
260  : m_Iter(ci)
261  {}
263  : m_Iter(r.m_Iter)
264  {}
265 
266  ConstIterator & operator*() { return *this; }
267  ConstIterator * operator->() { return this; }
268  ConstIterator &
270  {
271  ++m_Iter;
272  return *this;
273  }
276  {
277  ConstIterator temp(*this);
278  ++m_Iter;
279  return temp;
280  }
281  ConstIterator &
283  {
284  --m_Iter;
285  return *this;
286  }
289  {
290  ConstIterator temp(*this);
291  --m_Iter;
292  return temp;
293  }
294 
295  bool
296  operator==(const Iterator & r) const
297  {
298  return m_Iter == r.m_Iter;
299  }
300 
301  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator);
302 
303  bool
304  operator==(const ConstIterator & r) const
305  {
306  return m_Iter == r.m_Iter;
307  }
308 
309  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
310 
313  Index() const
314  {
315  return m_Iter->first;
316  }
317 
319  const Element &
320  Value() const
321  {
322  return m_Iter->second;
323  }
324 
325  private:
327  friend class Iterator;
328  };
329 
330  /* Declare the public interface routines. */
331 
339  Element & ElementAt(ElementIdentifier);
340 
345  const Element & ElementAt(ElementIdentifier) const;
346 
354  Element & CreateElementAt(ElementIdentifier);
355 
360  Element GetElement(ElementIdentifier) const;
361 
366  void SetElement(ElementIdentifier, Element);
367 
372  void InsertElement(ElementIdentifier, Element);
373 
378  bool IndexExists(ElementIdentifier) const;
379 
385  bool
386  GetElementIfIndexExists(ElementIdentifier, Element *) const;
387 
393  void CreateIndex(ElementIdentifier);
394 
399  void DeleteIndex(ElementIdentifier);
400 
405  Begin() const;
406 
411  End() const;
412 
416  Iterator
417  Begin();
418 
422  Iterator
423  End();
424 
429  Size() const;
430 
437  void Reserve(ElementIdentifier);
438 
444  void
445  Squeeze();
446 
451  void
452  Initialize();
453 };
454 } // end namespace itk
455 
456 #ifndef ITK_MANUAL_INSTANTIATION
457 # include "itkMapContainer.hxx"
458 #endif
459 
460 #endif
itk::MapContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkMapContainer.h:282
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const MapConstIterator &ci)
Definition: itkMapContainer.h:259
itk::MapContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:296
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:269
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:275
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:267
itk::MapContainer::Iterator::Value
Element & Value()
Definition: itkMapContainer.h:235
itk::MapContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkMapContainer.h:266
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:249
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:241
itk::MapContainer::ConstIterator::iterator_category
typename MapConstIterator::iterator_category iterator_category
Definition: itkMapContainer.h:252
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::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:228
itk::MapContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:219
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:320
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:304
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:326
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:255
itk::MapContainer::Iterator::pointer
typename MapIterator::pointer pointer
Definition: itkMapContainer.h:173
itk::MapContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:313
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:253
itk::MapContainer::ConstIterator::operator--
ConstIterator operator--(int)
Definition: itkMapContainer.h:288
itk::MapContainer::ConstIterator::difference_type
typename MapConstIterator::difference_type difference_type
Definition: itkMapContainer.h:254
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:262
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:256
itk::MapContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:211