1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009
4 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 /** @file debug/multimap.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
31 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
41 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
42 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
44 : public _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator>,
45 public __gnu_debug::_Safe_sequence<multimap<_Key, _Tp,
46 _Compare, _Allocator> >
48 typedef _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
49 typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
53 typedef _Key key_type;
54 typedef _Tp mapped_type;
55 typedef std::pair<const _Key, _Tp> value_type;
56 typedef _Compare key_compare;
57 typedef _Allocator allocator_type;
58 typedef typename _Base::reference reference;
59 typedef typename _Base::const_reference const_reference;
61 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
63 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
64 multimap> const_iterator;
66 typedef typename _Base::size_type size_type;
67 typedef typename _Base::difference_type difference_type;
68 typedef typename _Base::pointer pointer;
69 typedef typename _Base::const_pointer const_pointer;
70 typedef std::reverse_iterator<iterator> reverse_iterator;
71 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
73 using _Base::value_compare;
75 // 23.3.1.1 construct/copy/destroy:
76 explicit multimap(const _Compare& __comp = _Compare(),
77 const _Allocator& __a = _Allocator())
78 : _Base(__comp, __a) { }
80 template<typename _InputIterator>
81 multimap(_InputIterator __first, _InputIterator __last,
82 const _Compare& __comp = _Compare(),
83 const _Allocator& __a = _Allocator())
84 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
87 multimap(const multimap& __x)
88 : _Base(__x), _Safe_base() { }
90 multimap(const _Base& __x)
91 : _Base(__x), _Safe_base() { }
93 #ifdef __GXX_EXPERIMENTAL_CXX0X__
94 multimap(multimap&& __x)
95 : _Base(std::forward<multimap>(__x)), _Safe_base()
96 { this->_M_swap(__x); }
98 multimap(initializer_list<value_type> __l,
99 const _Compare& __c = _Compare(),
100 const allocator_type& __a = allocator_type())
101 : _Base(__l, __c, __a), _Safe_base() { }
107 operator=(const multimap& __x)
109 *static_cast<_Base*>(this) = __x;
110 this->_M_invalidate_all();
114 #ifdef __GXX_EXPERIMENTAL_CXX0X__
116 operator=(multimap&& __x)
125 operator=(initializer_list<value_type> __l)
133 using _Base::get_allocator;
138 { return iterator(_Base::begin(), this); }
142 { return const_iterator(_Base::begin(), this); }
146 { return iterator(_Base::end(), this); }
150 { return const_iterator(_Base::end(), this); }
154 { return reverse_iterator(end()); }
156 const_reverse_iterator
158 { return const_reverse_iterator(end()); }
162 { return reverse_iterator(begin()); }
164 const_reverse_iterator
166 { return const_reverse_iterator(begin()); }
168 #ifdef __GXX_EXPERIMENTAL_CXX0X__
171 { return const_iterator(_Base::begin(), this); }
175 { return const_iterator(_Base::end(), this); }
177 const_reverse_iterator
179 { return const_reverse_iterator(end()); }
181 const_reverse_iterator
183 { return const_reverse_iterator(begin()); }
189 using _Base::max_size;
193 insert(const value_type& __x)
194 { return iterator(_Base::insert(__x), this); }
196 #ifdef __GXX_EXPERIMENTAL_CXX0X__
198 insert(std::initializer_list<value_type> __list)
199 { _Base::insert(__list); }
203 insert(iterator __position, const value_type& __x)
205 __glibcxx_check_insert(__position);
206 return iterator(_Base::insert(__position.base(), __x), this);
209 template<typename _InputIterator>
211 insert(_InputIterator __first, _InputIterator __last)
213 __glibcxx_check_valid_range(__first, __last);
214 _Base::insert(__first, __last);
218 erase(iterator __position)
220 __glibcxx_check_erase(__position);
221 __position._M_invalidate();
222 _Base::erase(__position.base());
226 erase(const key_type& __x)
228 std::pair<iterator, iterator> __victims = this->equal_range(__x);
229 size_type __count = 0;
230 while (__victims.first != __victims.second)
232 iterator __victim = __victims.first++;
233 __victim._M_invalidate();
234 _Base::erase(__victim.base());
241 erase(iterator __first, iterator __last)
243 // _GLIBCXX_RESOLVE_LIB_DEFECTS
244 // 151. can't currently clear() empty container
245 __glibcxx_check_erase_range(__first, __last);
246 while (__first != __last)
247 this->erase(__first++);
251 #ifdef __GXX_EXPERIMENTAL_CXX0X__
263 { this->erase(begin(), end()); }
266 using _Base::key_comp;
267 using _Base::value_comp;
269 // 23.3.1.3 multimap operations:
271 find(const key_type& __x)
272 { return iterator(_Base::find(__x), this); }
275 find(const key_type& __x) const
276 { return const_iterator(_Base::find(__x), this); }
281 lower_bound(const key_type& __x)
282 { return iterator(_Base::lower_bound(__x), this); }
285 lower_bound(const key_type& __x) const
286 { return const_iterator(_Base::lower_bound(__x), this); }
289 upper_bound(const key_type& __x)
290 { return iterator(_Base::upper_bound(__x), this); }
293 upper_bound(const key_type& __x) const
294 { return const_iterator(_Base::upper_bound(__x), this); }
296 std::pair<iterator,iterator>
297 equal_range(const key_type& __x)
299 typedef typename _Base::iterator _Base_iterator;
300 std::pair<_Base_iterator, _Base_iterator> __res =
301 _Base::equal_range(__x);
302 return std::make_pair(iterator(__res.first, this),
303 iterator(__res.second, this));
306 std::pair<const_iterator,const_iterator>
307 equal_range(const key_type& __x) const
309 typedef typename _Base::const_iterator _Base_const_iterator;
310 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
311 _Base::equal_range(__x);
312 return std::make_pair(const_iterator(__res.first, this),
313 const_iterator(__res.second, this));
317 _M_base() { return *this; }
320 _M_base() const { return *this; }
326 typedef typename _Base::const_iterator _Base_const_iterator;
327 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
328 this->_M_invalidate_if(_Not_equal(_M_base().end()));
332 template<typename _Key, typename _Tp,
333 typename _Compare, typename _Allocator>
335 operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
336 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
337 { return __lhs._M_base() == __rhs._M_base(); }
339 template<typename _Key, typename _Tp,
340 typename _Compare, typename _Allocator>
342 operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
343 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
344 { return __lhs._M_base() != __rhs._M_base(); }
346 template<typename _Key, typename _Tp,
347 typename _Compare, typename _Allocator>
349 operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
350 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
351 { return __lhs._M_base() < __rhs._M_base(); }
353 template<typename _Key, typename _Tp,
354 typename _Compare, typename _Allocator>
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(); }
360 template<typename _Key, typename _Tp,
361 typename _Compare, typename _Allocator>
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(); }
367 template<typename _Key, typename _Tp,
368 typename _Compare, typename _Allocator>
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(); }
374 template<typename _Key, typename _Tp,
375 typename _Compare, typename _Allocator>
377 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
378 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
379 { __lhs.swap(__rhs); }
381 #ifdef __GXX_EXPERIMENTAL_CXX0X__
382 template<typename _Key, typename _Tp,
383 typename _Compare, typename _Allocator>
385 swap(multimap<_Key, _Tp, _Compare, _Allocator>&& __lhs,
386 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
387 { __lhs.swap(__rhs); }
389 template<typename _Key, typename _Tp,
390 typename _Compare, typename _Allocator>
392 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
393 multimap<_Key, _Tp, _Compare, _Allocator>&& __rhs)
394 { __lhs.swap(__rhs); }
397 } // namespace __debug