1 // Debugging multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
32 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
33 
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
36 #include <utility>
37 
38 namespace __gnu_debug_def
39 {
40   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
41 	   typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
42     class multimap
43     : public _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator>,
44     public __gnu_debug::_Safe_sequence<multimap<_Key,_Tp,_Compare,_Allocator> >
45     {
46       typedef _GLIBCXX_STD::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
47       typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
48 
49     public:
50       // types:
51       typedef _Key				     key_type;
52       typedef _Tp				     mapped_type;
53       typedef std::pair<const _Key, _Tp>             value_type;
54       typedef _Compare                               key_compare;
55       typedef _Allocator                             allocator_type;
56       typedef typename _Allocator::reference         reference;
57       typedef typename _Allocator::const_reference   const_reference;
58 
59       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
60                                                      iterator;
61       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
62                                            multimap> const_iterator;
63 
64       typedef typename _Base::size_type              size_type;
65       typedef typename _Base::difference_type        difference_type;
66       typedef typename _Allocator::pointer           pointer;
67       typedef typename _Allocator::const_pointer     const_pointer;
68       typedef std::reverse_iterator<iterator>        reverse_iterator;
69       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
70 
71       using _Base::value_compare;
72 
73       // 23.3.1.1 construct/copy/destroy:
74       explicit multimap(const _Compare& __comp = _Compare(),
75 			const _Allocator& __a = _Allocator())
76       : _Base(__comp, __a) { }
77 
78       template<typename _InputIterator>
79       multimap(_InputIterator __first, _InputIterator __last,
80 	       const _Compare& __comp = _Compare(),
81 	       const _Allocator& __a = _Allocator())
82       : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
83 	      __comp, __a) { }
84 
85       multimap(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
86       : _Base(__x), _Safe_base() { }
87 
88       multimap(const _Base& __x) : _Base(__x), _Safe_base() { }
89 
90       ~multimap() { }
91 
92       multimap<_Key,_Tp,_Compare,_Allocator>&
93       operator=(const multimap<_Key,_Tp,_Compare,_Allocator>& __x)
94       {
95 	*static_cast<_Base*>(this) = __x;
96 	this->_M_invalidate_all();
97 	return *this;
98       }
99 
100       using _Base::get_allocator;
101 
102       // iterators:
103       iterator
104       begin()
105       { return iterator(_Base::begin(), this); }
106 
107       const_iterator
108       begin() const
109       { return const_iterator(_Base::begin(), this); }
110 
111       iterator
112       end()
113       { return iterator(_Base::end(), this); }
114 
115       const_iterator
116       end() const
117       { return const_iterator(_Base::end(), this); }
118 
119       reverse_iterator
120       rbegin()
121       { return reverse_iterator(end()); }
122 
123       const_reverse_iterator
124       rbegin() const
125       { return const_reverse_iterator(end()); }
126 
127       reverse_iterator
128       rend()
129       { return reverse_iterator(begin()); }
130 
131       const_reverse_iterator
132       rend() const
133       { return const_reverse_iterator(begin()); }
134 
135       // capacity:
136       using _Base::empty;
137       using _Base::size;
138       using _Base::max_size;
139 
140       // modifiers:
141       iterator
142       insert(const value_type& __x)
143       { return iterator(_Base::insert(__x), this); }
144 
145       iterator
146       insert(iterator __position, const value_type& __x)
147       {
148 	__glibcxx_check_insert(__position);
149 	return iterator(_Base::insert(__position.base(), __x), this);
150       }
151 
152       template<typename _InputIterator>
153         void
154         insert(_InputIterator __first, _InputIterator __last)
155         {
156 	  __glibcxx_check_valid_range(__first, __last);
157 	  _Base::insert(__first, __last);
158 	}
159 
160       void
161       erase(iterator __position)
162       {
163 	__glibcxx_check_erase(__position);
164 	__position._M_invalidate();
165 	_Base::erase(__position.base());
166       }
167 
168       size_type
169       erase(const key_type& __x)
170       {
171 	std::pair<iterator, iterator> __victims = this->equal_range(__x);
172 	size_type __count = 0;
173 	while (__victims.first != __victims.second)
174 	{
175 	  iterator __victim = __victims.first++;
176 	  __victim._M_invalidate();
177 	  _Base::erase(__victim.base());
178 	  ++__count;
179 	}
180 	return __count;
181       }
182 
183       void
184       erase(iterator __first, iterator __last)
185       {
186 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
187 	// 151. can't currently clear() empty container
188 	__glibcxx_check_erase_range(__first, __last);
189 	while (__first != __last)
190 	this->erase(__first++);
191       }
192 
193       void
194       swap(multimap<_Key,_Tp,_Compare,_Allocator>& __x)
195       {
196 	_Base::swap(__x);
197 	this->_M_swap(__x);
198       }
199 
200       void
201       clear()
202       { this->erase(begin(), end()); }
203 
204       // observers:
205       using _Base::key_comp;
206       using _Base::value_comp;
207 
208       // 23.3.1.3 multimap operations:
209       iterator
210       find(const key_type& __x)
211       { return iterator(_Base::find(__x), this); }
212 
213       const_iterator
214       find(const key_type& __x) const
215       { return const_iterator(_Base::find(__x), this); }
216 
217       using _Base::count;
218 
219       iterator
220       lower_bound(const key_type& __x)
221       { return iterator(_Base::lower_bound(__x), this); }
222 
223       const_iterator
224       lower_bound(const key_type& __x) const
225       { return const_iterator(_Base::lower_bound(__x), this); }
226 
227       iterator
228       upper_bound(const key_type& __x)
229       { return iterator(_Base::upper_bound(__x), this); }
230 
231       const_iterator
232       upper_bound(const key_type& __x) const
233       { return const_iterator(_Base::upper_bound(__x), this); }
234 
235       std::pair<iterator,iterator>
236       equal_range(const key_type& __x)
237       {
238 	typedef typename _Base::iterator _Base_iterator;
239 	std::pair<_Base_iterator, _Base_iterator> __res =
240 	_Base::equal_range(__x);
241 	return std::make_pair(iterator(__res.first, this),
242 			      iterator(__res.second, this));
243       }
244 
245       std::pair<const_iterator,const_iterator>
246       equal_range(const key_type& __x) const
247       {
248 	typedef typename _Base::const_iterator _Base_const_iterator;
249 	std::pair<_Base_const_iterator, _Base_const_iterator> __res =
250 	_Base::equal_range(__x);
251 	return std::make_pair(const_iterator(__res.first, this),
252 			      const_iterator(__res.second, this));
253       }
254 
255       _Base&
256       _M_base() { return *this; }
257 
258       const _Base&
259       _M_base() const { return *this; }
260 
261     private:
262       void
263       _M_invalidate_all()
264       {
265 	typedef typename _Base::const_iterator _Base_const_iterator;
266 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
267 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
268       }
269     };
270 
271   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
272     inline bool
273     operator==(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
274 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
275     { return __lhs._M_base() == __rhs._M_base(); }
276 
277   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
278     inline bool
279     operator!=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
280 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
281     { return __lhs._M_base() != __rhs._M_base(); }
282 
283   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
284     inline bool
285     operator<(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
286 	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
287     { return __lhs._M_base() < __rhs._M_base(); }
288 
289   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
290     inline bool
291     operator<=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
292 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
293     { return __lhs._M_base() <= __rhs._M_base(); }
294 
295   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
296     inline bool
297     operator>=(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
298 	       const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
299     { return __lhs._M_base() >= __rhs._M_base(); }
300 
301   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
302     inline bool
303     operator>(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
304 	      const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
305     { return __lhs._M_base() > __rhs._M_base(); }
306 
307   template<typename _Key,typename _Tp,typename _Compare,typename _Allocator>
308     inline void
309     swap(multimap<_Key,_Tp,_Compare,_Allocator>& __lhs,
310 	 multimap<_Key,_Tp,_Compare,_Allocator>& __rhs)
311     { __lhs.swap(__rhs); }
312 } // namespace __gnu_debug_def
313 
314 #endif
315