1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/multimap.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
31 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
41 /// Class std::multimap with safety/checking/debug instrumentation.
42 template<typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
43 typename _Allocator
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
45 : public _GLIBCXX_STD_D::multimap
<_Key
, _Tp
, _Compare
, _Allocator
>,
46 public __gnu_debug::_Safe_sequence
<multimap
<_Key
, _Tp
,
47 _Compare
, _Allocator
> >
49 typedef _GLIBCXX_STD_D::multimap
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
50 typedef __gnu_debug::_Safe_sequence
<multimap
> _Safe_base
;
52 typedef typename
_Base::const_iterator _Base_const_iterator
;
53 typedef typename
_Base::iterator _Base_iterator
;
54 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
57 typedef _Key key_type
;
58 typedef _Tp mapped_type
;
59 typedef std::pair
<const _Key
, _Tp
> value_type
;
60 typedef _Compare key_compare
;
61 typedef _Allocator allocator_type
;
62 typedef typename
_Base::reference reference
;
63 typedef typename
_Base::const_reference const_reference
;
65 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, multimap
>
67 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
,
68 multimap
> const_iterator
;
70 typedef typename
_Base::size_type size_type
;
71 typedef typename
_Base::difference_type difference_type
;
72 typedef typename
_Base::pointer pointer
;
73 typedef typename
_Base::const_pointer const_pointer
;
74 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
75 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
77 using _Base::value_compare
;
79 // 23.3.1.1 construct/copy/destroy:
80 explicit multimap(const _Compare
& __comp
= _Compare(),
81 const _Allocator
& __a
= _Allocator())
82 : _Base(__comp
, __a
) { }
84 template<typename _InputIterator
>
85 multimap(_InputIterator __first
, _InputIterator __last
,
86 const _Compare
& __comp
= _Compare(),
87 const _Allocator
& __a
= _Allocator())
88 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
90 __gnu_debug::__base(__last
),
93 multimap(const multimap
& __x
)
94 : _Base(__x
), _Safe_base() { }
96 multimap(const _Base
& __x
)
97 : _Base(__x
), _Safe_base() { }
99 #ifdef __GXX_EXPERIMENTAL_CXX0X__
100 multimap(multimap
&& __x
)
101 : _Base(std::move(__x
)), _Safe_base()
102 { this->_M_swap(__x
); }
104 multimap(initializer_list
<value_type
> __l
,
105 const _Compare
& __c
= _Compare(),
106 const allocator_type
& __a
= allocator_type())
107 : _Base(__l
, __c
, __a
), _Safe_base() { }
113 operator=(const multimap
& __x
)
115 *static_cast<_Base
*>(this) = __x
;
116 this->_M_invalidate_all();
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
122 operator=(multimap
&& __x
)
132 operator=(initializer_list
<value_type
> __l
)
140 using _Base::get_allocator
;
145 { return iterator(_Base::begin(), this); }
149 { return const_iterator(_Base::begin(), this); }
153 { return iterator(_Base::end(), this); }
157 { return const_iterator(_Base::end(), this); }
161 { return reverse_iterator(end()); }
163 const_reverse_iterator
165 { return const_reverse_iterator(end()); }
169 { return reverse_iterator(begin()); }
171 const_reverse_iterator
173 { return const_reverse_iterator(begin()); }
175 #ifdef __GXX_EXPERIMENTAL_CXX0X__
178 { return const_iterator(_Base::begin(), this); }
182 { return const_iterator(_Base::end(), this); }
184 const_reverse_iterator
186 { return const_reverse_iterator(end()); }
188 const_reverse_iterator
190 { return const_reverse_iterator(begin()); }
196 using _Base::max_size
;
200 insert(const value_type
& __x
)
201 { return iterator(_Base::insert(__x
), this); }
203 #ifdef __GXX_EXPERIMENTAL_CXX0X__
204 template<typename _Pair
, typename
= typename
205 std::enable_if
<std::is_convertible
<_Pair
,
206 value_type
>::value
>::type
>
209 { return iterator(_Base::insert(std::forward
<_Pair
>(__x
)), this); }
212 #ifdef __GXX_EXPERIMENTAL_CXX0X__
214 insert(std::initializer_list
<value_type
> __list
)
215 { _Base::insert(__list
); }
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
220 insert(const_iterator __position
, const value_type
& __x
)
222 insert(iterator __position
, const value_type
& __x
)
225 __glibcxx_check_insert(__position
);
226 return iterator(_Base::insert(__position
.base(), __x
), this);
229 #ifdef __GXX_EXPERIMENTAL_CXX0X__
230 template<typename _Pair
, typename
= typename
231 std::enable_if
<std::is_convertible
<_Pair
,
232 value_type
>::value
>::type
>
234 insert(const_iterator __position
, _Pair
&& __x
)
236 __glibcxx_check_insert(__position
);
237 return iterator(_Base::insert(__position
.base(),
238 std::forward
<_Pair
>(__x
)), this);
242 template<typename _InputIterator
>
244 insert(_InputIterator __first
, _InputIterator __last
)
246 __glibcxx_check_valid_range(__first
, __last
);
247 _Base::insert(__gnu_debug::__base(__first
),
248 __gnu_debug::__base(__last
));
251 #ifdef __GXX_EXPERIMENTAL_CXX0X__
253 erase(const_iterator __position
)
255 __glibcxx_check_erase(__position
);
256 this->_M_invalidate_if(_Equal(__position
.base()));
257 return iterator(_Base::erase(__position
.base()), this);
261 erase(iterator __position
)
263 __glibcxx_check_erase(__position
);
264 this->_M_invalidate_if(_Equal(__position
.base()));
265 _Base::erase(__position
.base());
270 erase(const key_type
& __x
)
272 std::pair
<_Base_iterator
, _Base_iterator
> __victims
=
273 _Base::equal_range(__x
);
274 size_type __count
= 0;
275 _Base_iterator __victim
= __victims
.first
;
276 while (__victim
!= __victims
.second
)
278 this->_M_invalidate_if(_Equal(__victim
));
279 _Base::erase(__victim
++);
285 #ifdef __GXX_EXPERIMENTAL_CXX0X__
287 erase(const_iterator __first
, const_iterator __last
)
289 // _GLIBCXX_RESOLVE_LIB_DEFECTS
290 // 151. can't currently clear() empty container
291 __glibcxx_check_erase_range(__first
, __last
);
292 for (_Base_const_iterator __victim
= __first
.base();
293 __victim
!= __last
.base(); ++__victim
)
295 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
296 _M_message(__gnu_debug::__msg_valid_range
)
297 ._M_iterator(__first
, "first")
298 ._M_iterator(__last
, "last"));
299 this->_M_invalidate_if(_Equal(__victim
));
301 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
305 erase(iterator __first
, iterator __last
)
307 // _GLIBCXX_RESOLVE_LIB_DEFECTS
308 // 151. can't currently clear() empty container
309 __glibcxx_check_erase_range(__first
, __last
);
310 for (_Base_iterator __victim
= __first
.base();
311 __victim
!= __last
.base(); ++__victim
)
313 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
314 _M_message(__gnu_debug::__msg_valid_range
)
315 ._M_iterator(__first
, "first")
316 ._M_iterator(__last
, "last"));
317 this->_M_invalidate_if(_Equal(__victim
));
319 _Base::erase(__first
.base(), __last
.base());
333 this->_M_invalidate_all();
338 using _Base::key_comp
;
339 using _Base::value_comp
;
341 // 23.3.1.3 multimap operations:
343 find(const key_type
& __x
)
344 { return iterator(_Base::find(__x
), this); }
347 find(const key_type
& __x
) const
348 { return const_iterator(_Base::find(__x
), this); }
353 lower_bound(const key_type
& __x
)
354 { return iterator(_Base::lower_bound(__x
), this); }
357 lower_bound(const key_type
& __x
) const
358 { return const_iterator(_Base::lower_bound(__x
), this); }
361 upper_bound(const key_type
& __x
)
362 { return iterator(_Base::upper_bound(__x
), this); }
365 upper_bound(const key_type
& __x
) const
366 { return const_iterator(_Base::upper_bound(__x
), this); }
368 std::pair
<iterator
,iterator
>
369 equal_range(const key_type
& __x
)
371 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
372 _Base::equal_range(__x
);
373 return std::make_pair(iterator(__res
.first
, this),
374 iterator(__res
.second
, this));
377 std::pair
<const_iterator
,const_iterator
>
378 equal_range(const key_type
& __x
) const
380 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
381 _Base::equal_range(__x
);
382 return std::make_pair(const_iterator(__res
.first
, this),
383 const_iterator(__res
.second
, this));
387 _M_base() { return *this; }
390 _M_base() const { return *this; }
396 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
397 this->_M_invalidate_if(_Not_equal(_Base::end()));
401 template<typename _Key
, typename _Tp
,
402 typename _Compare
, typename _Allocator
>
404 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
405 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
406 { return __lhs
._M_base() == __rhs
._M_base(); }
408 template<typename _Key
, typename _Tp
,
409 typename _Compare
, typename _Allocator
>
411 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
412 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
413 { return __lhs
._M_base() != __rhs
._M_base(); }
415 template<typename _Key
, typename _Tp
,
416 typename _Compare
, typename _Allocator
>
418 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
419 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
420 { return __lhs
._M_base() < __rhs
._M_base(); }
422 template<typename _Key
, typename _Tp
,
423 typename _Compare
, typename _Allocator
>
425 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
426 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
427 { return __lhs
._M_base() <= __rhs
._M_base(); }
429 template<typename _Key
, typename _Tp
,
430 typename _Compare
, typename _Allocator
>
432 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
433 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
434 { return __lhs
._M_base() >= __rhs
._M_base(); }
436 template<typename _Key
, typename _Tp
,
437 typename _Compare
, typename _Allocator
>
439 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
440 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
441 { return __lhs
._M_base() > __rhs
._M_base(); }
443 template<typename _Key
, typename _Tp
,
444 typename _Compare
, typename _Allocator
>
446 swap(multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
447 multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
448 { __lhs
.swap(__rhs
); }
450 } // namespace __debug