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 42 namespace std 43 { 44 namespace __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 using _Base::value_compare; 78 79 // 23.3.1.1 construct/copy/destroy: 80 explicit multimap(const _Compare& __comp = _Compare(), 81 const _Allocator& __a = _Allocator()) _Base(__comp,__a)82 : _Base(__comp, __a) { } 83 84 template<typename _InputIterator> 85 multimap(_InputIterator __first, _InputIterator __last, 86 const _Compare& __comp = _Compare(), 87 const _Allocator& __a = _Allocator()) _Base(__gnu_debug::__check_valid_range (__first,__last),__last,__comp,__a)88 : _Base(__gnu_debug::__check_valid_range(__first, __last), __last, 89 __comp, __a) { } 90 multimap(const multimap<_Key,_Tp,_Compare,_Allocator> & __x)91 multimap(const multimap<_Key,_Tp,_Compare,_Allocator>& __x) 92 : _Base(__x), _Safe_base() { } 93 multimap(const _Base & __x)94 multimap(const _Base& __x) : _Base(__x), _Safe_base() { } 95 ~multimap()96 ~multimap() { } 97 98 multimap<_Key,_Tp,_Compare,_Allocator>& 99 operator=(const multimap<_Key,_Tp,_Compare,_Allocator>& __x) 100 { 101 *static_cast<_Base*>(this) = __x; 102 this->_M_invalidate_all(); 103 return *this; 104 } 105 106 using _Base::get_allocator; 107 108 // iterators: 109 iterator begin()110 begin() 111 { return iterator(_Base::begin(), this); } 112 113 const_iterator begin()114 begin() const 115 { return const_iterator(_Base::begin(), this); } 116 117 iterator end()118 end() 119 { return iterator(_Base::end(), this); } 120 121 const_iterator end()122 end() const 123 { return const_iterator(_Base::end(), this); } 124 125 reverse_iterator rbegin()126 rbegin() 127 { return reverse_iterator(end()); } 128 129 const_reverse_iterator rbegin()130 rbegin() const 131 { return const_reverse_iterator(end()); } 132 133 reverse_iterator rend()134 rend() 135 { return reverse_iterator(begin()); } 136 137 const_reverse_iterator rend()138 rend() const 139 { return const_reverse_iterator(begin()); } 140 141 // capacity: 142 using _Base::empty; 143 using _Base::size; 144 using _Base::max_size; 145 146 // modifiers: 147 iterator insert(const value_type & __x)148 insert(const value_type& __x) 149 { return iterator(_Base::insert(__x), this); } 150 151 iterator insert(iterator __position,const value_type & __x)152 insert(iterator __position, const value_type& __x) 153 { 154 __glibcxx_check_insert(__position); 155 return iterator(_Base::insert(__position.base(), __x), this); 156 } 157 158 template<typename _InputIterator> 159 void insert(_InputIterator __first,_InputIterator __last)160 insert(_InputIterator __first, _InputIterator __last) 161 { 162 __glibcxx_check_valid_range(__first, __last); 163 _Base::insert(__first, __last); 164 } 165 166 void erase(iterator __position)167 erase(iterator __position) 168 { 169 __glibcxx_check_erase(__position); 170 __position._M_invalidate(); 171 _Base::erase(__position.base()); 172 } 173 174 size_type erase(const key_type & __x)175 erase(const key_type& __x) 176 { 177 std::pair<iterator, iterator> __victims = this->equal_range(__x); 178 size_type __count = 0; 179 while (__victims.first != __victims.second) 180 { 181 iterator __victim = __victims.first++; 182 __victim._M_invalidate(); 183 _Base::erase(__victim.base()); 184 ++__count; 185 } 186 return __count; 187 } 188 189 void erase(iterator __first,iterator __last)190 erase(iterator __first, iterator __last) 191 { 192 // _GLIBCXX_RESOLVE_LIB_DEFECTS 193 // 151. can't currently clear() empty container 194 __glibcxx_check_erase_range(__first, __last); 195 while (__first != __last) 196 this->erase(__first++); 197 } 198 199 void swap(multimap<_Key,_Tp,_Compare,_Allocator> & __x)200 swap(multimap<_Key,_Tp,_Compare,_Allocator>& __x) 201 { 202 _Base::swap(__x); 203 this->_M_swap(__x); 204 } 205 206 void clear()207 clear() 208 { this->erase(begin(), end()); } 209 210 // observers: 211 using _Base::key_comp; 212 using _Base::value_comp; 213 214 // 23.3.1.3 multimap operations: 215 iterator find(const key_type & __x)216 find(const key_type& __x) 217 { return iterator(_Base::find(__x), this); } 218 219 const_iterator find(const key_type & __x)220 find(const key_type& __x) const 221 { return const_iterator(_Base::find(__x), this); } 222 223 using _Base::count; 224 225 iterator lower_bound(const key_type & __x)226 lower_bound(const key_type& __x) 227 { return iterator(_Base::lower_bound(__x), this); } 228 229 const_iterator lower_bound(const key_type & __x)230 lower_bound(const key_type& __x) const 231 { return const_iterator(_Base::lower_bound(__x), this); } 232 233 iterator upper_bound(const key_type & __x)234 upper_bound(const key_type& __x) 235 { return iterator(_Base::upper_bound(__x), this); } 236 237 const_iterator upper_bound(const key_type & __x)238 upper_bound(const key_type& __x) const 239 { return const_iterator(_Base::upper_bound(__x), this); } 240 241 std::pair<iterator,iterator> equal_range(const key_type & __x)242 equal_range(const key_type& __x) 243 { 244 typedef typename _Base::iterator _Base_iterator; 245 std::pair<_Base_iterator, _Base_iterator> __res = 246 _Base::equal_range(__x); 247 return std::make_pair(iterator(__res.first, this), 248 iterator(__res.second, this)); 249 } 250 251 std::pair<const_iterator,const_iterator> equal_range(const key_type & __x)252 equal_range(const key_type& __x) const 253 { 254 typedef typename _Base::const_iterator _Base_const_iterator; 255 std::pair<_Base_const_iterator, _Base_const_iterator> __res = 256 _Base::equal_range(__x); 257 return std::make_pair(const_iterator(__res.first, this), 258 const_iterator(__res.second, this)); 259 } 260 261 _Base& _M_base()262 _M_base() { return *this; } 263 264 const _Base& _M_base()265 _M_base() const { return *this; } 266 267 private: 268 void _M_invalidate_all()269 _M_invalidate_all() 270 { 271 typedef typename _Base::const_iterator _Base_const_iterator; 272 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; 273 this->_M_invalidate_if(_Not_equal(_M_base().end())); 274 } 275 }; 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 bool 309 operator>(const multimap<_Key,_Tp,_Compare,_Allocator>& __lhs, 310 const multimap<_Key,_Tp,_Compare,_Allocator>& __rhs) 311 { return __lhs._M_base() > __rhs._M_base(); } 312 313 template<typename _Key,typename _Tp,typename _Compare,typename _Allocator> 314 inline void swap(multimap<_Key,_Tp,_Compare,_Allocator> & __lhs,multimap<_Key,_Tp,_Compare,_Allocator> & __rhs)315 swap(multimap<_Key,_Tp,_Compare,_Allocator>& __lhs, 316 multimap<_Key,_Tp,_Compare,_Allocator>& __rhs) 317 { __lhs.swap(__rhs); } 318 } // namespace __debug 319 } // namespace std 320 321 #endif 322