1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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 /// 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> > >
45 : public _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<multimap<_Key, _Tp,
47 _Compare, _Allocator> >
49 typedef _GLIBCXX_STD_D::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
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;
62 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, multimap>
64 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
65 multimap> const_iterator;
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;
74 using _Base::value_compare;
76 // 23.3.1.1 construct/copy/destroy:
77 explicit multimap(const _Compare& __comp = _Compare(),
78 const _Allocator& __a = _Allocator())
79 : _Base(__comp, __a) { }
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,
88 multimap(const multimap& __x)
89 : _Base(__x), _Safe_base() { }
91 multimap(const _Base& __x)
92 : _Base(__x), _Safe_base() { }
94 #ifdef __GXX_EXPERIMENTAL_CXX0X__
95 multimap(multimap&& __x)
96 : _Base(std::forward<multimap>(__x)), _Safe_base()
97 { this->_M_swap(__x); }
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() { }
108 operator=(const multimap& __x)
110 *static_cast<_Base*>(this) = __x;
111 this->_M_invalidate_all();
115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
117 operator=(multimap&& __x)
127 operator=(initializer_list<value_type> __l)
135 using _Base::get_allocator;
140 { return iterator(_Base::begin(), this); }
144 { return const_iterator(_Base::begin(), this); }
148 { return iterator(_Base::end(), this); }
152 { return const_iterator(_Base::end(), this); }
156 { return reverse_iterator(end()); }
158 const_reverse_iterator
160 { return const_reverse_iterator(end()); }
164 { return reverse_iterator(begin()); }
166 const_reverse_iterator
168 { return const_reverse_iterator(begin()); }
170 #ifdef __GXX_EXPERIMENTAL_CXX0X__
173 { return const_iterator(_Base::begin(), this); }
177 { return const_iterator(_Base::end(), this); }
179 const_reverse_iterator
181 { return const_reverse_iterator(end()); }
183 const_reverse_iterator
185 { return const_reverse_iterator(begin()); }
191 using _Base::max_size;
195 insert(const value_type& __x)
196 { return iterator(_Base::insert(__x), this); }
198 #ifdef __GXX_EXPERIMENTAL_CXX0X__
200 insert(std::initializer_list<value_type> __list)
201 { _Base::insert(__list); }
205 insert(iterator __position, const value_type& __x)
207 __glibcxx_check_insert(__position);
208 return iterator(_Base::insert(__position.base(), __x), this);
211 template<typename _InputIterator>
213 insert(_InputIterator __first, _InputIterator __last)
215 __glibcxx_check_valid_range(__first, __last);
216 _Base::insert(__first, __last);
219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
221 erase(iterator __position)
223 __glibcxx_check_erase(__position);
224 __position._M_invalidate();
225 return iterator(_Base::erase(__position.base()), this);
229 erase(iterator __position)
231 __glibcxx_check_erase(__position);
232 __position._M_invalidate();
233 _Base::erase(__position.base());
238 erase(const key_type& __x)
240 std::pair<iterator, iterator> __victims = this->equal_range(__x);
241 size_type __count = 0;
242 while (__victims.first != __victims.second)
244 iterator __victim = __victims.first++;
245 __victim._M_invalidate();
246 _Base::erase(__victim.base());
252 #ifdef __GXX_EXPERIMENTAL_CXX0X__
254 erase(iterator __first, iterator __last)
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++);
265 erase(iterator __first, iterator __last)
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++);
284 { this->erase(begin(), end()); }
287 using _Base::key_comp;
288 using _Base::value_comp;
290 // 23.3.1.3 multimap operations:
292 find(const key_type& __x)
293 { return iterator(_Base::find(__x), this); }
296 find(const key_type& __x) const
297 { return const_iterator(_Base::find(__x), this); }
302 lower_bound(const key_type& __x)
303 { return iterator(_Base::lower_bound(__x), this); }
306 lower_bound(const key_type& __x) const
307 { return const_iterator(_Base::lower_bound(__x), this); }
310 upper_bound(const key_type& __x)
311 { return iterator(_Base::upper_bound(__x), this); }
314 upper_bound(const key_type& __x) const
315 { return const_iterator(_Base::upper_bound(__x), this); }
317 std::pair<iterator,iterator>
318 equal_range(const key_type& __x)
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));
327 std::pair<const_iterator,const_iterator>
328 equal_range(const key_type& __x) const
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));
338 _M_base() { return *this; }
341 _M_base() const { return *this; }
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()));
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 operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
378 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
379 { return __lhs._M_base() <= __rhs._M_base(); }
381 template<typename _Key, typename _Tp,
382 typename _Compare, typename _Allocator>
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(); }
388 template<typename _Key, typename _Tp,
389 typename _Compare, typename _Allocator>
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(); }
395 template<typename _Key, typename _Tp,
396 typename _Compare, typename _Allocator>
398 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
399 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
400 { __lhs.swap(__rhs); }
402 } // namespace __debug