1132720Skan// Debugging multimap implementation -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2003, 2004, 2005
4132720Skan// Free Software Foundation, Inc.
5132720Skan//
6132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
7132720Skan// software; you can redistribute it and/or modify it under the
8132720Skan// terms of the GNU General Public License as published by the
9132720Skan// Free Software Foundation; either version 2, or (at your option)
10132720Skan// any later version.
11132720Skan
12132720Skan// This library is distributed in the hope that it will be useful,
13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132720Skan// GNU General Public License for more details.
16132720Skan
17132720Skan// You should have received a copy of the GNU General Public License along
18132720Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20132720Skan// USA.
21132720Skan
22132720Skan// As a special exception, you may use this file as part of a free software
23132720Skan// library without restriction.  Specifically, if other files instantiate
24132720Skan// templates or use macros or inline functions from this file, or you compile
25132720Skan// this file and link it with other files to produce an executable, this
26132720Skan// file does not by itself cause the resulting executable to be covered by
27132720Skan// the GNU General Public License.  This exception does not however
28132720Skan// invalidate any other reasons why the executable file might be covered by
29132720Skan// the GNU General Public License.
30132720Skan
31169691Skan/** @file debug/multimap.h
32169691Skan *  This file is a GNU debug extension to the Standard C++ Library.
33169691Skan */
34169691Skan
35132720Skan#ifndef _GLIBCXX_DEBUG_MULTIMAP_H
36132720Skan#define _GLIBCXX_DEBUG_MULTIMAP_H 1
37132720Skan
38132720Skan#include <debug/safe_sequence.h>
39132720Skan#include <debug/safe_iterator.h>
40132720Skan#include <utility>
41132720Skan
42169691Skannamespace std
43132720Skan{
44169691Skannamespace __debug
45169691Skan{
46132720Skan  template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
47132720Skan	   typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
48132720Skan    class multimap
49132720Skan    : public _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator>,
50132720Skan    public __gnu_debug::_Safe_sequence<multimap<_Key,_Tp,_Compare,_Allocator> >
51132720Skan    {
52132720Skan      typedef _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
53132720Skan      typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
54132720Skan
55132720Skan    public:
56132720Skan      // types:
57132720Skan      typedef _Key				     key_type;
58132720Skan      typedef _Tp				     mapped_type;
59132720Skan      typedef std::pair<const _Key, _Tp>             value_type;
60132720Skan      typedef _Compare                               key_compare;
61132720Skan      typedef _Allocator                             allocator_type;
62169691Skan      typedef typename _Base::reference              reference;
63169691Skan      typedef typename _Base::const_reference        const_reference;
64132720Skan
65132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
66132720Skan                                                     iterator;
67132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
68132720Skan                                           multimap> const_iterator;
69132720Skan
70132720Skan      typedef typename _Base::size_type              size_type;
71132720Skan      typedef typename _Base::difference_type        difference_type;
72169691Skan      typedef typename _Base::pointer                pointer;
73169691Skan      typedef typename _Base::const_pointer          const_pointer;
74132720Skan      typedef std::reverse_iterator<iterator>        reverse_iterator;
75132720Skan      typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
76132720Skan
77132720Skan      // 23.3.1.1 construct/copy/destroy:
78132720Skan      explicit multimap(const _Compare& __comp = _Compare(),
79132720Skan			const _Allocator& __a = _Allocator())
80132720Skan      : _Base(__comp, __a) { }
81132720Skan
82132720Skan      template<typename _InputIterator>
83132720Skan      multimap(_InputIterator __first, _InputIterator __last,
84132720Skan	       const _Compare& __comp = _Compare(),
85132720Skan	       const _Allocator& __a = _Allocator())
86132720Skan      : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
87132720Skan	      __comp, __a) { }
88132720Skan
89132720Skan      multimap(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
90132720Skan      : _Base(__x), _Safe_base() { }
91132720Skan
92132720Skan      multimap(const _Base& __x) : _Base(__x), _Safe_base() { }
93132720Skan
94132720Skan      ~multimap() { }
95132720Skan
96132720Skan      multimap<_Key,_Tp,_Compare,_Allocator>&
97132720Skan      operator=(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
98132720Skan      {
99132720Skan	*static_cast<_Base*>(this) = __x;
100132720Skan	this->_M_invalidate_all();
101132720Skan	return *this;
102132720Skan      }
103132720Skan
104132720Skan      using _Base::get_allocator;
105132720Skan
106132720Skan      // iterators:
107132720Skan      iterator
108132720Skan      begin()
109132720Skan      { return iterator(_Base::begin(), this); }
110132720Skan
111132720Skan      const_iterator
112132720Skan      begin() const
113132720Skan      { return const_iterator(_Base::begin(), this); }
114132720Skan
115132720Skan      iterator
116132720Skan      end()
117132720Skan      { return iterator(_Base::end(), this); }
118132720Skan
119132720Skan      const_iterator
120132720Skan      end() const
121132720Skan      { return const_iterator(_Base::end(), this); }
122132720Skan
123132720Skan      reverse_iterator
124132720Skan      rbegin()
125132720Skan      { return reverse_iterator(end()); }
126132720Skan
127132720Skan      const_reverse_iterator
128132720Skan      rbegin() const
129132720Skan      { return const_reverse_iterator(end()); }
130132720Skan
131132720Skan      reverse_iterator
132132720Skan      rend()
133132720Skan      { return reverse_iterator(begin()); }
134132720Skan
135132720Skan      const_reverse_iterator
136132720Skan      rend() const
137132720Skan      { return const_reverse_iterator(begin()); }
138132720Skan
139132720Skan      // capacity:
140132720Skan      using _Base::empty;
141132720Skan      using _Base::size;
142132720Skan      using _Base::max_size;
143132720Skan
144132720Skan      // modifiers:
145132720Skan      iterator
146132720Skan      insert(const value_type& __x)
147132720Skan      { return iterator(_Base::insert(__x), this); }
148132720Skan
149132720Skan      iterator
150132720Skan      insert(iterator __position, const value_type& __x)
151132720Skan      {
152132720Skan	__glibcxx_check_insert(__position);
153132720Skan	return iterator(_Base::insert(__position.base(), __x), this);
154132720Skan      }
155132720Skan
156132720Skan      template<typename _InputIterator>
157132720Skan        void
158132720Skan        insert(_InputIterator __first, _InputIterator __last)
159132720Skan        {
160132720Skan	  __glibcxx_check_valid_range(__first, __last);
161132720Skan	  _Base::insert(__first, __last);
162132720Skan	}
163132720Skan
164132720Skan      void
165132720Skan      erase(iterator __position)
166132720Skan      {
167132720Skan	__glibcxx_check_erase(__position);
168132720Skan	__position._M_invalidate();
169132720Skan	_Base::erase(__position.base());
170132720Skan      }
171132720Skan
172132720Skan      size_type
173132720Skan      erase(const key_type& __x)
174132720Skan      {
175132720Skan	std::pair<iterator, iterator> __victims = this->equal_range(__x);
176132720Skan	size_type __count = 0;
177132720Skan	while (__victims.first != __victims.second)
178132720Skan	{
179132720Skan	  iterator __victim = __victims.first++;
180132720Skan	  __victim._M_invalidate();
181132720Skan	  _Base::erase(__victim.base());
182132720Skan	  ++__count;
183132720Skan	}
184132720Skan	return __count;
185132720Skan      }
186132720Skan
187132720Skan      void
188132720Skan      erase(iterator __first, iterator __last)
189132720Skan      {
190132720Skan	// _GLIBCXX_RESOLVE_LIB_DEFECTS
191132720Skan	// 151. can't currently clear() empty container
192132720Skan	__glibcxx_check_erase_range(__first, __last);
193132720Skan	while (__first != __last)
194132720Skan	this->erase(__first++);
195132720Skan      }
196132720Skan
197132720Skan      void
198132720Skan      swap(multimap<_Key,_Tp,_Compare,_Allocator>& __x)
199132720Skan      {
200132720Skan	_Base::swap(__x);
201132720Skan	this->_M_swap(__x);
202132720Skan      }
203132720Skan
204132720Skan      void
205132720Skan      clear()
206132720Skan      { this->erase(begin(), end()); }
207132720Skan
208132720Skan      // observers:
209132720Skan      using _Base::key_comp;
210132720Skan      using _Base::value_comp;
211132720Skan
212132720Skan      // 23.3.1.3 multimap operations:
213132720Skan      iterator
214132720Skan      find(const key_type& __x)
215132720Skan      { return iterator(_Base::find(__x), this); }
216132720Skan
217132720Skan      const_iterator
218132720Skan      find(const key_type& __x) const
219132720Skan      { return const_iterator(_Base::find(__x), this); }
220132720Skan
221132720Skan      using _Base::count;
222132720Skan
223132720Skan      iterator
224132720Skan      lower_bound(const key_type& __x)
225132720Skan      { return iterator(_Base::lower_bound(__x), this); }
226132720Skan
227132720Skan      const_iterator
228132720Skan      lower_bound(const key_type& __x) const
229132720Skan      { return const_iterator(_Base::lower_bound(__x), this); }
230132720Skan
231132720Skan      iterator
232132720Skan      upper_bound(const key_type& __x)
233132720Skan      { return iterator(_Base::upper_bound(__x), this); }
234132720Skan
235132720Skan      const_iterator
236132720Skan      upper_bound(const key_type& __x) const
237132720Skan      { return const_iterator(_Base::upper_bound(__x), this); }
238132720Skan
239132720Skan      std::pair<iterator,iterator>
240132720Skan      equal_range(const key_type& __x)
241132720Skan      {
242132720Skan	typedef typename _Base::iterator _Base_iterator;
243132720Skan	std::pair<_Base_iterator, _Base_iterator> __res =
244132720Skan	_Base::equal_range(__x);
245132720Skan	return std::make_pair(iterator(__res.first, this),
246132720Skan			      iterator(__res.second, this));
247132720Skan      }
248132720Skan
249132720Skan      std::pair<const_iterator,const_iterator>
250132720Skan      equal_range(const key_type& __x) const
251132720Skan      {
252132720Skan	typedef typename _Base::const_iterator _Base_const_iterator;
253132720Skan	std::pair<_Base_const_iterator, _Base_const_iterator> __res =
254132720Skan	_Base::equal_range(__x);
255132720Skan	return std::make_pair(const_iterator(__res.first, this),
256132720Skan			      const_iterator(__res.second, this));
257132720Skan      }
258132720Skan
259132720Skan      _Base&
260132720Skan      _M_base() { return *this; }
261132720Skan
262132720Skan      const _Base&
263132720Skan      _M_base() const { return *this; }
264132720Skan
265132720Skan    private:
266132720Skan      void
267132720Skan      _M_invalidate_all()
268132720Skan      {
269132720Skan	typedef typename _Base::const_iterator _Base_const_iterator;
270132720Skan	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
271132720Skan	this->_M_invalidate_if(_Not_equal(_M_base().end()));
272132720Skan      }
273132720Skan    };
274132720Skan
275132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
276132720Skan    inline bool
277132720Skan    operator==(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
278132720Skan	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
279132720Skan    { return __lhs._M_base() == __rhs._M_base(); }
280132720Skan
281132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
282132720Skan    inline bool
283132720Skan    operator!=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
284132720Skan	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
285132720Skan    { return __lhs._M_base() != __rhs._M_base(); }
286132720Skan
287132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
288132720Skan    inline bool
289132720Skan    operator<(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
290132720Skan	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
291132720Skan    { return __lhs._M_base() < __rhs._M_base(); }
292132720Skan
293132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
294132720Skan    inline bool
295132720Skan    operator<=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
296132720Skan	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
297132720Skan    { return __lhs._M_base() <= __rhs._M_base(); }
298132720Skan
299132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
300132720Skan    inline bool
301132720Skan    operator>=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
302132720Skan	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
303132720Skan    { return __lhs._M_base() >= __rhs._M_base(); }
304132720Skan
305132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
306132720Skan    inline bool
307132720Skan    operator>(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
308132720Skan	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
309132720Skan    { return __lhs._M_base() > __rhs._M_base(); }
310132720Skan
311132720Skan  template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
312132720Skan    inline void
313132720Skan    swap(multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
314132720Skan	 multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
315132720Skan    { __lhs.swap(__rhs); }
316169691Skan} // namespace __debug
317169691Skan} // namespace std
318132720Skan
319132720Skan#endif
320