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