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 #endif 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 #if __cplusplus >= 201103L 00414 noexcept(_Alloc_traits::_S_nothrow_swap()) 00415 #endif 00416 { _M_t.swap(__x._M_t); } 00417 00418 // insert/erase 00419 #if __cplusplus >= 201103L 00420 /** 00421 * @brief Attempts to build and insert an element into the %set. 00422 * @param __args Arguments used to generate an element. 00423 * @return A pair, of which the first element is an iterator that points 00424 * to the possibly inserted element, and the second is a bool 00425 * that is true if the element was actually inserted. 00426 * 00427 * This function attempts to build and insert an element into the %set. 00428 * A %set relies on unique keys and thus an element is only inserted if 00429 * it is not already present in the %set. 00430 * 00431 * Insertion requires logarithmic time. 00432 */ 00433 template<typename... _Args> 00434 std::pair<iterator, bool> 00435 emplace(_Args&&... __args) 00436 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } 00437 00438 /** 00439 * @brief Attempts to insert an element into the %set. 00440 * @param __pos An iterator that serves as a hint as to where the 00441 * element should be inserted. 00442 * @param __args Arguments used to generate the element to be 00443 * inserted. 00444 * @return An iterator that points to the element with key equivalent to 00445 * the one generated from @a __args (may or may not be the 00446 * element itself). 00447 * 00448 * This function is not concerned about whether the insertion took place, 00449 * and thus does not return a boolean like the single-argument emplace() 00450 * does. Note that the first parameter is only a hint and can 00451 * potentially improve the performance of the insertion process. A bad 00452 * hint would cause no gains in efficiency. 00453 * 00454 * For more on @a hinting, see: 00455 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00456 * 00457 * Insertion requires logarithmic time (if the hint is not taken). 00458 */ 00459 template<typename... _Args> 00460 iterator 00461 emplace_hint(const_iterator __pos, _Args&&... __args) 00462 { 00463 return _M_t._M_emplace_hint_unique(__pos, 00464 std::forward<_Args>(__args)...); 00465 } 00466 #endif 00467 00468 /** 00469 * @brief Attempts to insert an element into the %set. 00470 * @param __x Element to be inserted. 00471 * @return A pair, of which the first element is an iterator that points 00472 * to the possibly inserted element, and the second is a bool 00473 * that is true if the element was actually inserted. 00474 * 00475 * This function attempts to insert an element into the %set. A %set 00476 * relies on unique keys and thus an element is only inserted if it is 00477 * not already present in the %set. 00478 * 00479 * Insertion requires logarithmic time. 00480 */ 00481 std::pair<iterator, bool> 00482 insert(const value_type& __x) 00483 { 00484 std::pair<typename _Rep_type::iterator, bool> __p = 00485 _M_t._M_insert_unique(__x); 00486 return std::pair<iterator, bool>(__p.first, __p.second); 00487 } 00488 00489 #if __cplusplus >= 201103L 00490 std::pair<iterator, bool> 00491 insert(value_type&& __x) 00492 { 00493 std::pair<typename _Rep_type::iterator, bool> __p = 00494 _M_t._M_insert_unique(std::move(__x)); 00495 return std::pair<iterator, bool>(__p.first, __p.second); 00496 } 00497 #endif 00498 00499 /** 00500 * @brief Attempts to insert an element into the %set. 00501 * @param __position An iterator that serves as a hint as to where the 00502 * element should be inserted. 00503 * @param __x Element to be inserted. 00504 * @return An iterator that points to the element with key of 00505 * @a __x (may or may not be the element passed in). 00506 * 00507 * This function is not concerned about whether the insertion took place, 00508 * and thus does not return a boolean like the single-argument insert() 00509 * does. Note that the first parameter is only a hint and can 00510 * potentially improve the performance of the insertion process. A bad 00511 * hint would cause no gains in efficiency. 00512 * 00513 * For more on @a hinting, see: 00514 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00515 * 00516 * Insertion requires logarithmic time (if the hint is not taken). 00517 */ 00518 iterator 00519 insert(const_iterator __position, const value_type& __x) 00520 { return _M_t._M_insert_unique_(__position, __x); } 00521 00522 #if __cplusplus >= 201103L 00523 iterator 00524 insert(const_iterator __position, value_type&& __x) 00525 { return _M_t._M_insert_unique_(__position, std::move(__x)); } 00526 #endif 00527 00528 /** 00529 * @brief A template function that attempts to insert a range 00530 * of elements. 00531 * @param __first Iterator pointing to the start of the range to be 00532 * inserted. 00533 * @param __last Iterator pointing to the end of the range. 00534 * 00535 * Complexity similar to that of the range constructor. 00536 */ 00537 template<typename _InputIterator> 00538 void 00539 insert(_InputIterator __first, _InputIterator __last) 00540 { _M_t._M_insert_unique(__first, __last); } 00541 00542 #if __cplusplus >= 201103L 00543 /** 00544 * @brief Attempts to insert a list of elements into the %set. 00545 * @param __l A std::initializer_list<value_type> of elements 00546 * to be inserted. 00547 * 00548 * Complexity similar to that of the range constructor. 00549 */ 00550 void 00551 insert(initializer_list<value_type> __l) 00552 { this->insert(__l.begin(), __l.end()); } 00553 #endif 00554 00555 #if __cplusplus >= 201103L 00556 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00557 // DR 130. Associative erase should return an iterator. 00558 /** 00559 * @brief Erases an element from a %set. 00560 * @param __position An iterator pointing to the element to be erased. 00561 * @return An iterator pointing to the element immediately following 00562 * @a __position prior to the element being erased. If no such 00563 * element exists, end() is returned. 00564 * 00565 * This function erases an element, pointed to by the given iterator, 00566 * from a %set. Note that this function only erases the element, and 00567 * that if the element is itself a pointer, the pointed-to memory is not 00568 * touched in any way. Managing the pointer is the user's 00569 * responsibility. 00570 */ 00571 _GLIBCXX_ABI_TAG_CXX11 00572 iterator 00573 erase(const_iterator __position) 00574 { return _M_t.erase(__position); } 00575 #else 00576 /** 00577 * @brief Erases an element from a %set. 00578 * @param position An iterator pointing to the element to be erased. 00579 * 00580 * This function erases an element, pointed to by the given iterator, 00581 * from a %set. Note that this function only erases the element, and 00582 * that if the element is itself a pointer, the pointed-to memory is not 00583 * touched in any way. Managing the pointer is the user's 00584 * responsibility. 00585 */ 00586 void 00587 erase(iterator __position) 00588 { _M_t.erase(__position); } 00589 #endif 00590 00591 /** 00592 * @brief Erases elements according to the provided key. 00593 * @param __x Key of element to be erased. 00594 * @return The number of elements erased. 00595 * 00596 * This function erases all the elements located by the given key from 00597 * a %set. 00598 * Note that this function only erases the element, and that if 00599 * the element is itself a pointer, the pointed-to memory is not touched 00600 * in any way. Managing the pointer is the user's responsibility. 00601 */ 00602 size_type 00603 erase(const key_type& __x) 00604 { return _M_t.erase(__x); } 00605 00606 #if __cplusplus >= 201103L 00607 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00608 // DR 130. Associative erase should return an iterator. 00609 /** 00610 * @brief Erases a [__first,__last) range of elements from a %set. 00611 * @param __first Iterator pointing to the start of the range to be 00612 * erased. 00613 00614 * @param __last Iterator pointing to the end of the range to 00615 * be erased. 00616 * @return The iterator @a __last. 00617 * 00618 * This function erases a sequence of elements from a %set. 00619 * Note that this function only erases the element, and that if 00620 * the element is itself a pointer, the pointed-to memory is not touched 00621 * in any way. Managing the pointer is the user's responsibility. 00622 */ 00623 _GLIBCXX_ABI_TAG_CXX11 00624 iterator 00625 erase(const_iterator __first, const_iterator __last) 00626 { return _M_t.erase(__first, __last); } 00627 #else 00628 /** 00629 * @brief Erases a [first,last) range of elements from a %set. 00630 * @param __first Iterator pointing to the start of the range to be 00631 * erased. 00632 * @param __last Iterator pointing to the end of the range to 00633 * be erased. 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 void 00641 erase(iterator __first, iterator __last) 00642 { _M_t.erase(__first, __last); } 00643 #endif 00644 00645 /** 00646 * Erases all elements in a %set. Note that this function only erases 00647 * the elements, and that if the elements themselves are pointers, the 00648 * pointed-to memory is not touched in any way. Managing the pointer is 00649 * the user's responsibility. 00650 */ 00651 void 00652 clear() _GLIBCXX_NOEXCEPT 00653 { _M_t.clear(); } 00654 00655 // set operations: 00656 00657 //@{ 00658 /** 00659 * @brief Finds the number of elements. 00660 * @param __x Element to located. 00661 * @return Number of elements with specified key. 00662 * 00663 * This function only makes sense for multisets; for set the result will 00664 * either be 0 (not present) or 1 (present). 00665 */ 00666 size_type 00667 count(const key_type& __x) const 00668 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00669 00670 #if __cplusplus > 201103L 00671 template<typename _Kt> 00672 auto 00673 count(const _Kt& __x) const 00674 -> decltype(_M_t._M_count_tr(__x)) 00675 { return _M_t._M_find_tr(__x) == _M_t.end() ? 0 : 1; } 00676 #endif 00677 //@} 00678 00679 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00680 // 214. set::find() missing const overload 00681 //@{ 00682 /** 00683 * @brief Tries to locate an element in a %set. 00684 * @param __x Element to be located. 00685 * @return Iterator pointing to sought-after element, or end() if not 00686 * found. 00687 * 00688 * This function takes a key and tries to locate the element with which 00689 * the key matches. If successful the function returns an iterator 00690 * pointing to the sought after element. If unsuccessful it returns the 00691 * past-the-end ( @c end() ) iterator. 00692 */ 00693 iterator 00694 find(const key_type& __x) 00695 { return _M_t.find(__x); } 00696 00697 const_iterator 00698 find(const key_type& __x) const 00699 { return _M_t.find(__x); } 00700 00701 #if __cplusplus > 201103L 00702 template<typename _Kt> 00703 auto 00704 find(const _Kt& __x) 00705 -> decltype(iterator{_M_t._M_find_tr(__x)}) 00706 { return iterator{_M_t._M_find_tr(__x)}; } 00707 00708 template<typename _Kt> 00709 auto 00710 find(const _Kt& __x) const 00711 -> decltype(const_iterator{_M_t._M_find_tr(__x)}) 00712 { return const_iterator{_M_t._M_find_tr(__x)}; } 00713 #endif 00714 //@} 00715 00716 //@{ 00717 /** 00718 * @brief Finds the beginning of a subsequence matching given key. 00719 * @param __x Key to be located. 00720 * @return Iterator pointing to first element equal to or greater 00721 * than key, or end(). 00722 * 00723 * This function returns the first element of a subsequence of elements 00724 * that matches the given key. If unsuccessful it returns an iterator 00725 * pointing to the first element that has a greater value than given key 00726 * or end() if no such element exists. 00727 */ 00728 iterator 00729 lower_bound(const key_type& __x) 00730 { return _M_t.lower_bound(__x); } 00731 00732 const_iterator 00733 lower_bound(const key_type& __x) const 00734 { return _M_t.lower_bound(__x); } 00735 00736 #if __cplusplus > 201103L 00737 template<typename _Kt> 00738 auto 00739 lower_bound(const _Kt& __x) 00740 -> decltype(_M_t._M_lower_bound_tr(__x)) 00741 { return _M_t._M_lower_bound_tr(__x); } 00742 00743 template<typename _Kt> 00744 auto 00745 lower_bound(const _Kt& __x) const 00746 -> decltype(_M_t._M_lower_bound_tr(__x)) 00747 { return _M_t._M_lower_bound_tr(__x); } 00748 #endif 00749 //@} 00750 00751 //@{ 00752 /** 00753 * @brief Finds the end of a subsequence matching given key. 00754 * @param __x Key to be located. 00755 * @return Iterator pointing to the first element 00756 * greater than key, or end(). 00757 */ 00758 iterator 00759 upper_bound(const key_type& __x) 00760 { return _M_t.upper_bound(__x); } 00761 00762 const_iterator 00763 upper_bound(const key_type& __x) const 00764 { return _M_t.upper_bound(__x); } 00765 00766 #if __cplusplus > 201103L 00767 template<typename _Kt> 00768 auto 00769 upper_bound(const _Kt& __x) 00770 -> decltype(_M_t._M_upper_bound_tr(__x)) 00771 { return _M_t._M_upper_bound_tr(__x); } 00772 00773 template<typename _Kt> 00774 auto 00775 upper_bound(const _Kt& __x) const 00776 -> decltype(_M_t._M_upper_bound_tr(__x)) 00777 { return _M_t._M_upper_bound_tr(__x); } 00778 #endif 00779 //@} 00780 00781 //@{ 00782 /** 00783 * @brief Finds a subsequence matching given key. 00784 * @param __x Key to be located. 00785 * @return Pair of iterators that possibly points to the subsequence 00786 * matching given key. 00787 * 00788 * This function is equivalent to 00789 * @code 00790 * std::make_pair(c.lower_bound(val), 00791 * c.upper_bound(val)) 00792 * @endcode 00793 * (but is faster than making the calls separately). 00794 * 00795 * This function probably only makes sense for multisets. 00796 */ 00797 std::pair<iterator, iterator> 00798 equal_range(const key_type& __x) 00799 { return _M_t.equal_range(__x); } 00800 00801 std::pair<const_iterator, const_iterator> 00802 equal_range(const key_type& __x) const 00803 { return _M_t.equal_range(__x); } 00804 00805 #if __cplusplus > 201103L 00806 template<typename _Kt> 00807 auto 00808 equal_range(const _Kt& __x) 00809 -> decltype(_M_t._M_equal_range_tr(__x)) 00810 { return _M_t._M_equal_range_tr(__x); } 00811 00812 template<typename _Kt> 00813 auto 00814 equal_range(const _Kt& __x) const 00815 -> decltype(_M_t._M_equal_range_tr(__x)) 00816 { return _M_t._M_equal_range_tr(__x); } 00817 #endif 00818 //@} 00819 00820 template<typename _K1, typename _C1, typename _A1> 00821 friend bool 00822 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00823 00824 template<typename _K1, typename _C1, typename _A1> 00825 friend bool 00826 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00827 }; 00828 00829 00830 /** 00831 * @brief Set equality comparison. 00832 * @param __x A %set. 00833 * @param __y A %set of the same type as @a x. 00834 * @return True iff the size and elements of the sets are equal. 00835 * 00836 * This is an equivalence relation. It is linear in the size of the sets. 00837 * Sets are considered equivalent if their sizes are equal, and if 00838 * corresponding elements compare equal. 00839 */ 00840 template<typename _Key, typename _Compare, typename _Alloc> 00841 inline bool 00842 operator==(const set<_Key, _Compare, _Alloc>& __x, 00843 const set<_Key, _Compare, _Alloc>& __y) 00844 { return __x._M_t == __y._M_t; } 00845 00846 /** 00847 * @brief Set ordering relation. 00848 * @param __x A %set. 00849 * @param __y A %set of the same type as @a x. 00850 * @return True iff @a __x is lexicographically less than @a __y. 00851 * 00852 * This is a total ordering relation. It is linear in the size of the 00853 * sets. The elements must be comparable with @c <. 00854 * 00855 * See std::lexicographical_compare() for how the determination is made. 00856 */ 00857 template<typename _Key, typename _Compare, typename _Alloc> 00858 inline bool 00859 operator<(const set<_Key, _Compare, _Alloc>& __x, 00860 const set<_Key, _Compare, _Alloc>& __y) 00861 { return __x._M_t < __y._M_t; } 00862 00863 /// Returns !(x == y). 00864 template<typename _Key, typename _Compare, typename _Alloc> 00865 inline bool 00866 operator!=(const set<_Key, _Compare, _Alloc>& __x, 00867 const set<_Key, _Compare, _Alloc>& __y) 00868 { return !(__x == __y); } 00869 00870 /// Returns y < x. 00871 template<typename _Key, typename _Compare, typename _Alloc> 00872 inline bool 00873 operator>(const set<_Key, _Compare, _Alloc>& __x, 00874 const set<_Key, _Compare, _Alloc>& __y) 00875 { return __y < __x; } 00876 00877 /// Returns !(y < x) 00878 template<typename _Key, typename _Compare, typename _Alloc> 00879 inline bool 00880 operator<=(const set<_Key, _Compare, _Alloc>& __x, 00881 const set<_Key, _Compare, _Alloc>& __y) 00882 { return !(__y < __x); } 00883 00884 /// Returns !(x < y) 00885 template<typename _Key, typename _Compare, typename _Alloc> 00886 inline bool 00887 operator>=(const set<_Key, _Compare, _Alloc>& __x, 00888 const set<_Key, _Compare, _Alloc>& __y) 00889 { return !(__x < __y); } 00890 00891 /// See std::set::swap(). 00892 template<typename _Key, typename _Compare, typename _Alloc> 00893 inline void 00894 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) 00895 { __x.swap(__y); } 00896 00897 _GLIBCXX_END_NAMESPACE_CONTAINER 00898 } //namespace std 00899 #endif /* _STL_SET_H */