00001 // Multiset 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 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_multiset.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_MULTISET_H 00063 #define _STL_MULTISET_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 elements, which can be retrieved 00071 * 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 equivalent 00079 * keys). For a @c multiset<Key> the key_type and value_type are Key. 00080 * 00081 * Multisets support bidirectional iterators. 00082 * 00083 * The private tree data is declared exactly the same way for set and 00084 * multiset; the distinction is made entirely in how the tree functions are 00085 * called (*_unique versus *_equal, same as the standard). 00086 */ 00087 template <typename _Key, typename _Compare = std::less<_Key>, 00088 typename _Alloc = std::allocator<_Key> > 00089 class multiset 00090 { 00091 // concept requirements 00092 typedef typename _Alloc::value_type _Alloc_value_type; 00093 __glibcxx_class_requires(_Key, _SGIAssignableConcept) 00094 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00095 _BinaryFunctionConcept) 00096 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept) 00097 00098 public: 00099 // typedefs: 00100 typedef _Key key_type; 00101 typedef _Key value_type; 00102 typedef _Compare key_compare; 00103 typedef _Compare value_compare; 00104 typedef _Alloc allocator_type; 00105 00106 private: 00107 /// This turns a red-black tree into a [multi]set. 00108 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type; 00109 00110 typedef _Rb_tree<key_type, value_type, _Identity<value_type>, 00111 key_compare, _Key_alloc_type> _Rep_type; 00112 /// The actual tree structure. 00113 _Rep_type _M_t; 00114 00115 public: 00116 typedef typename _Key_alloc_type::pointer pointer; 00117 typedef typename _Key_alloc_type::const_pointer const_pointer; 00118 typedef typename _Key_alloc_type::reference reference; 00119 typedef typename _Key_alloc_type::const_reference const_reference; 00120 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00121 // DR 103. set::iterator is required to be modifiable, 00122 // but this allows modification of keys. 00123 typedef typename _Rep_type::const_iterator iterator; 00124 typedef typename _Rep_type::const_iterator const_iterator; 00125 typedef typename _Rep_type::const_reverse_iterator reverse_iterator; 00126 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00127 typedef typename _Rep_type::size_type size_type; 00128 typedef typename _Rep_type::difference_type difference_type; 00129 00130 // allocation/deallocation 00131 /** 00132 * @brief Default constructor creates no elements. 00133 */ 00134 multiset() 00135 : _M_t() { } 00136 00137 /** 00138 * @brief Creates a %multiset with no elements. 00139 * @param comp Comparator to use. 00140 * @param a An allocator object. 00141 */ 00142 explicit 00143 multiset(const _Compare& __comp, 00144 const allocator_type& __a = allocator_type()) 00145 : _M_t(__comp, __a) { } 00146 00147 /** 00148 * @brief Builds a %multiset from a range. 00149 * @param first An input iterator. 00150 * @param last An input iterator. 00151 * 00152 * Create a %multiset consisting of copies of the elements from 00153 * [first,last). This is linear in N if the range is already sorted, 00154 * and NlogN otherwise (where N is distance(first,last)). 00155 */ 00156 template<typename _InputIterator> 00157 multiset(_InputIterator __first, _InputIterator __last) 00158 : _M_t() 00159 { _M_t._M_insert_equal(__first, __last); } 00160 00161 /** 00162 * @brief Builds a %multiset from a range. 00163 * @param first An input iterator. 00164 * @param last An input iterator. 00165 * @param comp A comparison functor. 00166 * @param a An allocator object. 00167 * 00168 * Create a %multiset consisting of copies of the elements from 00169 * [first,last). This is linear in N if the range is already sorted, 00170 * and NlogN otherwise (where N is distance(first,last)). 00171 */ 00172 template<typename _InputIterator> 00173 multiset(_InputIterator __first, _InputIterator __last, 00174 const _Compare& __comp, 00175 const allocator_type& __a = allocator_type()) 00176 : _M_t(__comp, __a) 00177 { _M_t._M_insert_equal(__first, __last); } 00178 00179 /** 00180 * @brief %Multiset copy constructor. 00181 * @param x A %multiset of identical element and allocator types. 00182 * 00183 * The newly-created %multiset uses a copy of the allocation object used 00184 * by @a x. 00185 */ 00186 multiset(const multiset& __x) 00187 : _M_t(__x._M_t) { } 00188 00189 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00190 /** 00191 * @brief %Multiset move constructor. 00192 * @param x A %multiset of identical element and allocator types. 00193 * 00194 * The newly-created %multiset contains the exact contents of @a x. 00195 * The contents of @a x are a valid, but unspecified %multiset. 00196 */ 00197 multiset(multiset&& __x) 00198 : _M_t(std::forward<_Rep_type>(__x._M_t)) { } 00199 #endif 00200 00201 /** 00202 * @brief %Multiset assignment operator. 00203 * @param x A %multiset of identical element and allocator types. 00204 * 00205 * All the elements of @a x are copied, but unlike the copy constructor, 00206 * the allocator object is not copied. 00207 */ 00208 multiset& 00209 operator=(const multiset& __x) 00210 { 00211 _M_t = __x._M_t; 00212 return *this; 00213 } 00214 00215 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00216 /** 00217 * @brief %Multiset move assignment operator. 00218 * @param x A %multiset of identical element and allocator types. 00219 * 00220 * The contents of @a x are moved into this %multiset (without copying). 00221 * @a x is a valid, but unspecified %multiset. 00222 */ 00223 multiset& 00224 operator=(multiset&& __x) 00225 { 00226 // NB: DR 675. 00227 this->clear(); 00228 this->swap(__x); 00229 return *this; 00230 } 00231 #endif 00232 00233 // accessors: 00234 00235 /// Returns the comparison object. 00236 key_compare 00237 key_comp() const 00238 { return _M_t.key_comp(); } 00239 /// Returns the comparison object. 00240 value_compare 00241 value_comp() const 00242 { return _M_t.key_comp(); } 00243 /// Returns the memory allocation object. 00244 allocator_type 00245 get_allocator() const 00246 { return _M_t.get_allocator(); } 00247 00248 /** 00249 * Returns a read-only (constant) iterator that points to the first 00250 * element in the %multiset. Iteration is done in ascending order 00251 * according to the keys. 00252 */ 00253 iterator 00254 begin() const 00255 { return _M_t.begin(); } 00256 00257 /** 00258 * Returns a read-only (constant) iterator that points one past the last 00259 * element in the %multiset. Iteration is done in ascending order 00260 * according to the keys. 00261 */ 00262 iterator 00263 end() const 00264 { return _M_t.end(); } 00265 00266 /** 00267 * Returns a read-only (constant) reverse iterator that points to the 00268 * last element in the %multiset. Iteration is done in descending order 00269 * according to the keys. 00270 */ 00271 reverse_iterator 00272 rbegin() const 00273 { return _M_t.rbegin(); } 00274 00275 /** 00276 * Returns a read-only (constant) reverse iterator that points to the 00277 * last element in the %multiset. Iteration is done in descending order 00278 * according to the keys. 00279 */ 00280 reverse_iterator 00281 rend() const 00282 { return _M_t.rend(); } 00283 00284 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00285 /** 00286 * Returns a read-only (constant) iterator that points to the first 00287 * element in the %multiset. Iteration is done in ascending order 00288 * according to the keys. 00289 */ 00290 iterator 00291 cbegin() const 00292 { return _M_t.begin(); } 00293 00294 /** 00295 * Returns a read-only (constant) iterator that points one past the last 00296 * element in the %multiset. Iteration is done in ascending order 00297 * according to the keys. 00298 */ 00299 iterator 00300 cend() const 00301 { return _M_t.end(); } 00302 00303 /** 00304 * Returns a read-only (constant) reverse iterator that points to the 00305 * last element in the %multiset. Iteration is done in descending order 00306 * according to the keys. 00307 */ 00308 reverse_iterator 00309 crbegin() const 00310 { return _M_t.rbegin(); } 00311 00312 /** 00313 * Returns a read-only (constant) reverse iterator that points to the 00314 * last element in the %multiset. Iteration is done in descending order 00315 * according to the keys. 00316 */ 00317 reverse_iterator 00318 crend() const 00319 { return _M_t.rend(); } 00320 #endif 00321 00322 /// Returns true if the %set is empty. 00323 bool 00324 empty() const 00325 { return _M_t.empty(); } 00326 00327 /// Returns the size of the %set. 00328 size_type 00329 size() const 00330 { return _M_t.size(); } 00331 00332 /// Returns the maximum size of the %set. 00333 size_type 00334 max_size() const 00335 { return _M_t.max_size(); } 00336 00337 /** 00338 * @brief Swaps data with another %multiset. 00339 * @param x A %multiset of the same element and allocator types. 00340 * 00341 * This exchanges the elements between two multisets in constant time. 00342 * (It is only swapping a pointer, an integer, and an instance of the @c 00343 * Compare type (which itself is often stateless and empty), so it should 00344 * be quite fast.) 00345 * Note that the global std::swap() function is specialized such that 00346 * std::swap(s1,s2) will feed to this function. 00347 */ 00348 void 00349 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00350 swap(multiset&& __x) 00351 #else 00352 swap(multiset& __x) 00353 #endif 00354 { _M_t.swap(__x._M_t); } 00355 00356 // insert/erase 00357 /** 00358 * @brief Inserts an element into the %multiset. 00359 * @param x Element to be inserted. 00360 * @return An iterator that points to the inserted element. 00361 * 00362 * This function inserts an element into the %multiset. Contrary 00363 * to a std::set the %multiset does not rely on unique keys and thus 00364 * multiple copies of the same element can be inserted. 00365 * 00366 * Insertion requires logarithmic time. 00367 */ 00368 iterator 00369 insert(const value_type& __x) 00370 { return _M_t._M_insert_equal(__x); } 00371 00372 /** 00373 * @brief Inserts an element into the %multiset. 00374 * @param position An iterator that serves as a hint as to where the 00375 * element should be inserted. 00376 * @param x Element to be inserted. 00377 * @return An iterator that points to the inserted element. 00378 * 00379 * This function inserts an element into the %multiset. Contrary 00380 * to a std::set the %multiset does not rely on unique keys and thus 00381 * multiple copies of the same element can be inserted. 00382 * 00383 * Note that the first parameter is only a hint and can potentially 00384 * improve the performance of the insertion process. A bad hint would 00385 * cause no gains in efficiency. 00386 * 00387 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4 00388 * for more on "hinting". 00389 * 00390 * Insertion requires logarithmic time (if the hint is not taken). 00391 */ 00392 iterator 00393 insert(iterator __position, const value_type& __x) 00394 { return _M_t._M_insert_equal_(__position, __x); } 00395 00396 /** 00397 * @brief A template function that attempts to insert a range of elements. 00398 * @param first Iterator pointing to the start of the range to be 00399 * inserted. 00400 * @param last Iterator pointing to the end of the range. 00401 * 00402 * Complexity similar to that of the range constructor. 00403 */ 00404 template<typename _InputIterator> 00405 void 00406 insert(_InputIterator __first, _InputIterator __last) 00407 { _M_t._M_insert_equal(__first, __last); } 00408 00409 /** 00410 * @brief Erases an element from a %multiset. 00411 * @param position An iterator pointing to the element to be erased. 00412 * 00413 * This function erases an element, pointed to by the given iterator, 00414 * from a %multiset. Note that this function only erases the element, 00415 * and that if the element is itself a pointer, the pointed-to memory is 00416 * not touched in any way. Managing the pointer is the user's 00417 * responsibility. 00418 */ 00419 void 00420 erase(iterator __position) 00421 { _M_t.erase(__position); } 00422 00423 /** 00424 * @brief Erases elements according to the provided key. 00425 * @param x Key of element to be erased. 00426 * @return The number of elements erased. 00427 * 00428 * This function erases all elements located by the given key from a 00429 * %multiset. 00430 * Note that this function only erases the element, and that if 00431 * the element is itself a pointer, the pointed-to memory is not touched 00432 * in any way. Managing the pointer is the user's responsibility. 00433 */ 00434 size_type 00435 erase(const key_type& __x) 00436 { return _M_t.erase(__x); } 00437 00438 /** 00439 * @brief Erases a [first,last) range of elements from a %multiset. 00440 * @param first Iterator pointing to the start of the range to be 00441 * erased. 00442 * @param last Iterator pointing to the end of the range to be erased. 00443 * 00444 * This function erases a sequence of elements from a %multiset. 00445 * Note that this function only erases the elements, and that if 00446 * the elements themselves are pointers, the pointed-to memory is not 00447 * touched in any way. Managing the pointer is the user's responsibility. 00448 */ 00449 void 00450 erase(iterator __first, iterator __last) 00451 { _M_t.erase(__first, __last); } 00452 00453 /** 00454 * Erases all elements in a %multiset. Note that this function only 00455 * erases the elements, and that if the elements themselves are pointers, 00456 * the pointed-to memory is not touched in any way. Managing the pointer 00457 * is the user's responsibility. 00458 */ 00459 void 00460 clear() 00461 { _M_t.clear(); } 00462 00463 // multiset operations: 00464 00465 /** 00466 * @brief Finds the number of elements with given key. 00467 * @param x Key of elements to be located. 00468 * @return Number of elements with specified key. 00469 */ 00470 size_type 00471 count(const key_type& __x) const 00472 { return _M_t.count(__x); } 00473 00474 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00475 // 214. set::find() missing const overload 00476 //@{ 00477 /** 00478 * @brief Tries to locate an element in a %set. 00479 * @param x Element to be located. 00480 * @return Iterator pointing to sought-after element, or end() if not 00481 * found. 00482 * 00483 * This function takes a key and tries to locate the element with which 00484 * the key matches. If successful the function returns an iterator 00485 * pointing to the sought after element. If unsuccessful it returns the 00486 * past-the-end ( @c end() ) iterator. 00487 */ 00488 iterator 00489 find(const key_type& __x) 00490 { return _M_t.find(__x); } 00491 00492 const_iterator 00493 find(const key_type& __x) const 00494 { return _M_t.find(__x); } 00495 //@} 00496 00497 //@{ 00498 /** 00499 * @brief Finds the beginning of a subsequence matching given key. 00500 * @param x Key to be located. 00501 * @return Iterator pointing to first element equal to or greater 00502 * than key, or end(). 00503 * 00504 * This function returns the first element of a subsequence of elements 00505 * that matches the given key. If unsuccessful it returns an iterator 00506 * pointing to the first element that has a greater value than given key 00507 * or end() if no such element exists. 00508 */ 00509 iterator 00510 lower_bound(const key_type& __x) 00511 { return _M_t.lower_bound(__x); } 00512 00513 const_iterator 00514 lower_bound(const key_type& __x) const 00515 { return _M_t.lower_bound(__x); } 00516 //@} 00517 00518 //@{ 00519 /** 00520 * @brief Finds the end of a subsequence matching given key. 00521 * @param x Key to be located. 00522 * @return Iterator pointing to the first element 00523 * greater than key, or end(). 00524 */ 00525 iterator 00526 upper_bound(const key_type& __x) 00527 { return _M_t.upper_bound(__x); } 00528 00529 const_iterator 00530 upper_bound(const key_type& __x) const 00531 { return _M_t.upper_bound(__x); } 00532 //@} 00533 00534 //@{ 00535 /** 00536 * @brief Finds a subsequence matching given key. 00537 * @param x Key to be located. 00538 * @return Pair of iterators that possibly points to the subsequence 00539 * matching given key. 00540 * 00541 * This function is equivalent to 00542 * @code 00543 * std::make_pair(c.lower_bound(val), 00544 * c.upper_bound(val)) 00545 * @endcode 00546 * (but is faster than making the calls separately). 00547 * 00548 * This function probably only makes sense for multisets. 00549 */ 00550 std::pair<iterator, iterator> 00551 equal_range(const key_type& __x) 00552 { return _M_t.equal_range(__x); } 00553 00554 std::pair<const_iterator, const_iterator> 00555 equal_range(const key_type& __x) const 00556 { return _M_t.equal_range(__x); } 00557 00558 template<typename _K1, typename _C1, typename _A1> 00559 friend bool 00560 operator==(const multiset<_K1, _C1, _A1>&, 00561 const multiset<_K1, _C1, _A1>&); 00562 00563 template<typename _K1, typename _C1, typename _A1> 00564 friend bool 00565 operator< (const multiset<_K1, _C1, _A1>&, 00566 const multiset<_K1, _C1, _A1>&); 00567 }; 00568 00569 /** 00570 * @brief Multiset equality comparison. 00571 * @param x A %multiset. 00572 * @param y A %multiset of the same type as @a x. 00573 * @return True iff the size and elements of the multisets are equal. 00574 * 00575 * This is an equivalence relation. It is linear in the size of the 00576 * multisets. 00577 * Multisets are considered equivalent if their sizes are equal, and if 00578 * corresponding elements compare equal. 00579 */ 00580 template<typename _Key, typename _Compare, typename _Alloc> 00581 inline bool 00582 operator==(const multiset<_Key, _Compare, _Alloc>& __x, 00583 const multiset<_Key, _Compare, _Alloc>& __y) 00584 { return __x._M_t == __y._M_t; } 00585 00586 /** 00587 * @brief Multiset ordering relation. 00588 * @param x A %multiset. 00589 * @param y A %multiset of the same type as @a x. 00590 * @return True iff @a x is lexicographically less than @a y. 00591 * 00592 * This is a total ordering relation. It is linear in the size of the 00593 * maps. The elements must be comparable with @c <. 00594 * 00595 * See std::lexicographical_compare() for how the determination is made. 00596 */ 00597 template<typename _Key, typename _Compare, typename _Alloc> 00598 inline bool 00599 operator<(const multiset<_Key, _Compare, _Alloc>& __x, 00600 const multiset<_Key, _Compare, _Alloc>& __y) 00601 { return __x._M_t < __y._M_t; } 00602 00603 /// Returns !(x == y). 00604 template<typename _Key, typename _Compare, typename _Alloc> 00605 inline bool 00606 operator!=(const multiset<_Key, _Compare, _Alloc>& __x, 00607 const multiset<_Key, _Compare, _Alloc>& __y) 00608 { return !(__x == __y); } 00609 00610 /// Returns y < x. 00611 template<typename _Key, typename _Compare, typename _Alloc> 00612 inline bool 00613 operator>(const multiset<_Key,_Compare,_Alloc>& __x, 00614 const multiset<_Key,_Compare,_Alloc>& __y) 00615 { return __y < __x; } 00616 00617 /// Returns !(y < x) 00618 template<typename _Key, typename _Compare, typename _Alloc> 00619 inline bool 00620 operator<=(const multiset<_Key, _Compare, _Alloc>& __x, 00621 const multiset<_Key, _Compare, _Alloc>& __y) 00622 { return !(__y < __x); } 00623 00624 /// Returns !(x < y) 00625 template<typename _Key, typename _Compare, typename _Alloc> 00626 inline bool 00627 operator>=(const multiset<_Key, _Compare, _Alloc>& __x, 00628 const multiset<_Key, _Compare, _Alloc>& __y) 00629 { return !(__x < __y); } 00630 00631 /// See std::multiset::swap(). 00632 template<typename _Key, typename _Compare, typename _Alloc> 00633 inline void 00634 swap(multiset<_Key, _Compare, _Alloc>& __x, 00635 multiset<_Key, _Compare, _Alloc>& __y) 00636 { __x.swap(__y); } 00637 00638 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00639 template<typename _Key, typename _Compare, typename _Alloc> 00640 inline void 00641 swap(multiset<_Key, _Compare, _Alloc>&& __x, 00642 multiset<_Key, _Compare, _Alloc>& __y) 00643 { __x.swap(__y); } 00644 00645 template<typename _Key, typename _Compare, typename _Alloc> 00646 inline void 00647 swap(multiset<_Key, _Compare, _Alloc>& __x, 00648 multiset<_Key, _Compare, _Alloc>&& __y) 00649 { __x.swap(__y); } 00650 #endif 00651 00652 _GLIBCXX_END_NESTED_NAMESPACE 00653 00654 #endif /* _STL_MULTISET_H */