libstdc++
|
00001 // Profiling set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2009-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 profile/set.h 00026 * This file is a GNU profile extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _GLIBCXX_PROFILE_SET_H 00030 #define _GLIBCXX_PROFILE_SET_H 1 00031 00032 #include <utility> 00033 00034 namespace std _GLIBCXX_VISIBILITY(default) 00035 { 00036 namespace __profile 00037 { 00038 /// Class std::set wrapper with performance instrumentation. 00039 template<typename _Key, typename _Compare = std::less<_Key>, 00040 typename _Allocator = std::allocator<_Key> > 00041 class set 00042 : public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator> 00043 { 00044 typedef _GLIBCXX_STD_C::set<_Key, _Compare, _Allocator> _Base; 00045 00046 public: 00047 // types: 00048 typedef _Key key_type; 00049 typedef _Key value_type; 00050 typedef _Compare key_compare; 00051 typedef _Compare value_compare; 00052 typedef _Allocator allocator_type; 00053 typedef typename _Base::reference reference; 00054 typedef typename _Base::const_reference const_reference; 00055 00056 typedef typename _Base::iterator iterator; 00057 typedef typename _Base::const_iterator const_iterator; 00058 typedef typename _Base::reverse_iterator reverse_iterator; 00059 typedef typename _Base::const_reverse_iterator const_reverse_iterator; 00060 00061 typedef typename _Base::size_type size_type; 00062 typedef typename _Base::difference_type difference_type; 00063 typedef typename _Base::pointer pointer; 00064 typedef typename _Base::const_pointer const_pointer; 00065 00066 // 23.3.3.1 construct/copy/destroy: 00067 explicit set(const _Compare& __comp = _Compare(), 00068 const _Allocator& __a = _Allocator()) 00069 : _Base(__comp, __a) { } 00070 00071 #if __cplusplus >= 201103L 00072 template<typename _InputIterator, 00073 typename = std::_RequireInputIter<_InputIterator>> 00074 #else 00075 template<typename _InputIterator> 00076 #endif 00077 set(_InputIterator __first, _InputIterator __last, 00078 const _Compare& __comp = _Compare(), 00079 const _Allocator& __a = _Allocator()) 00080 : _Base(__first, __last, __comp, __a) { } 00081 00082 set(const set& __x) 00083 : _Base(__x) { } 00084 00085 set(const _Base& __x) 00086 : _Base(__x) { } 00087 00088 #if __cplusplus >= 201103L 00089 set(set&& __x) 00090 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00091 : _Base(std::move(__x)) 00092 { } 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 #endif 00099 00100 ~set() _GLIBCXX_NOEXCEPT { } 00101 00102 set& 00103 operator=(const set& __x) 00104 { 00105 *static_cast<_Base*>(this) = __x; 00106 return *this; 00107 } 00108 00109 #if __cplusplus >= 201103L 00110 set& 00111 operator=(set&& __x) 00112 { 00113 // NB: DR 1204. 00114 // NB: DR 675. 00115 this->clear(); 00116 this->swap(__x); 00117 return *this; 00118 } 00119 00120 set& 00121 operator=(initializer_list<value_type> __l) 00122 { 00123 this->clear(); 00124 this->insert(__l); 00125 return *this; 00126 } 00127 #endif 00128 00129 using _Base::get_allocator; 00130 00131 // iterators: 00132 iterator 00133 begin() _GLIBCXX_NOEXCEPT 00134 { return iterator(_Base::begin()); } 00135 00136 const_iterator 00137 begin() const _GLIBCXX_NOEXCEPT 00138 { return const_iterator(_Base::begin()); } 00139 00140 iterator 00141 end() _GLIBCXX_NOEXCEPT 00142 { return iterator(_Base::end()); } 00143 00144 const_iterator 00145 end() const _GLIBCXX_NOEXCEPT 00146 { return const_iterator(_Base::end()); } 00147 00148 reverse_iterator 00149 rbegin() _GLIBCXX_NOEXCEPT 00150 { return reverse_iterator(end()); } 00151 00152 const_reverse_iterator 00153 rbegin() const _GLIBCXX_NOEXCEPT 00154 { return const_reverse_iterator(end()); } 00155 00156 reverse_iterator 00157 rend() _GLIBCXX_NOEXCEPT 00158 { return reverse_iterator(begin()); } 00159 00160 const_reverse_iterator 00161 rend() const _GLIBCXX_NOEXCEPT 00162 { return const_reverse_iterator(begin()); } 00163 00164 #if __cplusplus >= 201103L 00165 const_iterator 00166 cbegin() const noexcept 00167 { return const_iterator(_Base::begin()); } 00168 00169 const_iterator 00170 cend() const noexcept 00171 { return const_iterator(_Base::end()); } 00172 00173 const_reverse_iterator 00174 crbegin() const noexcept 00175 { return const_reverse_iterator(end()); } 00176 00177 const_reverse_iterator 00178 crend() const noexcept 00179 { return const_reverse_iterator(begin()); } 00180 #endif 00181 00182 // capacity: 00183 using _Base::empty; 00184 using _Base::size; 00185 using _Base::max_size; 00186 00187 // modifiers: 00188 #if __cplusplus >= 201103L 00189 template<typename... _Args> 00190 std::pair<iterator, bool> 00191 emplace(_Args&&... __args) 00192 { 00193 auto __res = _Base::emplace(std::forward<_Args>(__args)...); 00194 return std::pair<iterator, bool>(iterator(__res.first), 00195 __res.second); 00196 } 00197 00198 template<typename... _Args> 00199 iterator 00200 emplace_hint(const_iterator __pos, _Args&&... __args) 00201 { 00202 return iterator(_Base::emplace_hint(__pos, 00203 std::forward<_Args>(__args)...)); 00204 } 00205 #endif 00206 00207 std::pair<iterator, bool> 00208 insert(const value_type& __x) 00209 { 00210 typedef typename _Base::iterator _Base_iterator; 00211 std::pair<_Base_iterator, bool> __res = _Base::insert(__x); 00212 return std::pair<iterator, bool>(iterator(__res.first), 00213 __res.second); 00214 } 00215 00216 #if __cplusplus >= 201103L 00217 std::pair<iterator, bool> 00218 insert(value_type&& __x) 00219 { 00220 typedef typename _Base::iterator _Base_iterator; 00221 std::pair<_Base_iterator, bool> __res 00222 = _Base::insert(std::move(__x)); 00223 return std::pair<iterator, bool>(iterator(__res.first), 00224 __res.second); 00225 } 00226 #endif 00227 00228 iterator 00229 insert(const_iterator __position, const value_type& __x) 00230 { return iterator(_Base::insert(__position, __x)); } 00231 00232 #if __cplusplus >= 201103L 00233 iterator 00234 insert(const_iterator __position, value_type&& __x) 00235 { return iterator(_Base::insert(__position, std::move(__x))); } 00236 #endif 00237 00238 #if __cplusplus >= 201103L 00239 template<typename _InputIterator, 00240 typename = std::_RequireInputIter<_InputIterator>> 00241 #else 00242 template<typename _InputIterator> 00243 #endif 00244 void 00245 insert(_InputIterator __first, _InputIterator __last) 00246 { _Base::insert(__first, __last); } 00247 00248 #if __cplusplus >= 201103L 00249 void 00250 insert(initializer_list<value_type> __l) 00251 { _Base::insert(__l); } 00252 #endif 00253 00254 #if __cplusplus >= 201103L 00255 iterator 00256 erase(const_iterator __position) 00257 { return iterator(_Base::erase(__position)); } 00258 #else 00259 void 00260 erase(iterator __position) 00261 { _Base::erase(__position); } 00262 #endif 00263 00264 size_type 00265 erase(const key_type& __x) 00266 { 00267 iterator __victim = find(__x); 00268 if (__victim == end()) 00269 return 0; 00270 else 00271 { 00272 _Base::erase(__victim); 00273 return 1; 00274 } 00275 } 00276 00277 #if __cplusplus >= 201103L 00278 iterator 00279 erase(const_iterator __first, const_iterator __last) 00280 { return iterator(_Base::erase(__first, __last)); } 00281 #else 00282 void 00283 erase(iterator __first, iterator __last) 00284 { _Base::erase(__first, __last); } 00285 #endif 00286 00287 void 00288 swap(set& __x) 00289 { _Base::swap(__x); } 00290 00291 void 00292 clear() _GLIBCXX_NOEXCEPT 00293 { this->erase(begin(), end()); } 00294 00295 // observers: 00296 using _Base::key_comp; 00297 using _Base::value_comp; 00298 00299 // set operations: 00300 iterator 00301 find(const key_type& __x) 00302 { return iterator(_Base::find(__x)); } 00303 00304 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00305 // 214. set::find() missing const overload 00306 const_iterator 00307 find(const key_type& __x) const 00308 { return const_iterator(_Base::find(__x)); } 00309 00310 using _Base::count; 00311 00312 iterator 00313 lower_bound(const key_type& __x) 00314 { return iterator(_Base::lower_bound(__x)); } 00315 00316 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00317 // 214. set::find() missing const overload 00318 const_iterator 00319 lower_bound(const key_type& __x) const 00320 { return const_iterator(_Base::lower_bound(__x)); } 00321 00322 iterator 00323 upper_bound(const key_type& __x) 00324 { return iterator(_Base::upper_bound(__x)); } 00325 00326 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00327 // 214. set::find() missing const overload 00328 const_iterator 00329 upper_bound(const key_type& __x) const 00330 { return const_iterator(_Base::upper_bound(__x)); } 00331 00332 std::pair<iterator,iterator> 00333 equal_range(const key_type& __x) 00334 { 00335 typedef typename _Base::iterator _Base_iterator; 00336 std::pair<_Base_iterator, _Base_iterator> __res = 00337 _Base::equal_range(__x); 00338 return std::make_pair(iterator(__res.first), 00339 iterator(__res.second)); 00340 } 00341 00342 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00343 // 214. set::find() missing const overload 00344 std::pair<const_iterator,const_iterator> 00345 equal_range(const key_type& __x) const 00346 { 00347 typedef typename _Base::const_iterator _Base_iterator; 00348 std::pair<_Base_iterator, _Base_iterator> __res = 00349 _Base::equal_range(__x); 00350 return std::make_pair(const_iterator(__res.first), 00351 const_iterator(__res.second)); 00352 } 00353 00354 _Base& 00355 _M_base() _GLIBCXX_NOEXCEPT { return *this; } 00356 00357 const _Base& 00358 _M_base() const _GLIBCXX_NOEXCEPT { return *this; } 00359 00360 }; 00361 00362 template<typename _Key, typename _Compare, typename _Allocator> 00363 inline bool 00364 operator==(const set<_Key, _Compare, _Allocator>& __lhs, 00365 const set<_Key, _Compare, _Allocator>& __rhs) 00366 { return __lhs._M_base() == __rhs._M_base(); } 00367 00368 template<typename _Key, typename _Compare, typename _Allocator> 00369 inline bool 00370 operator!=(const set<_Key, _Compare, _Allocator>& __lhs, 00371 const set<_Key, _Compare, _Allocator>& __rhs) 00372 { return __lhs._M_base() != __rhs._M_base(); } 00373 00374 template<typename _Key, typename _Compare, typename _Allocator> 00375 inline bool 00376 operator<(const set<_Key, _Compare, _Allocator>& __lhs, 00377 const set<_Key, _Compare, _Allocator>& __rhs) 00378 { return __lhs._M_base() < __rhs._M_base(); } 00379 00380 template<typename _Key, typename _Compare, typename _Allocator> 00381 inline bool 00382 operator<=(const set<_Key, _Compare, _Allocator>& __lhs, 00383 const set<_Key, _Compare, _Allocator>& __rhs) 00384 { return __lhs._M_base() <= __rhs._M_base(); } 00385 00386 template<typename _Key, typename _Compare, typename _Allocator> 00387 inline bool 00388 operator>=(const set<_Key, _Compare, _Allocator>& __lhs, 00389 const set<_Key, _Compare, _Allocator>& __rhs) 00390 { return __lhs._M_base() >= __rhs._M_base(); } 00391 00392 template<typename _Key, typename _Compare, typename _Allocator> 00393 inline bool 00394 operator>(const set<_Key, _Compare, _Allocator>& __lhs, 00395 const set<_Key, _Compare, _Allocator>& __rhs) 00396 { return __lhs._M_base() > __rhs._M_base(); } 00397 00398 template<typename _Key, typename _Compare, typename _Allocator> 00399 void 00400 swap(set<_Key, _Compare, _Allocator>& __x, 00401 set<_Key, _Compare, _Allocator>& __y) 00402 { return __x.swap(__y); } 00403 00404 } // namespace __profile 00405 } // namespace std 00406 00407 #endif