1 // Multiset implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_multiset.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_MULTISET_H
58 #define _STL_MULTISET_H 1
60 #include <bits/concept_check.h>
61 #include <initializer_list>
63 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
66 * @brief A standard container made up of elements, which can be retrieved
67 * in logarithmic time.
69 * @ingroup associative_containers
71 * Meets the requirements of a <a href="tables.html#65">container</a>, a
72 * <a href="tables.html#66">reversible container</a>, and an
73 * <a href="tables.html#69">associative container</a> (using equivalent
74 * keys). For a @c multiset<Key> the key_type and value_type are Key.
76 * Multisets support bidirectional iterators.
78 * The private tree data is declared exactly the same way for set and
79 * multiset; the distinction is made entirely in how the tree functions are
80 * called (*_unique versus *_equal, same as the standard).
82 template <typename _Key
, typename _Compare
= std::less
<_Key
>,
83 typename _Alloc
= std::allocator
<_Key
> >
86 // concept requirements
87 typedef typename
_Alloc::value_type _Alloc_value_type
;
88 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
89 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
90 _BinaryFunctionConcept
)
91 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
95 typedef _Key key_type
;
96 typedef _Key value_type
;
97 typedef _Compare key_compare
;
98 typedef _Compare value_compare
;
99 typedef _Alloc allocator_type
;
102 /// This turns a red-black tree into a [multi]set.
103 typedef typename
_Alloc::template rebind
<_Key
>::other _Key_alloc_type
;
105 typedef _Rb_tree
<key_type
, value_type
, _Identity
<value_type
>,
106 key_compare
, _Key_alloc_type
> _Rep_type
;
107 /// The actual tree structure.
111 typedef typename
_Key_alloc_type::pointer pointer
;
112 typedef typename
_Key_alloc_type::const_pointer const_pointer
;
113 typedef typename
_Key_alloc_type::reference reference
;
114 typedef typename
_Key_alloc_type::const_reference const_reference
;
115 // _GLIBCXX_RESOLVE_LIB_DEFECTS
116 // DR 103. set::iterator is required to be modifiable,
117 // but this allows modification of keys.
118 typedef typename
_Rep_type::const_iterator iterator
;
119 typedef typename
_Rep_type::const_iterator const_iterator
;
120 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
121 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
122 typedef typename
_Rep_type::size_type size_type
;
123 typedef typename
_Rep_type::difference_type difference_type
;
125 // allocation/deallocation
127 * @brief Default constructor creates no elements.
133 * @brief Creates a %multiset with no elements.
134 * @param comp Comparator to use.
135 * @param a An allocator object.
138 multiset(const _Compare
& __comp
,
139 const allocator_type
& __a
= allocator_type())
140 : _M_t(__comp
, __a
) { }
143 * @brief Builds a %multiset from a range.
144 * @param first An input iterator.
145 * @param last An input iterator.
147 * Create a %multiset consisting of copies of the elements from
148 * [first,last). This is linear in N if the range is already sorted,
149 * and NlogN otherwise (where N is distance(first,last)).
151 template<typename _InputIterator
>
152 multiset(_InputIterator __first
, _InputIterator __last
)
154 { _M_t
._M_insert_equal(__first
, __last
); }
157 * @brief Builds a %multiset from a range.
158 * @param first An input iterator.
159 * @param last An input iterator.
160 * @param comp A comparison functor.
161 * @param a An allocator object.
163 * Create a %multiset consisting of copies of the elements from
164 * [first,last). This is linear in N if the range is already sorted,
165 * and NlogN otherwise (where N is distance(first,last)).
167 template<typename _InputIterator
>
168 multiset(_InputIterator __first
, _InputIterator __last
,
169 const _Compare
& __comp
,
170 const allocator_type
& __a
= allocator_type())
172 { _M_t
._M_insert_equal(__first
, __last
); }
175 * @brief %Multiset copy constructor.
176 * @param x A %multiset of identical element and allocator types.
178 * The newly-created %multiset uses a copy of the allocation object used
181 multiset(const multiset
& __x
)
184 #ifdef __GXX_EXPERIMENTAL_CXX0X__
186 * @brief %Multiset move constructor.
187 * @param x A %multiset of identical element and allocator types.
189 * The newly-created %multiset contains the exact contents of @a x.
190 * The contents of @a x are a valid, but unspecified %multiset.
192 multiset(multiset
&& __x
)
193 : _M_t(std::forward
<_Rep_type
>(__x
._M_t
)) { }
196 * @brief Builds a %multiset from an initializer_list.
197 * @param l An initializer_list.
198 * @param comp A comparison functor.
199 * @param a An allocator object.
201 * Create a %multiset consisting of copies of the elements from
202 * the list. This is linear in N if the list is already sorted,
203 * and NlogN otherwise (where N is @a l.size()).
205 multiset(initializer_list
<value_type
> __l
,
206 const _Compare
& __comp
= _Compare(),
207 const allocator_type
& __a
= allocator_type())
209 { _M_t
._M_insert_equal(__l
.begin(), __l
.end()); }
213 * @brief %Multiset assignment operator.
214 * @param x A %multiset of identical element and allocator types.
216 * All the elements of @a x are copied, but unlike the copy constructor,
217 * the allocator object is not copied.
220 operator=(const multiset
& __x
)
226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
228 * @brief %Multiset move assignment operator.
229 * @param x A %multiset of identical element and allocator types.
231 * The contents of @a x are moved into this %multiset (without copying).
232 * @a x is a valid, but unspecified %multiset.
235 operator=(multiset
&& __x
)
245 * @brief %Multiset list assignment operator.
246 * @param l An initializer_list.
248 * This function fills a %multiset with copies of the elements in the
249 * initializer list @a l.
251 * Note that the assignment completely changes the %multiset and
252 * that the resulting %multiset's size is the same as the number
253 * of elements assigned. Old data may be lost.
256 operator=(initializer_list
<value_type
> __l
)
259 this->insert(__l
.begin(), __l
.end());
266 /// Returns the comparison object.
269 { return _M_t
.key_comp(); }
270 /// Returns the comparison object.
273 { return _M_t
.key_comp(); }
274 /// Returns the memory allocation object.
276 get_allocator() const
277 { return _M_t
.get_allocator(); }
280 * Returns a read-only (constant) iterator that points to the first
281 * element in the %multiset. Iteration is done in ascending order
282 * according to the keys.
286 { return _M_t
.begin(); }
289 * Returns a read-only (constant) iterator that points one past the last
290 * element in the %multiset. Iteration is done in ascending order
291 * according to the keys.
295 { return _M_t
.end(); }
298 * Returns a read-only (constant) reverse iterator that points to the
299 * last element in the %multiset. Iteration is done in descending order
300 * according to the keys.
304 { return _M_t
.rbegin(); }
307 * Returns a read-only (constant) reverse iterator that points to the
308 * last element in the %multiset. Iteration is done in descending order
309 * according to the keys.
313 { return _M_t
.rend(); }
315 #ifdef __GXX_EXPERIMENTAL_CXX0X__
317 * Returns a read-only (constant) iterator that points to the first
318 * element in the %multiset. Iteration is done in ascending order
319 * according to the keys.
323 { return _M_t
.begin(); }
326 * Returns a read-only (constant) iterator that points one past the last
327 * element in the %multiset. Iteration is done in ascending order
328 * according to the keys.
332 { return _M_t
.end(); }
335 * Returns a read-only (constant) reverse iterator that points to the
336 * last element in the %multiset. Iteration is done in descending order
337 * according to the keys.
341 { return _M_t
.rbegin(); }
344 * Returns a read-only (constant) reverse iterator that points to the
345 * last element in the %multiset. Iteration is done in descending order
346 * according to the keys.
350 { return _M_t
.rend(); }
353 /// Returns true if the %set is empty.
356 { return _M_t
.empty(); }
358 /// Returns the size of the %set.
361 { return _M_t
.size(); }
363 /// Returns the maximum size of the %set.
366 { return _M_t
.max_size(); }
369 * @brief Swaps data with another %multiset.
370 * @param x A %multiset of the same element and allocator types.
372 * This exchanges the elements between two multisets in constant time.
373 * (It is only swapping a pointer, an integer, and an instance of the @c
374 * Compare type (which itself is often stateless and empty), so it should
376 * Note that the global std::swap() function is specialized such that
377 * std::swap(s1,s2) will feed to this function.
381 { _M_t
.swap(__x
._M_t
); }
385 * @brief Inserts an element into the %multiset.
386 * @param x Element to be inserted.
387 * @return An iterator that points to the inserted element.
389 * This function inserts an element into the %multiset. Contrary
390 * to a std::set the %multiset does not rely on unique keys and thus
391 * multiple copies of the same element can be inserted.
393 * Insertion requires logarithmic time.
396 insert(const value_type
& __x
)
397 { return _M_t
._M_insert_equal(__x
); }
400 * @brief Inserts an element into the %multiset.
401 * @param position An iterator that serves as a hint as to where the
402 * element should be inserted.
403 * @param x Element to be inserted.
404 * @return An iterator that points to the inserted element.
406 * This function inserts an element into the %multiset. Contrary
407 * to a std::set the %multiset does not rely on unique keys and thus
408 * multiple copies of the same element can be inserted.
410 * Note that the first parameter is only a hint and can potentially
411 * improve the performance of the insertion process. A bad hint would
412 * cause no gains in efficiency.
414 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
415 * for more on @a hinting.
417 * Insertion requires logarithmic time (if the hint is not taken).
420 insert(iterator __position
, const value_type
& __x
)
421 { return _M_t
._M_insert_equal_(__position
, __x
); }
424 * @brief A template function that tries to insert a range of elements.
425 * @param first Iterator pointing to the start of the range to be
427 * @param last Iterator pointing to the end of the range.
429 * Complexity similar to that of the range constructor.
431 template<typename _InputIterator
>
433 insert(_InputIterator __first
, _InputIterator __last
)
434 { _M_t
._M_insert_equal(__first
, __last
); }
436 #ifdef __GXX_EXPERIMENTAL_CXX0X__
438 * @brief Attempts to insert a list of elements into the %multiset.
439 * @param list A std::initializer_list<value_type> of elements
442 * Complexity similar to that of the range constructor.
445 insert(initializer_list
<value_type
> __l
)
446 { this->insert(__l
.begin(), __l
.end()); }
449 #ifdef __GXX_EXPERIMENTAL_CXX0X__
450 // _GLIBCXX_RESOLVE_LIB_DEFECTS
451 // DR 130. Associative erase should return an iterator.
453 * @brief Erases an element from a %multiset.
454 * @param position An iterator pointing to the element to be erased.
455 * @return An iterator pointing to the element immediately following
456 * @a position prior to the element being erased. If no such
457 * element exists, end() is returned.
459 * This function erases an element, pointed to by the given iterator,
460 * from a %multiset. Note that this function only erases the element,
461 * and that if the element is itself a pointer, the pointed-to memory is
462 * not touched in any way. Managing the pointer is the user's
466 erase(iterator __position
)
467 { return _M_t
.erase(__position
); }
470 * @brief Erases an element from a %multiset.
471 * @param position An iterator pointing to the element to be erased.
473 * This function erases an element, pointed to by the given iterator,
474 * from a %multiset. Note that this function only erases the element,
475 * and that if the element is itself a pointer, the pointed-to memory is
476 * not touched in any way. Managing the pointer is the user's
480 erase(iterator __position
)
481 { _M_t
.erase(__position
); }
485 * @brief Erases elements according to the provided key.
486 * @param x Key of element to be erased.
487 * @return The number of elements erased.
489 * This function erases all elements located by the given key from a
491 * Note that this function only erases the element, and that if
492 * the element is itself a pointer, the pointed-to memory is not touched
493 * in any way. Managing the pointer is the user's responsibility.
496 erase(const key_type
& __x
)
497 { return _M_t
.erase(__x
); }
499 #ifdef __GXX_EXPERIMENTAL_CXX0X__
500 // _GLIBCXX_RESOLVE_LIB_DEFECTS
501 // DR 130. Associative erase should return an iterator.
503 * @brief Erases a [first,last) range of elements from a %multiset.
504 * @param first Iterator pointing to the start of the range to be
506 * @param last Iterator pointing to the end of the range to be erased.
507 * @return The iterator @a last.
509 * This function erases a sequence of elements from a %multiset.
510 * Note that this function only erases the elements, and that if
511 * the elements themselves are pointers, the pointed-to memory is not
512 * touched in any way. Managing the pointer is the user's responsibility.
515 erase(iterator __first
, iterator __last
)
516 { return _M_t
.erase(__first
, __last
); }
519 * @brief Erases a [first,last) range of elements from a %multiset.
520 * @param first Iterator pointing to the start of the range to be
522 * @param last Iterator pointing to the end of the range to be erased.
524 * This function erases a sequence of elements from a %multiset.
525 * Note that this function only erases the elements, and that if
526 * the elements themselves are pointers, the pointed-to memory is not
527 * touched in any way. Managing the pointer is the user's responsibility.
530 erase(iterator __first
, iterator __last
)
531 { _M_t
.erase(__first
, __last
); }
535 * Erases all elements in a %multiset. Note that this function only
536 * erases the elements, and that if the elements themselves are pointers,
537 * the pointed-to memory is not touched in any way. Managing the pointer
538 * is the user's responsibility.
544 // multiset operations:
547 * @brief Finds the number of elements with given key.
548 * @param x Key of elements to be located.
549 * @return Number of elements with specified key.
552 count(const key_type
& __x
) const
553 { return _M_t
.count(__x
); }
555 // _GLIBCXX_RESOLVE_LIB_DEFECTS
556 // 214. set::find() missing const overload
559 * @brief Tries to locate an element in a %set.
560 * @param x Element to be located.
561 * @return Iterator pointing to sought-after element, or end() if not
564 * This function takes a key and tries to locate the element with which
565 * the key matches. If successful the function returns an iterator
566 * pointing to the sought after element. If unsuccessful it returns the
567 * past-the-end ( @c end() ) iterator.
570 find(const key_type
& __x
)
571 { return _M_t
.find(__x
); }
574 find(const key_type
& __x
) const
575 { return _M_t
.find(__x
); }
580 * @brief Finds the beginning of a subsequence matching given key.
581 * @param x Key to be located.
582 * @return Iterator pointing to first element equal to or greater
583 * than key, or end().
585 * This function returns the first element of a subsequence of elements
586 * that matches the given key. If unsuccessful it returns an iterator
587 * pointing to the first element that has a greater value than given key
588 * or end() if no such element exists.
591 lower_bound(const key_type
& __x
)
592 { return _M_t
.lower_bound(__x
); }
595 lower_bound(const key_type
& __x
) const
596 { return _M_t
.lower_bound(__x
); }
601 * @brief Finds the end of a subsequence matching given key.
602 * @param x Key to be located.
603 * @return Iterator pointing to the first element
604 * greater than key, or end().
607 upper_bound(const key_type
& __x
)
608 { return _M_t
.upper_bound(__x
); }
611 upper_bound(const key_type
& __x
) const
612 { return _M_t
.upper_bound(__x
); }
617 * @brief Finds a subsequence matching given key.
618 * @param x Key to be located.
619 * @return Pair of iterators that possibly points to the subsequence
620 * matching given key.
622 * This function is equivalent to
624 * std::make_pair(c.lower_bound(val),
625 * c.upper_bound(val))
627 * (but is faster than making the calls separately).
629 * This function probably only makes sense for multisets.
631 std::pair
<iterator
, iterator
>
632 equal_range(const key_type
& __x
)
633 { return _M_t
.equal_range(__x
); }
635 std::pair
<const_iterator
, const_iterator
>
636 equal_range(const key_type
& __x
) const
637 { return _M_t
.equal_range(__x
); }
639 template<typename _K1
, typename _C1
, typename _A1
>
641 operator==(const multiset
<_K1
, _C1
, _A1
>&,
642 const multiset
<_K1
, _C1
, _A1
>&);
644 template<typename _K1
, typename _C1
, typename _A1
>
646 operator< (const multiset
<_K1
, _C1
, _A1
>&,
647 const multiset
<_K1
, _C1
, _A1
>&);
651 * @brief Multiset equality comparison.
652 * @param x A %multiset.
653 * @param y A %multiset of the same type as @a x.
654 * @return True iff the size and elements of the multisets are equal.
656 * This is an equivalence relation. It is linear in the size of the
658 * Multisets are considered equivalent if their sizes are equal, and if
659 * corresponding elements compare equal.
661 template<typename _Key
, typename _Compare
, typename _Alloc
>
663 operator==(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
664 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
665 { return __x
._M_t
== __y
._M_t
; }
668 * @brief Multiset ordering relation.
669 * @param x A %multiset.
670 * @param y A %multiset of the same type as @a x.
671 * @return True iff @a x is lexicographically less than @a y.
673 * This is a total ordering relation. It is linear in the size of the
674 * maps. The elements must be comparable with @c <.
676 * See std::lexicographical_compare() for how the determination is made.
678 template<typename _Key
, typename _Compare
, typename _Alloc
>
680 operator<(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
681 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
682 { return __x
._M_t
< __y
._M_t
; }
684 /// Returns !(x == y).
685 template<typename _Key
, typename _Compare
, typename _Alloc
>
687 operator!=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
688 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
689 { return !(__x
== __y
); }
692 template<typename _Key
, typename _Compare
, typename _Alloc
>
694 operator>(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
695 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
696 { return __y
< __x
; }
699 template<typename _Key
, typename _Compare
, typename _Alloc
>
701 operator<=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
702 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
703 { return !(__y
< __x
); }
706 template<typename _Key
, typename _Compare
, typename _Alloc
>
708 operator>=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
709 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
710 { return !(__x
< __y
); }
712 /// See std::multiset::swap().
713 template<typename _Key
, typename _Compare
, typename _Alloc
>
715 swap(multiset
<_Key
, _Compare
, _Alloc
>& __x
,
716 multiset
<_Key
, _Compare
, _Alloc
>& __y
)
719 _GLIBCXX_END_NESTED_NAMESPACE
721 #endif /* _STL_MULTISET_H */