ITK  5.1.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_ASSIGN(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;
177  Iterator(const Iterator & i)
178  : m_Iter(i.m_Iter)
179  {}
181  : m_Iter(i)
182  {}
183  Iterator &
184  operator=(const Iterator & r)
185  {
186  m_Iter = r.m_Iter;
187  return *this;
188  }
189 
190  Iterator & operator*() { return *this; }
191  Iterator * operator->() { return this; }
192  Iterator &
194  {
195  ++m_Iter;
196  return *this;
197  }
198  Iterator
200  {
201  Iterator temp(*this);
202  ++m_Iter;
203  return temp;
204  }
205  Iterator &
207  {
208  --m_Iter;
209  return *this;
210  }
211  Iterator
213  {
214  Iterator temp(*this);
215  --m_Iter;
216  return temp;
217  }
218 
219  bool
220  operator==(const Iterator & r) const
221  {
222  return m_Iter == r.m_Iter;
223  }
224  bool
225  operator!=(const Iterator & r) const
226  {
227  return m_Iter != r.m_Iter;
228  }
229  bool
230  operator==(const ConstIterator & r) const
231  {
232  return m_Iter == r.m_Iter;
233  }
234  bool
235  operator!=(const ConstIterator & r) const
236  {
237  return m_Iter != r.m_Iter;
238  }
239 
242  Index() const
243  {
244  return m_Iter->first;
245  }
246 
248  Element &
250  {
251  return m_Iter->second;
252  }
253 
254  private:
256  friend class ConstIterator;
257  };
258 
264  {
265  public:
266  using iterator_category = typename MapConstIterator::iterator_category;
267  using value_type = typename MapConstIterator::value_type;
268  using difference_type = typename MapConstIterator::difference_type;
269  using pointer = typename MapConstIterator::pointer;
270  using reference = typename MapConstIterator::reference;
271 
272  ConstIterator() = default;
274  : m_Iter(ci)
275  {}
277  : m_Iter(r.m_Iter)
278  {}
279  ConstIterator &
281  {
282  m_Iter = r.m_Iter;
283  return *this;
284  }
285 
286  ConstIterator & operator*() { return *this; }
287  ConstIterator * operator->() { return this; }
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  bool
321  operator!=(const Iterator & r) const
322  {
323  return m_Iter != r.m_Iter;
324  }
325  bool
326  operator==(const ConstIterator & r) const
327  {
328  return m_Iter == r.m_Iter;
329  }
330  bool
331  operator!=(const ConstIterator & r) const
332  {
333  return m_Iter != r.m_Iter;
334  }
335 
338  Index() const
339  {
340  return m_Iter->first;
341  }
342 
344  const Element &
345  Value() const
346  {
347  return m_Iter->second;
348  }
349 
350  private:
352  friend class Iterator;
353  };
354 
355  /* Declare the public interface routines. */
356 
364  Element & ElementAt(ElementIdentifier);
365 
370  const Element & ElementAt(ElementIdentifier) const;
371 
379  Element & CreateElementAt(ElementIdentifier);
380 
385  Element GetElement(ElementIdentifier) const;
386 
391  void SetElement(ElementIdentifier, Element);
392 
397  void InsertElement(ElementIdentifier, Element);
398 
403  bool IndexExists(ElementIdentifier) const;
404 
410  bool
411  GetElementIfIndexExists(ElementIdentifier, Element *) const;
412 
418  void CreateIndex(ElementIdentifier);
419 
424  void DeleteIndex(ElementIdentifier);
425 
430  Begin() const;
431 
436  End() const;
437 
441  Iterator
442  Begin();
443 
447  Iterator
448  End();
449 
454  Size() const;
455 
462  void Reserve(ElementIdentifier);
463 
469  void
470  Squeeze();
471 
476  void
477  Initialize();
478 };
479 } // end namespace itk
480 
481 #ifndef ITK_MANUAL_INSTANTIATION
482 # include "itkMapContainer.hxx"
483 #endif
484 
485 #endif
itk::MapContainer::ConstIterator::operator--
ConstIterator & operator--()
Definition: itkMapContainer.h:302
itk::MapContainer::ConstIterator::ConstIterator
ConstIterator(const MapConstIterator &ci)
Definition: itkMapContainer.h:273
itk::MapContainer::ConstIterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:316
itkObjectFactory.h
itk::MapContainer::Iterator::operator!=
bool operator!=(const Iterator &r) const
Definition: itkMapContainer.h:225
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=(const ConstIterator &r)
Definition: itkMapContainer.h:280
itk::MapContainer::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkMapContainer.h:289
itk::MapContainer::Iterator::operator++
Iterator operator++(int)
Definition: itkMapContainer.h:199
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!=
bool operator!=(const Iterator &r) const
Definition: itkMapContainer.h:321
itk::swap
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:239
itk::MapContainer::ConstIterator::operator->
ConstIterator * operator->()
Definition: itkMapContainer.h:287
itk::MapContainer::Iterator::Value
Element & Value()
Definition: itkMapContainer.h:249
itk::MapContainer::ConstIterator::operator*
ConstIterator & operator*()
Definition: itkMapContainer.h:286
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:263
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:255
itk::MapContainer::ConstIterator::iterator_category
typename MapConstIterator::iterator_category iterator_category
Definition: itkMapContainer.h:266
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:212
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:331
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:242
itk::MapContainer::Iterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:230
itk::MapContainer::Iterator::operator=
Iterator & operator=(const Iterator &r)
Definition: itkMapContainer.h:184
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:345
itk::MapContainer::Iterator::difference_type
typename MapIterator::difference_type difference_type
Definition: itkMapContainer.h:172
itk::MapContainer::Iterator::Iterator
Iterator(const Iterator &i)
Definition: itkMapContainer.h:177
itkObject.h
itk::MapContainer::ConstIterator::operator==
bool operator==(const ConstIterator &r) const
Definition: itkMapContainer.h:326
itk::MapContainer::Iterator::operator--
Iterator & operator--()
Definition: itkMapContainer.h:206
itk::MapContainer::Iterator::operator*
Iterator & operator*()
Definition: itkMapContainer.h:190
itk::MapContainer::ConstIterator::m_Iter
MapConstIterator m_Iter
Definition: itkMapContainer.h:351
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
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:269
itk::MapContainer::Iterator::pointer
typename MapIterator::pointer pointer
Definition: itkMapContainer.h:173
itk::MapContainer::ConstIterator::Index
ElementIdentifier Index() const
Definition: itkMapContainer.h:338
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:60
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:267
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:268
itk::MapContainer::Iterator::operator!=
bool operator!=(const ConstIterator &r) const
Definition: itkMapContainer.h:235
itk::MapContainer::Iterator::Iterator
Iterator(const MapIterator &i)
Definition: itkMapContainer.h:180
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:276
itk::MapContainer::ElementIdentifier
TElementIdentifier ElementIdentifier
Definition: itkMapContainer.h:62
itk::MapContainer::Iterator::operator++
Iterator & operator++()
Definition: itkMapContainer.h:193
itk::MapContainer::ConstIterator::reference
typename MapConstIterator::reference reference
Definition: itkMapContainer.h:270
itk::MapContainer::Iterator::operator==
bool operator==(const Iterator &r) const
Definition: itkMapContainer.h:220