ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMapContainer.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  MapContainer(const MapKeyCompareType & comp):MapType(comp) {}
78  // MapContainer(const Self& r):MapType(r) {}
79  template< typename TInputIterator >
80  MapContainer(TInputIterator first, TInputIterator last):MapType(first, last) {}
81  template< typename TInputIterator >
82  MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType & comp):
83  MapType(first, last, comp) {}
85 
87  itkNewMacro(Self);
88 
91 
94  {
95  return *this;
96  }
97 
99  const STLContainerType & CastToSTLConstContainer() const ITK_NOEXCEPT
100  {
101  return *this;
102  }
103 
104  using STLContainerType::begin;
105  using STLContainerType::end;
106  using STLContainerType::rbegin;
107  using STLContainerType::rend;
108 
109  using STLContainerType::empty;
110  using STLContainerType::size;
111  using STLContainerType::max_size;
112 
113  using STLContainerType::operator[];
114 
115  using STLContainerType::insert;
116  using STLContainerType::erase;
118  using STLContainerType::clear;
119 
120  using STLContainerType::key_comp;
121  using STLContainerType::value_comp;
122 
123  using STLContainerType::find;
124  using STLContainerType::count;
125  using STLContainerType::lower_bound;
126  using STLContainerType::upper_bound;
127  using STLContainerType::equal_range;
128 
129  using STLContainerType::get_allocator;
130 
131  using typename STLContainerType::key_type;
132  using typename STLContainerType::mapped_type;
133  using typename STLContainerType::value_type;
134  using typename STLContainerType::key_compare;
135  using typename STLContainerType::value_compare;
136  using typename STLContainerType::allocator_type;
137  using typename STLContainerType::reference;
138  using typename STLContainerType::const_reference;
139  using typename STLContainerType::iterator;
140  using typename STLContainerType::const_iterator;
141  using typename STLContainerType::size_type;
142  using typename STLContainerType::difference_type;
143  using typename STLContainerType::pointer;
144  using typename STLContainerType::const_pointer;
145  using typename STLContainerType::reverse_iterator;
146  using typename STLContainerType::const_reverse_iterator;
147 
149  class Iterator;
151  friend class Iterator;
152  friend class ConstIterator;
153 
158  class Iterator
159  {
160 public:
161  using iterator_category = typename MapIterator::iterator_category;
162  using value_type = typename MapIterator::value_type;
163  using difference_type = typename MapIterator::difference_type;
164  using pointer = typename MapIterator::pointer;
165  using reference = typename MapIterator::reference;
166 
167  Iterator() = default;
168  Iterator(const Iterator & i):m_Iter(i.m_Iter) {}
169  Iterator(const MapIterator & i):m_Iter(i) {}
170  Iterator & operator=(const Iterator & r ) { m_Iter = r.m_Iter; return *this; }
171 
172  Iterator & operator*() { return *this; }
173  Iterator * operator->() { return this; }
174  Iterator & operator++() { ++m_Iter; return *this; }
175  Iterator operator++(int) { Iterator temp(*this); ++m_Iter; return temp; }
176  Iterator & operator--() { --m_Iter; return *this; }
177  Iterator operator--(int) { Iterator temp(*this); --m_Iter; return temp; }
178 
179  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
180  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
181  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
182  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
183 
185  ElementIdentifier Index() const { return m_Iter->first; }
186 
188  Element & Value() { return m_Iter->second; }
189 
190 private:
192  friend class ConstIterator;
193  };
194 
200  {
201 public:
202  using iterator_category = typename MapConstIterator::iterator_category;
203  using value_type = typename MapConstIterator::value_type;
204  using difference_type = typename MapConstIterator::difference_type;
205  using pointer = typename MapConstIterator::pointer;
206  using reference = typename MapConstIterator::reference;
207 
208  ConstIterator() = default;
209  ConstIterator(const MapConstIterator & ci):m_Iter(ci) {}
210  ConstIterator(const Iterator & r) : m_Iter( r.m_Iter ) {}
211  ConstIterator & operator=(const ConstIterator & r ) { m_Iter = r.m_Iter; return *this; }
212 
213  ConstIterator & operator*() { return *this; }
214  ConstIterator * operator->() { return this; }
215  ConstIterator & operator++() { ++m_Iter; return *this; }
216  ConstIterator operator++(int) { ConstIterator temp(*this); ++m_Iter; return temp; }
217  ConstIterator & operator--() { --m_Iter; return *this; }
218  ConstIterator operator--(int) { ConstIterator temp(*this); --m_Iter; return temp; }
219 
220  bool operator==(const Iterator & r) const { return m_Iter == r.m_Iter; }
221  bool operator!=(const Iterator & r) const { return m_Iter != r.m_Iter; }
222  bool operator==(const ConstIterator & r) const { return m_Iter == r.m_Iter; }
223  bool operator!=(const ConstIterator & r) const { return m_Iter != r.m_Iter; }
224 
226  ElementIdentifier Index() const { return m_Iter->first; }
227 
229  const Element & Value() const { return m_Iter->second; }
230 
231 private:
233  friend class Iterator;
234  };
235 
236  /* Declare the public interface routines. */
237 
245  Element & ElementAt(ElementIdentifier);
246 
251  const Element & ElementAt(ElementIdentifier) const;
252 
260  Element & CreateElementAt(ElementIdentifier);
261 
266  Element GetElement(ElementIdentifier) const;
267 
272  void SetElement(ElementIdentifier, Element);
273 
278  void InsertElement(ElementIdentifier, Element);
279 
284  bool IndexExists(ElementIdentifier) const;
285 
291  bool GetElementIfIndexExists(ElementIdentifier, Element *) const;
292 
298  void CreateIndex(ElementIdentifier);
299 
304  void DeleteIndex(ElementIdentifier);
305 
309  ConstIterator Begin() const;
310 
314  ConstIterator End() const;
315 
319  Iterator Begin();
320 
324  Iterator End();
325 
329  ElementIdentifier Size() const;
330 
337  void Reserve(ElementIdentifier);
338 
344  void Squeeze();
345 
350  void Initialize();
351 };
352 } // end namespace itk
353 
354 #ifndef ITK_MANUAL_INSTANTIATION
355 #include "itkMapContainer.hxx"
356 #endif
357 
358 #endif
typename MapConstIterator::difference_type difference_type
bool operator==(const ConstIterator &r) const
bool operator==(const Iterator &r) const
Light weight base class for most itk classes.
bool operator==(const ConstIterator &r) const
MapContainer(const MapKeyCompareType &comp)
A wrapper of the STL &quot;map&quot; container.
typename MapConstIterator::pointer pointer
ConstIterator & operator=(const ConstIterator &r)
typename MapType::const_iterator MapConstIterator
bool operator!=(const ConstIterator &r) const
const Element & Value() const
typename MapIterator::difference_type difference_type
bool operator==(const Iterator &r) const
STLContainerType & CastToSTLContainer() noexcept
bool operator!=(const ConstIterator &r) const
typename MapIterator::reference reference
Iterator(const Iterator &i)
void swap(Array< T > &a, Array< T > &b)
Definition: itkArray.h:211
The non-const iterator type for the map.
bool operator!=(const Iterator &r) const
typename MapType::key_compare MapKeyCompareType
The const iterator type for the map.
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
MapContainer(TInputIterator first, TInputIterator last, const MapKeyCompareType &comp)
bool operator!=(const Iterator &r) const
ElementIdentifier Index() const
ConstIterator(const MapConstIterator &ci)
MapContainer(TInputIterator first, TInputIterator last)
TElementIdentifier ElementIdentifier
typename MapConstIterator::value_type value_type
Iterator(const MapIterator &i)
typename MapConstIterator::reference reference
typename MapIterator::pointer pointer
const STLContainerType & CastToSTLConstContainer() const noexcept
typename MapIterator::value_type value_type
std::map< ElementIdentifier, Element > MapType
typename MapIterator::iterator_category iterator_category
typename MapConstIterator::iterator_category iterator_category
ElementIdentifier Index() const
Iterator & operator=(const Iterator &r)
Base class for most ITK classes.
Definition: itkObject.h:60
typename MapType::iterator MapIterator