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