libstdc++
|
00001 // Set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-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 /* 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 _GLIBCXX_NOEXCEPT_IF( 00144 is_nothrow_default_constructible<allocator_type>::value 00145 && is_nothrow_default_constructible<key_compare>::value) 00146 : _M_t() { } 00147 00148 /** 00149 * @brief Creates a %set with no elements. 00150 * @param __comp Comparator to use. 00151 * @param __a An allocator object. 00152 */ 00153 explicit 00154 set(const _Compare& __comp, 00155 const allocator_type& __a = allocator_type()) 00156 : _M_t(__comp, _Key_alloc_type(__a)) { } 00157 00158 /** 00159 * @brief Builds a %set from a range. 00160 * @param __first An input iterator. 00161 * @param __last An input iterator. 00162 * 00163 * Create a %set consisting of copies of the elements from 00164 * [__first,__last). This is linear in N if the range is 00165 * already sorted, and NlogN otherwise (where N is 00166 * distance(__first,__last)). 00167 */ 00168 template<typename _InputIterator> 00169 set(_InputIterator __first, _InputIterator __last) 00170 : _M_t() 00171 { _M_t._M_insert_unique(__first, __last); } 00172 00173 /** 00174 * @brief Builds a %set from a range. 00175 * @param __first An input iterator. 00176 * @param __last An input iterator. 00177 * @param __comp A comparison functor. 00178 * @param __a An allocator object. 00179 * 00180 * Create a %set consisting of copies of the elements from 00181 * [__first,__last). This is linear in N if the range is 00182 * already sorted, and NlogN otherwise (where N is 00183 * distance(__first,__last)). 00184 */ 00185 template<typename _InputIterator> 00186 set(_InputIterator __first, _InputIterator __last, 00187 const _Compare& __comp, 00188 const allocator_type& __a = allocator_type()) 00189 : _M_t(__comp, _Key_alloc_type(__a)) 00190 { _M_t._M_insert_unique(__first, __last); } 00191 00192 /** 00193 * @brief %Set copy constructor. 00194 * @param __x A %set of identical element and allocator types. 00195 * 00196 * The newly-created %set uses a copy of the allocation object used 00197 * by @a __x. 00198 */ 00199 set(const set& __x) 00200 : _M_t(__x._M_t) { } 00201 00202 #if __cplusplus >= 201103L 00203 /** 00204 * @brief %Set move constructor 00205 * @param __x A %set of identical element and allocator types. 00206 * 00207 * The newly-created %set contains the exact contents of @a x. 00208 * The contents of @a x are a valid, but unspecified %set. 00209 */ 00210 set(set&& __x) 00211 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00212 : _M_t(std::move(__x._M_t)) { } 00213 00214 /** 00215 * @brief Builds a %set from an initializer_list. 00216 * @param __l An initializer_list. 00217 * @param __comp A comparison functor. 00218 * @param __a An allocator object. 00219 * 00220 * Create a %set consisting of copies of the elements in the list. 00221 * This is linear in N if the list is already sorted, and NlogN 00222 * otherwise (where N is @a __l.size()). 00223 */ 00224 set(initializer_list<value_type> __l, 00225 const _Compare& __comp = _Compare(), 00226 const allocator_type& __a = allocator_type()) 00227 : _M_t(__comp, _Key_alloc_type(__a)) 00228 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00229 00230 /// Allocator-extended default constructor. 00231 explicit 00232 set(const allocator_type& __a) 00233 : _M_t(_Compare(), _Key_alloc_type(__a)) { } 00234 00235 /// Allocator-extended copy constructor. 00236 set(const set& __x, const allocator_type& __a) 00237 : _M_t(__x._M_t, _Key_alloc_type(__a)) { } 00238 00239 /// Allocator-extended move constructor. 00240 set(set&& __x, const allocator_type& __a) 00241 noexcept(is_nothrow_copy_constructible<_Compare>::value 00242 && _Alloc_traits::_S_always_equal()) 00243 : _M_t(std::move(__x._M_t), _Key_alloc_type(__a)) { } 00244 00245 /// Allocator-extended initialier-list constructor. 00246 set(initializer_list<value_type> __l, const allocator_type& __a) 00247 : _M_t(_Compare(), _Key_alloc_type(__a)) 00248 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00249 00250 /// Allocator-extended range constructor. 00251 template<typename _InputIterator> 00252 set(_InputIterator __first, _InputIterator __last, 00253 const allocator_type& __a) 00254 : _M_t(_Compare(), _Key_alloc_type(__a)) 00255 { _M_t._M_insert_unique(__first, __last); } 00256 #endif 00257 00258 /** 00259 * @brief %Set assignment operator. 00260 * @param __x A %set of identical element and allocator types. 00261 * 00262 * All the elements of @a __x are copied, but unlike the copy 00263 * constructor, the allocator object is not copied. 00264 */ 00265 set& 00266 operator=(const set& __x) 00267 { 00268 _M_t = __x._M_t; 00269 return *this; 00270 } 00271 00272 #if __cplusplus >= 201103L 00273 /// Move assignment operator. 00274 set& 00275 operator=(set&&) = default; 00276 00277 /** 00278 * @brief %Set list assignment operator. 00279 * @param __l An initializer_list. 00280 * 00281 * This function fills a %set with copies of the elements in the 00282 * initializer list @a __l. 00283 * 00284 * Note that the assignment completely changes the %set and 00285 * that the resulting %set's size is the same as the number 00286 * of elements assigned. Old data may be lost. 00287 */ 00288 set& 00289 operator=(initializer_list<value_type> __l) 00290 { 00291 _M_t._M_assign_unique(__l.begin(), __l.end()); 00292 return *this; 00293 } 00294 #endif 00295 00296 // accessors: 00297 00298 /// Returns the comparison object with which the %set was constructed. 00299 key_compare 00300 key_comp() const 00301 { return _M_t.key_comp(); } 00302 /// Returns the comparison object with which the %set was constructed. 00303 value_compare 00304 value_comp() const 00305 { return _M_t.key_comp(); } 00306 /// Returns the allocator object with which the %set was constructed. 00307 allocator_type 00308 get_allocator() const _GLIBCXX_NOEXCEPT 00309 { return allocator_type(_M_t.get_allocator()); } 00310 00311 /** 00312 * Returns a read-only (constant) iterator that points to the first 00313 * element in the %set. Iteration is done in ascending order according 00314 * to the keys. 00315 */ 00316 iterator 00317 begin() const _GLIBCXX_NOEXCEPT 00318 { return _M_t.begin(); } 00319 00320 /** 00321 * Returns a read-only (constant) iterator that points one past the last 00322 * element in the %set. Iteration is done in ascending order according 00323 * to the keys. 00324 */ 00325 iterator 00326 end() const _GLIBCXX_NOEXCEPT 00327 { return _M_t.end(); } 00328 00329 /** 00330 * Returns a read-only (constant) iterator that points to the last 00331 * element in the %set. Iteration is done in descending order according 00332 * to the keys. 00333 */ 00334 reverse_iterator 00335 rbegin() const _GLIBCXX_NOEXCEPT 00336 { return _M_t.rbegin(); } 00337 00338 /** 00339 * Returns a read-only (constant) reverse iterator that points to the 00340 * last pair in the %set. Iteration is done in descending order 00341 * according to the keys. 00342 */ 00343 reverse_iterator 00344 rend() const _GLIBCXX_NOEXCEPT 00345 { return _M_t.rend(); } 00346 00347 #if __cplusplus >= 201103L 00348 /** 00349 * Returns a read-only (constant) iterator that points to the first 00350 * element in the %set. Iteration is done in ascending order according 00351 * to the keys. 00352 */ 00353 iterator 00354 cbegin() const noexcept 00355 { return _M_t.begin(); } 00356 00357 /** 00358 * Returns a read-only (constant) iterator that points one past the last 00359 * element in the %set. Iteration is done in ascending order according 00360 * to the keys. 00361 */ 00362 iterator 00363 cend() const noexcept 00364 { return _M_t.end(); } 00365 00366 /** 00367 * Returns a read-only (constant) iterator that points to the last 00368 * element in the %set. Iteration is done in descending order according 00369 * to the keys. 00370 */ 00371 reverse_iterator 00372 crbegin() const noexcept 00373 { return _M_t.rbegin(); } 00374 00375 /** 00376 * Returns a read-only (constant) reverse iterator that points to the 00377 * last pair in the %set. Iteration is done in descending order 00378 * according to the keys. 00379 */ 00380 reverse_iterator 00381 crend() const noexcept 00382 { return _M_t.rend(); } 00383 #endif 00384 00385 /// Returns true if the %set is empty. 00386 bool 00387 empty() const _GLIBCXX_NOEXCEPT 00388 { return _M_t.empty(); } 00389 00390 /// Returns the size of the %set. 00391 size_type 00392 size() const _GLIBCXX_NOEXCEPT 00393 { return _M_t.size(); } 00394 00395 /// Returns the maximum size of the %set. 00396 size_type 00397 max_size() const _GLIBCXX_NOEXCEPT 00398 { return _M_t.max_size(); } 00399 00400 /** 00401 * @brief Swaps data with another %set. 00402 * @param __x A %set of the same element and allocator types. 00403 * 00404 * This exchanges the elements between two sets in constant 00405 * time. (It is only swapping a pointer, an integer, and an 00406 * instance of the @c Compare type (which itself is often 00407 * stateless and empty), so it should be quite fast.) Note 00408 * that the global std::swap() function is specialized such 00409 * that std::swap(s1,s2) will feed to this function. 00410 */ 00411 void 00412 swap(set& __x) 00413 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value) 00414 { _M_t.swap(__x._M_t); } 00415 00416 // insert/erase 00417 #if __cplusplus >= 201103L 00418 /** 00419 * @brief Attempts to build and insert an element into the %set. 00420 * @param __args Arguments used to generate an element. 00421 * @return A pair, of which the first element is an iterator that points 00422 * to the possibly inserted element, and the second is a bool 00423 * that is true if the element was actually inserted. 00424 * 00425 * This function attempts to build and insert an element into the %set. 00426 * A %set relies on unique keys and thus an element is only inserted if 00427 * it is not already present in the %set. 00428 * 00429 * Insertion requires logarithmic time. 00430 */ 00431 template<typename... _Args> 00432 std::pair<iterator, bool> 00433 emplace(_Args&&... __args) 00434 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } 00435 00436 /** 00437 * @brief Attempts to insert an element into the %set. 00438 * @param __pos An iterator that serves as a hint as to where the 00439 * element should be inserted. 00440 * @param __args Arguments used to generate the element to be 00441 * inserted. 00442 * @return An iterator that points to the element with key equivalent to 00443 * the one generated from @a __args (may or may not be the 00444 * element itself). 00445 * 00446 * This function is not concerned about whether the insertion took place, 00447 * and thus does not return a boolean like the single-argument emplace() 00448 * does. Note that the first parameter is only a hint and can 00449 * potentially improve the performance of the insertion process. A bad 00450 * hint would cause no gains in efficiency. 00451 * 00452 * For more on @a hinting, see: 00453 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00454 * 00455 * Insertion requires logarithmic time (if the hint is not taken). 00456 */ 00457 template<typename... _Args> 00458 iterator 00459 emplace_hint(const_iterator __pos, _Args&&... __args) 00460 { 00461 return _M_t._M_emplace_hint_unique(__pos, 00462 std::forward<_Args>(__args)...); 00463 } 00464 #endif 00465 00466 /** 00467 * @brief Attempts to insert an element into the %set. 00468 * @param __x Element to be inserted. 00469 * @return A pair, of which the first element is an iterator that points 00470 * to the possibly inserted element, and the second is a bool 00471 * that is true if the element was actually inserted. 00472 * 00473 * This function attempts to insert an element into the %set. A %set 00474 * relies on unique keys and thus an element is only inserted if it is 00475 * not already present in the %set. 00476 * 00477 * Insertion requires logarithmic time. 00478 */ 00479 std::pair<iterator, bool> 00480 insert(const value_type& __x) 00481 { 00482 std::pair<typename _Rep_type::iterator, bool> __p = 00483 _M_t._M_insert_unique(__x); 00484 return std::pair<iterator, bool>(__p.first, __p.second); 00485 } 00486 00487 #if __cplusplus >= 201103L 00488 std::pair<iterator, bool> 00489 insert(value_type&& __x) 00490 { 00491 std::pair<typename _Rep_type::iterator, bool> __p = 00492 _M_t._M_insert_unique(std::move(__x)); 00493 return std::pair<iterator, bool>(__p.first, __p.second); 00494 } 00495 #endif 00496 00497 /** 00498 * @brief Attempts to insert an element into the %set. 00499 * @param __position An iterator that serves as a hint as to where the 00500 * element should be inserted. 00501 * @param __x Element to be inserted. 00502 * @return An iterator that points to the element with key of 00503 * @a __x (may or may not be the element passed in). 00504 * 00505 * This function is not concerned about whether the insertion took place, 00506 * and thus does not return a boolean like the single-argument insert() 00507 * does. Note that the first parameter is only a hint and can 00508 * potentially improve the performance of the insertion process. A bad 00509 * hint would cause no gains in efficiency. 00510 * 00511 * For more on @a hinting, see: 00512 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00513 * 00514 * Insertion requires logarithmic time (if the hint is not taken). 00515 */ 00516 iterator 00517 insert(const_iterator __position, const value_type& __x) 00518 { return _M_t._M_insert_unique_(__position, __x); } 00519 00520 #if __cplusplus >= 201103L 00521 iterator 00522 insert(const_iterator __position, value_type&& __x) 00523 { return _M_t._M_insert_unique_(__position, std::move(__x)); } 00524 #endif 00525 00526 /** 00527 * @brief A template function that attempts to insert a range 00528 * of elements. 00529 * @param __first Iterator pointing to the start of the range to be 00530 * inserted. 00531 * @param __last Iterator pointing to the end of the range. 00532 * 00533 * Complexity similar to that of the range constructor. 00534 */ 00535 template<typename _InputIterator> 00536 void 00537 insert(_InputIterator __first, _InputIterator __last) 00538 { _M_t._M_insert_unique(__first, __last); } 00539 00540 #if __cplusplus >= 201103L 00541 /** 00542 * @brief Attempts to insert a list of elements into the %set. 00543 * @param __l A std::initializer_list<value_type> of elements 00544 * to be inserted. 00545 * 00546 * Complexity similar to that of the range constructor. 00547 */ 00548 void 00549 insert(initializer_list<value_type> __l) 00550 { this->insert(__l.begin(), __l.end()); } 00551 #endif 00552 00553 #if __cplusplus >= 201103L 00554 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00555 // DR 130. Associative erase should return an iterator. 00556 /** 00557 * @brief Erases an element from a %set. 00558 * @param __position An iterator pointing to the element to be erased. 00559 * @return An iterator pointing to the element immediately following 00560 * @a __position prior to the element being erased. If no such 00561 * element exists, end() is returned. 00562 * 00563 * This function erases an element, pointed to by the given iterator, 00564 * from a %set. Note that this function only erases the element, and 00565 * that if the element is itself a pointer, the pointed-to memory is not 00566 * touched in any way. Managing the pointer is the user's 00567 * responsibility. 00568 */ 00569 _GLIBCXX_ABI_TAG_CXX11 00570 iterator 00571 erase(const_iterator __position) 00572 { return _M_t.erase(__position); } 00573 #else 00574 /** 00575 * @brief Erases an element from a %set. 00576 * @param position An iterator pointing to the element to be erased. 00577 * 00578 * This function erases an element, pointed to by the given iterator, 00579 * from a %set. Note that this function only erases the element, and 00580 * that if the element is itself a pointer, the pointed-to memory is not 00581 * touched in any way. Managing the pointer is the user's 00582 * responsibility. 00583 */ 00584 void 00585 erase(iterator __position) 00586 { _M_t.erase(__position); } 00587 #endif 00588 00589 /** 00590 * @brief Erases elements according to the provided key. 00591 * @param __x Key of element to be erased. 00592 * @return The number of elements erased. 00593 * 00594 * This function erases all the elements located by the given key from 00595 * a %set. 00596 * Note that this function only erases the element, and that if 00597 * the element is itself a pointer, the pointed-to memory is not touched 00598 * in any way. Managing the pointer is the user's responsibility. 00599 */ 00600 size_type 00601 erase(const key_type& __x) 00602 { return _M_t.erase(__x); } 00603 00604 #if __cplusplus >= 201103L 00605 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00606 // DR 130. Associative erase should return an iterator. 00607 /** 00608 * @brief Erases a [__first,__last) range of elements from a %set. 00609 * @param __first Iterator pointing to the start of the range to be 00610 * erased. 00611 00612 * @param __last Iterator pointing to the end of the range to 00613 * be erased. 00614 * @return The iterator @a __last. 00615 * 00616 * This function erases a sequence of elements from a %set. 00617 * Note that this function only erases the element, and that if 00618 * the element is itself a pointer, the pointed-to memory is not touched 00619 * in any way. Managing the pointer is the user's responsibility. 00620 */ 00621 _GLIBCXX_ABI_TAG_CXX11 00622 iterator 00623 erase(const_iterator __first, const_iterator __last) 00624 { return _M_t.erase(__first, __last); } 00625 #else 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 * @param __last Iterator pointing to the end of the range to 00631 * be erased. 00632 * 00633 * This function erases a sequence of elements from a %set. 00634 * Note that this function only erases the element, and that if 00635 * the element is itself a pointer, the pointed-to memory is not touched 00636 * in any way. Managing the pointer is the user's responsibility. 00637 */ 00638 void 00639 erase(iterator __first, iterator __last) 00640 { _M_t.erase(__first, __last); } 00641 #endif 00642 00643 /** 00644 * Erases all elements in a %set. Note that this function only erases 00645 * the elements, and that if the elements themselves are pointers, the 00646 * pointed-to memory is not touched in any way. Managing the pointer is 00647 * the user's responsibility. 00648 */ 00649 void 00650 clear() _GLIBCXX_NOEXCEPT 00651 { _M_t.clear(); } 00652 00653 // set operations: 00654 00655 //@{ 00656 /** 00657 * @brief Finds the number of elements. 00658 * @param __x Element to located. 00659 * @return Number of elements with specified key. 00660 * 00661 * This function only makes sense for multisets; for set the result will 00662 * either be 0 (not present) or 1 (present). 00663 */ 00664 size_type 00665 count(const key_type& __x) const 00666 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00667 00668 #if __cplusplus > 201103L 00669 template<typename _Kt> 00670 auto 00671 count(const _Kt& __x) const 00672 -> decltype(_M_t._M_count_tr(__x)) 00673 { return _M_t._M_count_tr(__x); } 00674 #endif 00675 //@} 00676 00677 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00678 // 214. set::find() missing const overload 00679 //@{ 00680 /** 00681 * @brief Tries to locate an element in a %set. 00682 * @param __x Element to be located. 00683 * @return Iterator pointing to sought-after element, or end() if not 00684 * found. 00685 * 00686 * This function takes a key and tries to locate the element with which 00687 * the key matches. If successful the function returns an iterator 00688 * pointing to the sought after element. If unsuccessful it returns the 00689 * past-the-end ( @c end() ) iterator. 00690 */ 00691 iterator 00692 find(const key_type& __x) 00693 { return _M_t.find(__x); } 00694 00695 const_iterator 00696 find(const key_type& __x) const 00697 { return _M_t.find(__x); } 00698 00699 #if __cplusplus > 201103L 00700 template<typename _Kt> 00701 auto 00702 find(const _Kt& __x) 00703 -> decltype(iterator{_M_t._M_find_tr(__x)}) 00704 { return iterator{_M_t._M_find_tr(__x)}; } 00705 00706 template<typename _Kt> 00707 auto 00708 find(const _Kt& __x) const 00709 -> decltype(const_iterator{_M_t._M_find_tr(__x)}) 00710 { return const_iterator{_M_t._M_find_tr(__x)}; } 00711 #endif 00712 //@} 00713 00714 //@{ 00715 /** 00716 * @brief Finds the beginning of a subsequence matching given key. 00717 * @param __x Key to be located. 00718 * @return Iterator pointing to first element equal to or greater 00719 * than key, or end(). 00720 * 00721 * This function returns the first element of a subsequence of elements 00722 * that matches the given key. If unsuccessful it returns an iterator 00723 * pointing to the first element that has a greater value than given key 00724 * or end() if no such element exists. 00725 */ 00726 iterator 00727 lower_bound(const key_type& __x) 00728 { return _M_t.lower_bound(__x); } 00729 00730 const_iterator 00731 lower_bound(const key_type& __x) const 00732 { return _M_t.lower_bound(__x); } 00733 00734 #if __cplusplus > 201103L 00735 template<typename _Kt> 00736 auto 00737 lower_bound(const _Kt& __x) 00738 -> decltype(iterator(_M_t._M_lower_bound_tr(__x))) 00739 { return iterator(_M_t._M_lower_bound_tr(__x)); } 00740 00741 template<typename _Kt> 00742 auto 00743 lower_bound(const _Kt& __x) const 00744 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x))) 00745 { return const_iterator(_M_t._M_lower_bound_tr(__x)); } 00746 #endif 00747 //@} 00748 00749 //@{ 00750 /** 00751 * @brief Finds the end of a subsequence matching given key. 00752 * @param __x Key to be located. 00753 * @return Iterator pointing to the first element 00754 * greater than key, or end(). 00755 */ 00756 iterator 00757 upper_bound(const key_type& __x) 00758 { return _M_t.upper_bound(__x); } 00759 00760 const_iterator 00761 upper_bound(const key_type& __x) const 00762 { return _M_t.upper_bound(__x); } 00763 00764 #if __cplusplus > 201103L 00765 template<typename _Kt> 00766 auto 00767 upper_bound(const _Kt& __x) 00768 -> decltype(iterator(_M_t._M_upper_bound_tr(__x))) 00769 { return iterator(_M_t._M_upper_bound_tr(__x)); } 00770 00771 template<typename _Kt> 00772 auto 00773 upper_bound(const _Kt& __x) const 00774 -> decltype(iterator(_M_t._M_upper_bound_tr(__x))) 00775 { return const_iterator(_M_t._M_upper_bound_tr(__x)); } 00776 #endif 00777 //@} 00778 00779 //@{ 00780 /** 00781 * @brief Finds a subsequence matching given key. 00782 * @param __x Key to be located. 00783 * @return Pair of iterators that possibly points to the subsequence 00784 * matching given key. 00785 * 00786 * This function is equivalent to 00787 * @code 00788 * std::make_pair(c.lower_bound(val), 00789 * c.upper_bound(val)) 00790 * @endcode 00791 * (but is faster than making the calls separately). 00792 * 00793 * This function probably only makes sense for multisets. 00794 */ 00795 std::pair<iterator, iterator> 00796 equal_range(const key_type& __x) 00797 { return _M_t.equal_range(__x); } 00798 00799 std::pair<const_iterator, const_iterator> 00800 equal_range(const key_type& __x) const 00801 { return _M_t.equal_range(__x); } 00802 00803 #if __cplusplus > 201103L 00804 template<typename _Kt> 00805 auto 00806 equal_range(const _Kt& __x) 00807 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x))) 00808 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); } 00809 00810 template<typename _Kt> 00811 auto 00812 equal_range(const _Kt& __x) const 00813 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x))) 00814 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); } 00815 #endif 00816 //@} 00817 00818 template<typename _K1, typename _C1, typename _A1> 00819 friend bool 00820 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00821 00822 template<typename _K1, typename _C1, typename _A1> 00823 friend bool 00824 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00825 }; 00826 00827 00828 /** 00829 * @brief Set equality comparison. 00830 * @param __x A %set. 00831 * @param __y A %set of the same type as @a x. 00832 * @return True iff the size and elements of the sets are equal. 00833 * 00834 * This is an equivalence relation. It is linear in the size of the sets. 00835 * Sets are considered equivalent if their sizes are equal, and if 00836 * corresponding elements compare equal. 00837 */ 00838 template<typename _Key, typename _Compare, typename _Alloc> 00839 inline bool 00840 operator==(const set<_Key, _Compare, _Alloc>& __x, 00841 const set<_Key, _Compare, _Alloc>& __y) 00842 { return __x._M_t == __y._M_t; } 00843 00844 /** 00845 * @brief Set ordering relation. 00846 * @param __x A %set. 00847 * @param __y A %set of the same type as @a x. 00848 * @return True iff @a __x is lexicographically less than @a __y. 00849 * 00850 * This is a total ordering relation. It is linear in the size of the 00851 * sets. The elements must be comparable with @c <. 00852 * 00853 * See std::lexicographical_compare() for how the determination is made. 00854 */ 00855 template<typename _Key, typename _Compare, typename _Alloc> 00856 inline bool 00857 operator<(const set<_Key, _Compare, _Alloc>& __x, 00858 const set<_Key, _Compare, _Alloc>& __y) 00859 { return __x._M_t < __y._M_t; } 00860 00861 /// Returns !(x == y). 00862 template<typename _Key, typename _Compare, typename _Alloc> 00863 inline bool 00864 operator!=(const set<_Key, _Compare, _Alloc>& __x, 00865 const set<_Key, _Compare, _Alloc>& __y) 00866 { return !(__x == __y); } 00867 00868 /// Returns y < x. 00869 template<typename _Key, typename _Compare, typename _Alloc> 00870 inline bool 00871 operator>(const set<_Key, _Compare, _Alloc>& __x, 00872 const set<_Key, _Compare, _Alloc>& __y) 00873 { return __y < __x; } 00874 00875 /// Returns !(y < x) 00876 template<typename _Key, typename _Compare, typename _Alloc> 00877 inline bool 00878 operator<=(const set<_Key, _Compare, _Alloc>& __x, 00879 const set<_Key, _Compare, _Alloc>& __y) 00880 { return !(__y < __x); } 00881 00882 /// Returns !(x < y) 00883 template<typename _Key, typename _Compare, typename _Alloc> 00884 inline bool 00885 operator>=(const set<_Key, _Compare, _Alloc>& __x, 00886 const set<_Key, _Compare, _Alloc>& __y) 00887 { return !(__x < __y); } 00888 00889 /// See std::set::swap(). 00890 template<typename _Key, typename _Compare, typename _Alloc> 00891 inline void 00892 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) 00893 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) 00894 { __x.swap(__y); } 00895 00896 _GLIBCXX_END_NAMESPACE_CONTAINER 00897 } //namespace std 00898 #endif /* _STL_SET_H */