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
26namespace itk
27{
44template <typename TElementIdentifier, typename TElement>
45class ITK_TEMPLATE_EXPORT MapContainer
46 : public Object
47 , private std::map<TElementIdentifier, TElement>
48{
49public:
50 ITK_DISALLOW_COPY_AND_MOVE(MapContainer);
51
57
59 itkOverrideGetNameOfClassMacro(MapContainer);
60
62 using ElementIdentifier = TElementIdentifier;
63 using Element = TElement;
64
65private:
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
72public:
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;
163 class ConstIterator;
164 friend class Iterator;
165 friend class ConstIterator;
166
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 }
203 {
204 Iterator temp(*this);
205 ++m_Iter;
206 return temp;
207 }
208 Iterator &
210 {
211 --m_Iter;
212 return *this;
213 }
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
229
230 bool
231 operator==(const ConstIterator & r) const
232 {
233 return m_Iter == r.m_Iter;
234 }
235
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
280 {
281 return *this;
282 }
285 {
286 return this;
287 }
290 {
291 ++m_Iter;
292 return *this;
293 }
296 {
297 ConstIterator temp(*this);
298 ++m_Iter;
299 return temp;
300 }
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
322
323 bool
324 operator==(const ConstIterator & r) const
325 {
326 return m_Iter == r.m_Iter;
327 }
328
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
360
366
375
381
387
393
399
405 bool
407
414
420
425 Begin() const;
426
431 End() const;
432
438
444
449 Size() const;
450
458
464 void
466
471 void
473};
474} // end namespace itk
475
476#ifndef ITK_MANUAL_INSTANTIATION
477# include "itkMapContainer.hxx"
478#endif
479
480#endif
Light weight base class for most itk classes.
The const iterator type for the map.
bool operator==(const ConstIterator &r) const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
typename MapConstIterator::iterator_category iterator_category
ConstIterator(const MapConstIterator &ci)
typename MapConstIterator::pointer pointer
typename MapConstIterator::reference reference
bool operator==(const Iterator &r) const
typename MapConstIterator::difference_type difference_type
typename MapConstIterator::value_type value_type
const Element & Value() const
ElementIdentifier Index() const
The non-const iterator type for the map.
typename MapIterator::difference_type difference_type
bool operator==(const ConstIterator &r) const
typename MapIterator::value_type value_type
typename MapIterator::reference reference
typename MapIterator::pointer pointer
bool operator==(const Iterator &r) const
typename MapIterator::iterator_category iterator_category
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Iterator)
Iterator(const MapIterator &i)
ElementIdentifier Index() const
ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator)
A wrapper of the STL "map" container.
bool GetElementIfIndexExists(ElementIdentifier, Element *) const
void InsertElement(ElementIdentifier, Element)
Element & CreateElementAt(ElementIdentifier)
MapContainer(const MapKeyCompareType &comp)
MapContainer(TInputIterator first, TInputIterator last)
ElementIdentifier Size() const
void CreateIndex(ElementIdentifier)
MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType &comp)
void SetElement(ElementIdentifier, Element)
Element & ElementAt(ElementIdentifier)
void DeleteIndex(ElementIdentifier)
typename MapType::key_compare MapKeyCompareType
TElementIdentifier ElementIdentifier
Element GetElement(ElementIdentifier) const
std::map< ElementIdentifier, Element > MapType
typename MapType::const_iterator MapConstIterator
Iterator Begin()
void Reserve(ElementIdentifier)
STLContainerType & CastToSTLContainer() noexcept
typename MapType::iterator MapIterator
ConstIterator Begin() const
const Element & ElementAt(ElementIdentifier) const
ConstIterator End() const
bool IndexExists(ElementIdentifier) const
const STLContainerType & CastToSTLConstContainer() const noexcept
Base class for most ITK classes.
Definition: itkObject.h:62
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void swap(Array< T > &a, Array< T > &b) noexcept
Definition: itkArray.h:242