libstdc++
|
00001 // Debugging multimap implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003-2016 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_container.h> 00034 #include <debug/safe_iterator.h> 00035 #include <utility> 00036 00037 namespace std _GLIBCXX_VISIBILITY(default) 00038 { 00039 namespace __debug 00040 { 00041 /// Class std::multimap with safety/checking/debug instrumentation. 00042 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 00043 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > > 00044 class multimap 00045 : public __gnu_debug::_Safe_container< 00046 multimap<_Key, _Tp, _Compare, _Allocator>, _Allocator, 00047 __gnu_debug::_Safe_node_sequence>, 00048 public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator> 00049 { 00050 typedef _GLIBCXX_STD_C::multimap< 00051 _Key, _Tp, _Compare, _Allocator> _Base; 00052 typedef __gnu_debug::_Safe_container< 00053 multimap, _Allocator, __gnu_debug::_Safe_node_sequence> _Safe; 00054 00055 typedef typename _Base::const_iterator _Base_const_iterator; 00056 typedef typename _Base::iterator _Base_iterator; 00057 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal; 00058 00059 public: 00060 // types: 00061 typedef _Key key_type; 00062 typedef _Tp mapped_type; 00063 typedef std::pair<const _Key, _Tp> value_type; 00064 typedef _Compare key_compare; 00065 typedef _Allocator allocator_type; 00066 typedef typename _Base::reference reference; 00067 typedef typename _Base::const_reference const_reference; 00068 00069 typedef __gnu_debug::_Safe_iterator<_Base_iterator, multimap> 00070 iterator; 00071 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, 00072 multimap> const_iterator; 00073 00074 typedef typename _Base::size_type size_type; 00075 typedef typename _Base::difference_type difference_type; 00076 typedef typename _Base::pointer pointer; 00077 typedef typename _Base::const_pointer const_pointer; 00078 typedef std::reverse_iterator<iterator> reverse_iterator; 00079 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00080 00081 // 23.3.1.1 construct/copy/destroy: 00082 00083 #if __cplusplus < 201103L 00084 multimap() : _Base() { } 00085 00086 multimap(const multimap& __x) 00087 : _Base(__x) { } 00088 00089 ~multimap() { } 00090 #else 00091 multimap() = default; 00092 multimap(const multimap&) = default; 00093 multimap(multimap&&) = default; 00094 00095 multimap(initializer_list<value_type> __l, 00096 const _Compare& __c = _Compare(), 00097 const allocator_type& __a = allocator_type()) 00098 : _Base(__l, __c, __a) { } 00099 00100 explicit 00101 multimap(const allocator_type& __a) 00102 : _Base(__a) { } 00103 00104 multimap(const multimap& __m, const allocator_type& __a) 00105 : _Base(__m, __a) { } 00106 00107 multimap(multimap&& __m, const allocator_type& __a) 00108 : _Safe(std::move(__m._M_safe()), __a), 00109 _Base(std::move(__m._M_base()), __a) { } 00110 00111 multimap(initializer_list<value_type> __l, const allocator_type& __a) 00112 : _Base(__l, __a) { } 00113 00114 template<typename _InputIterator> 00115 multimap(_InputIterator __first, _InputIterator __last, 00116 const allocator_type& __a) 00117 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first, 00118 __last)), 00119 __gnu_debug::__base(__last), __a) { } 00120 00121 ~multimap() = default; 00122 #endif 00123 00124 explicit multimap(const _Compare& __comp, 00125 const _Allocator& __a = _Allocator()) 00126 : _Base(__comp, __a) { } 00127 00128 template<typename _InputIterator> 00129 multimap(_InputIterator __first, _InputIterator __last, 00130 const _Compare& __comp = _Compare(), 00131 const _Allocator& __a = _Allocator()) 00132 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first, 00133 __last)), 00134 __gnu_debug::__base(__last), 00135 __comp, __a) { } 00136 00137 multimap(const _Base& __x) 00138 : _Base(__x) { } 00139 00140 #if __cplusplus < 201103L 00141 multimap& 00142 operator=(const multimap& __x) 00143 { 00144 this->_M_safe() = __x; 00145 _M_base() = __x; 00146 return *this; 00147 } 00148 #else 00149 multimap& 00150 operator=(const multimap&) = default; 00151 00152 multimap& 00153 operator=(multimap&&) = default; 00154 00155 multimap& 00156 operator=(initializer_list<value_type> __l) 00157 { 00158 _M_base() = __l; 00159 this->_M_invalidate_all(); 00160 return *this; 00161 } 00162 #endif 00163 00164 using _Base::get_allocator; 00165 00166 // iterators: 00167 iterator 00168 begin() _GLIBCXX_NOEXCEPT 00169 { return iterator(_Base::begin(), this); } 00170 00171 const_iterator 00172 begin() const _GLIBCXX_NOEXCEPT 00173 { return const_iterator(_Base::begin(), this); } 00174 00175 iterator 00176 end() _GLIBCXX_NOEXCEPT 00177 { return iterator(_Base::end(), this); } 00178 00179 const_iterator 00180 end() const _GLIBCXX_NOEXCEPT 00181 { return const_iterator(_Base::end(), this); } 00182 00183 reverse_iterator 00184 rbegin() _GLIBCXX_NOEXCEPT 00185 { return reverse_iterator(end()); } 00186 00187 const_reverse_iterator 00188 rbegin() const _GLIBCXX_NOEXCEPT 00189 { return const_reverse_iterator(end()); } 00190 00191 reverse_iterator 00192 rend() _GLIBCXX_NOEXCEPT 00193 { return reverse_iterator(begin()); } 00194 00195 const_reverse_iterator 00196 rend() const _GLIBCXX_NOEXCEPT 00197 { return const_reverse_iterator(begin()); } 00198 00199 #if __cplusplus >= 201103L 00200 const_iterator 00201 cbegin() const noexcept 00202 { return const_iterator(_Base::begin(), this); } 00203 00204 const_iterator 00205 cend() const noexcept 00206 { return const_iterator(_Base::end(), this); } 00207 00208 const_reverse_iterator 00209 crbegin() const noexcept 00210 { return const_reverse_iterator(end()); } 00211 00212 const_reverse_iterator 00213 crend() const noexcept 00214 { return const_reverse_iterator(begin()); } 00215 #endif 00216 00217 // capacity: 00218 using _Base::empty; 00219 using _Base::size; 00220 using _Base::max_size; 00221 00222 // modifiers: 00223 #if __cplusplus >= 201103L 00224 template<typename... _Args> 00225 iterator 00226 emplace(_Args&&... __args) 00227 { 00228 return iterator(_Base::emplace(std::forward<_Args>(__args)...), this); 00229 } 00230 00231 template<typename... _Args> 00232 iterator 00233 emplace_hint(const_iterator __pos, _Args&&... __args) 00234 { 00235 __glibcxx_check_insert(__pos); 00236 return iterator(_Base::emplace_hint(__pos.base(), 00237 std::forward<_Args>(__args)...), 00238 this); 00239 } 00240 #endif 00241 00242 iterator 00243 insert(const value_type& __x) 00244 { return iterator(_Base::insert(__x), this); } 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(_Pair&& __x) 00252 { return iterator(_Base::insert(std::forward<_Pair>(__x)), this); } 00253 #endif 00254 00255 #if __cplusplus >= 201103L 00256 void 00257 insert(std::initializer_list<value_type> __list) 00258 { _Base::insert(__list); } 00259 #endif 00260 00261 iterator 00262 #if __cplusplus >= 201103L 00263 insert(const_iterator __position, const value_type& __x) 00264 #else 00265 insert(iterator __position, const value_type& __x) 00266 #endif 00267 { 00268 __glibcxx_check_insert(__position); 00269 return iterator(_Base::insert(__position.base(), __x), this); 00270 } 00271 00272 #if __cplusplus >= 201103L 00273 template<typename _Pair, typename = typename 00274 std::enable_if<std::is_constructible<value_type, 00275 _Pair&&>::value>::type> 00276 iterator 00277 insert(const_iterator __position, _Pair&& __x) 00278 { 00279 __glibcxx_check_insert(__position); 00280 return iterator(_Base::insert(__position.base(), 00281 std::forward<_Pair>(__x)), this); 00282 } 00283 #endif 00284 00285 template<typename _InputIterator> 00286 void 00287 insert(_InputIterator __first, _InputIterator __last) 00288 { 00289 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; 00290 __glibcxx_check_valid_range2(__first, __last, __dist); 00291 00292 if (__dist.second >= __gnu_debug::__dp_sign) 00293 _Base::insert(__gnu_debug::__unsafe(__first), 00294 __gnu_debug::__unsafe(__last)); 00295 else 00296 _Base::insert(__first, __last); 00297 } 00298 00299 #if __cplusplus >= 201103L 00300 iterator 00301 erase(const_iterator __position) 00302 { 00303 __glibcxx_check_erase(__position); 00304 this->_M_invalidate_if(_Equal(__position.base())); 00305 return iterator(_Base::erase(__position.base()), this); 00306 } 00307 00308 iterator 00309 erase(iterator __position) 00310 { return erase(const_iterator(__position)); } 00311 #else 00312 void 00313 erase(iterator __position) 00314 { 00315 __glibcxx_check_erase(__position); 00316 this->_M_invalidate_if(_Equal(__position.base())); 00317 _Base::erase(__position.base()); 00318 } 00319 #endif 00320 00321 size_type 00322 erase(const key_type& __x) 00323 { 00324 std::pair<_Base_iterator, _Base_iterator> __victims = 00325 _Base::equal_range(__x); 00326 size_type __count = 0; 00327 _Base_iterator __victim = __victims.first; 00328 while (__victim != __victims.second) 00329 { 00330 this->_M_invalidate_if(_Equal(__victim)); 00331 _Base::erase(__victim++); 00332 ++__count; 00333 } 00334 return __count; 00335 } 00336 00337 #if __cplusplus >= 201103L 00338 iterator 00339 erase(const_iterator __first, const_iterator __last) 00340 { 00341 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00342 // 151. can't currently clear() empty container 00343 __glibcxx_check_erase_range(__first, __last); 00344 for (_Base_const_iterator __victim = __first.base(); 00345 __victim != __last.base(); ++__victim) 00346 { 00347 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00348 _M_message(__gnu_debug::__msg_valid_range) 00349 ._M_iterator(__first, "first") 00350 ._M_iterator(__last, "last")); 00351 this->_M_invalidate_if(_Equal(__victim)); 00352 } 00353 return iterator(_Base::erase(__first.base(), __last.base()), this); 00354 } 00355 #else 00356 void 00357 erase(iterator __first, iterator __last) 00358 { 00359 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00360 // 151. can't currently clear() empty container 00361 __glibcxx_check_erase_range(__first, __last); 00362 for (_Base_iterator __victim = __first.base(); 00363 __victim != __last.base(); ++__victim) 00364 { 00365 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00366 _M_message(__gnu_debug::__msg_valid_range) 00367 ._M_iterator(__first, "first") 00368 ._M_iterator(__last, "last")); 00369 this->_M_invalidate_if(_Equal(__victim)); 00370 } 00371 _Base::erase(__first.base(), __last.base()); 00372 } 00373 #endif 00374 00375 void 00376 swap(multimap& __x) 00377 _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) ) 00378 { 00379 _Safe::_M_swap(__x); 00380 _Base::swap(__x); 00381 } 00382 00383 void 00384 clear() _GLIBCXX_NOEXCEPT 00385 { 00386 this->_M_invalidate_all(); 00387 _Base::clear(); 00388 } 00389 00390 // observers: 00391 using _Base::key_comp; 00392 using _Base::value_comp; 00393 00394 // 23.3.1.3 multimap operations: 00395 iterator 00396 find(const key_type& __x) 00397 { return iterator(_Base::find(__x), this); } 00398 00399 #if __cplusplus > 201103L 00400 template<typename _Kt, 00401 typename _Req = 00402 typename __has_is_transparent<_Compare, _Kt>::type> 00403 iterator 00404 find(const _Kt& __x) 00405 { return { _Base::find(__x), this }; } 00406 #endif 00407 00408 const_iterator 00409 find(const key_type& __x) const 00410 { return const_iterator(_Base::find(__x), this); } 00411 00412 #if __cplusplus > 201103L 00413 template<typename _Kt, 00414 typename _Req = 00415 typename __has_is_transparent<_Compare, _Kt>::type> 00416 const_iterator 00417 find(const _Kt& __x) const 00418 { return { _Base::find(__x), this }; } 00419 #endif 00420 00421 using _Base::count; 00422 00423 iterator 00424 lower_bound(const key_type& __x) 00425 { return iterator(_Base::lower_bound(__x), this); } 00426 00427 #if __cplusplus > 201103L 00428 template<typename _Kt, 00429 typename _Req = 00430 typename __has_is_transparent<_Compare, _Kt>::type> 00431 iterator 00432 lower_bound(const _Kt& __x) 00433 { return { _Base::lower_bound(__x), this }; } 00434 #endif 00435 00436 const_iterator 00437 lower_bound(const key_type& __x) const 00438 { return const_iterator(_Base::lower_bound(__x), this); } 00439 00440 #if __cplusplus > 201103L 00441 template<typename _Kt, 00442 typename _Req = 00443 typename __has_is_transparent<_Compare, _Kt>::type> 00444 const_iterator 00445 lower_bound(const _Kt& __x) const 00446 { return { _Base::lower_bound(__x), this }; } 00447 #endif 00448 00449 iterator 00450 upper_bound(const key_type& __x) 00451 { return iterator(_Base::upper_bound(__x), this); } 00452 00453 #if __cplusplus > 201103L 00454 template<typename _Kt, 00455 typename _Req = 00456 typename __has_is_transparent<_Compare, _Kt>::type> 00457 iterator 00458 upper_bound(const _Kt& __x) 00459 { return { _Base::upper_bound(__x), this }; } 00460 #endif 00461 00462 const_iterator 00463 upper_bound(const key_type& __x) const 00464 { return const_iterator(_Base::upper_bound(__x), this); } 00465 00466 #if __cplusplus > 201103L 00467 template<typename _Kt, 00468 typename _Req = 00469 typename __has_is_transparent<_Compare, _Kt>::type> 00470 const_iterator 00471 upper_bound(const _Kt& __x) const 00472 { return { _Base::upper_bound(__x), this }; } 00473 #endif 00474 00475 std::pair<iterator,iterator> 00476 equal_range(const key_type& __x) 00477 { 00478 std::pair<_Base_iterator, _Base_iterator> __res = 00479 _Base::equal_range(__x); 00480 return std::make_pair(iterator(__res.first, this), 00481 iterator(__res.second, this)); 00482 } 00483 00484 #if __cplusplus > 201103L 00485 template<typename _Kt, 00486 typename _Req = 00487 typename __has_is_transparent<_Compare, _Kt>::type> 00488 std::pair<iterator, iterator> 00489 equal_range(const _Kt& __x) 00490 { 00491 auto __res = _Base::equal_range(__x); 00492 return { { __res.first, this }, { __res.second, this } }; 00493 } 00494 #endif 00495 00496 std::pair<const_iterator,const_iterator> 00497 equal_range(const key_type& __x) const 00498 { 00499 std::pair<_Base_const_iterator, _Base_const_iterator> __res = 00500 _Base::equal_range(__x); 00501 return std::make_pair(const_iterator(__res.first, this), 00502 const_iterator(__res.second, this)); 00503 } 00504 00505 #if __cplusplus > 201103L 00506 template<typename _Kt, 00507 typename _Req = 00508 typename __has_is_transparent<_Compare, _Kt>::type> 00509 std::pair<const_iterator, const_iterator> 00510 equal_range(const _Kt& __x) const 00511 { 00512 auto __res = _Base::equal_range(__x); 00513 return { { __res.first, this }, { __res.second, this } }; 00514 } 00515 #endif 00516 00517 _Base& 00518 _M_base() _GLIBCXX_NOEXCEPT { return *this; } 00519 00520 const _Base& 00521 _M_base() const _GLIBCXX_NOEXCEPT { return *this; } 00522 }; 00523 00524 template<typename _Key, typename _Tp, 00525 typename _Compare, typename _Allocator> 00526 inline bool 00527 operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00528 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00529 { return __lhs._M_base() == __rhs._M_base(); } 00530 00531 template<typename _Key, typename _Tp, 00532 typename _Compare, typename _Allocator> 00533 inline bool 00534 operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00535 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00536 { return __lhs._M_base() != __rhs._M_base(); } 00537 00538 template<typename _Key, typename _Tp, 00539 typename _Compare, typename _Allocator> 00540 inline bool 00541 operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00542 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00543 { return __lhs._M_base() < __rhs._M_base(); } 00544 00545 template<typename _Key, typename _Tp, 00546 typename _Compare, typename _Allocator> 00547 inline bool 00548 operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00549 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00550 { return __lhs._M_base() <= __rhs._M_base(); } 00551 00552 template<typename _Key, typename _Tp, 00553 typename _Compare, typename _Allocator> 00554 inline bool 00555 operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00556 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00557 { return __lhs._M_base() >= __rhs._M_base(); } 00558 00559 template<typename _Key, typename _Tp, 00560 typename _Compare, typename _Allocator> 00561 inline bool 00562 operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00563 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00564 { return __lhs._M_base() > __rhs._M_base(); } 00565 00566 template<typename _Key, typename _Tp, 00567 typename _Compare, typename _Allocator> 00568 inline void 00569 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs, 00570 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs) 00571 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs))) 00572 { __lhs.swap(__rhs); } 00573 00574 } // namespace __debug 00575 } // namespace std 00576 00577 #endif