xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/debug/multimap.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 // Debugging multimap implementation -*- C++ -*-
2 
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file debug/multimap.h
27  *  This file is a GNU debug extension to the Standard C++ Library.
28  */
29 
30 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
31 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
32 
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
35 #include <utility>
36 
37 namespace std
38 {
39 namespace __debug
40 {
41   /// Class std::multimap with safety/checking/debug instrumentation.
42   template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
43 	   typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
44     class multimap
45     : public _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator>,
46       public __gnu_debug::_Safe_sequence<multimap<_Key, _Tp,
47 						  _Compare, _Allocator> >
48     {
49       typedef _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
50       typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
51 
52     public:
53       // types:
54       typedef _Key				     key_type;
55       typedef _Tp				     mapped_type;
56       typedef std::pair<const _Key, _Tp>             value_type;
57       typedef _Compare                               key_compare;
58       typedef _Allocator                             allocator_type;
59       typedef typename _Base::reference              reference;
60       typedef typename _Base::const_reference        const_reference;
61 
62       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
63                                                      iterator;
64       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
65                                            multimap> const_iterator;
66 
67       typedef typename _Base::size_type              size_type;
68       typedef typename _Base::difference_type        difference_type;
69       typedef typename _Base::pointer                pointer;
70       typedef typename _Base::const_pointer          const_pointer;
71       typedef std::reverse_iterator<iterator>        reverse_iterator;
72       typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
73 
74       using _Base::value_compare;
75 
76       // 23.3.1.1 construct/copy/destroy:
77       explicit multimap(const _Compare& __comp = _Compare(),
78 			const _Allocator& __a = _Allocator())
79       : _Base(__comp, __a) { }
80 
81       template<typename _InputIterator>
82       multimap(_InputIterator __first, _InputIterator __last,
83 	       const _Compare& __comp = _Compare(),
84 	       const _Allocator& __a = _Allocator())
85       : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
86 	      __comp, __a) { }
87 
88       multimap(const multimap& __x)
89       : _Base(__x), _Safe_base() { }
90 
91       multimap(const _Base& __x)
92       : _Base(__x), _Safe_base() { }
93 
94 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95       multimap(multimap&& __x)
96       : _Base(std::forward<multimap>(__x)), _Safe_base()
97       { this->_M_swap(__x); }
98 
99       multimap(initializer_list<value_type> __l,
100 	       const _Compare& __c = _Compare(),
101 	       const allocator_type& __a = allocator_type())
102       : _Base(__l, __c, __a), _Safe_base() { }
103 #endif
104 
105       ~multimap() { }
106 
107       multimap&
108       operator=(const multimap& __x)
109       {
110 	*static_cast<_Base*>(this) = __x;
111 	this->_M_invalidate_all();
112 	return *this;
113       }
114 
115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
116       multimap&
117       operator=(multimap&& __x)
118       {
119 	// NB: DR 1204.
120 	// NB: DR 675.
121 	clear();
122 	swap(__x);
123 	return *this;
124       }
125 
126       multimap&
127       operator=(initializer_list<value_type> __l)
128       {
129 	this->clear();
130 	this->insert(__l);
131 	return *this;
132       }
133 #endif
134 
135       using _Base::get_allocator;
136 
137       // iterators:
138       iterator
139       begin()
140       { return iterator(_Base::begin(), this); }
141 
142       const_iterator
143       begin() const
144       { return const_iterator(_Base::begin(), this); }
145 
146       iterator
147       end()
148       { return iterator(_Base::end(), this); }
149 
150       const_iterator
151       end() const
152       { return const_iterator(_Base::end(), this); }
153 
154       reverse_iterator
155       rbegin()
156       { return reverse_iterator(end()); }
157 
158       const_reverse_iterator
159       rbegin() const
160       { return const_reverse_iterator(end()); }
161 
162       reverse_iterator
163       rend()
164       { return reverse_iterator(begin()); }
165 
166       const_reverse_iterator
167       rend() const
168       { return const_reverse_iterator(begin()); }
169 
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171       const_iterator
172       cbegin() const
173       { return const_iterator(_Base::begin(), this); }
174 
175       const_iterator
176       cend() const
177       { return const_iterator(_Base::end(), this); }
178 
179       const_reverse_iterator
180       crbegin() const
181       { return const_reverse_iterator(end()); }
182 
183       const_reverse_iterator
184       crend() const
185       { return const_reverse_iterator(begin()); }
186 #endif
187 
188       // capacity:
189       using _Base::empty;
190       using _Base::size;
191       using _Base::max_size;
192 
193       // modifiers:
194       iterator
195       insert(const value_type& __x)
196       { return iterator(_Base::insert(__x), this); }
197 
198 #ifdef __GXX_EXPERIMENTAL_CXX0X__
199       void
200       insert(std::initializer_list<value_type> __list)
201       { _Base::insert(__list); }
202 #endif
203 
204       iterator
205       insert(iterator __position, const value_type& __x)
206       {
207 	__glibcxx_check_insert(__position);
208 	return iterator(_Base::insert(__position.base(), __x), this);
209       }
210 
211       template<typename _InputIterator>
212         void
213         insert(_InputIterator __first, _InputIterator __last)
214         {
215 	  __glibcxx_check_valid_range(__first, __last);
216 	  _Base::insert(__first, __last);
217 	}
218 
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
220       iterator
221       erase(iterator __position)
222       {
223 	__glibcxx_check_erase(__position);
224 	__position._M_invalidate();
225 	return iterator(_Base::erase(__position.base()), this);
226       }
227 #else
228       void
229       erase(iterator __position)
230       {
231 	__glibcxx_check_erase(__position);
232 	__position._M_invalidate();
233 	_Base::erase(__position.base());
234       }
235 #endif
236 
237       size_type
238       erase(const key_type& __x)
239       {
240 	std::pair<iterator, iterator> __victims = this->equal_range(__x);
241 	size_type __count = 0;
242 	while (__victims.first != __victims.second)
243 	{
244 	  iterator __victim = __victims.first++;
245 	  __victim._M_invalidate();
246 	  _Base::erase(__victim.base());
247 	  ++__count;
248 	}
249 	return __count;
250       }
251 
252 #ifdef __GXX_EXPERIMENTAL_CXX0X__
253       iterator
254       erase(iterator __first, iterator __last)
255       {
256 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
257 	// 151. can't currently clear() empty container
258 	__glibcxx_check_erase_range(__first, __last);
259 	while (__first != __last)
260 	  this->erase(__first++);
261 	return __last;
262       }
263 #else
264       void
265       erase(iterator __first, iterator __last)
266       {
267 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
268 	// 151. can't currently clear() empty container
269 	__glibcxx_check_erase_range(__first, __last);
270 	while (__first != __last)
271 	  this->erase(__first++);
272       }
273 #endif
274 
275       void
276       swap(multimap& __x)
277       {
278 	_Base::swap(__x);
279 	this->_M_swap(__x);
280       }
281 
282       void
283       clear()
284       { this->erase(begin(), end()); }
285 
286       // observers:
287       using _Base::key_comp;
288       using _Base::value_comp;
289 
290       // 23.3.1.3 multimap operations:
291       iterator
292       find(const key_type& __x)
293       { return iterator(_Base::find(__x), this); }
294 
295       const_iterator
296       find(const key_type& __x) const
297       { return const_iterator(_Base::find(__x), this); }
298 
299       using _Base::count;
300 
301       iterator
302       lower_bound(const key_type& __x)
303       { return iterator(_Base::lower_bound(__x), this); }
304 
305       const_iterator
306       lower_bound(const key_type& __x) const
307       { return const_iterator(_Base::lower_bound(__x), this); }
308 
309       iterator
310       upper_bound(const key_type& __x)
311       { return iterator(_Base::upper_bound(__x), this); }
312 
313       const_iterator
314       upper_bound(const key_type& __x) const
315       { return const_iterator(_Base::upper_bound(__x), this); }
316 
317       std::pair<iterator,iterator>
318       equal_range(const key_type& __x)
319       {
320 	typedef typename _Base::iterator _Base_iterator;
321 	std::pair<_Base_iterator, _Base_iterator> __res =
322 	_Base::equal_range(__x);
323 	return std::make_pair(iterator(__res.first, this),
324 			      iterator(__res.second, this));
325       }
326 
327       std::pair<const_iterator,const_iterator>
328       equal_range(const key_type& __x) const
329       {
330 	typedef typename _Base::const_iterator _Base_const_iterator;
331 	std::pair<_Base_const_iterator, _Base_const_iterator> __res =
332 	_Base::equal_range(__x);
333 	return std::make_pair(const_iterator(__res.first, this),
334 			      const_iterator(__res.second, this));
335       }
336 
337       _Base&
338       _M_base() { return *this; }
339 
340       const _Base&
341       _M_base() const { return *this; }
342 
343     private:
344       void
345       _M_invalidate_all()
346       {
347 	typedef typename _Base::const_iterator _Base_const_iterator;
348 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
349 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
350       }
351     };
352 
353   template<typename _Key, typename _Tp,
354 	   typename _Compare, typename _Allocator>
355     inline bool
356     operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
357 	       const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
358     { return __lhs._M_base() == __rhs._M_base(); }
359 
360   template<typename _Key, typename _Tp,
361 	   typename _Compare, typename _Allocator>
362     inline bool
363     operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
364 	       const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
365     { return __lhs._M_base() != __rhs._M_base(); }
366 
367   template<typename _Key, typename _Tp,
368 	   typename _Compare, typename _Allocator>
369     inline bool
370     operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
371 	      const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
372     { return __lhs._M_base() < __rhs._M_base(); }
373 
374   template<typename _Key, typename _Tp,
375 	   typename _Compare, typename _Allocator>
376     inline bool
377     operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
378 	       const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
379     { return __lhs._M_base() <= __rhs._M_base(); }
380 
381   template<typename _Key, typename _Tp,
382 	   typename _Compare, typename _Allocator>
383     inline bool
384     operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
385 	       const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
386     { return __lhs._M_base() >= __rhs._M_base(); }
387 
388   template<typename _Key, typename _Tp,
389 	   typename _Compare, typename _Allocator>
390     inline bool
391     operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
392 	      const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
393     { return __lhs._M_base() > __rhs._M_base(); }
394 
395   template<typename _Key, typename _Tp,
396 	   typename _Compare, typename _Allocator>
397     inline void
398     swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
399 	 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
400     { __lhs.swap(__rhs); }
401 
402 } // namespace __debug
403 } // namespace std
404 
405 #endif
406