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