libstdc++
|
00001 // Debugging set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003-2015 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 __glibcxx_check_valid_range(__first, __last); 00284 _Base::insert(__gnu_debug::__base(__first), 00285 __gnu_debug::__base(__last)); 00286 } 00287 00288 #if __cplusplus >= 201103L 00289 void 00290 insert(initializer_list<value_type> __l) 00291 { _Base::insert(__l); } 00292 #endif 00293 00294 #if __cplusplus >= 201103L 00295 iterator 00296 erase(const_iterator __position) 00297 { 00298 __glibcxx_check_erase(__position); 00299 this->_M_invalidate_if(_Equal(__position.base())); 00300 return iterator(_Base::erase(__position.base()), this); 00301 } 00302 #else 00303 void 00304 erase(iterator __position) 00305 { 00306 __glibcxx_check_erase(__position); 00307 this->_M_invalidate_if(_Equal(__position.base())); 00308 _Base::erase(__position.base()); 00309 } 00310 #endif 00311 00312 size_type 00313 erase(const key_type& __x) 00314 { 00315 _Base_iterator __victim = _Base::find(__x); 00316 if (__victim == _Base::end()) 00317 return 0; 00318 else 00319 { 00320 this->_M_invalidate_if(_Equal(__victim)); 00321 _Base::erase(__victim); 00322 return 1; 00323 } 00324 } 00325 00326 #if __cplusplus >= 201103L 00327 iterator 00328 erase(const_iterator __first, const_iterator __last) 00329 { 00330 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00331 // 151. can't currently clear() empty container 00332 __glibcxx_check_erase_range(__first, __last); 00333 for (_Base_const_iterator __victim = __first.base(); 00334 __victim != __last.base(); ++__victim) 00335 { 00336 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00337 _M_message(__gnu_debug::__msg_valid_range) 00338 ._M_iterator(__first, "first") 00339 ._M_iterator(__last, "last")); 00340 this->_M_invalidate_if(_Equal(__victim)); 00341 } 00342 return iterator(_Base::erase(__first.base(), __last.base()), this); 00343 } 00344 #else 00345 void 00346 erase(iterator __first, iterator __last) 00347 { 00348 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00349 // 151. can't currently clear() empty container 00350 __glibcxx_check_erase_range(__first, __last); 00351 for (_Base_iterator __victim = __first.base(); 00352 __victim != __last.base(); ++__victim) 00353 { 00354 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), 00355 _M_message(__gnu_debug::__msg_valid_range) 00356 ._M_iterator(__first, "first") 00357 ._M_iterator(__last, "last")); 00358 this->_M_invalidate_if(_Equal(__victim)); 00359 } 00360 _Base::erase(__first.base(), __last.base()); 00361 } 00362 #endif 00363 00364 void 00365 swap(set& __x) 00366 #if __cplusplus >= 201103L 00367 noexcept( noexcept(declval<_Base>().swap(__x)) ) 00368 #endif 00369 { 00370 _Safe::_M_swap(__x); 00371 _Base::swap(__x); 00372 } 00373 00374 void 00375 clear() _GLIBCXX_NOEXCEPT 00376 { 00377 this->_M_invalidate_all(); 00378 _Base::clear(); 00379 } 00380 00381 // observers: 00382 using _Base::key_comp; 00383 using _Base::value_comp; 00384 00385 // set operations: 00386 iterator 00387 find(const key_type& __x) 00388 { return iterator(_Base::find(__x), this); } 00389 00390 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00391 // 214. set::find() missing const overload 00392 const_iterator 00393 find(const key_type& __x) const 00394 { return const_iterator(_Base::find(__x), this); } 00395 00396 using _Base::count; 00397 00398 iterator 00399 lower_bound(const key_type& __x) 00400 { return iterator(_Base::lower_bound(__x), this); } 00401 00402 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00403 // 214. set::find() missing const overload 00404 const_iterator 00405 lower_bound(const key_type& __x) const 00406 { return const_iterator(_Base::lower_bound(__x), this); } 00407 00408 iterator 00409 upper_bound(const key_type& __x) 00410 { return iterator(_Base::upper_bound(__x), this); } 00411 00412 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00413 // 214. set::find() missing const overload 00414 const_iterator 00415 upper_bound(const key_type& __x) const 00416 { return const_iterator(_Base::upper_bound(__x), this); } 00417 00418 std::pair<iterator,iterator> 00419 equal_range(const key_type& __x) 00420 { 00421 std::pair<_Base_iterator, _Base_iterator> __res = 00422 _Base::equal_range(__x); 00423 return std::make_pair(iterator(__res.first, this), 00424 iterator(__res.second, this)); 00425 } 00426 00427 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00428 // 214. set::find() missing const overload 00429 std::pair<const_iterator,const_iterator> 00430 equal_range(const key_type& __x) const 00431 { 00432 std::pair<_Base_iterator, _Base_iterator> __res = 00433 _Base::equal_range(__x); 00434 return std::make_pair(const_iterator(__res.first, this), 00435 const_iterator(__res.second, this)); 00436 } 00437 00438 _Base& 00439 _M_base() _GLIBCXX_NOEXCEPT { return *this; } 00440 00441 const _Base& 00442 _M_base() const _GLIBCXX_NOEXCEPT { return *this; } 00443 }; 00444 00445 template<typename _Key, typename _Compare, typename _Allocator> 00446 inline bool 00447 operator==(const set<_Key, _Compare, _Allocator>& __lhs, 00448 const set<_Key, _Compare, _Allocator>& __rhs) 00449 { return __lhs._M_base() == __rhs._M_base(); } 00450 00451 template<typename _Key, typename _Compare, typename _Allocator> 00452 inline bool 00453 operator!=(const set<_Key, _Compare, _Allocator>& __lhs, 00454 const set<_Key, _Compare, _Allocator>& __rhs) 00455 { return __lhs._M_base() != __rhs._M_base(); } 00456 00457 template<typename _Key, typename _Compare, typename _Allocator> 00458 inline bool 00459 operator<(const set<_Key, _Compare, _Allocator>& __lhs, 00460 const set<_Key, _Compare, _Allocator>& __rhs) 00461 { return __lhs._M_base() < __rhs._M_base(); } 00462 00463 template<typename _Key, typename _Compare, typename _Allocator> 00464 inline bool 00465 operator<=(const set<_Key, _Compare, _Allocator>& __lhs, 00466 const set<_Key, _Compare, _Allocator>& __rhs) 00467 { return __lhs._M_base() <= __rhs._M_base(); } 00468 00469 template<typename _Key, typename _Compare, typename _Allocator> 00470 inline bool 00471 operator>=(const set<_Key, _Compare, _Allocator>& __lhs, 00472 const set<_Key, _Compare, _Allocator>& __rhs) 00473 { return __lhs._M_base() >= __rhs._M_base(); } 00474 00475 template<typename _Key, typename _Compare, typename _Allocator> 00476 inline bool 00477 operator>(const set<_Key, _Compare, _Allocator>& __lhs, 00478 const set<_Key, _Compare, _Allocator>& __rhs) 00479 { return __lhs._M_base() > __rhs._M_base(); } 00480 00481 template<typename _Key, typename _Compare, typename _Allocator> 00482 void 00483 swap(set<_Key, _Compare, _Allocator>& __x, 00484 set<_Key, _Compare, _Allocator>& __y) 00485 { return __x.swap(__y); } 00486 00487 } // namespace __debug 00488 } // namespace std 00489 00490 #endif