1 // Multiset implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_multiset.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_MULTISET_H
63 #define _STL_MULTISET_H 1
65 #include <bits/concept_check.h>
66 #include <initializer_list>
68 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
71 * @brief A standard container made up of elements, which can be retrieved
72 * in logarithmic time.
75 * @ingroup Assoc_containers
77 * Meets the requirements of a <a href="tables.html#65">container</a>, a
78 * <a href="tables.html#66">reversible container</a>, and an
79 * <a href="tables.html#69">associative container</a> (using equivalent
80 * keys). For a @c multiset<Key> the key_type and value_type are Key.
82 * Multisets support bidirectional iterators.
84 * The private tree data is declared exactly the same way for set and
85 * multiset; the distinction is made entirely in how the tree functions are
86 * called (*_unique versus *_equal, same as the standard).
88 template <typename _Key, typename _Compare = std::less<_Key>,
89 typename _Alloc = std::allocator<_Key> >
92 // concept requirements
93 typedef typename _Alloc::value_type _Alloc_value_type;
94 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
95 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
96 _BinaryFunctionConcept)
97 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
101 typedef _Key key_type;
102 typedef _Key value_type;
103 typedef _Compare key_compare;
104 typedef _Compare value_compare;
105 typedef _Alloc allocator_type;
108 /// This turns a red-black tree into a [multi]set.
109 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
111 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
112 key_compare, _Key_alloc_type> _Rep_type;
113 /// The actual tree structure.
117 typedef typename _Key_alloc_type::pointer pointer;
118 typedef typename _Key_alloc_type::const_pointer const_pointer;
119 typedef typename _Key_alloc_type::reference reference;
120 typedef typename _Key_alloc_type::const_reference const_reference;
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // DR 103. set::iterator is required to be modifiable,
123 // but this allows modification of keys.
124 typedef typename _Rep_type::const_iterator iterator;
125 typedef typename _Rep_type::const_iterator const_iterator;
126 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
127 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
128 typedef typename _Rep_type::size_type size_type;
129 typedef typename _Rep_type::difference_type difference_type;
131 // allocation/deallocation
133 * @brief Default constructor creates no elements.
139 * @brief Creates a %multiset with no elements.
140 * @param comp Comparator to use.
141 * @param a An allocator object.
144 multiset(const _Compare& __comp,
145 const allocator_type& __a = allocator_type())
146 : _M_t(__comp, __a) { }
149 * @brief Builds a %multiset from a range.
150 * @param first An input iterator.
151 * @param last An input iterator.
153 * Create a %multiset consisting of copies of the elements from
154 * [first,last). This is linear in N if the range is already sorted,
155 * and NlogN otherwise (where N is distance(first,last)).
157 template<typename _InputIterator>
158 multiset(_InputIterator __first, _InputIterator __last)
160 { _M_t._M_insert_equal(__first, __last); }
163 * @brief Builds a %multiset from a range.
164 * @param first An input iterator.
165 * @param last An input iterator.
166 * @param comp A comparison functor.
167 * @param a An allocator object.
169 * Create a %multiset consisting of copies of the elements from
170 * [first,last). This is linear in N if the range is already sorted,
171 * and NlogN otherwise (where N is distance(first,last)).
173 template<typename _InputIterator>
174 multiset(_InputIterator __first, _InputIterator __last,
175 const _Compare& __comp,
176 const allocator_type& __a = allocator_type())
178 { _M_t._M_insert_equal(__first, __last); }
181 * @brief %Multiset copy constructor.
182 * @param x A %multiset of identical element and allocator types.
184 * The newly-created %multiset uses a copy of the allocation object used
187 multiset(const multiset& __x)
190 #ifdef __GXX_EXPERIMENTAL_CXX0X__
192 * @brief %Multiset move constructor.
193 * @param x A %multiset of identical element and allocator types.
195 * The newly-created %multiset contains the exact contents of @a x.
196 * The contents of @a x are a valid, but unspecified %multiset.
198 multiset(multiset&& __x)
199 : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
202 * @brief Builds a %multiset from an initializer_list.
203 * @param l An initializer_list.
204 * @param comp A comparison functor.
205 * @param a An allocator object.
207 * Create a %multiset consisting of copies of the elements from
208 * the list. This is linear in N if the list is already sorted,
209 * and NlogN otherwise (where N is @a l.size()).
211 multiset(initializer_list<value_type> __l,
212 const _Compare& __comp = _Compare(),
213 const allocator_type& __a = allocator_type())
215 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
219 * @brief %Multiset assignment operator.
220 * @param x A %multiset of identical element and allocator types.
222 * All the elements of @a x are copied, but unlike the copy constructor,
223 * the allocator object is not copied.
226 operator=(const multiset& __x)
232 #ifdef __GXX_EXPERIMENTAL_CXX0X__
234 * @brief %Multiset move assignment operator.
235 * @param x A %multiset of identical element and allocator types.
237 * The contents of @a x are moved into this %multiset (without copying).
238 * @a x is a valid, but unspecified %multiset.
241 operator=(multiset&& __x)
250 * @brief %Multiset list assignment operator.
251 * @param l An initializer_list.
253 * This function fills a %multiset with copies of the elements in the
254 * initializer list @a l.
256 * Note that the assignment completely changes the %multiset and
257 * that the resulting %multiset's size is the same as the number
258 * of elements assigned. Old data may be lost.
261 operator=(initializer_list<value_type> __l)
264 this->insert(__l.begin(), __l.end());
271 /// Returns the comparison object.
274 { return _M_t.key_comp(); }
275 /// Returns the comparison object.
278 { return _M_t.key_comp(); }
279 /// Returns the memory allocation object.
281 get_allocator() const
282 { return _M_t.get_allocator(); }
285 * Returns a read-only (constant) iterator that points to the first
286 * element in the %multiset. Iteration is done in ascending order
287 * according to the keys.
291 { return _M_t.begin(); }
294 * Returns a read-only (constant) iterator that points one past the last
295 * element in the %multiset. Iteration is done in ascending order
296 * according to the keys.
300 { return _M_t.end(); }
303 * Returns a read-only (constant) reverse iterator that points to the
304 * last element in the %multiset. Iteration is done in descending order
305 * according to the keys.
309 { return _M_t.rbegin(); }
312 * Returns a read-only (constant) reverse iterator that points to the
313 * last element in the %multiset. Iteration is done in descending order
314 * according to the keys.
318 { return _M_t.rend(); }
320 #ifdef __GXX_EXPERIMENTAL_CXX0X__
322 * Returns a read-only (constant) iterator that points to the first
323 * element in the %multiset. Iteration is done in ascending order
324 * according to the keys.
328 { return _M_t.begin(); }
331 * Returns a read-only (constant) iterator that points one past the last
332 * element in the %multiset. Iteration is done in ascending order
333 * according to the keys.
337 { return _M_t.end(); }
340 * Returns a read-only (constant) reverse iterator that points to the
341 * last element in the %multiset. Iteration is done in descending order
342 * according to the keys.
346 { return _M_t.rbegin(); }
349 * Returns a read-only (constant) reverse iterator that points to the
350 * last element in the %multiset. Iteration is done in descending order
351 * according to the keys.
355 { return _M_t.rend(); }
358 /// Returns true if the %set is empty.
361 { return _M_t.empty(); }
363 /// Returns the size of the %set.
366 { return _M_t.size(); }
368 /// Returns the maximum size of the %set.
371 { return _M_t.max_size(); }
374 * @brief Swaps data with another %multiset.
375 * @param x A %multiset of the same element and allocator types.
377 * This exchanges the elements between two multisets in constant time.
378 * (It is only swapping a pointer, an integer, and an instance of the @c
379 * Compare type (which itself is often stateless and empty), so it should
381 * Note that the global std::swap() function is specialized such that
382 * std::swap(s1,s2) will feed to this function.
385 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 { _M_t.swap(__x._M_t); }
394 * @brief Inserts an element into the %multiset.
395 * @param x Element to be inserted.
396 * @return An iterator that points to the inserted element.
398 * This function inserts an element into the %multiset. Contrary
399 * to a std::set the %multiset does not rely on unique keys and thus
400 * multiple copies of the same element can be inserted.
402 * Insertion requires logarithmic time.
405 insert(const value_type& __x)
406 { return _M_t._M_insert_equal(__x); }
409 * @brief Inserts an element into the %multiset.
410 * @param position An iterator that serves as a hint as to where the
411 * element should be inserted.
412 * @param x Element to be inserted.
413 * @return An iterator that points to the inserted element.
415 * This function inserts an element into the %multiset. Contrary
416 * to a std::set the %multiset does not rely on unique keys and thus
417 * multiple copies of the same element can be inserted.
419 * Note that the first parameter is only a hint and can potentially
420 * improve the performance of the insertion process. A bad hint would
421 * cause no gains in efficiency.
423 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
424 * for more on "hinting".
426 * Insertion requires logarithmic time (if the hint is not taken).
429 insert(iterator __position, const value_type& __x)
430 { return _M_t._M_insert_equal_(__position, __x); }
433 * @brief A template function that attempts to insert a range of elements.
434 * @param first Iterator pointing to the start of the range to be
436 * @param last Iterator pointing to the end of the range.
438 * Complexity similar to that of the range constructor.
440 template<typename _InputIterator>
442 insert(_InputIterator __first, _InputIterator __last)
443 { _M_t._M_insert_equal(__first, __last); }
445 #ifdef __GXX_EXPERIMENTAL_CXX0X__
447 * @brief Attempts to insert a list of elements into the %multiset.
448 * @param list A std::initializer_list<value_type> of elements
451 * Complexity similar to that of the range constructor.
454 insert(initializer_list<value_type> __l)
455 { this->insert(__l.begin(), __l.end()); }
459 * @brief Erases an element from a %multiset.
460 * @param position An iterator pointing to the element to be erased.
462 * This function erases an element, pointed to by the given iterator,
463 * from a %multiset. Note that this function only erases the element,
464 * and that if the element is itself a pointer, the pointed-to memory is
465 * not touched in any way. Managing the pointer is the user's
469 erase(iterator __position)
470 { _M_t.erase(__position); }
473 * @brief Erases elements according to the provided key.
474 * @param x Key of element to be erased.
475 * @return The number of elements erased.
477 * This function erases all elements located by the given key from a
479 * Note that this function only erases the element, and that if
480 * the element is itself a pointer, the pointed-to memory is not touched
481 * in any way. Managing the pointer is the user's responsibility.
484 erase(const key_type& __x)
485 { return _M_t.erase(__x); }
488 * @brief Erases a [first,last) range of elements from a %multiset.
489 * @param first Iterator pointing to the start of the range to be
491 * @param last Iterator pointing to the end of the range to be erased.
493 * This function erases a sequence of elements from a %multiset.
494 * Note that this function only erases the elements, and that if
495 * the elements themselves are pointers, the pointed-to memory is not
496 * touched in any way. Managing the pointer is the user's responsibility.
499 erase(iterator __first, iterator __last)
500 { _M_t.erase(__first, __last); }
503 * Erases all elements in a %multiset. Note that this function only
504 * erases the elements, and that if the elements themselves are pointers,
505 * the pointed-to memory is not touched in any way. Managing the pointer
506 * is the user's responsibility.
512 // multiset operations:
515 * @brief Finds the number of elements with given key.
516 * @param x Key of elements to be located.
517 * @return Number of elements with specified key.
520 count(const key_type& __x) const
521 { return _M_t.count(__x); }
523 // _GLIBCXX_RESOLVE_LIB_DEFECTS
524 // 214. set::find() missing const overload
527 * @brief Tries to locate an element in a %set.
528 * @param x Element to be located.
529 * @return Iterator pointing to sought-after element, or end() if not
532 * This function takes a key and tries to locate the element with which
533 * the key matches. If successful the function returns an iterator
534 * pointing to the sought after element. If unsuccessful it returns the
535 * past-the-end ( @c end() ) iterator.
538 find(const key_type& __x)
539 { return _M_t.find(__x); }
542 find(const key_type& __x) const
543 { return _M_t.find(__x); }
548 * @brief Finds the beginning of a subsequence matching given key.
549 * @param x Key to be located.
550 * @return Iterator pointing to first element equal to or greater
551 * than key, or end().
553 * This function returns the first element of a subsequence of elements
554 * that matches the given key. If unsuccessful it returns an iterator
555 * pointing to the first element that has a greater value than given key
556 * or end() if no such element exists.
559 lower_bound(const key_type& __x)
560 { return _M_t.lower_bound(__x); }
563 lower_bound(const key_type& __x) const
564 { return _M_t.lower_bound(__x); }
569 * @brief Finds the end of a subsequence matching given key.
570 * @param x Key to be located.
571 * @return Iterator pointing to the first element
572 * greater than key, or end().
575 upper_bound(const key_type& __x)
576 { return _M_t.upper_bound(__x); }
579 upper_bound(const key_type& __x) const
580 { return _M_t.upper_bound(__x); }
585 * @brief Finds a subsequence matching given key.
586 * @param x Key to be located.
587 * @return Pair of iterators that possibly points to the subsequence
588 * matching given key.
590 * This function is equivalent to
592 * std::make_pair(c.lower_bound(val),
593 * c.upper_bound(val))
595 * (but is faster than making the calls separately).
597 * This function probably only makes sense for multisets.
599 std::pair<iterator, iterator>
600 equal_range(const key_type& __x)
601 { return _M_t.equal_range(__x); }
603 std::pair<const_iterator, const_iterator>
604 equal_range(const key_type& __x) const
605 { return _M_t.equal_range(__x); }
607 template<typename _K1, typename _C1, typename _A1>
609 operator==(const multiset<_K1, _C1, _A1>&,
610 const multiset<_K1, _C1, _A1>&);
612 template<typename _K1, typename _C1, typename _A1>
614 operator< (const multiset<_K1, _C1, _A1>&,
615 const multiset<_K1, _C1, _A1>&);
619 * @brief Multiset equality comparison.
620 * @param x A %multiset.
621 * @param y A %multiset of the same type as @a x.
622 * @return True iff the size and elements of the multisets are equal.
624 * This is an equivalence relation. It is linear in the size of the
626 * Multisets are considered equivalent if their sizes are equal, and if
627 * corresponding elements compare equal.
629 template<typename _Key, typename _Compare, typename _Alloc>
631 operator==(const multiset<_Key, _Compare, _Alloc>& __x,
632 const multiset<_Key, _Compare, _Alloc>& __y)
633 { return __x._M_t == __y._M_t; }
636 * @brief Multiset ordering relation.
637 * @param x A %multiset.
638 * @param y A %multiset of the same type as @a x.
639 * @return True iff @a x is lexicographically less than @a y.
641 * This is a total ordering relation. It is linear in the size of the
642 * maps. The elements must be comparable with @c <.
644 * See std::lexicographical_compare() for how the determination is made.
646 template<typename _Key, typename _Compare, typename _Alloc>
648 operator<(const multiset<_Key, _Compare, _Alloc>& __x,
649 const multiset<_Key, _Compare, _Alloc>& __y)
650 { return __x._M_t < __y._M_t; }
652 /// Returns !(x == y).
653 template<typename _Key, typename _Compare, typename _Alloc>
655 operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
656 const multiset<_Key, _Compare, _Alloc>& __y)
657 { return !(__x == __y); }
660 template<typename _Key, typename _Compare, typename _Alloc>
662 operator>(const multiset<_Key,_Compare,_Alloc>& __x,
663 const multiset<_Key,_Compare,_Alloc>& __y)
664 { return __y < __x; }
667 template<typename _Key, typename _Compare, typename _Alloc>
669 operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
670 const multiset<_Key, _Compare, _Alloc>& __y)
671 { return !(__y < __x); }
674 template<typename _Key, typename _Compare, typename _Alloc>
676 operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
677 const multiset<_Key, _Compare, _Alloc>& __y)
678 { return !(__x < __y); }
680 /// See std::multiset::swap().
681 template<typename _Key, typename _Compare, typename _Alloc>
683 swap(multiset<_Key, _Compare, _Alloc>& __x,
684 multiset<_Key, _Compare, _Alloc>& __y)
687 #ifdef __GXX_EXPERIMENTAL_CXX0X__
688 template<typename _Key, typename _Compare, typename _Alloc>
690 swap(multiset<_Key, _Compare, _Alloc>&& __x,
691 multiset<_Key, _Compare, _Alloc>& __y)
694 template<typename _Key, typename _Compare, typename _Alloc>
696 swap(multiset<_Key, _Compare, _Alloc>& __x,
697 multiset<_Key, _Compare, _Alloc>&& __y)
701 _GLIBCXX_END_NESTED_NAMESPACE
703 #endif /* _STL_MULTISET_H */