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