1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file debug/multimap.h
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
30 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
32 #include <debug/safe_sequence.h>
33 #include <debug/safe_container.h>
34 #include <debug/safe_iterator.h>
37 namespace std
_GLIBCXX_VISIBILITY(default)
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 __gnu_debug::_Safe_container
<
46 multimap
<_Key
, _Tp
, _Compare
, _Allocator
>, _Allocator
,
47 __gnu_debug::_Safe_node_sequence
>,
48 public _GLIBCXX_STD_C::multimap
<_Key
, _Tp
, _Compare
, _Allocator
>
50 typedef _GLIBCXX_STD_C::multimap
<
51 _Key
, _Tp
, _Compare
, _Allocator
> _Base
;
52 typedef __gnu_debug::_Safe_container
<
53 multimap
, _Allocator
, __gnu_debug::_Safe_node_sequence
> _Safe
;
55 typedef typename
_Base::const_iterator _Base_const_iterator
;
56 typedef typename
_Base::iterator _Base_iterator
;
57 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
61 typedef _Key key_type
;
62 typedef _Tp mapped_type
;
63 typedef std::pair
<const _Key
, _Tp
> value_type
;
64 typedef _Compare key_compare
;
65 typedef _Allocator allocator_type
;
66 typedef typename
_Base::reference reference
;
67 typedef typename
_Base::const_reference const_reference
;
69 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, multimap
>
71 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
,
72 multimap
> const_iterator
;
74 typedef typename
_Base::size_type size_type
;
75 typedef typename
_Base::difference_type difference_type
;
76 typedef typename
_Base::pointer pointer
;
77 typedef typename
_Base::const_pointer const_pointer
;
78 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
79 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
81 // 23.3.1.1 construct/copy/destroy:
83 #if __cplusplus < 201103L
84 multimap() : _Base() { }
86 multimap(const multimap
& __x
)
92 multimap(const multimap
&) = default;
93 multimap(multimap
&&) = default;
95 multimap(initializer_list
<value_type
> __l
,
96 const _Compare
& __c
= _Compare(),
97 const allocator_type
& __a
= allocator_type())
98 : _Base(__l
, __c
, __a
) { }
101 multimap(const allocator_type
& __a
)
104 multimap(const multimap
& __m
, const allocator_type
& __a
)
105 : _Base(__m
, __a
) { }
107 multimap(multimap
&& __m
, const allocator_type
& __a
)
108 : _Safe(std::move(__m
._M_safe()), __a
),
109 _Base(std::move(__m
._M_base()), __a
) { }
111 multimap(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
112 : _Base(__l
, __a
) { }
114 template<typename _InputIterator
>
115 multimap(_InputIterator __first
, _InputIterator __last
,
116 const allocator_type
& __a
)
117 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
119 __gnu_debug::__base(__last
), __a
) { }
121 ~multimap() = default;
124 explicit multimap(const _Compare
& __comp
,
125 const _Allocator
& __a
= _Allocator())
126 : _Base(__comp
, __a
) { }
128 template<typename _InputIterator
>
129 multimap(_InputIterator __first
, _InputIterator __last
,
130 const _Compare
& __comp
= _Compare(),
131 const _Allocator
& __a
= _Allocator())
132 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
134 __gnu_debug::__base(__last
),
137 multimap(const _Base
& __x
)
140 #if __cplusplus < 201103L
142 operator=(const multimap
& __x
)
144 this->_M_safe() = __x
;
150 operator=(const multimap
&) = default;
153 operator=(multimap
&&) = default;
156 operator=(initializer_list
<value_type
> __l
)
159 this->_M_invalidate_all();
164 using _Base::get_allocator
;
168 begin() _GLIBCXX_NOEXCEPT
169 { return iterator(_Base::begin(), this); }
172 begin() const _GLIBCXX_NOEXCEPT
173 { return const_iterator(_Base::begin(), this); }
176 end() _GLIBCXX_NOEXCEPT
177 { return iterator(_Base::end(), this); }
180 end() const _GLIBCXX_NOEXCEPT
181 { return const_iterator(_Base::end(), this); }
184 rbegin() _GLIBCXX_NOEXCEPT
185 { return reverse_iterator(end()); }
187 const_reverse_iterator
188 rbegin() const _GLIBCXX_NOEXCEPT
189 { return const_reverse_iterator(end()); }
192 rend() _GLIBCXX_NOEXCEPT
193 { return reverse_iterator(begin()); }
195 const_reverse_iterator
196 rend() const _GLIBCXX_NOEXCEPT
197 { return const_reverse_iterator(begin()); }
199 #if __cplusplus >= 201103L
201 cbegin() const noexcept
202 { return const_iterator(_Base::begin(), this); }
205 cend() const noexcept
206 { return const_iterator(_Base::end(), this); }
208 const_reverse_iterator
209 crbegin() const noexcept
210 { return const_reverse_iterator(end()); }
212 const_reverse_iterator
213 crend() const noexcept
214 { return const_reverse_iterator(begin()); }
220 using _Base::max_size
;
223 #if __cplusplus >= 201103L
224 template<typename
... _Args
>
226 emplace(_Args
&&... __args
)
228 return iterator(_Base::emplace(std::forward
<_Args
>(__args
)...), this);
231 template<typename
... _Args
>
233 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
235 __glibcxx_check_insert(__pos
);
236 return iterator(_Base::emplace_hint(__pos
.base(),
237 std::forward
<_Args
>(__args
)...),
243 insert(const value_type
& __x
)
244 { return iterator(_Base::insert(__x
), this); }
246 #if __cplusplus >= 201103L
247 template<typename _Pair
, typename
= typename
248 std::enable_if
<std::is_constructible
<value_type
,
249 _Pair
&&>::value
>::type
>
252 { return iterator(_Base::insert(std::forward
<_Pair
>(__x
)), this); }
255 #if __cplusplus >= 201103L
257 insert(std::initializer_list
<value_type
> __list
)
258 { _Base::insert(__list
); }
262 #if __cplusplus >= 201103L
263 insert(const_iterator __position
, const value_type
& __x
)
265 insert(iterator __position
, const value_type
& __x
)
268 __glibcxx_check_insert(__position
);
269 return iterator(_Base::insert(__position
.base(), __x
), this);
272 #if __cplusplus >= 201103L
273 template<typename _Pair
, typename
= typename
274 std::enable_if
<std::is_constructible
<value_type
,
275 _Pair
&&>::value
>::type
>
277 insert(const_iterator __position
, _Pair
&& __x
)
279 __glibcxx_check_insert(__position
);
280 return iterator(_Base::insert(__position
.base(),
281 std::forward
<_Pair
>(__x
)), this);
285 template<typename _InputIterator
>
287 insert(_InputIterator __first
, _InputIterator __last
)
289 typename
__gnu_debug::_Distance_traits
<_InputIterator
>::__type __dist
;
290 __glibcxx_check_valid_range2(__first
, __last
, __dist
);
292 if (__dist
.second
>= __gnu_debug::__dp_sign
)
293 _Base::insert(__gnu_debug::__unsafe(__first
),
294 __gnu_debug::__unsafe(__last
));
296 _Base::insert(__first
, __last
);
299 #if __cplusplus > 201402L
300 using node_type
= typename
_Base::node_type
;
303 extract(const_iterator __position
)
305 __glibcxx_check_erase(__position
);
306 this->_M_invalidate_if(_Equal(__position
.base()));
307 return _Base::extract(__position
.base());
311 extract(const key_type
& __key
)
313 const auto __position
= find(__key
);
314 if (__position
!= end())
315 return extract(__position
);
320 insert(node_type
&& __nh
)
321 { return iterator(_Base::insert(std::move(__nh
)), this); }
324 insert(const_iterator __hint
, node_type
&& __nh
)
326 __glibcxx_check_insert(__hint
);
327 return iterator(_Base::insert(__hint
.base(), std::move(__nh
)), this);
333 #if __cplusplus >= 201103L
335 erase(const_iterator __position
)
337 __glibcxx_check_erase(__position
);
338 this->_M_invalidate_if(_Equal(__position
.base()));
339 return iterator(_Base::erase(__position
.base()), this);
343 erase(iterator __position
)
344 { return erase(const_iterator(__position
)); }
347 erase(iterator __position
)
349 __glibcxx_check_erase(__position
);
350 this->_M_invalidate_if(_Equal(__position
.base()));
351 _Base::erase(__position
.base());
356 erase(const key_type
& __x
)
358 std::pair
<_Base_iterator
, _Base_iterator
> __victims
=
359 _Base::equal_range(__x
);
360 size_type __count
= 0;
361 _Base_iterator __victim
= __victims
.first
;
362 while (__victim
!= __victims
.second
)
364 this->_M_invalidate_if(_Equal(__victim
));
365 _Base::erase(__victim
++);
371 #if __cplusplus >= 201103L
373 erase(const_iterator __first
, const_iterator __last
)
375 // _GLIBCXX_RESOLVE_LIB_DEFECTS
376 // 151. can't currently clear() empty container
377 __glibcxx_check_erase_range(__first
, __last
);
378 for (_Base_const_iterator __victim
= __first
.base();
379 __victim
!= __last
.base(); ++__victim
)
381 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
382 _M_message(__gnu_debug::__msg_valid_range
)
383 ._M_iterator(__first
, "first")
384 ._M_iterator(__last
, "last"));
385 this->_M_invalidate_if(_Equal(__victim
));
387 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
391 erase(iterator __first
, iterator __last
)
393 // _GLIBCXX_RESOLVE_LIB_DEFECTS
394 // 151. can't currently clear() empty container
395 __glibcxx_check_erase_range(__first
, __last
);
396 for (_Base_iterator __victim
= __first
.base();
397 __victim
!= __last
.base(); ++__victim
)
399 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
400 _M_message(__gnu_debug::__msg_valid_range
)
401 ._M_iterator(__first
, "first")
402 ._M_iterator(__last
, "last"));
403 this->_M_invalidate_if(_Equal(__victim
));
405 _Base::erase(__first
.base(), __last
.base());
411 _GLIBCXX_NOEXCEPT_IF( noexcept(declval
<_Base
&>().swap(__x
)) )
418 clear() _GLIBCXX_NOEXCEPT
420 this->_M_invalidate_all();
425 using _Base::key_comp
;
426 using _Base::value_comp
;
428 // 23.3.1.3 multimap operations:
430 find(const key_type
& __x
)
431 { return iterator(_Base::find(__x
), this); }
433 #if __cplusplus > 201103L
434 template<typename _Kt
,
436 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
439 { return { _Base::find(__x
), this }; }
443 find(const key_type
& __x
) const
444 { return const_iterator(_Base::find(__x
), this); }
446 #if __cplusplus > 201103L
447 template<typename _Kt
,
449 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
451 find(const _Kt
& __x
) const
452 { return { _Base::find(__x
), this }; }
458 lower_bound(const key_type
& __x
)
459 { return iterator(_Base::lower_bound(__x
), this); }
461 #if __cplusplus > 201103L
462 template<typename _Kt
,
464 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
466 lower_bound(const _Kt
& __x
)
467 { return { _Base::lower_bound(__x
), this }; }
471 lower_bound(const key_type
& __x
) const
472 { return const_iterator(_Base::lower_bound(__x
), this); }
474 #if __cplusplus > 201103L
475 template<typename _Kt
,
477 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
479 lower_bound(const _Kt
& __x
) const
480 { return { _Base::lower_bound(__x
), this }; }
484 upper_bound(const key_type
& __x
)
485 { return iterator(_Base::upper_bound(__x
), this); }
487 #if __cplusplus > 201103L
488 template<typename _Kt
,
490 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
492 upper_bound(const _Kt
& __x
)
493 { return { _Base::upper_bound(__x
), this }; }
497 upper_bound(const key_type
& __x
) const
498 { return const_iterator(_Base::upper_bound(__x
), this); }
500 #if __cplusplus > 201103L
501 template<typename _Kt
,
503 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
505 upper_bound(const _Kt
& __x
) const
506 { return { _Base::upper_bound(__x
), this }; }
509 std::pair
<iterator
,iterator
>
510 equal_range(const key_type
& __x
)
512 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
513 _Base::equal_range(__x
);
514 return std::make_pair(iterator(__res
.first
, this),
515 iterator(__res
.second
, this));
518 #if __cplusplus > 201103L
519 template<typename _Kt
,
521 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
522 std::pair
<iterator
, iterator
>
523 equal_range(const _Kt
& __x
)
525 auto __res
= _Base::equal_range(__x
);
526 return { { __res
.first
, this }, { __res
.second
, this } };
530 std::pair
<const_iterator
,const_iterator
>
531 equal_range(const key_type
& __x
) const
533 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
534 _Base::equal_range(__x
);
535 return std::make_pair(const_iterator(__res
.first
, this),
536 const_iterator(__res
.second
, this));
539 #if __cplusplus > 201103L
540 template<typename _Kt
,
542 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
543 std::pair
<const_iterator
, const_iterator
>
544 equal_range(const _Kt
& __x
) const
546 auto __res
= _Base::equal_range(__x
);
547 return { { __res
.first
, this }, { __res
.second
, this } };
552 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
555 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
558 template<typename _Key
, typename _Tp
,
559 typename _Compare
, typename _Allocator
>
561 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
562 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
563 { return __lhs
._M_base() == __rhs
._M_base(); }
565 template<typename _Key
, typename _Tp
,
566 typename _Compare
, typename _Allocator
>
568 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
569 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
570 { return __lhs
._M_base() != __rhs
._M_base(); }
572 template<typename _Key
, typename _Tp
,
573 typename _Compare
, typename _Allocator
>
575 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
576 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
577 { return __lhs
._M_base() < __rhs
._M_base(); }
579 template<typename _Key
, typename _Tp
,
580 typename _Compare
, typename _Allocator
>
582 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
583 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
584 { return __lhs
._M_base() <= __rhs
._M_base(); }
586 template<typename _Key
, typename _Tp
,
587 typename _Compare
, typename _Allocator
>
589 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
590 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
591 { return __lhs
._M_base() >= __rhs
._M_base(); }
593 template<typename _Key
, typename _Tp
,
594 typename _Compare
, typename _Allocator
>
596 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
597 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
598 { return __lhs
._M_base() > __rhs
._M_base(); }
600 template<typename _Key
, typename _Tp
,
601 typename _Compare
, typename _Allocator
>
603 swap(multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
604 multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
605 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
606 { __lhs
.swap(__rhs
); }
608 } // namespace __debug