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 & operator*() { return *this; }
186 Iterator * operator->() { return this; }
187 Iterator &
189 {
190 ++m_Iter;
191 return *this;
192 }
195 {
196 Iterator temp(*this);
197 ++m_Iter;
198 return temp;
199 }
200 Iterator &
202 {
203 --m_Iter;
204 return *this;
205 }
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
221
222 bool
223 operator==(const ConstIterator & r) const
224 {
225 return m_Iter == r.m_Iter;
226 }
227
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; }
274 {
275 ++m_Iter;
276 return *this;
277 }
280 {
281 ConstIterator temp(*this);
282 ++m_Iter;
283 return temp;
284 }
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
306
307 bool
308 operator==(const ConstIterator & r) const
309 {
310 return m_Iter == r.m_Iter;
311 }
312
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
344
350
359
365
371
377
383
389 bool
391
398
404
409 Begin() const;
410
415 End() const;
416
422
428
433 Size() const;
434
442
448 void
450
455 void
457};
458} // end namespace itk
459
460#ifndef ITK_MANUAL_INSTANTIATION
461# include "itkMapContainer.hxx"
462#endif
463
464#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