00001 // Set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 /* 00032 * 00033 * Copyright (c) 1994 00034 * Hewlett-Packard Company 00035 * 00036 * Permission to use, copy, modify, distribute and sell this software 00037 * and its documentation for any purpose is hereby granted without fee, 00038 * provided that the above copyright notice appear in all copies and 00039 * that both that copyright notice and this permission notice appear 00040 * in supporting documentation. Hewlett-Packard Company makes no 00041 * representations about the suitability of this software for any 00042 * purpose. It is provided "as is" without express or implied warranty. 00043 * 00044 * 00045 * Copyright (c) 1996,1997 00046 * Silicon Graphics Computer Systems, Inc. 00047 * 00048 * Permission to use, copy, modify, distribute and sell this software 00049 * and its documentation for any purpose is hereby granted without fee, 00050 * provided that the above copyright notice appear in all copies and 00051 * that both that copyright notice and this permission notice appear 00052 * in supporting documentation. Silicon Graphics makes no 00053 * representations about the suitability of this software for any 00054 * purpose. It is provided "as is" without express or implied warranty. 00055 */ 00056 00057 /** @file stl_set.h 00058 * This is an internal header file, included by other library headers. 00059 * You should not attempt to use it directly. 00060 */ 00061 00062 #ifndef _STL_SET_H 00063 #define _STL_SET_H 1 00064 00065 #include <bits/concept_check.h> 00066 00067 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D) 00068 00069 /** 00070 * @brief A standard container made up of unique keys, which can be 00071 * retrieved in logarithmic time. 00072 * 00073 * @ingroup Containers 00074 * @ingroup Assoc_containers 00075 * 00076 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00077 * <a href="tables.html#66">reversible container</a>, and an 00078 * <a href="tables.html#69">associative container</a> (using unique keys). 00079 * 00080 * Sets support bidirectional iterators. 00081 * 00082 * @param Key Type of key objects. 00083 * @param Compare Comparison function object type, defaults to less<Key>. 00084 * @param Alloc Allocator type, defaults to allocator<Key>. 00085 * 00086 * The private tree data is declared exactly the same way for set and 00087 * multiset; the distinction is made entirely in how the tree functions are 00088 * called (*_unique versus *_equal, same as the standard). 00089 */ 00090 template<typename _Key, typename _Compare = std::less<_Key>, 00091 typename _Alloc = std::allocator<_Key> > 00092 class set 00093 { 00094 // concept requirements 00095 typedef typename _Alloc::value_type _Alloc_value_type; 00096 __glibcxx_class_requires(_Key, _SGIAssignableConcept) 00097 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00098 _BinaryFunctionConcept) 00099 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept) 00100 00101 public: 00102 // typedefs: 00103 //@{ 00104 /// Public typedefs. 00105 typedef _Key key_type; 00106 typedef _Key value_type; 00107 typedef _Compare key_compare; 00108 typedef _Compare value_compare; 00109 typedef _Alloc allocator_type; 00110 //@} 00111 00112 private: 00113 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type; 00114 00115 typedef _Rb_tree<key_type, value_type, _Identity<value_type>, 00116 key_compare, _Key_alloc_type> _Rep_type; 00117 _Rep_type _M_t; // Red-black tree representing set. 00118 00119 public: 00120 //@{ 00121 /// Iterator-related typedefs. 00122 typedef typename _Key_alloc_type::pointer pointer; 00123 typedef typename _Key_alloc_type::const_pointer const_pointer; 00124 typedef typename _Key_alloc_type::reference reference; 00125 typedef typename _Key_alloc_type::const_reference const_reference; 00126 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00127 // DR 103. set::iterator is required to be modifiable, 00128 // but this allows modification of keys. 00129 typedef typename _Rep_type::const_iterator iterator; 00130 typedef typename _Rep_type::const_iterator const_iterator; 00131 typedef typename _Rep_type::const_reverse_iterator reverse_iterator; 00132 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00133 typedef typename _Rep_type::size_type size_type; 00134 typedef typename _Rep_type::difference_type difference_type; 00135 //@} 00136 00137 // allocation/deallocation 00138 /** 00139 * @brief Default constructor creates no elements. 00140 */ 00141 set() 00142 : _M_t() { } 00143 00144 /** 00145 * @brief Creates a %set with no elements. 00146 * @param comp Comparator to use. 00147 * @param a An allocator object. 00148 */ 00149 explicit 00150 set(const _Compare& __comp, 00151 const allocator_type& __a = allocator_type()) 00152 : _M_t(__comp, __a) { } 00153 00154 /** 00155 * @brief Builds a %set from a range. 00156 * @param first An input iterator. 00157 * @param last An input iterator. 00158 * 00159 * Create a %set consisting of copies of the elements from [first,last). 00160 * This is linear in N if the range is already sorted, and NlogN 00161 * otherwise (where N is distance(first,last)). 00162 */ 00163 template<typename _InputIterator> 00164 set(_InputIterator __first, _InputIterator __last) 00165 : _M_t() 00166 { _M_t._M_insert_unique(__first, __last); } 00167 00168 /** 00169 * @brief Builds a %set from a range. 00170 * @param first An input iterator. 00171 * @param last An input iterator. 00172 * @param comp A comparison functor. 00173 * @param a An allocator object. 00174 * 00175 * Create a %set consisting of copies of the elements from [first,last). 00176 * This is linear in N if the range is already sorted, and NlogN 00177 * otherwise (where N is distance(first,last)). 00178 */ 00179 template<typename _InputIterator> 00180 set(_InputIterator __first, _InputIterator __last, 00181 const _Compare& __comp, 00182 const allocator_type& __a = allocator_type()) 00183 : _M_t(__comp, __a) 00184 { _M_t._M_insert_unique(__first, __last); } 00185 00186 /** 00187 * @brief %Set copy constructor. 00188 * @param x A %set of identical element and allocator types. 00189 * 00190 * The newly-created %set uses a copy of the allocation object used 00191 * by @a x. 00192 */ 00193 set(const set& __x) 00194 : _M_t(__x._M_t) { } 00195 00196 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00197 /** 00198 * @brief %Set move constructor 00199 * @param x A %set of identical element and allocator types. 00200 * 00201 * The newly-created %set contains the exact contents of @a x. 00202 * The contents of @a x are a valid, but unspecified %set. 00203 */ 00204 set(set&& __x) 00205 : _M_t(std::forward<_Rep_type>(__x._M_t)) { } 00206 #endif 00207 00208 /** 00209 * @brief %Set assignment operator. 00210 * @param x A %set of identical element and allocator types. 00211 * 00212 * All the elements of @a x are copied, but unlike the copy constructor, 00213 * the allocator object is not copied. 00214 */ 00215 set& 00216 operator=(const set& __x) 00217 { 00218 _M_t = __x._M_t; 00219 return *this; 00220 } 00221 00222 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00223 /** 00224 * @brief %Set move assignment operator. 00225 * @param x A %set of identical element and allocator types. 00226 * 00227 * The contents of @a x are moved into this %set (without copying). 00228 * @a x is a valid, but unspecified %set. 00229 */ 00230 set& 00231 operator=(set&& __x) 00232 { 00233 // NB: DR 675. 00234 this->clear(); 00235 this->swap(__x); 00236 return *this; 00237 } 00238 #endif 00239 00240 // accessors: 00241 00242 /// Returns the comparison object with which the %set was constructed. 00243 key_compare 00244 key_comp() const 00245 { return _M_t.key_comp(); } 00246 /// Returns the comparison object with which the %set was constructed. 00247 value_compare 00248 value_comp() const 00249 { return _M_t.key_comp(); } 00250 /// Returns the allocator object with which the %set was constructed. 00251 allocator_type 00252 get_allocator() const 00253 { return _M_t.get_allocator(); } 00254 00255 /** 00256 * Returns a read-only (constant) iterator that points to the first 00257 * element in the %set. Iteration is done in ascending order according 00258 * to the keys. 00259 */ 00260 iterator 00261 begin() const 00262 { return _M_t.begin(); } 00263 00264 /** 00265 * Returns a read-only (constant) iterator that points one past the last 00266 * element in the %set. Iteration is done in ascending order according 00267 * to the keys. 00268 */ 00269 iterator 00270 end() const 00271 { return _M_t.end(); } 00272 00273 /** 00274 * Returns a read-only (constant) iterator that points to the last 00275 * element in the %set. Iteration is done in descending order according 00276 * to the keys. 00277 */ 00278 reverse_iterator 00279 rbegin() const 00280 { return _M_t.rbegin(); } 00281 00282 /** 00283 * Returns a read-only (constant) reverse iterator that points to the 00284 * last pair in the %set. Iteration is done in descending order 00285 * according to the keys. 00286 */ 00287 reverse_iterator 00288 rend() const 00289 { return _M_t.rend(); } 00290 00291 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00292 /** 00293 * Returns a read-only (constant) iterator that points to the first 00294 * element in the %set. Iteration is done in ascending order according 00295 * to the keys. 00296 */ 00297 iterator 00298 cbegin() const 00299 { return _M_t.begin(); } 00300 00301 /** 00302 * Returns a read-only (constant) iterator that points one past the last 00303 * element in the %set. Iteration is done in ascending order according 00304 * to the keys. 00305 */ 00306 iterator 00307 cend() const 00308 { return _M_t.end(); } 00309 00310 /** 00311 * Returns a read-only (constant) iterator that points to the last 00312 * element in the %set. Iteration is done in descending order according 00313 * to the keys. 00314 */ 00315 reverse_iterator 00316 crbegin() const 00317 { return _M_t.rbegin(); } 00318 00319 /** 00320 * Returns a read-only (constant) reverse iterator that points to the 00321 * last pair in the %set. Iteration is done in descending order 00322 * according to the keys. 00323 */ 00324 reverse_iterator 00325 crend() const 00326 { return _M_t.rend(); } 00327 #endif 00328 00329 /// Returns true if the %set is empty. 00330 bool 00331 empty() const 00332 { return _M_t.empty(); } 00333 00334 /// Returns the size of the %set. 00335 size_type 00336 size() const 00337 { return _M_t.size(); } 00338 00339 /// Returns the maximum size of the %set. 00340 size_type 00341 max_size() const 00342 { return _M_t.max_size(); } 00343 00344 /** 00345 * @brief Swaps data with another %set. 00346 * @param x A %set of the same element and allocator types. 00347 * 00348 * This exchanges the elements between two sets in constant time. 00349 * (It is only swapping a pointer, an integer, and an instance of 00350 * the @c Compare type (which itself is often stateless and empty), so it 00351 * should be quite fast.) 00352 * Note that the global std::swap() function is specialized such that 00353 * std::swap(s1,s2) will feed to this function. 00354 */ 00355 void 00356 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00357 swap(set&& __x) 00358 #else 00359 swap(set& __x) 00360 #endif 00361 { _M_t.swap(__x._M_t); } 00362 00363 // insert/erase 00364 /** 00365 * @brief Attempts to insert an element into the %set. 00366 * @param x Element to be inserted. 00367 * @return A pair, of which the first element is an iterator that points 00368 * to the possibly inserted element, and the second is a bool 00369 * that is true if the element was actually inserted. 00370 * 00371 * This function attempts to insert an element into the %set. A %set 00372 * relies on unique keys and thus an element is only inserted if it is 00373 * not already present in the %set. 00374 * 00375 * Insertion requires logarithmic time. 00376 */ 00377 std::pair<iterator, bool> 00378 insert(const value_type& __x) 00379 { 00380 std::pair<typename _Rep_type::iterator, bool> __p = 00381 _M_t._M_insert_unique(__x); 00382 return std::pair<iterator, bool>(__p.first, __p.second); 00383 } 00384 00385 /** 00386 * @brief Attempts to insert an element into the %set. 00387 * @param position An iterator that serves as a hint as to where the 00388 * element should be inserted. 00389 * @param x Element to be inserted. 00390 * @return An iterator that points to the element with key of @a x (may 00391 * or may not be the element passed in). 00392 * 00393 * This function is not concerned about whether the insertion took place, 00394 * and thus does not return a boolean like the single-argument insert() 00395 * does. Note that the first parameter is only a hint and can 00396 * potentially improve the performance of the insertion process. A bad 00397 * hint would cause no gains in efficiency. 00398 * 00399 * For more on "hinting", see: 00400 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html 00401 * 00402 * Insertion requires logarithmic time (if the hint is not taken). 00403 */ 00404 iterator 00405 insert(iterator __position, const value_type& __x) 00406 { return _M_t._M_insert_unique_(__position, __x); } 00407 00408 /** 00409 * @brief A template function that attempts to insert a range 00410 * of elements. 00411 * @param first Iterator pointing to the start of the range to be 00412 * inserted. 00413 * @param last Iterator pointing to the end of the range. 00414 * 00415 * Complexity similar to that of the range constructor. 00416 */ 00417 template<typename _InputIterator> 00418 void 00419 insert(_InputIterator __first, _InputIterator __last) 00420 { _M_t._M_insert_unique(__first, __last); } 00421 00422 /** 00423 * @brief Erases an element from a %set. 00424 * @param position An iterator pointing to the element to be erased. 00425 * 00426 * This function erases an element, pointed to by the given iterator, 00427 * from a %set. Note that this function only erases the element, and 00428 * that if the element is itself a pointer, the pointed-to memory is not 00429 * touched in any way. Managing the pointer is the user's responsibility. 00430 */ 00431 void 00432 erase(iterator __position) 00433 { _M_t.erase(__position); } 00434 00435 /** 00436 * @brief Erases elements according to the provided key. 00437 * @param x Key of element to be erased. 00438 * @return The number of elements erased. 00439 * 00440 * This function erases all the elements located by the given key from 00441 * a %set. 00442 * Note that this function only erases the element, and that if 00443 * the element is itself a pointer, the pointed-to memory is not touched 00444 * in any way. Managing the pointer is the user's responsibility. 00445 */ 00446 size_type 00447 erase(const key_type& __x) 00448 { return _M_t.erase(__x); } 00449 00450 /** 00451 * @brief Erases a [first,last) range of elements from a %set. 00452 * @param first Iterator pointing to the start of the range to be 00453 * erased. 00454 * @param last Iterator pointing to the end of the range to be erased. 00455 * 00456 * This function erases a sequence of elements from a %set. 00457 * Note that this function only erases the element, and that if 00458 * the element is itself a pointer, the pointed-to memory is not touched 00459 * in any way. Managing the pointer is the user's responsibility. 00460 */ 00461 void 00462 erase(iterator __first, iterator __last) 00463 { _M_t.erase(__first, __last); } 00464 00465 /** 00466 * Erases all elements in a %set. Note that this function only erases 00467 * the elements, and that if the elements themselves are pointers, the 00468 * pointed-to memory is not touched in any way. Managing the pointer is 00469 * the user's responsibility. 00470 */ 00471 void 00472 clear() 00473 { _M_t.clear(); } 00474 00475 // set operations: 00476 00477 /** 00478 * @brief Finds the number of elements. 00479 * @param x Element to located. 00480 * @return Number of elements with specified key. 00481 * 00482 * This function only makes sense for multisets; for set the result will 00483 * either be 0 (not present) or 1 (present). 00484 */ 00485 size_type 00486 count(const key_type& __x) const 00487 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00488 00489 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00490 // 214. set::find() missing const overload 00491 //@{ 00492 /** 00493 * @brief Tries to locate an element in a %set. 00494 * @param x Element to be located. 00495 * @return Iterator pointing to sought-after element, or end() if not 00496 * found. 00497 * 00498 * This function takes a key and tries to locate the element with which 00499 * the key matches. If successful the function returns an iterator 00500 * pointing to the sought after element. If unsuccessful it returns the 00501 * past-the-end ( @c end() ) iterator. 00502 */ 00503 iterator 00504 find(const key_type& __x) 00505 { return _M_t.find(__x); } 00506 00507 const_iterator 00508 find(const key_type& __x) const 00509 { return _M_t.find(__x); } 00510 //@} 00511 00512 //@{ 00513 /** 00514 * @brief Finds the beginning of a subsequence matching given key. 00515 * @param x Key to be located. 00516 * @return Iterator pointing to first element equal to or greater 00517 * than key, or end(). 00518 * 00519 * This function returns the first element of a subsequence of elements 00520 * that matches the given key. If unsuccessful it returns an iterator 00521 * pointing to the first element that has a greater value than given key 00522 * or end() if no such element exists. 00523 */ 00524 iterator 00525 lower_bound(const key_type& __x) 00526 { return _M_t.lower_bound(__x); } 00527 00528 const_iterator 00529 lower_bound(const key_type& __x) const 00530 { return _M_t.lower_bound(__x); } 00531 //@} 00532 00533 //@{ 00534 /** 00535 * @brief Finds the end of a subsequence matching given key. 00536 * @param x Key to be located. 00537 * @return Iterator pointing to the first element 00538 * greater than key, or end(). 00539 */ 00540 iterator 00541 upper_bound(const key_type& __x) 00542 { return _M_t.upper_bound(__x); } 00543 00544 const_iterator 00545 upper_bound(const key_type& __x) const 00546 { return _M_t.upper_bound(__x); } 00547 //@} 00548 00549 //@{ 00550 /** 00551 * @brief Finds a subsequence matching given key. 00552 * @param x Key to be located. 00553 * @return Pair of iterators that possibly points to the subsequence 00554 * matching given key. 00555 * 00556 * This function is equivalent to 00557 * @code 00558 * std::make_pair(c.lower_bound(val), 00559 * c.upper_bound(val)) 00560 * @endcode 00561 * (but is faster than making the calls separately). 00562 * 00563 * This function probably only makes sense for multisets. 00564 */ 00565 std::pair<iterator, iterator> 00566 equal_range(const key_type& __x) 00567 { return _M_t.equal_range(__x); } 00568 00569 std::pair<const_iterator, const_iterator> 00570 equal_range(const key_type& __x) const 00571 { return _M_t.equal_range(__x); } 00572 //@} 00573 00574 template<typename _K1, typename _C1, typename _A1> 00575 friend bool 00576 operator==(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00577 00578 template<typename _K1, typename _C1, typename _A1> 00579 friend bool 00580 operator<(const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00581 }; 00582 00583 00584 /** 00585 * @brief Set equality comparison. 00586 * @param x A %set. 00587 * @param y A %set of the same type as @a x. 00588 * @return True iff the size and elements of the sets are equal. 00589 * 00590 * This is an equivalence relation. It is linear in the size of the sets. 00591 * Sets are considered equivalent if their sizes are equal, and if 00592 * corresponding elements compare equal. 00593 */ 00594 template<typename _Key, typename _Compare, typename _Alloc> 00595 inline bool 00596 operator==(const set<_Key, _Compare, _Alloc>& __x, 00597 const set<_Key, _Compare, _Alloc>& __y) 00598 { return __x._M_t == __y._M_t; } 00599 00600 /** 00601 * @brief Set ordering relation. 00602 * @param x A %set. 00603 * @param y A %set of the same type as @a x. 00604 * @return True iff @a x is lexicographically less than @a y. 00605 * 00606 * This is a total ordering relation. It is linear in the size of the 00607 * maps. The elements must be comparable with @c <. 00608 * 00609 * See std::lexicographical_compare() for how the determination is made. 00610 */ 00611 template<typename _Key, typename _Compare, typename _Alloc> 00612 inline bool 00613 operator<(const set<_Key, _Compare, _Alloc>& __x, 00614 const set<_Key, _Compare, _Alloc>& __y) 00615 { return __x._M_t < __y._M_t; } 00616 00617 /// Returns !(x == y). 00618 template<typename _Key, typename _Compare, typename _Alloc> 00619 inline bool 00620 operator!=(const set<_Key, _Compare, _Alloc>& __x, 00621 const set<_Key, _Compare, _Alloc>& __y) 00622 { return !(__x == __y); } 00623 00624 /// Returns y < x. 00625 template<typename _Key, typename _Compare, typename _Alloc> 00626 inline bool 00627 operator>(const set<_Key, _Compare, _Alloc>& __x, 00628 const set<_Key, _Compare, _Alloc>& __y) 00629 { return __y < __x; } 00630 00631 /// Returns !(y < x) 00632 template<typename _Key, typename _Compare, typename _Alloc> 00633 inline bool 00634 operator<=(const set<_Key, _Compare, _Alloc>& __x, 00635 const set<_Key, _Compare, _Alloc>& __y) 00636 { return !(__y < __x); } 00637 00638 /// Returns !(x < y) 00639 template<typename _Key, typename _Compare, typename _Alloc> 00640 inline bool 00641 operator>=(const set<_Key, _Compare, _Alloc>& __x, 00642 const set<_Key, _Compare, _Alloc>& __y) 00643 { return !(__x < __y); } 00644 00645 /// See std::set::swap(). 00646 template<typename _Key, typename _Compare, typename _Alloc> 00647 inline void 00648 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) 00649 { __x.swap(__y); } 00650 00651 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00652 template<typename _Key, typename _Compare, typename _Alloc> 00653 inline void 00654 swap(set<_Key, _Compare, _Alloc>&& __x, set<_Key, _Compare, _Alloc>& __y) 00655 { __x.swap(__y); } 00656 00657 template<typename _Key, typename _Compare, typename _Alloc> 00658 inline void 00659 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>&& __y) 00660 { __x.swap(__y); } 00661 #endif 00662 00663 _GLIBCXX_END_NESTED_NAMESPACE 00664 00665 #endif /* _STL_SET_H */