00001 // Set implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2004, 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1996,1997 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00056 /** @file stl_set.h 00057 * This is an internal header file, included by other library headers. 00058 * You should not attempt to use it directly. 00059 */ 00060 00061 #ifndef _SET_H 00062 #define _SET_H 1 00063 00064 #include <bits/concept_check.h> 00065 00066 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD) 00067 00068 /** 00069 * @brief A standard container made up of unique keys, which can be 00070 * retrieved in logarithmic time. 00071 * 00072 * @ingroup Containers 00073 * @ingroup Assoc_containers 00074 * 00075 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00076 * <a href="tables.html#66">reversible container</a>, and an 00077 * <a href="tables.html#69">associative container</a> (using unique keys). 00078 * 00079 * Sets support bidirectional iterators. 00080 * 00081 * @param Key Type of key objects. 00082 * @param Compare Comparison function object type, defaults to less<Key>. 00083 * @param Alloc Allocator type, defaults to allocator<Key>. 00084 * 00085 * @if maint 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 * @endif 00090 */ 00091 template<class _Key, class _Compare = std::less<_Key>, 00092 class _Alloc = std::allocator<_Key> > 00093 class set 00094 { 00095 // concept requirements 00096 typedef typename _Alloc::value_type _Alloc_value_type; 00097 __glibcxx_class_requires(_Key, _SGIAssignableConcept) 00098 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00099 _BinaryFunctionConcept) 00100 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept) 00101 00102 public: 00103 // typedefs: 00104 //@{ 00105 /// Public typedefs. 00106 typedef _Key key_type; 00107 typedef _Key value_type; 00108 typedef _Compare key_compare; 00109 typedef _Compare value_compare; 00110 typedef _Alloc allocator_type; 00111 //@} 00112 00113 private: 00114 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type; 00115 00116 typedef _Rb_tree<key_type, value_type, _Identity<value_type>, 00117 key_compare, _Key_alloc_type> _Rep_type; 00118 _Rep_type _M_t; // red-black tree representing set 00119 00120 public: 00121 //@{ 00122 /// Iterator-related typedefs. 00123 typedef typename _Key_alloc_type::pointer pointer; 00124 typedef typename _Key_alloc_type::const_pointer const_pointer; 00125 typedef typename _Key_alloc_type::reference reference; 00126 typedef typename _Key_alloc_type::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 /// Default constructor creates no elements. 00140 set() 00141 : _M_t(_Compare(), allocator_type()) {} 00142 00143 /** 00144 * @brief Default constructor creates no elements. 00145 * 00146 * @param comp Comparator to use. 00147 * @param a Allocator to use. 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<class _InputIterator> 00164 set(_InputIterator __first, _InputIterator __last) 00165 : _M_t(_Compare(), allocator_type()) 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<class _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<_Key,_Compare,_Alloc>& __x) 00194 : _M_t(__x._M_t) { } 00195 00196 /** 00197 * @brief Set assignment operator. 00198 * @param x A %set of identical element and allocator types. 00199 * 00200 * All the elements of @a x are copied, but unlike the copy constructor, 00201 * the allocator object is not copied. 00202 */ 00203 set<_Key,_Compare,_Alloc>& 00204 operator=(const set<_Key, _Compare, _Alloc>& __x) 00205 { 00206 _M_t = __x._M_t; 00207 return *this; 00208 } 00209 00210 // accessors: 00211 00212 /// Returns the comparison object with which the %set was constructed. 00213 key_compare 00214 key_comp() const 00215 { return _M_t.key_comp(); } 00216 /// Returns the comparison object with which the %set was constructed. 00217 value_compare 00218 value_comp() const 00219 { return _M_t.key_comp(); } 00220 /// Returns the allocator object with which the %set was constructed. 00221 allocator_type 00222 get_allocator() const 00223 { return _M_t.get_allocator(); } 00224 00225 /** 00226 * Returns a read/write iterator that points to the first element in the 00227 * %set. Iteration is done in ascending order according to the keys. 00228 */ 00229 iterator 00230 begin() const 00231 { return _M_t.begin(); } 00232 00233 /** 00234 * Returns a read/write iterator that points one past the last element in 00235 * the %set. Iteration is done in ascending order according to the keys. 00236 */ 00237 iterator 00238 end() const 00239 { return _M_t.end(); } 00240 00241 /** 00242 * Returns a read/write reverse iterator that points to the last element 00243 * in the %set. Iteration is done in descending order according to the 00244 * keys. 00245 */ 00246 reverse_iterator 00247 rbegin() const 00248 { return _M_t.rbegin(); } 00249 00250 /** 00251 * Returns a read-only (constant) reverse iterator that points to the 00252 * last pair in the %map. Iteration is done in descending order 00253 * according to the keys. 00254 */ 00255 reverse_iterator 00256 rend() const 00257 { return _M_t.rend(); } 00258 00259 /// Returns true if the %set is empty. 00260 bool 00261 empty() const 00262 { return _M_t.empty(); } 00263 00264 /// Returns the size of the %set. 00265 size_type 00266 size() const 00267 { return _M_t.size(); } 00268 00269 /// Returns the maximum size of the %set. 00270 size_type 00271 max_size() const 00272 { return _M_t.max_size(); } 00273 00274 /** 00275 * @brief Swaps data with another %set. 00276 * @param x A %set of the same element and allocator types. 00277 * 00278 * This exchanges the elements between two sets in constant time. 00279 * (It is only swapping a pointer, an integer, and an instance of 00280 * the @c Compare type (which itself is often stateless and empty), so it 00281 * should be quite fast.) 00282 * Note that the global std::swap() function is specialized such that 00283 * std::swap(s1,s2) will feed to this function. 00284 */ 00285 void 00286 swap(set<_Key,_Compare,_Alloc>& __x) 00287 { _M_t.swap(__x._M_t); } 00288 00289 // insert/erase 00290 /** 00291 * @brief Attempts to insert an element into the %set. 00292 * @param x Element to be inserted. 00293 * @return A pair, of which the first element is an iterator that points 00294 * to the possibly inserted element, and the second is a bool 00295 * that is true if the element was actually inserted. 00296 * 00297 * This function attempts to insert an element into the %set. A %set 00298 * relies on unique keys and thus an element is only inserted if it is 00299 * not already present in the %set. 00300 * 00301 * Insertion requires logarithmic time. 00302 */ 00303 std::pair<iterator,bool> 00304 insert(const value_type& __x) 00305 { 00306 std::pair<typename _Rep_type::iterator, bool> __p = 00307 _M_t._M_insert_unique(__x); 00308 return std::pair<iterator, bool>(__p.first, __p.second); 00309 } 00310 00311 /** 00312 * @brief Attempts to insert an element into the %set. 00313 * @param position An iterator that serves as a hint as to where the 00314 * element should be inserted. 00315 * @param x Element to be inserted. 00316 * @return An iterator that points to the element with key of @a x (may 00317 * or may not be the element passed in). 00318 * 00319 * This function is not concerned about whether the insertion took place, 00320 * and thus does not return a boolean like the single-argument insert() 00321 * does. Note that the first parameter is only a hint and can 00322 * potentially improve the performance of the insertion process. A bad 00323 * hint would cause no gains in efficiency. 00324 * 00325 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4 00326 * for more on "hinting". 00327 * 00328 * Insertion requires logarithmic time (if the hint is not taken). 00329 */ 00330 iterator 00331 insert(iterator __position, const value_type& __x) 00332 { return _M_t._M_insert_unique(__position, __x); } 00333 00334 /** 00335 * @brief A template function that attemps to insert a range of elements. 00336 * @param first Iterator pointing to the start of the range to be 00337 * inserted. 00338 * @param last Iterator pointing to the end of the range. 00339 * 00340 * Complexity similar to that of the range constructor. 00341 */ 00342 template<class _InputIterator> 00343 void 00344 insert(_InputIterator __first, _InputIterator __last) 00345 { _M_t._M_insert_unique(__first, __last); } 00346 00347 /** 00348 * @brief Erases an element from a %set. 00349 * @param position An iterator pointing to the element to be erased. 00350 * 00351 * This function erases an element, pointed to by the given iterator, 00352 * from a %set. Note that this function only erases the element, and 00353 * that if the element is itself a pointer, the pointed-to memory is not 00354 * touched in any way. Managing the pointer is the user's responsibilty. 00355 */ 00356 void 00357 erase(iterator __position) 00358 { _M_t.erase(__position); } 00359 00360 /** 00361 * @brief Erases elements according to the provided key. 00362 * @param x Key of element to be erased. 00363 * @return The number of elements erased. 00364 * 00365 * This function erases all the elements located by the given key from 00366 * a %set. 00367 * Note that this function only erases the element, and that if 00368 * the element is itself a pointer, the pointed-to memory is not touched 00369 * in any way. Managing the pointer is the user's responsibilty. 00370 */ 00371 size_type 00372 erase(const key_type& __x) 00373 { return _M_t.erase(__x); } 00374 00375 /** 00376 * @brief Erases a [first,last) range of elements from a %set. 00377 * @param first Iterator pointing to the start of the range to be 00378 * erased. 00379 * @param last Iterator pointing to the end of the range to be erased. 00380 * 00381 * This function erases a sequence of elements from a %set. 00382 * Note that this function only erases the element, and that if 00383 * the element is itself a pointer, the pointed-to memory is not touched 00384 * in any way. Managing the pointer is the user's responsibilty. 00385 */ 00386 void 00387 erase(iterator __first, iterator __last) 00388 { _M_t.erase(__first, __last); } 00389 00390 /** 00391 * Erases all elements in a %set. Note that this function only erases 00392 * the elements, and that if the elements themselves are pointers, the 00393 * pointed-to memory is not touched in any way. Managing the pointer is 00394 * the user's responsibilty. 00395 */ 00396 void 00397 clear() 00398 { _M_t.clear(); } 00399 00400 // set operations: 00401 00402 /** 00403 * @brief Finds the number of elements. 00404 * @param x Element to located. 00405 * @return Number of elements with specified key. 00406 * 00407 * This function only makes sense for multisets; for set the result will 00408 * either be 0 (not present) or 1 (present). 00409 */ 00410 size_type 00411 count(const key_type& __x) const 00412 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00413 00414 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00415 // 214. set::find() missing const overload 00416 //@{ 00417 /** 00418 * @brief Tries to locate an element in a %set. 00419 * @param x Element to be located. 00420 * @return Iterator pointing to sought-after element, or end() if not 00421 * found. 00422 * 00423 * This function takes a key and tries to locate the element with which 00424 * the key matches. If successful the function returns an iterator 00425 * pointing to the sought after element. If unsuccessful it returns the 00426 * past-the-end ( @c end() ) iterator. 00427 */ 00428 iterator 00429 find(const key_type& __x) 00430 { return _M_t.find(__x); } 00431 00432 const_iterator 00433 find(const key_type& __x) const 00434 { return _M_t.find(__x); } 00435 //@} 00436 00437 //@{ 00438 /** 00439 * @brief Finds the beginning of a subsequence matching given key. 00440 * @param x Key to be located. 00441 * @return Iterator pointing to first element equal to or greater 00442 * than key, or end(). 00443 * 00444 * This function returns the first element of a subsequence of elements 00445 * that matches the given key. If unsuccessful it returns an iterator 00446 * pointing to the first element that has a greater value than given key 00447 * or end() if no such element exists. 00448 */ 00449 iterator 00450 lower_bound(const key_type& __x) 00451 { return _M_t.lower_bound(__x); } 00452 00453 const_iterator 00454 lower_bound(const key_type& __x) const 00455 { return _M_t.lower_bound(__x); } 00456 //@} 00457 00458 //@{ 00459 /** 00460 * @brief Finds the end of a subsequence matching given key. 00461 * @param x Key to be located. 00462 * @return Iterator pointing to the first element 00463 * greater than key, or end(). 00464 */ 00465 iterator 00466 upper_bound(const key_type& __x) 00467 { return _M_t.upper_bound(__x); } 00468 00469 const_iterator 00470 upper_bound(const key_type& __x) const 00471 { return _M_t.upper_bound(__x); } 00472 //@} 00473 00474 //@{ 00475 /** 00476 * @brief Finds a subsequence matching given key. 00477 * @param x Key to be located. 00478 * @return Pair of iterators that possibly points to the subsequence 00479 * matching given key. 00480 * 00481 * This function is equivalent to 00482 * @code 00483 * std::make_pair(c.lower_bound(val), 00484 * c.upper_bound(val)) 00485 * @endcode 00486 * (but is faster than making the calls separately). 00487 * 00488 * This function probably only makes sense for multisets. 00489 */ 00490 std::pair<iterator, iterator> 00491 equal_range(const key_type& __x) 00492 { return _M_t.equal_range(__x); } 00493 00494 std::pair<const_iterator, const_iterator> 00495 equal_range(const key_type& __x) const 00496 { return _M_t.equal_range(__x); } 00497 //@} 00498 00499 template<class _K1, class _C1, class _A1> 00500 friend bool 00501 operator== (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00502 00503 template<class _K1, class _C1, class _A1> 00504 friend bool 00505 operator< (const set<_K1, _C1, _A1>&, const set<_K1, _C1, _A1>&); 00506 }; 00507 00508 00509 /** 00510 * @brief Set equality comparison. 00511 * @param x A %set. 00512 * @param y A %set of the same type as @a x. 00513 * @return True iff the size and elements of the sets are equal. 00514 * 00515 * This is an equivalence relation. It is linear in the size of the sets. 00516 * Sets are considered equivalent if their sizes are equal, and if 00517 * corresponding elements compare equal. 00518 */ 00519 template<class _Key, class _Compare, class _Alloc> 00520 inline bool 00521 operator==(const set<_Key, _Compare, _Alloc>& __x, 00522 const set<_Key, _Compare, _Alloc>& __y) 00523 { return __x._M_t == __y._M_t; } 00524 00525 /** 00526 * @brief Set ordering relation. 00527 * @param x A %set. 00528 * @param y A %set of the same type as @a x. 00529 * @return True iff @a x is lexicographically less than @a y. 00530 * 00531 * This is a total ordering relation. It is linear in the size of the 00532 * maps. The elements must be comparable with @c <. 00533 * 00534 * See std::lexicographical_compare() for how the determination is made. 00535 */ 00536 template<class _Key, class _Compare, class _Alloc> 00537 inline bool 00538 operator<(const set<_Key, _Compare, _Alloc>& __x, 00539 const set<_Key, _Compare, _Alloc>& __y) 00540 { return __x._M_t < __y._M_t; } 00541 00542 /// Returns !(x == y). 00543 template<class _Key, class _Compare, class _Alloc> 00544 inline bool 00545 operator!=(const set<_Key, _Compare, _Alloc>& __x, 00546 const set<_Key, _Compare, _Alloc>& __y) 00547 { return !(__x == __y); } 00548 00549 /// Returns y < x. 00550 template<class _Key, class _Compare, class _Alloc> 00551 inline bool 00552 operator>(const set<_Key, _Compare, _Alloc>& __x, 00553 const set<_Key, _Compare, _Alloc>& __y) 00554 { return __y < __x; } 00555 00556 /// Returns !(y < x) 00557 template<class _Key, class _Compare, class _Alloc> 00558 inline bool 00559 operator<=(const set<_Key, _Compare, _Alloc>& __x, 00560 const set<_Key, _Compare, _Alloc>& __y) 00561 { return !(__y < __x); } 00562 00563 /// Returns !(x < y) 00564 template<class _Key, class _Compare, class _Alloc> 00565 inline bool 00566 operator>=(const set<_Key, _Compare, _Alloc>& __x, 00567 const set<_Key, _Compare, _Alloc>& __y) 00568 { return !(__x < __y); } 00569 00570 /// See std::set::swap(). 00571 template<class _Key, class _Compare, class _Alloc> 00572 inline void 00573 swap(set<_Key, _Compare, _Alloc>& __x, set<_Key, _Compare, _Alloc>& __y) 00574 { __x.swap(__y); } 00575 00576 _GLIBCXX_END_NESTED_NAMESPACE 00577 00578 #endif /* _SET_H */