xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/multimap.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Debugging multimap implementation -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2003, 2004, 2005
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file debug/multimap.h
32*404b540aSrobert  *  This file is a GNU debug extension to the Standard C++ Library.
33*404b540aSrobert  */
34*404b540aSrobert 
35*404b540aSrobert #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
36*404b540aSrobert #define _GLIBCXX_DEBUG_MULTIMAP_H 1
37*404b540aSrobert 
38*404b540aSrobert #include <debug/safe_sequence.h>
39*404b540aSrobert #include <debug/safe_iterator.h>
40*404b540aSrobert #include <utility>
41*404b540aSrobert 
42*404b540aSrobert namespace std
43*404b540aSrobert {
44*404b540aSrobert namespace __debug
45*404b540aSrobert {
46*404b540aSrobert   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
47*404b540aSrobert 	   typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
48*404b540aSrobert     class multimap
49*404b540aSrobert     : public _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator>,
50*404b540aSrobert     public __gnu_debug::_Safe_sequence<multimap<_Key,_Tp,_Compare,_Allocator> >
51*404b540aSrobert     {
52*404b540aSrobert       typedef _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
53*404b540aSrobert       typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
54*404b540aSrobert 
55*404b540aSrobert     public:
56*404b540aSrobert       // types:
57*404b540aSrobert       typedef _Key				     key_type;
58*404b540aSrobert       typedef _Tp				     mapped_type;
59*404b540aSrobert       typedef std::pair<const _Key, _Tp>             value_type;
60*404b540aSrobert       typedef _Compare                               key_compare;
61*404b540aSrobert       typedef _Allocator                             allocator_type;
62*404b540aSrobert       typedef typename _Base::reference              reference;
63*404b540aSrobert       typedef typename _Base::const_reference        const_reference;
64*404b540aSrobert 
65*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
66*404b540aSrobert                                                      iterator;
67*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
68*404b540aSrobert                                            multimap> const_iterator;
69*404b540aSrobert 
70*404b540aSrobert       typedef typename _Base::size_type              size_type;
71*404b540aSrobert       typedef typename _Base::difference_type        difference_type;
72*404b540aSrobert       typedef typename _Base::pointer                pointer;
73*404b540aSrobert       typedef typename _Base::const_pointer          const_pointer;
74*404b540aSrobert       typedef std::reverse_iterator<iterator>        reverse_iterator;
75*404b540aSrobert       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
76*404b540aSrobert 
77*404b540aSrobert       using _Base::value_compare;
78*404b540aSrobert 
79*404b540aSrobert       // 23.3.1.1 construct/copy/destroy:
80*404b540aSrobert       explicit multimap(const _Compare& __comp = _Compare(),
81*404b540aSrobert 			const _Allocator& __a = _Allocator())
_Base(__comp,__a)82*404b540aSrobert       : _Base(__comp, __a) { }
83*404b540aSrobert 
84*404b540aSrobert       template<typename _InputIterator>
85*404b540aSrobert       multimap(_InputIterator __first, _InputIterator __last,
86*404b540aSrobert 	       const _Compare& __comp = _Compare(),
87*404b540aSrobert 	       const _Allocator& __a = _Allocator())
_Base(__gnu_debug::__check_valid_range (__first,__last),__last,__comp,__a)88*404b540aSrobert       : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
89*404b540aSrobert 	      __comp, __a) { }
90*404b540aSrobert 
multimap(const multimap<_Key,_Tp,_Compare,_Allocator> & __x)91*404b540aSrobert       multimap(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
92*404b540aSrobert       : _Base(__x), _Safe_base() { }
93*404b540aSrobert 
multimap(const _Base & __x)94*404b540aSrobert       multimap(const _Base& __x) : _Base(__x), _Safe_base() { }
95*404b540aSrobert 
~multimap()96*404b540aSrobert       ~multimap() { }
97*404b540aSrobert 
98*404b540aSrobert       multimap<_Key,_Tp,_Compare,_Allocator>&
99*404b540aSrobert       operator=(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
100*404b540aSrobert       {
101*404b540aSrobert 	*static_cast<_Base*>(this) = __x;
102*404b540aSrobert 	this->_M_invalidate_all();
103*404b540aSrobert 	return *this;
104*404b540aSrobert       }
105*404b540aSrobert 
106*404b540aSrobert       using _Base::get_allocator;
107*404b540aSrobert 
108*404b540aSrobert       // iterators:
109*404b540aSrobert       iterator
begin()110*404b540aSrobert       begin()
111*404b540aSrobert       { return iterator(_Base::begin(), this); }
112*404b540aSrobert 
113*404b540aSrobert       const_iterator
begin()114*404b540aSrobert       begin() const
115*404b540aSrobert       { return const_iterator(_Base::begin(), this); }
116*404b540aSrobert 
117*404b540aSrobert       iterator
end()118*404b540aSrobert       end()
119*404b540aSrobert       { return iterator(_Base::end(), this); }
120*404b540aSrobert 
121*404b540aSrobert       const_iterator
end()122*404b540aSrobert       end() const
123*404b540aSrobert       { return const_iterator(_Base::end(), this); }
124*404b540aSrobert 
125*404b540aSrobert       reverse_iterator
rbegin()126*404b540aSrobert       rbegin()
127*404b540aSrobert       { return reverse_iterator(end()); }
128*404b540aSrobert 
129*404b540aSrobert       const_reverse_iterator
rbegin()130*404b540aSrobert       rbegin() const
131*404b540aSrobert       { return const_reverse_iterator(end()); }
132*404b540aSrobert 
133*404b540aSrobert       reverse_iterator
rend()134*404b540aSrobert       rend()
135*404b540aSrobert       { return reverse_iterator(begin()); }
136*404b540aSrobert 
137*404b540aSrobert       const_reverse_iterator
rend()138*404b540aSrobert       rend() const
139*404b540aSrobert       { return const_reverse_iterator(begin()); }
140*404b540aSrobert 
141*404b540aSrobert       // capacity:
142*404b540aSrobert       using _Base::empty;
143*404b540aSrobert       using _Base::size;
144*404b540aSrobert       using _Base::max_size;
145*404b540aSrobert 
146*404b540aSrobert       // modifiers:
147*404b540aSrobert       iterator
insert(const value_type & __x)148*404b540aSrobert       insert(const value_type& __x)
149*404b540aSrobert       { return iterator(_Base::insert(__x), this); }
150*404b540aSrobert 
151*404b540aSrobert       iterator
insert(iterator __position,const value_type & __x)152*404b540aSrobert       insert(iterator __position, const value_type& __x)
153*404b540aSrobert       {
154*404b540aSrobert 	__glibcxx_check_insert(__position);
155*404b540aSrobert 	return iterator(_Base::insert(__position.base(), __x), this);
156*404b540aSrobert       }
157*404b540aSrobert 
158*404b540aSrobert       template<typename _InputIterator>
159*404b540aSrobert         void
insert(_InputIterator __first,_InputIterator __last)160*404b540aSrobert         insert(_InputIterator __first, _InputIterator __last)
161*404b540aSrobert         {
162*404b540aSrobert 	  __glibcxx_check_valid_range(__first, __last);
163*404b540aSrobert 	  _Base::insert(__first, __last);
164*404b540aSrobert 	}
165*404b540aSrobert 
166*404b540aSrobert       void
erase(iterator __position)167*404b540aSrobert       erase(iterator __position)
168*404b540aSrobert       {
169*404b540aSrobert 	__glibcxx_check_erase(__position);
170*404b540aSrobert 	__position._M_invalidate();
171*404b540aSrobert 	_Base::erase(__position.base());
172*404b540aSrobert       }
173*404b540aSrobert 
174*404b540aSrobert       size_type
erase(const key_type & __x)175*404b540aSrobert       erase(const key_type& __x)
176*404b540aSrobert       {
177*404b540aSrobert 	std::pair<iterator, iterator> __victims = this->equal_range(__x);
178*404b540aSrobert 	size_type __count = 0;
179*404b540aSrobert 	while (__victims.first != __victims.second)
180*404b540aSrobert 	{
181*404b540aSrobert 	  iterator __victim = __victims.first++;
182*404b540aSrobert 	  __victim._M_invalidate();
183*404b540aSrobert 	  _Base::erase(__victim.base());
184*404b540aSrobert 	  ++__count;
185*404b540aSrobert 	}
186*404b540aSrobert 	return __count;
187*404b540aSrobert       }
188*404b540aSrobert 
189*404b540aSrobert       void
erase(iterator __first,iterator __last)190*404b540aSrobert       erase(iterator __first, iterator __last)
191*404b540aSrobert       {
192*404b540aSrobert 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
193*404b540aSrobert 	// 151. can't currently clear() empty container
194*404b540aSrobert 	__glibcxx_check_erase_range(__first, __last);
195*404b540aSrobert 	while (__first != __last)
196*404b540aSrobert 	this->erase(__first++);
197*404b540aSrobert       }
198*404b540aSrobert 
199*404b540aSrobert       void
swap(multimap<_Key,_Tp,_Compare,_Allocator> & __x)200*404b540aSrobert       swap(multimap<_Key,_Tp,_Compare,_Allocator>& __x)
201*404b540aSrobert       {
202*404b540aSrobert 	_Base::swap(__x);
203*404b540aSrobert 	this->_M_swap(__x);
204*404b540aSrobert       }
205*404b540aSrobert 
206*404b540aSrobert       void
clear()207*404b540aSrobert       clear()
208*404b540aSrobert       { this->erase(begin(), end()); }
209*404b540aSrobert 
210*404b540aSrobert       // observers:
211*404b540aSrobert       using _Base::key_comp;
212*404b540aSrobert       using _Base::value_comp;
213*404b540aSrobert 
214*404b540aSrobert       // 23.3.1.3 multimap operations:
215*404b540aSrobert       iterator
find(const key_type & __x)216*404b540aSrobert       find(const key_type& __x)
217*404b540aSrobert       { return iterator(_Base::find(__x), this); }
218*404b540aSrobert 
219*404b540aSrobert       const_iterator
find(const key_type & __x)220*404b540aSrobert       find(const key_type& __x) const
221*404b540aSrobert       { return const_iterator(_Base::find(__x), this); }
222*404b540aSrobert 
223*404b540aSrobert       using _Base::count;
224*404b540aSrobert 
225*404b540aSrobert       iterator
lower_bound(const key_type & __x)226*404b540aSrobert       lower_bound(const key_type& __x)
227*404b540aSrobert       { return iterator(_Base::lower_bound(__x), this); }
228*404b540aSrobert 
229*404b540aSrobert       const_iterator
lower_bound(const key_type & __x)230*404b540aSrobert       lower_bound(const key_type& __x) const
231*404b540aSrobert       { return const_iterator(_Base::lower_bound(__x), this); }
232*404b540aSrobert 
233*404b540aSrobert       iterator
upper_bound(const key_type & __x)234*404b540aSrobert       upper_bound(const key_type& __x)
235*404b540aSrobert       { return iterator(_Base::upper_bound(__x), this); }
236*404b540aSrobert 
237*404b540aSrobert       const_iterator
upper_bound(const key_type & __x)238*404b540aSrobert       upper_bound(const key_type& __x) const
239*404b540aSrobert       { return const_iterator(_Base::upper_bound(__x), this); }
240*404b540aSrobert 
241*404b540aSrobert       std::pair<iterator,iterator>
equal_range(const key_type & __x)242*404b540aSrobert       equal_range(const key_type& __x)
243*404b540aSrobert       {
244*404b540aSrobert 	typedef typename _Base::iterator _Base_iterator;
245*404b540aSrobert 	std::pair<_Base_iterator, _Base_iterator> __res =
246*404b540aSrobert 	_Base::equal_range(__x);
247*404b540aSrobert 	return std::make_pair(iterator(__res.first, this),
248*404b540aSrobert 			      iterator(__res.second, this));
249*404b540aSrobert       }
250*404b540aSrobert 
251*404b540aSrobert       std::pair<const_iterator,const_iterator>
equal_range(const key_type & __x)252*404b540aSrobert       equal_range(const key_type& __x) const
253*404b540aSrobert       {
254*404b540aSrobert 	typedef typename _Base::const_iterator _Base_const_iterator;
255*404b540aSrobert 	std::pair<_Base_const_iterator, _Base_const_iterator> __res =
256*404b540aSrobert 	_Base::equal_range(__x);
257*404b540aSrobert 	return std::make_pair(const_iterator(__res.first, this),
258*404b540aSrobert 			      const_iterator(__res.second, this));
259*404b540aSrobert       }
260*404b540aSrobert 
261*404b540aSrobert       _Base&
_M_base()262*404b540aSrobert       _M_base() { return *this; }
263*404b540aSrobert 
264*404b540aSrobert       const _Base&
_M_base()265*404b540aSrobert       _M_base() const { return *this; }
266*404b540aSrobert 
267*404b540aSrobert     private:
268*404b540aSrobert       void
_M_invalidate_all()269*404b540aSrobert       _M_invalidate_all()
270*404b540aSrobert       {
271*404b540aSrobert 	typedef typename _Base::const_iterator _Base_const_iterator;
272*404b540aSrobert 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
273*404b540aSrobert 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
274*404b540aSrobert       }
275*404b540aSrobert     };
276*404b540aSrobert 
277*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
278*404b540aSrobert     inline bool
279*404b540aSrobert     operator==(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
280*404b540aSrobert 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
281*404b540aSrobert     { return __lhs._M_base() == __rhs._M_base(); }
282*404b540aSrobert 
283*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
284*404b540aSrobert     inline bool
285*404b540aSrobert     operator!=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
286*404b540aSrobert 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
287*404b540aSrobert     { return __lhs._M_base() != __rhs._M_base(); }
288*404b540aSrobert 
289*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
290*404b540aSrobert     inline bool
291*404b540aSrobert     operator<(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
292*404b540aSrobert 	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
293*404b540aSrobert     { return __lhs._M_base() < __rhs._M_base(); }
294*404b540aSrobert 
295*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
296*404b540aSrobert     inline bool
297*404b540aSrobert     operator<=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
298*404b540aSrobert 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
299*404b540aSrobert     { return __lhs._M_base() <= __rhs._M_base(); }
300*404b540aSrobert 
301*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
302*404b540aSrobert     inline bool
303*404b540aSrobert     operator>=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
304*404b540aSrobert 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
305*404b540aSrobert     { return __lhs._M_base() >= __rhs._M_base(); }
306*404b540aSrobert 
307*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
308*404b540aSrobert     inline bool
309*404b540aSrobert     operator>(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
310*404b540aSrobert 	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
311*404b540aSrobert     { return __lhs._M_base() > __rhs._M_base(); }
312*404b540aSrobert 
313*404b540aSrobert   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
314*404b540aSrobert     inline void
swap(multimap<_Key,_Tp,_Compare,_Allocator> & __lhs,multimap<_Key,_Tp,_Compare,_Allocator> & __rhs)315*404b540aSrobert     swap(multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
316*404b540aSrobert 	 multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
317*404b540aSrobert     { __lhs.swap(__rhs); }
318*404b540aSrobert } // namespace __debug
319*404b540aSrobert } // namespace std
320*404b540aSrobert 
321*404b540aSrobert #endif
322