libstdc++
|
00001 // Set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2014 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 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996,1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_set.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{set} 00054 */ 00055 00056 #ifndef _STL_SET_H 00057 #define _STL_SET_H 1 00058 00059 #include <bits/concept_check.h> 00060 #if __cplusplus >= 201103L 00061 #include <initializer_list> 00062 #endif 00063 00064 namespace std _GLIBCXX_VISIBILITY(default) 00065 { 00066 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00067 00068 /** 00069 * @brief A standard container made up of unique keys, which can be 00070 * retrieved in logarithmic time. 00071 * 00072 * @ingroup associative_containers 00073 * 00074 * @tparam _Key Type of key objects. 00075 * @tparam _Compare Comparison function object type, defaults to less<_Key>. 00076 * @tparam _Alloc Allocator type, defaults to allocator<_Key>. 00077 * 00078 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00079 * <a href="tables.html#66">reversible container</a>, and an 00080 * <a href="tables.html#69">associative container</a> (using unique keys). 00081 * 00082 * Sets support bidirectional iterators. 00083 * 00084 * The private tree data is declared exactly the same way for set and 00085 * multiset; the distinction is made entirely in how the tree functions are 00086 * called (*_unique versus *_equal, same as the standard). 00087 */ 00088 template<typename _Key, typename _Compare = std::less<_Key>, 00089 typename _Alloc = std::allocator<_Key> > 00090 class set 00091 { 00092 // concept requirements 00093 typedef typename _Alloc::value_type _Alloc_value_type; 00094 __glibcxx_class_requires(_Key, _SGIAssignableConcept) 00095 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00096 _BinaryFunctionConcept) 00097 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept) 00098 00099 public: 00100 // typedefs: 00101 //@{ 00102 /// Public typedefs. 00103 typedef _Key key_type; 00104 typedef _Key value_type; 00105 typedef _Compare key_compare; 00106 typedef _Compare value_compare; 00107 typedef _Alloc allocator_type; 00108 //@} 00109 00110 private: 00111 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 00112 rebind<_Key>::other _Key_alloc_type; 00113 00114 typedef _Rb_tree<key_type, value_type, _Identity<value_type>, 00115 key_compare, _Key_alloc_type> _Rep_type; 00116 _Rep_type _M_t; // Red-black tree representing set. 00117 00118 typedef __gnu_cxx::__alloc_traits<_Key_alloc_type> _Alloc_traits; 00119 00120 public: 00121 //@{ 00122 /// Iterator-related typedefs. 00123 typedef typename _Alloc_traits::pointer pointer; 00124 typedef typename _Alloc_traits::const_pointer const_pointer; 00125 typedef typename _Alloc_traits::reference reference; 00126 typedef typename _Alloc_traits::const_reference const_reference; 00127 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00128 // DR 103. set::iterator is required to be modifiable, 00129 // but this allows modification of keys. 00130 typedef typename _Rep_type::const_iterator iterator; 00131 typedef typename _Rep_type::const_iterator const_iterator; 00132 typedef typename _Rep_type::const_reverse_iterator reverse_iterator; 00133 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00134 typedef typename _Rep_type::size_type size_type; 00135 typedef typename _Rep_type::difference_type difference_type; 00136 //@} 00137 00138 // allocation/deallocation 00139 /** 00140 * @brief Default constructor creates no elements. 00141 */ 00142 set() 00143 : _M_t() { } 00144 00145 /** 00146 * @brief Creates a %set with no elements. 00147 * @param __comp Comparator to use. 00148 * @param __a An allocator object. 00149 */ 00150 explicit 00151 set(const _Compare& __comp, 00152 const allocator_type& __a = allocator_type()) 00153 : _M_t(__comp, _Key_alloc_type(__a)) { } 00154 00155 /** 00156 * @brief Builds a %set from a range. 00157 * @param __first An input iterator. 00158 * @param __last An input iterator. 00159 * 00160 * Create a %set consisting of copies of the elements from 00161 * [__first,__last). This is linear in N if the range is 00162 * already sorted, and NlogN otherwise (where N is 00163 * distance(__first,__last)). 00164 */ 00165 template<typename _InputIterator> 00166 set(_InputIterator __first, _InputIterator __last) 00167 : _M_t() 00168 { _M_t._M_insert_unique(__first, __last); } 00169 00170 /** 00171 * @brief Builds a %set from a range. 00172 * @param __first An input iterator. 00173 * @param __last An input iterator. 00174 * @param __comp A comparison functor. 00175 * @param __a An allocator object. 00176 * 00177 * Create a %set consisting of copies of the elements from 00178 * [__first,__last). This is linear in N if the range is 00179 * already sorted, and NlogN otherwise (where N is 00180 * distance(__first,__last)). 00181 */ 00182 template<typename _InputIterator> 00183 set(_InputIterator __first, _InputIterator __last, 00184 const _Compare& __comp, 00185 const allocator_type& __a = allocator_type()) 00186 : _M_t(__comp, _Key_alloc_type(__a)) 00187 { _M_t._M_insert_unique(__first, __last); } 00188 00189 /** 00190 * @brief %Set copy constructor. 00191 * @param __x A %set of identical element and allocator types. 00192 * 00193 * The newly-created %set uses a copy of the allocation object used 00194 * by @a __x. 00195 */ 00196 set(const set& __x) 00197 : _M_t(__x._M_t) { } 00198 00199 #if __cplusplus >= 201103L 00200 /** 00201 * @brief %Set move constructor 00202 * @param __x A %set of identical element and allocator types. 00203 * 00204 * The newly-created %set contains the exact contents of @a x. 00205 * The contents of @a x are a valid, but unspecified %set. 00206 */ 00207 set(set&& __x) 00208 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00209 : _M_t(std::move(__x._M_t)) { } 00210 00211 /** 00212 * @brief Builds a %set from an initializer_list. 00213 * @param __l An initializer_list. 00214 * @param __comp A comparison functor. 00215 * @param __a An allocator object. 00216 * 00217 * Create a %set consisting of copies of the elements in the list. 00218 * This is linear in N if the list is already sorted, and NlogN 00219 * otherwise (where N is @a __l.size()). 00220 */ 00221 set(initializer_list<value_type> __l, 00222 const _Compare& __comp = _Compare(), 00223 const allocator_type& __a = allocator_type()) 00224 : _M_t(__comp, _Key_alloc_type(__a)) 00225 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00226 00227 /// Allocator-extended default constructor. 00228 explicit 00229 set(const allocator_type& __a) 00230 : _M_t(_Compare(), _Key_alloc_type(__a)) { } 00231 00232 /// Allocator-extended copy constructor. 00233 set(const set& __x, const allocator_type& __a) 00234 : _M_t(__x._M_t, _Key_alloc_type(__a)) { } 00235 00236 /// Allocator-extended move constructor. 00237 set(set&& __x, const allocator_type& __a) 00238 noexcept(is_nothrow_copy_constructible<_Compare>::value 00239 && _Alloc_traits::_S_always_equal()) 00240 : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { } 00241 00242 /// Allocator-extended initialier-list constructor. 00243 set(initializer_list<value_type> __l, const allocator_type& __a) 00244 : _M_t(_Compare(), _Key_alloc_type(__a)) 00245 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00246 00247 /// Allocator-extended range constructor. 00248 template<typename _InputIterator> 00249 set(_InputIterator __first, _InputIterator __last, 00250 const allocator_type& __a) 00251 : _M_t(_Compare(), _Key_alloc_type(__a)) 00252 { _M_t._M_insert_unique(__first, __last); } 00253 #endif 00254 00255 /** 00256 * @brief %Set assignment operator. 00257 * @param __x A %set of identical element and allocator types. 00258 * 00259 * All the elements of @a __x are copied, but unlike the copy 00260 * constructor, the allocator object is not copied. 00261 */ 00262 set& 00263 operator=(const set& __x) 00264 { 00265 _M_t = __x._M_t; 00266 return *this; 00267 } 00268 00269 #if __cplusplus >= 201103L 00270 /** 00271 * @brief %Set move assignment operator. 00272 * @param __x A %set of identical element and allocator types. 00273 * 00274 * The contents of @a __x are moved into this %set (without copying 00275 * if the allocators compare equal or get moved on assignment). 00276 * Afterwards @a __x is in a valid, but unspecified state. 00277 */ 00278 set& 00279 operator=(set&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) 00280 { 00281 if (!_M_t._M_move_assign(__x._M_t)) 00282 { 00283 // The rvalue's allocator cannot be moved and is not equal, 00284 // so we need to individually move each element. 00285 clear(); 00286 insert(std::__make_move_if_noexcept_iterator(__x._M_t.begin()), 00287 std::__make_move_if_noexcept_iterator(__x._M_t.end())); 00288 __x.clear(); 00289 } 00290 return *this; 00291 } 00292 00293 /** 00294 * @brief %Set list assignment operator. 00295 * @param __l An initializer_list. 00296 * 00297 * This function fills a %set with copies of the elements in the 00298 * initializer list @a __l. 00299 * 00300 * Note that the assignment completely changes the %set and 00301 * that the resulting %set's size is the same as the number 00302 * of elements assigned. Old data may be lost. 00303 */ 00304 set& 00305 operator=(initializer_list<value_type> __l) 00306 { 00307 this->clear(); 00308 this->insert(__l.begin(), __l.end()); 00309 return *this; 00310 } 00311 #endif 00312 00313 // accessors: 00314 00315 /// Returns the comparison object with which the %set was constructed. 00316 key_compare 00317 key_comp() const 00318 { return _M_t.key_comp(); } 00319 /// Returns the comparison object with which the %set was constructed. 00320 value_compare 00321 value_comp() const 00322 { return _M_t.key_comp(); } 00323 /// Returns the allocator object with which the %set was constructed. 00324 allocator_type 00325 get_allocator() const _GLIBCXX_NOEXCEPT 00326 { return allocator_type(_M_t.get_allocator()); } 00327 00328 /** 00329 * Returns a read-only (constant) iterator that points to the first 00330 * element in the %set. Iteration is done in ascending order according 00331 * to the keys. 00332 */ 00333 iterator 00334 begin() const _GLIBCXX_NOEXCEPT 00335 { return _M_t.begin(); } 00336 00337 /** 00338 * Returns a read-only (constant) iterator that points one past the last 00339 * element in the %set. Iteration is done in ascending order according 00340 * to the keys. 00341 */ 00342 iterator 00343 end() const _GLIBCXX_NOEXCEPT 00344 { return _M_t.end(); } 00345 00346 /** 00347 * Returns a read-only (constant) iterator that points to the last 00348 * element in the %set. Iteration is done in descending order according 00349 * to the keys. 00350 */ 00351 reverse_iterator 00352 rbegin() const _GLIBCXX_NOEXCEPT 00353 { return _M_t.rbegin(); } 00354 00355 /** 00356 * Returns a read-only (constant) reverse iterator that points to the 00357 * last pair in the %set. Iteration is done in descending order 00358 * according to the keys. 00359 */ 00360 reverse_iterator 00361 rend() const _GLIBCXX_NOEXCEPT 00362 { return _M_t.rend(); } 00363 00364 #if __cplusplus >= 201103L 00365 /** 00366 * Returns a read-only (constant) iterator that points to the first 00367 * element in the %set. Iteration is done in ascending order according 00368 * to the keys. 00369 */ 00370 iterator 00371 cbegin() const noexcept 00372 { return _M_t.begin(); } 00373 00374 /** 00375 * Returns a read-only (constant) iterator that points one past the last 00376 * element in the %set. Iteration is done in ascending order according 00377 * to the keys. 00378 */ 00379 iterator 00380 cend() const noexcept 00381 { return _M_t.end(); } 00382 00383 /** 00384 * Returns a read-only (constant) iterator that points to the last 00385 * element in the %set. Iteration is done in descending order according 00386 * to the keys. 00387 */ 00388 reverse_iterator 00389 crbegin() const noexcept 00390 { return _M_t.rbegin(); } 00391 00392 /** 00393 * Returns a read-only (constant) reverse iterator that points to the 00394 * last pair in the %set. Iteration is done in descending order 00395 * according to the keys. 00396 */ 00397 reverse_iterator 00398 crend() const noexcept 00399 { return _M_t.rend(); } 00400 #endif 00401 00402 /// Returns true if the %set is empty. 00403 bool 00404 empty() const _GLIBCXX_NOEXCEPT 00405 { return _M_t.empty(); } 00406 00407 /// Returns the size of the %set. 00408 size_type 00409 size() const _GLIBCXX_NOEXCEPT 00410 { return _M_t.size(); } 00411 00412 /// Returns the maximum size of the %set. 00413 size_type 00414 max_size() const _GLIBCXX_NOEXCEPT 00415 { return _M_t.max_size(); } 00416 00417 /** 00418 * @brief Swaps data with another %set. 00419 * @param __x A %set of the same element and allocator types. 00420 * 00421 * This exchanges the elements between two sets in constant 00422 * time. (It is only swapping a pointer, an integer, and an 00423 * instance of the @c Compare type (which itself is often 00424 * stateless and empty), so it should be quite fast.) Note 00425 * that the global std::swap() function is specialized such 00426 * that std::swap(s1,s2) will feed to this function. 00427 */ 00428 void 00429 swap(set& __x) 00430 #if __cplusplus >= 201103L 00431 noexcept(_Alloc_traits::_S_nothrow_swap()) 00432 #endif 00433 { _M_t.swap(__x._M_t); } 00434 00435 // insert/erase 00436 #if __cplusplus >= 201103L 00437 /** 00438 * @brief Attempts to build and insert an element into the %set. 00439 * @param __args Arguments used to generate an element. 00440 * @return A pair, of which the first element is an iterator that points 00441 * to the possibly inserted element, and the second is a bool 00442 * that is true if the element was actually inserted. 00443 * 00444 * This function attempts to build and insert an element into the %set. 00445 * A %set relies on unique keys and thus an element is only inserted if 00446 * it is not already present in the %set. 00447 * 00448 * Insertion requires logarithmic time. 00449 */ 00450 template<typename... _Args> 00451 std::pair<iterator, bool> 00452 emplace(_Args&&... __args) 00453 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } 00454 00455 /** 00456 * @brief Attempts to insert an element into the %set. 00457 * @param __pos An iterator that serves as a hint as to where the 00458 * element should be inserted. 00459 * @param __args Arguments used to generate the element to be 00460 * inserted. 00461 * @return An iterator that points to the element with key equivalent to 00462 * the one generated from @a __args (may or may not be the 00463 * element itself). 00464 * 00465 * This function is not concerned about whether the insertion took place, 00466 * and thus does not return a boolean like the single-argument emplace() 00467 * does. Note that the first parameter is only a hint and can 00468 * potentially improve the performance of the insertion process. A bad 00469 * hint would cause no gains in efficiency. 00470 * 00471 * For more on @a hinting, see: 00472 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html 00473 * 00474 * Insertion requires logarithmic time (if the hint is not taken). 00475 */ 00476 template<typename... _Args> 00477 iterator 00478 emplace_hint(const_iterator __pos, _Args&&... __args) 00479 { 00480 return _M_t._M_emplace_hint_unique(__pos, 00481 std::forward<_Args>(__args)...); 00482 } 00483 #endif 00484 00485 /** 00486 * @brief Attempts to insert an element into the %set. 00487 * @param __x Element to be inserted. 00488 * @return A pair, of which the first element is an iterator that points 00489 * to the possibly inserted element, and the second is a bool 00490 * that is true if the element was actually inserted. 00491 * 00492 * This function attempts to insert an element into the %set. A %set 00493 * relies on unique keys and thus an element is only inserted if it is 00494 * not already present in the %set. 00495 * 00496 * Insertion requires logarithmic time. 00497 */ 00498 std::pair<iterator, bool> 00499 insert(const value_type& __x) 00500 { 00501 std::pair<typename _Rep_type::iterator, bool> __p = 00502 _M_t._M_insert_unique(__x); 00503 return std::pair<iterator, bool>(__p.first, __p.second); 00504 } 00505 00506 #if __cplusplus >= 201103L 00507 std::pair<iterator, bool> 00508 insert(value_type&& __x) 00509 { 00510 std::pair<typename _Rep_type::iterator, bool> __p = 00511 _M_t._M_insert_unique(std::move(__x)); 00512 return std::pair<iterator, bool>(__p.first, __p.second); 00513 } 00514 #endif 00515 00516 /** 00517 * @brief Attempts to insert an element into the %set. 00518 * @param __position An iterator that serves as a hint as to where the 00519 * element should be inserted. 00520 * @param __x Element to be inserted. 00521 * @return An iterator that points to the element with key of 00522 * @a __x (may or may not be the element passed in). 00523 * 00524 * This function is not concerned about whether the insertion took place, 00525 * and thus does not return a boolean like the single-argument insert() 00526 * does. Note that the first parameter is only a hint and can 00527 * potentially improve the performance of the insertion process. A bad 00528 * hint would cause no gains in efficiency. 00529 * 00530 * For more on @a hinting, see: 00531 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html 00532 * 00533 * Insertion requires logarithmic time (if the hint is not taken). 00534 */ 00535 iterator 00536 insert(const_iterator __position, const value_type& __x) 00537 { return _M_t._M_insert_unique_(__position, __x); } 00538 00539 #if __cplusplus >= 201103L 00540 iterator 00541 insert(const_iterator __position, value_type&& __x) 00542 { return _M_t._M_insert_unique_(__position, std::move(__x)); } 00543 #endif 00544 00545 /** 00546 * @brief A template function that attempts to insert a range 00547 * of elements. 00548 * @param __first Iterator pointing to the start of the range to be 00549 * inserted. 00550 * @param __last Iterator pointing to the end of the range. 00551 * 00552 * Complexity similar to that of the range constructor. 00553 */ 00554 template<typename _InputIterator> 00555 void 00556 insert(_InputIterator __first, _InputIterator __last) 00557 { _M_t._M_insert_unique(__first, __last); } 00558 00559 #if __cplusplus >= 201103L 00560 /** 00561 * @brief Attempts to insert a list of elements into the %set. 00562 * @param __l A std::initializer_list<value_type> of elements 00563 * to be inserted. 00564 * 00565 * Complexity similar to that of the range constructor. 00566 */ 00567 void 00568 insert(initializer_list<value_type> __l) 00569 { this->insert(__l.begin(), __l.end()); } 00570 #endif 00571 00572 #if __cplusplus >= 201103L 00573 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00574 // DR 130. Associative erase should return an iterator. 00575 /** 00576 * @brief Erases an element from a %set. 00577 * @param __position An iterator pointing to the element to be erased. 00578 * @return An iterator pointing to the element immediately following 00579 * @a __position prior to the element being erased. If no such 00580 * element exists, end() is returned. 00581 * 00582 * This function erases an element, pointed to by the given iterator, 00583 * from a %set. Note that this function only erases the element, and 00584 * that if the element is itself a pointer, the pointed-to memory is not 00585 * touched in any way. Managing the pointer is the user's 00586 * responsibility. 00587 */ 00588 _GLIBCXX_ABI_TAG_CXX11 00589 iterator 00590 erase(const_iterator __position) 00591 { return _M_t.erase(__position); } 00592 #else 00593 /** 00594 * @brief Erases an element from a %set. 00595 * @param position An iterator pointing to the element to be erased. 00596 * 00597 * This function erases an element, pointed to by the given iterator, 00598 * from a %set. Note that this function only erases the element, and 00599 * that if the element is itself a pointer, the pointed-to memory is not 00600 * touched in any way. Managing the pointer is the user's 00601 * responsibility. 00602 */ 00603 void 00604 erase(iterator __position) 00605 { _M_t.erase(__position); } 00606 #endif 00607 00608 /** 00609 * @brief Erases elements according to the provided key. 00610 * @param __x Key of element to be erased. 00611 * @return The number of elements erased. 00612 * 00613 * This function erases all the elements located by the given key from 00614 * a %set. 00615 * Note that this function only erases the element, and that if 00616 * the element is itself a pointer, the pointed-to memory is not touched 00617 * in any way. Managing the pointer is the user's responsibility. 00618 */ 00619 size_type 00620 erase(const key_type& __x) 00621 { return _M_t.erase(__x); } 00622 00623 #if __cplusplus >= 201103L 00624 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00625 // DR 130. Associative erase should return an iterator. 00626 /** 00627 * @brief Erases a [__first,__last) range of elements from a %set. 00628 * @param __first Iterator pointing to the start of the range to be 00629 * erased. 00630 00631 * @param __last Iterator pointing to the end of the range to 00632 * be erased. 00633 * @return The iterator @a __last. 00634 * 00635 * This function erases a sequence of elements from a %set. 00636 * Note that this function only erases the element, and that if 00637 * the element is itself a pointer, the pointed-to memory is not touched 00638 * in any way. Managing the pointer is the user's responsibility. 00639 */ 00640 _GLIBCXX_ABI_TAG_CXX11 00641 iterator 00642 erase(const_iterator __first, const_iterator __last) 00643 { return _M_t.erase(__first, __last); } 00644 #else 00645 /** 00646 * @brief Erases a [first,last) range of elements from a %set. 00647 * @param __first Iterator pointing to the start of the range to be 00648 * erased. 00649 * @param __last Iterator pointing to the end of the range to 00650 * be erased. 00651 * 00652 * This function erases a sequence of elements from a %set. 00653 * Note that this function only erases the element, and that if 00654 * the element is itself a pointer, the pointed-to memory is not touched 00655 * in any way. Managing the pointer is the user's responsibility. 00656 */ 00657 void 00658 erase(iterator __first, iterator __last) 00659 { _M_t.erase(__first, __last); } 00660 #endif 00661 00662 /** 00663 * Erases all elements in a %set. Note that this function only erases 00664 * the elements, and that if the elements themselves are pointers, the 00665 * pointed-to memory is not touched in any way. Managing the pointer is 00666 * the user's responsibility. 00667 */ 00668 void 00669 clear() _GLIBCXX_NOEXCEPT 00670 { _M_t.clear(); } 00671 00672 // set operations: 00673 00674 /** 00675 * @brief Finds the number of elements. 00676 * @param __x Element to located. 00677 * @return Number of elements with specified key. 00678 * 00679 * This function only makes sense for multisets; for set the result will 00680 * either be 0 (not present) or 1 (present). 00681 */ 00682 size_type 00683 count(const key_type& __x) const 00684 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00685 00686 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00687 // 214. set::find() missing const overload 00688 //@{ 00689 /** 00690 * @brief Tries to locate an element in a %set. 00691 * @param __x Element to be located. 00692 * @return Iterator pointing to sought-after element, or end() if not 00693 * found. 00694 * 00695 * This function takes a key and tries to locate the element with which 00696 * the key matches. If successful the function returns an iterator 00697 * pointing to the sought after element. If unsuccessful it returns the 00698 * past-the-end ( @c end() ) iterator. 00699 */ 00700 iterator 00701 find(const key_type& __x) 00702 { return _M_t.find(__x); } 00703 00704 const_iterator 00705 find(const key_type& __x) const 00706 { return _M_t.find(__x); } 00707 //@} 00708 00709 //@{ 00710 /** 00711 * @brief Finds the beginning of a subsequence matching given key. 00712 * @param __x Key to be located. 00713 * @return Iterator pointing to first element equal to or greater 00714 * than key, or end(). 00715 * 00716 * This function returns the first element of a subsequence of elements 00717 * that matches the given key. If unsuccessful it returns an iterator 00718 * pointing to the first element that has a greater value than given key 00719 * or end() if no such element exists. 00720 */ 00721 iterator 00722 lower_bound(const key_type& __x) 00723 { return _M_t.lower_bound(__x); } 00724 00725 const_iterator 00726 lower_bound(const key_type& __x) const 00727 { return _M_t.lower_bound(__x); } 00728 //@} 00729 00730 //@{ 00731 /** 00732 * @brief Finds the end of a subsequence matching given key. 00733 * @param __x Key to be located. 00734 * @return Iterator pointing to the first element 00735 * greater than key, or end(). 00736 */ 00737 iterator 00738 upper_bound(const key_type& __x) 00739 { return _M_t.upper_bound(__x); } 00740 00741 const_iterator 00742 upper_bound(const key_type& __x) const 00743 { return _M_t.upper_bound(__x); } 00744 //@} 00745 00746 //@{ 00747 /** 00748 * @brief Finds a subsequence matching given key. 00749 * @param __x Key to be located. 00750 * @return Pair of iterators that possibly points to the subsequence 00751 * matching given key. 00752 * 00753 * This function is equivalent to 00754 * @code 00755 * std::make_pair(c.lower_bound(val), 00756 * c.upper_bound(val)) 00757 * @endcode 00758 * (but is faster than making the calls separately). 00759 * 00760 * This function probably only makes sense for multisets. 00761 */ 00762 std::pair<iterator, iterator> 00763 equal_range(const key_type& __x) 00764 { return _M_t.equal_range(__x); } 00765 00766 std::pair<const_iterator, const_iterator> 00767 equal_range(const key_type& __x) const 00768 { return _M_t.equal_range(__x); } 00769 //@} 00770 00771 template<typename _K1, typename _C1, typename _A1> 00772 friend bool 00773 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00774 00775 template<typename _K1, typename _C1, typename _A1> 00776 friend bool 00777 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00778 }; 00779 00780 00781 /** 00782 * @brief Set equality comparison. 00783 * @param __x A %set. 00784 * @param __y A %set of the same type as @a x. 00785 * @return True iff the size and elements of the sets are equal. 00786 * 00787 * This is an equivalence relation. It is linear in the size of the sets. 00788 * Sets are considered equivalent if their sizes are equal, and if 00789 * corresponding elements compare equal. 00790 */ 00791 template<typename _Key, typename _Compare, typename _Alloc> 00792 inline bool 00793 operator==(const set<_Key, _Compare, _Alloc>& __x, 00794 const set<_Key, _Compare, _Alloc>& __y) 00795 { return __x._M_t == __y._M_t; } 00796 00797 /** 00798 * @brief Set ordering relation. 00799 * @param __x A %set. 00800 * @param __y A %set of the same type as @a x. 00801 * @return True iff @a __x is lexicographically less than @a __y. 00802 * 00803 * This is a total ordering relation. It is linear in the size of the 00804 * sets. The elements must be comparable with @c <. 00805 * 00806 * See std::lexicographical_compare() for how the determination is made. 00807 */ 00808 template<typename _Key, typename _Compare, typename _Alloc> 00809 inline bool 00810 operator<(const set<_Key, _Compare, _Alloc>& __x, 00811 const set<_Key, _Compare, _Alloc>& __y) 00812 { return __x._M_t < __y._M_t; } 00813 00814 /// Returns !(x == y). 00815 template<typename _Key, typename _Compare, typename _Alloc> 00816 inline bool 00817 operator!=(const set<_Key, _Compare, _Alloc>& __x, 00818 const set<_Key, _Compare, _Alloc>& __y) 00819 { return !(__x == __y); } 00820 00821 /// Returns y < x. 00822 template<typename _Key, typename _Compare, typename _Alloc> 00823 inline bool 00824 operator>(const set<_Key, _Compare, _Alloc>& __x, 00825 const set<_Key, _Compare, _Alloc>& __y) 00826 { return __y < __x; } 00827 00828 /// Returns !(y < x) 00829 template<typename _Key, typename _Compare, typename _Alloc> 00830 inline bool 00831 operator<=(const set<_Key, _Compare, _Alloc>& __x, 00832 const set<_Key, _Compare, _Alloc>& __y) 00833 { return !(__y < __x); } 00834 00835 /// Returns !(x < y) 00836 template<typename _Key, typename _Compare, typename _Alloc> 00837 inline bool 00838 operator>=(const set<_Key, _Compare, _Alloc>& __x, 00839 const set<_Key, _Compare, _Alloc>& __y) 00840 { return !(__x < __y); } 00841 00842 /// See std::set::swap(). 00843 template<typename _Key, typename _Compare, typename _Alloc> 00844 inline void 00845 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) 00846 { __x.swap(__y); } 00847 00848 _GLIBCXX_END_NAMESPACE_CONTAINER 00849 } //namespace std 00850 #endif /* _STL_SET_H */