1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003-2014 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_iterator.h>
36 namespace std
_GLIBCXX_VISIBILITY(default)
40 /// Class std::multimap with safety/checking/debug instrumentation.
41 template<typename _Key
, typename _Tp
, typename _Compare
= std::less
<_Key
>,
42 typename _Allocator
= std::allocator
<std::pair
<const _Key
, _Tp
> > >
44 : public _GLIBCXX_STD_C::multimap
<_Key
, _Tp
, _Compare
, _Allocator
>,
45 public __gnu_debug::_Safe_sequence
<multimap
<_Key
, _Tp
,
46 _Compare
, _Allocator
> >
48 typedef _GLIBCXX_STD_C::multimap
<_Key
, _Tp
, _Compare
, _Allocator
> _Base
;
50 typedef typename
_Base::const_iterator _Base_const_iterator
;
51 typedef typename
_Base::iterator _Base_iterator
;
52 typedef __gnu_debug::_Equal_to
<_Base_const_iterator
> _Equal
;
54 #if __cplusplus >= 201103L
55 typedef __gnu_cxx::__alloc_traits
<typename
56 _Base::allocator_type
> _Alloc_traits
;
60 typedef _Key key_type
;
61 typedef _Tp mapped_type
;
62 typedef std::pair
<const _Key
, _Tp
> value_type
;
63 typedef _Compare key_compare
;
64 typedef _Allocator allocator_type
;
65 typedef typename
_Base::reference reference
;
66 typedef typename
_Base::const_reference const_reference
;
68 typedef __gnu_debug::_Safe_iterator
<_Base_iterator
, multimap
>
70 typedef __gnu_debug::_Safe_iterator
<_Base_const_iterator
,
71 multimap
> const_iterator
;
73 typedef typename
_Base::size_type size_type
;
74 typedef typename
_Base::difference_type difference_type
;
75 typedef typename
_Base::pointer pointer
;
76 typedef typename
_Base::const_pointer const_pointer
;
77 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
78 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
80 // 23.3.1.1 construct/copy/destroy:
82 multimap() : _Base() { }
84 explicit multimap(const _Compare
& __comp
,
85 const _Allocator
& __a
= _Allocator())
86 : _Base(__comp
, __a
) { }
88 template<typename _InputIterator
>
89 multimap(_InputIterator __first
, _InputIterator __last
,
90 const _Compare
& __comp
= _Compare(),
91 const _Allocator
& __a
= _Allocator())
92 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
94 __gnu_debug::__base(__last
),
97 multimap(const multimap
& __x
)
100 multimap(const _Base
& __x
)
103 #if __cplusplus >= 201103L
104 multimap(multimap
&& __x
)
105 noexcept(is_nothrow_copy_constructible
<_Compare
>::value
)
106 : _Base(std::move(__x
))
107 { this->_M_swap(__x
); }
109 multimap(initializer_list
<value_type
> __l
,
110 const _Compare
& __c
= _Compare(),
111 const allocator_type
& __a
= allocator_type())
112 : _Base(__l
, __c
, __a
) { }
115 multimap(const allocator_type
& __a
)
118 multimap(const multimap
& __m
, const allocator_type
& __a
)
119 : _Base(__m
, __a
) { }
121 multimap(multimap
&& __m
, const allocator_type
& __a
)
122 : _Base(std::move(__m
._M_base()), __a
) { }
124 multimap(initializer_list
<value_type
> __l
, const allocator_type
& __a
)
128 template<typename _InputIterator
>
129 multimap(_InputIterator __first
, _InputIterator __last
,
130 const allocator_type
& __a
)
131 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first
,
133 __gnu_debug::__base(__last
), __a
)
137 ~multimap() _GLIBCXX_NOEXCEPT
{ }
140 operator=(const multimap
& __x
)
143 this->_M_invalidate_all();
147 #if __cplusplus >= 201103L
149 operator=(multimap
&& __x
)
150 noexcept(_Alloc_traits::_S_nothrow_move())
152 __glibcxx_check_self_move_assign(__x
);
153 bool __xfer_memory
= _Alloc_traits::_S_propagate_on_move_assign()
154 || __x
.get_allocator() == this->get_allocator();
155 _M_base() = std::move(__x
._M_base());
159 this->_M_invalidate_all();
160 __x
._M_invalidate_all();
165 operator=(initializer_list
<value_type
> __l
)
168 this->_M_invalidate_all();
173 using _Base::get_allocator
;
177 begin() _GLIBCXX_NOEXCEPT
178 { return iterator(_Base::begin(), this); }
181 begin() const _GLIBCXX_NOEXCEPT
182 { return const_iterator(_Base::begin(), this); }
185 end() _GLIBCXX_NOEXCEPT
186 { return iterator(_Base::end(), this); }
189 end() const _GLIBCXX_NOEXCEPT
190 { return const_iterator(_Base::end(), this); }
193 rbegin() _GLIBCXX_NOEXCEPT
194 { return reverse_iterator(end()); }
196 const_reverse_iterator
197 rbegin() const _GLIBCXX_NOEXCEPT
198 { return const_reverse_iterator(end()); }
201 rend() _GLIBCXX_NOEXCEPT
202 { return reverse_iterator(begin()); }
204 const_reverse_iterator
205 rend() const _GLIBCXX_NOEXCEPT
206 { return const_reverse_iterator(begin()); }
208 #if __cplusplus >= 201103L
210 cbegin() const noexcept
211 { return const_iterator(_Base::begin(), this); }
214 cend() const noexcept
215 { return const_iterator(_Base::end(), this); }
217 const_reverse_iterator
218 crbegin() const noexcept
219 { return const_reverse_iterator(end()); }
221 const_reverse_iterator
222 crend() const noexcept
223 { return const_reverse_iterator(begin()); }
229 using _Base::max_size
;
232 #if __cplusplus >= 201103L
233 template<typename
... _Args
>
235 emplace(_Args
&&... __args
)
237 return iterator(_Base::emplace(std::forward
<_Args
>(__args
)...), this);
240 template<typename
... _Args
>
242 emplace_hint(const_iterator __pos
, _Args
&&... __args
)
244 __glibcxx_check_insert(__pos
);
245 return iterator(_Base::emplace_hint(__pos
.base(),
246 std::forward
<_Args
>(__args
)...),
252 insert(const value_type
& __x
)
253 { return iterator(_Base::insert(__x
), this); }
255 #if __cplusplus >= 201103L
256 template<typename _Pair
, typename
= typename
257 std::enable_if
<std::is_constructible
<value_type
,
258 _Pair
&&>::value
>::type
>
261 { return iterator(_Base::insert(std::forward
<_Pair
>(__x
)), this); }
264 #if __cplusplus >= 201103L
266 insert(std::initializer_list
<value_type
> __list
)
267 { _Base::insert(__list
); }
271 #if __cplusplus >= 201103L
272 insert(const_iterator __position
, const value_type
& __x
)
274 insert(iterator __position
, const value_type
& __x
)
277 __glibcxx_check_insert(__position
);
278 return iterator(_Base::insert(__position
.base(), __x
), this);
281 #if __cplusplus >= 201103L
282 template<typename _Pair
, typename
= typename
283 std::enable_if
<std::is_constructible
<value_type
,
284 _Pair
&&>::value
>::type
>
286 insert(const_iterator __position
, _Pair
&& __x
)
288 __glibcxx_check_insert(__position
);
289 return iterator(_Base::insert(__position
.base(),
290 std::forward
<_Pair
>(__x
)), this);
294 template<typename _InputIterator
>
296 insert(_InputIterator __first
, _InputIterator __last
)
298 __glibcxx_check_valid_range(__first
, __last
);
299 _Base::insert(__gnu_debug::__base(__first
),
300 __gnu_debug::__base(__last
));
303 #if __cplusplus >= 201103L
305 erase(const_iterator __position
)
307 __glibcxx_check_erase(__position
);
308 this->_M_invalidate_if(_Equal(__position
.base()));
309 return iterator(_Base::erase(__position
.base()), this);
313 erase(iterator __position
)
314 { return erase(const_iterator(__position
)); }
317 erase(iterator __position
)
319 __glibcxx_check_erase(__position
);
320 this->_M_invalidate_if(_Equal(__position
.base()));
321 _Base::erase(__position
.base());
326 erase(const key_type
& __x
)
328 std::pair
<_Base_iterator
, _Base_iterator
> __victims
=
329 _Base::equal_range(__x
);
330 size_type __count
= 0;
331 _Base_iterator __victim
= __victims
.first
;
332 while (__victim
!= __victims
.second
)
334 this->_M_invalidate_if(_Equal(__victim
));
335 _Base::erase(__victim
++);
341 #if __cplusplus >= 201103L
343 erase(const_iterator __first
, const_iterator __last
)
345 // _GLIBCXX_RESOLVE_LIB_DEFECTS
346 // 151. can't currently clear() empty container
347 __glibcxx_check_erase_range(__first
, __last
);
348 for (_Base_const_iterator __victim
= __first
.base();
349 __victim
!= __last
.base(); ++__victim
)
351 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
352 _M_message(__gnu_debug::__msg_valid_range
)
353 ._M_iterator(__first
, "first")
354 ._M_iterator(__last
, "last"));
355 this->_M_invalidate_if(_Equal(__victim
));
357 return iterator(_Base::erase(__first
.base(), __last
.base()), this);
361 erase(iterator __first
, iterator __last
)
363 // _GLIBCXX_RESOLVE_LIB_DEFECTS
364 // 151. can't currently clear() empty container
365 __glibcxx_check_erase_range(__first
, __last
);
366 for (_Base_iterator __victim
= __first
.base();
367 __victim
!= __last
.base(); ++__victim
)
369 _GLIBCXX_DEBUG_VERIFY(__victim
!= _Base::end(),
370 _M_message(__gnu_debug::__msg_valid_range
)
371 ._M_iterator(__first
, "first")
372 ._M_iterator(__last
, "last"));
373 this->_M_invalidate_if(_Equal(__victim
));
375 _Base::erase(__first
.base(), __last
.base());
381 #if __cplusplus >= 201103L
382 noexcept(_Alloc_traits::_S_nothrow_swap())
385 #if __cplusplus >= 201103L
386 if (!_Alloc_traits::_S_propagate_on_swap())
387 __glibcxx_check_equal_allocs(__x
);
394 clear() _GLIBCXX_NOEXCEPT
396 this->_M_invalidate_all();
401 using _Base::key_comp
;
402 using _Base::value_comp
;
404 // 23.3.1.3 multimap operations:
406 find(const key_type
& __x
)
407 { return iterator(_Base::find(__x
), this); }
410 find(const key_type
& __x
) const
411 { return const_iterator(_Base::find(__x
), this); }
416 lower_bound(const key_type
& __x
)
417 { return iterator(_Base::lower_bound(__x
), this); }
420 lower_bound(const key_type
& __x
) const
421 { return const_iterator(_Base::lower_bound(__x
), this); }
424 upper_bound(const key_type
& __x
)
425 { return iterator(_Base::upper_bound(__x
), this); }
428 upper_bound(const key_type
& __x
) const
429 { return const_iterator(_Base::upper_bound(__x
), this); }
431 std::pair
<iterator
,iterator
>
432 equal_range(const key_type
& __x
)
434 std::pair
<_Base_iterator
, _Base_iterator
> __res
=
435 _Base::equal_range(__x
);
436 return std::make_pair(iterator(__res
.first
, this),
437 iterator(__res
.second
, this));
440 std::pair
<const_iterator
,const_iterator
>
441 equal_range(const key_type
& __x
) const
443 std::pair
<_Base_const_iterator
, _Base_const_iterator
> __res
=
444 _Base::equal_range(__x
);
445 return std::make_pair(const_iterator(__res
.first
, this),
446 const_iterator(__res
.second
, this));
450 _M_base() _GLIBCXX_NOEXCEPT
{ return *this; }
453 _M_base() const _GLIBCXX_NOEXCEPT
{ return *this; }
459 typedef __gnu_debug::_Not_equal_to
<_Base_const_iterator
> _Not_equal
;
460 this->_M_invalidate_if(_Not_equal(_Base::end()));
464 template<typename _Key
, typename _Tp
,
465 typename _Compare
, typename _Allocator
>
467 operator==(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
468 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
469 { return __lhs
._M_base() == __rhs
._M_base(); }
471 template<typename _Key
, typename _Tp
,
472 typename _Compare
, typename _Allocator
>
474 operator!=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
475 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
476 { return __lhs
._M_base() != __rhs
._M_base(); }
478 template<typename _Key
, typename _Tp
,
479 typename _Compare
, typename _Allocator
>
481 operator<(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
482 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
483 { return __lhs
._M_base() < __rhs
._M_base(); }
485 template<typename _Key
, typename _Tp
,
486 typename _Compare
, typename _Allocator
>
488 operator<=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
489 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
490 { return __lhs
._M_base() <= __rhs
._M_base(); }
492 template<typename _Key
, typename _Tp
,
493 typename _Compare
, typename _Allocator
>
495 operator>=(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
496 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
497 { return __lhs
._M_base() >= __rhs
._M_base(); }
499 template<typename _Key
, typename _Tp
,
500 typename _Compare
, typename _Allocator
>
502 operator>(const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
503 const multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
504 { return __lhs
._M_base() > __rhs
._M_base(); }
506 template<typename _Key
, typename _Tp
,
507 typename _Compare
, typename _Allocator
>
509 swap(multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __lhs
,
510 multimap
<_Key
, _Tp
, _Compare
, _Allocator
>& __rhs
)
511 { __lhs
.swap(__rhs
); }
513 } // namespace __debug