libstdc++
|
00001 // Debugging multimap implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file debug/multimap.h 00026 * This file is a GNU debug extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H 00030 #define _GLIBCXX_DEBUG_MULTIMAP_H 1 00031 00032 #include <debug/safe_sequence.h> 00033 #include <debug/safe_iterator.h> 00034 #include <utility> 00035 00036 namespace std _GLIBCXX_VISIBILITY(default) 00037 { 00038 namespace __debug 00039 { 00040 /// Class std::multimap with safety/checking/debug instrumentation. 00041 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 00042 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > > 00043 class multimap 00044 : public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator>, 00045 public __gnu_debug::_Safe_sequence<multimap<_Key, _Tp, 00046 _Compare, _Allocator> > 00047 { 00048 typedef _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator> _Base; 00049 00050 typedef typename _Base::const_iterator _Base_const_iterator; 00051 typedef typename _Base::iterator _Base_iterator; 00052 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal; 00053 public: 00054 // types: 00055 typedef _Key key_type; 00056 typedef _Tp mapped_type; 00057 typedef std::pair<const _Key, _Tp> value_type; 00058 typedef _Compare key_compare; 00059 typedef _Allocator allocator_type; 00060 typedef typename _Base::reference reference; 00061 typedef typename _Base::const_reference const_reference; 00062 00063 typedef __gnu_debug::_Safe_iterator<_Base_iterator, multimap> 00064 iterator; 00065 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, 00066 multimap> const_iterator; 00067 00068 typedef typename _Base::size_type size_type; 00069 typedef typename _Base::difference_type difference_type; 00070 typedef typename _Base::pointer pointer; 00071 typedef typename _Base::const_pointer const_pointer; 00072 typedef std::reverse_iterator<iterator> reverse_iterator; 00073 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00074 00075 // 23.3.1.1 construct/copy/destroy: 00076 explicit multimap(const _Compare& __comp = _Compare(), 00077 const _Allocator& __a = _Allocator()) 00078 : _Base(__comp, __a) { } 00079 00080 template<typename _InputIterator> 00081 multimap(_InputIterator __first, _InputIterator __last, 00082 const _Compare& __comp = _Compare(), 00083 const _Allocator& __a = _Allocator()) 00084 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first, 00085 __last)), 00086 __gnu_debug::__base(__last), 00087 __comp, __a) { } 00088 00089 multimap(const multimap& __x) 00090 : _Base(__x) { } 00091 00092 multimap(const _Base& __x) 00093 : _Base(__x) { } 00094 00095 #if __cplusplus >= 201103L 00096 multimap(multimap&& __x) 00097 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00098 : _Base(std::move(__x)) 00099 { this->_M_swap(__x); } 00100 00101 multimap(initializer_list<value_type> __l, 00102 const _Compare& __c = _Compare(), 00103 const allocator_type& __a = allocator_type()) 00104 : _Base(__l, __c, __a) { } 00105 #endif 00106 00107 ~multimap() _GLIBCXX_NOEXCEPT { } 00108 00109 multimap& 00110 operator=(const multimap& __x) 00111 { 00112 *static_cast<_Base*>(this) = __x; 00113 this->_M_invalidate_all(); 00114 return *this; 00115 } 00116 00117 #if __cplusplus >= 201103L 00118 multimap& 00119 operator=(multimap&& __x) 00120 { 00121 // NB: DR 1204. 00122 // NB: DR 675. 00123 __glibcxx_check_self_move_assign(__x); 00124 clear(); 00125 swap(__x); 00126 return *this; 00127 } 00128 00129 multimap& 00130 operator=(initializer_list<value_type> __l) 00131 { 00132 this->clear(); 00133 this->insert(__l); 00134 return *this; 00135 } 00136 #endif 00137 00138 using _Base::get_allocator; 00139 00140 // iterators: 00141 iterator 00142 begin() _GLIBCXX_NOEXCEPT 00143 { return iterator(_Base::begin(), this); } 00144 00145 const_iterator 00146 begin() const _GLIBCXX_NOEXCEPT 00147 { return const_iterator(_Base::begin(), this); } 00148 00149 iterator 00150 end() _GLIBCXX_NOEXCEPT 00151 { return iterator(_Base::end(), this); } 00152 00153 const_iterator 00154 end() const _GLIBCXX_NOEXCEPT 00155 { return const_iterator(_Base::end(), this); } 00156 00157 reverse_iterator 00158 rbegin() _GLIBCXX_NOEXCEPT 00159 { return reverse_iterator(end()); } 00160 00161 const_reverse_iterator 00162 rbegin() const _GLIBCXX_NOEXCEPT 00163 { return const_reverse_iterator(end()); } 00164 00165 reverse_iterator 00166 rend() _GLIBCXX_NOEXCEPT 00167 { return reverse_iterator(begin()); } 00168 00169 const_reverse_iterator 00170 rend() const _GLIBCXX_NOEXCEPT 00171 { return const_reverse_iterator(begin()); } 00172 00173 #if __cplusplus >= 201103L 00174 const_iterator 00175 cbegin() const noexcept 00176 { return const_iterator(_Base::begin(), this); } 00177 00178 const_iterator 00179 cend() const noexcept 00180 { return const_iterator(_Base::end(), this); } 00181 00182 const_reverse_iterator 00183 crbegin() const noexcept 00184 { return const_reverse_iterator(end()); } 00185 00186 const_reverse_iterator 00187 crend() const noexcept 00188 { return const_reverse_iterator(begin()); } 00189 #endif 00190 00191 // capacity: 00192 using _Base::empty; 00193 using _Base::size; 00194 using _Base::max_size; 00195 00196 // modifiers: 00197 #if __cplusplus >= 201103L 00198 template<typename... _Args> 00199 iterator 00200 emplace(_Args&&... __args) 00201 { 00202 return iterator(_Base::emplace(std::forward<_Args>(__args)...), this); 00203 } 00204 00205 template<typename... _Args> 00206 iterator 00207 emplace_hint(const_iterator __pos, _Args&&... __args) 00208 { 00209 __glibcxx_check_insert(__pos); 00210 return iterator(_Base::emplace_hint(__pos.base(), 00211 std::forward<_Args>(__args)...), 00212 this); 00213 } 00214 #endif 00215 00216 iterator 00217 insert(const value_type& __x) 00218 { return iterator(_Base::insert(__x), this); } 00219 00220 #if __cplusplus >= 201103L 00221 template<typename _Pair, typename = typename 00222 std::enable_if<std::is_constructible<value_type, 00223 _Pair&&>::value>::type> 00224 iterator 00225 insert(_Pair&& __x) 00226 { return iterator(_Base::insert(std::forward<_Pair>(__x)), this); } 00227 #endif 00228 00229 #if __cplusplus >= 201103L 00230 void 00231 insert(std::initializer_list<value_type> __list) 00232 { _Base::insert(__list); } 00233 #endif 00234 00235 iterator 00236 #if __cplusplus >= 201103L 00237 insert(const_iterator __position, const value_type& __x) 00238 #else 00239 insert(iterator __position, const value_type& __x) 00240 #endif 00241 { 00242 __glibcxx_check_insert(__position); 00243 return iterator(_Base::insert(__position.base(), __x), this); 00244 } 00245 00246 #if __cplusplus >= 201103L 00247 template<typename _Pair, typename = typename 00248 std::enable_if<std::is_constructible<value_type, 00249 _Pair&&>::value>::type> 00250 iterator 00251 insert(const_iterator __position, _Pair&& __x) 00252 { 00253 __glibcxx_check_insert(__position); 00254 return iterator(_Base::insert(__position.base(), 00255 std::forward<_Pair>(__x)), this); 00256 } 00257 #endif 00258 00259 template<typename _InputIterator> 00260 void 00261 insert(_InputIterator __first, _InputIterator __last) 00262 { 00263 __glibcxx_check_valid_range(__first, __last); 00264 _Base::insert(__gnu_debug::__base(__first), 00265 __gnu_debug::__base(__last)); 00266 } 00267 00268 #if __cplusplus >= 201103L 00269 iterator 00270 erase(const_iterator __position) 00271 { 00272 __glibcxx_check_erase(__position); 00273 this->_M_invalidate_if(_Equal(__position.base())); 00274 return iterator(_Base::erase(__position.base()), this); 00275 } 00276 00277 iterator 00278 erase(iterator __position) 00279 { return erase(const_iterator(__position)); } 00280 #else 00281 void 00282 erase(iterator __position) 00283 { 00284 __glibcxx_check_erase(__position); 00285 this->_M_invalidate_if(_Equal(__position.base())); 00286 _Base::erase(__position.base()); 00287 } 00288 #endif 00289 00290 size_type 00291 erase(const key_type& __x) 00292 { 00293 std::pair<_Base_iterator, _Base_iterator> __victims = 00294 _Base::equal_range(__x); 00295 size_type __count = 0; 00296 _Base_iterator __victim = __victims.first; 00297 while (__victim != __victims.second) 00298 { 00299 this->_M_invalidate_if(_Equal(__victim)); 00300 _Base::erase(__victim++); 00301 ++__count; 00302 } 00303 return __count; 00304 } 00305 00306 #if __cplusplus >= 201103L 00307 iterator 00308 erase(const_iterator __first, const_iterator __last) 00309 { 00310 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00311 // 151. can't currently clear() empty container 00312 __glibcxx_check_erase_range(__first, __last); 00313 for (_Base_const_iterator __victim = __first.base(); 00314 __victim != __last.base(); ++__victim) 00315 { 00316 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00317 _M_message(__gnu_debug::__msg_valid_range) 00318 ._M_iterator(__first, "first") 00319 ._M_iterator(__last, "last")); 00320 this->_M_invalidate_if(_Equal(__victim)); 00321 } 00322 return iterator(_Base::erase(__first.base(), __last.base()), this); 00323 } 00324 #else 00325 void 00326 erase(iterator __first, iterator __last) 00327 { 00328 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00329 // 151. can't currently clear() empty container 00330 __glibcxx_check_erase_range(__first, __last); 00331 for (_Base_iterator __victim = __first.base(); 00332 __victim != __last.base(); ++__victim) 00333 { 00334 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00335 _M_message(__gnu_debug::__msg_valid_range) 00336 ._M_iterator(__first, "first") 00337 ._M_iterator(__last, "last")); 00338 this->_M_invalidate_if(_Equal(__victim)); 00339 } 00340 _Base::erase(__first.base(), __last.base()); 00341 } 00342 #endif 00343 00344 void 00345 swap(multimap& __x) 00346 { 00347 _Base::swap(__x); 00348 this->_M_swap(__x); 00349 } 00350 00351 void 00352 clear() _GLIBCXX_NOEXCEPT 00353 { 00354 this->_M_invalidate_all(); 00355 _Base::clear(); 00356 } 00357 00358 // observers: 00359 using _Base::key_comp; 00360 using _Base::value_comp; 00361 00362 // 23.3.1.3 multimap operations: 00363 iterator 00364 find(const key_type& __x) 00365 { return iterator(_Base::find(__x), this); } 00366 00367 const_iterator 00368 find(const key_type& __x) const 00369 { return const_iterator(_Base::find(__x), this); } 00370 00371 using _Base::count; 00372 00373 iterator 00374 lower_bound(const key_type& __x) 00375 { return iterator(_Base::lower_bound(__x), this); } 00376 00377 const_iterator 00378 lower_bound(const key_type& __x) const 00379 { return const_iterator(_Base::lower_bound(__x), this); } 00380 00381 iterator 00382 upper_bound(const key_type& __x) 00383 { return iterator(_Base::upper_bound(__x), this); } 00384 00385 const_iterator 00386 upper_bound(const key_type& __x) const 00387 { return const_iterator(_Base::upper_bound(__x), this); } 00388 00389 std::pair<iterator,iterator> 00390 equal_range(const key_type& __x) 00391 { 00392 std::pair<_Base_iterator, _Base_iterator> __res = 00393 _Base::equal_range(__x); 00394 return std::make_pair(iterator(__res.first, this), 00395 iterator(__res.second, this)); 00396 } 00397 00398 std::pair<const_iterator,const_iterator> 00399 equal_range(const key_type& __x) const 00400 { 00401 std::pair<_Base_const_iterator, _Base_const_iterator> __res = 00402 _Base::equal_range(__x); 00403 return std::make_pair(const_iterator(__res.first, this), 00404 const_iterator(__res.second, this)); 00405 } 00406 00407 _Base& 00408 _M_base() _GLIBCXX_NOEXCEPT { return *this; } 00409 00410 const _Base& 00411 _M_base() const _GLIBCXX_NOEXCEPT { return *this; } 00412 00413 private: 00414 void 00415 _M_invalidate_all() 00416 { 00417 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal; 00418 this->_M_invalidate_if(_Not_equal(_Base::end())); 00419 } 00420 }; 00421 00422 template<typename _Key, typename _Tp, 00423 typename _Compare, typename _Allocator> 00424 inline bool 00425 operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00426 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00427 { return __lhs._M_base() == __rhs._M_base(); } 00428 00429 template<typename _Key, typename _Tp, 00430 typename _Compare, typename _Allocator> 00431 inline bool 00432 operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00433 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00434 { return __lhs._M_base() != __rhs._M_base(); } 00435 00436 template<typename _Key, typename _Tp, 00437 typename _Compare, typename _Allocator> 00438 inline bool 00439 operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00440 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00441 { return __lhs._M_base() < __rhs._M_base(); } 00442 00443 template<typename _Key, typename _Tp, 00444 typename _Compare, typename _Allocator> 00445 inline bool 00446 operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00447 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00448 { return __lhs._M_base() <= __rhs._M_base(); } 00449 00450 template<typename _Key, typename _Tp, 00451 typename _Compare, typename _Allocator> 00452 inline bool 00453 operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00454 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00455 { return __lhs._M_base() >= __rhs._M_base(); } 00456 00457 template<typename _Key, typename _Tp, 00458 typename _Compare, typename _Allocator> 00459 inline bool 00460 operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00461 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00462 { return __lhs._M_base() > __rhs._M_base(); } 00463 00464 template<typename _Key, typename _Tp, 00465 typename _Compare, typename _Allocator> 00466 inline void 00467 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00468 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00469 { __lhs.swap(__rhs); } 00470 00471 } // namespace __debug 00472 } // namespace std 00473 00474 #endif