1 // Multiset implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD
)
70 * @brief A standard container made up of elements, which can be retrieved
71 * in logarithmic time.
74 * @ingroup Assoc_containers
76 * Meets the requirements of a <a href="tables.html#65">container</a>, a
77 * <a href="tables.html#66">reversible container</a>, and an
78 * <a href="tables.html#69">associative container</a> (using equivalent
79 * keys). For a @c multiset<Key> the key_type and value_type are Key.
81 * 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).
89 template <class _Key
, class _Compare
= std::less
<_Key
>,
90 class _Alloc
= std::allocator
<_Key
> >
93 // concept requirements
94 typedef typename
_Alloc::value_type _Alloc_value_type
;
95 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
96 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
97 _BinaryFunctionConcept
)
98 __glibcxx_class_requires2(_Key
, _Alloc_value_type
, _SameTypeConcept
)
102 typedef _Key key_type
;
103 typedef _Key value_type
;
104 typedef _Compare key_compare
;
105 typedef _Compare value_compare
;
106 typedef _Alloc allocator_type
;
109 /// @if maint This turns a red-black tree into a [multi]set. @endif
110 typedef typename
_Alloc::template rebind
<_Key
>::other _Key_alloc_type
;
112 typedef _Rb_tree
<key_type
, value_type
, _Identity
<value_type
>,
113 key_compare
, _Key_alloc_type
> _Rep_type
;
114 /// @if maint The actual tree structure. @endif
118 typedef typename
_Key_alloc_type::pointer pointer
;
119 typedef typename
_Key_alloc_type::const_pointer const_pointer
;
120 typedef typename
_Key_alloc_type::reference reference
;
121 typedef typename
_Key_alloc_type::const_reference const_reference
;
122 // _GLIBCXX_RESOLVE_LIB_DEFECTS
123 // DR 103. set::iterator is required to be modifiable,
124 // but this allows modification of keys.
125 typedef typename
_Rep_type::const_iterator iterator
;
126 typedef typename
_Rep_type::const_iterator const_iterator
;
127 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
128 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
129 typedef typename
_Rep_type::size_type size_type
;
130 typedef typename
_Rep_type::difference_type difference_type
;
132 // allocation/deallocation
135 * @brief Default constructor creates no elements.
138 : _M_t(_Compare(), allocator_type()) { }
141 multiset(const _Compare
& __comp
,
142 const allocator_type
& __a
= allocator_type())
143 : _M_t(__comp
, __a
) { }
146 * @brief Builds a %multiset from a range.
147 * @param first An input iterator.
148 * @param last An input iterator.
150 * Create a %multiset consisting of copies of the elements from
151 * [first,last). This is linear in N if the range is already sorted,
152 * and NlogN otherwise (where N is distance(first,last)).
154 template <class _InputIterator
>
155 multiset(_InputIterator __first
, _InputIterator __last
)
156 : _M_t(_Compare(), allocator_type())
157 { _M_t
._M_insert_equal(__first
, __last
); }
160 * @brief Builds a %multiset from a range.
161 * @param first An input iterator.
162 * @param last An input iterator.
163 * @param comp A comparison functor.
164 * @param a An allocator object.
166 * Create a %multiset consisting of copies of the elements from
167 * [first,last). This is linear in N if the range is already sorted,
168 * and NlogN otherwise (where N is distance(first,last)).
170 template <class _InputIterator
>
171 multiset(_InputIterator __first
, _InputIterator __last
,
172 const _Compare
& __comp
,
173 const allocator_type
& __a
= allocator_type())
175 { _M_t
._M_insert_equal(__first
, __last
); }
178 * @brief %Multiset copy constructor.
179 * @param x A %multiset of identical element and allocator types.
181 * The newly-created %multiset uses a copy of the allocation object used
184 multiset(const multiset
<_Key
,_Compare
,_Alloc
>& __x
)
188 * @brief %Multiset assignment operator.
189 * @param x A %multiset of identical element and allocator types.
191 * All the elements of @a x are copied, but unlike the copy constructor,
192 * the allocator object is not copied.
194 multiset
<_Key
,_Compare
,_Alloc
>&
195 operator=(const multiset
<_Key
,_Compare
,_Alloc
>& __x
)
203 /// Returns the comparison object.
206 { return _M_t
.key_comp(); }
207 /// Returns the comparison object.
210 { return _M_t
.key_comp(); }
211 /// Returns the memory allocation object.
213 get_allocator() const
214 { return _M_t
.get_allocator(); }
217 * Returns a read/write iterator that points to the first element in the
218 * %multiset. Iteration is done in ascending order according to the
223 { return _M_t
.begin(); }
226 * Returns a read/write iterator that points one past the last element in
227 * the %multiset. Iteration is done in ascending order according to the
232 { return _M_t
.end(); }
235 * Returns a read/write reverse iterator that points to the last element
236 * in the %multiset. Iteration is done in descending order according to
241 { return _M_t
.rbegin(); }
244 * Returns a read/write reverse iterator that points to the last element
245 * in the %multiset. Iteration is done in descending order according to
250 { return _M_t
.rend(); }
252 /// Returns true if the %set is empty.
255 { return _M_t
.empty(); }
257 /// Returns the size of the %set.
260 { return _M_t
.size(); }
262 /// Returns the maximum size of the %set.
265 { return _M_t
.max_size(); }
268 * @brief Swaps data with another %multiset.
269 * @param x A %multiset of the same element and allocator types.
271 * This exchanges the elements between two multisets in constant time.
272 * (It is only swapping a pointer, an integer, and an instance of the @c
273 * Compare type (which itself is often stateless and empty), so it should
275 * Note that the global std::swap() function is specialized such that
276 * std::swap(s1,s2) will feed to this function.
279 swap(multiset
<_Key
, _Compare
, _Alloc
>& __x
)
280 { _M_t
.swap(__x
._M_t
); }
284 * @brief Inserts an element into the %multiset.
285 * @param x Element to be inserted.
286 * @return An iterator that points to the inserted element.
288 * This function inserts an element into the %multiset. Contrary
289 * to a std::set the %multiset does not rely on unique keys and thus
290 * multiple copies of the same element can be inserted.
292 * Insertion requires logarithmic time.
295 insert(const value_type
& __x
)
296 { return _M_t
._M_insert_equal(__x
); }
299 * @brief Inserts an element into the %multiset.
300 * @param position An iterator that serves as a hint as to where the
301 * element should be inserted.
302 * @param x Element to be inserted.
303 * @return An iterator that points to the inserted element.
305 * This function inserts an element into the %multiset. Contrary
306 * to a std::set the %multiset does not rely on unique keys and thus
307 * multiple copies of the same element can be inserted.
309 * Note that the first parameter is only a hint and can potentially
310 * improve the performance of the insertion process. A bad hint would
311 * cause no gains in efficiency.
313 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
314 * for more on "hinting".
316 * Insertion requires logarithmic time (if the hint is not taken).
319 insert(iterator __position
, const value_type
& __x
)
320 { return _M_t
._M_insert_equal_(__position
, __x
); }
323 * @brief A template function that attemps to insert a range of elements.
324 * @param first Iterator pointing to the start of the range to be
326 * @param last Iterator pointing to the end of the range.
328 * Complexity similar to that of the range constructor.
330 template <class _InputIterator
>
332 insert(_InputIterator __first
, _InputIterator __last
)
333 { _M_t
._M_insert_equal(__first
, __last
); }
336 * @brief Erases an element from a %multiset.
337 * @param position An iterator pointing to the element to be erased.
339 * This function erases an element, pointed to by the given iterator,
340 * from a %multiset. Note that this function only erases the element,
341 * and that if the element is itself a pointer, the pointed-to memory is
342 * not touched in any way. Managing the pointer is the user's
346 erase(iterator __position
)
347 { _M_t
.erase(__position
); }
350 * @brief Erases elements according to the provided key.
351 * @param x Key of element to be erased.
352 * @return The number of elements erased.
354 * This function erases all elements located by the given key from a
356 * Note that this function only erases the element, and that if
357 * the element is itself a pointer, the pointed-to memory is not touched
358 * in any way. Managing the pointer is the user's responsibilty.
361 erase(const key_type
& __x
)
362 { return _M_t
.erase(__x
); }
365 * @brief Erases a [first,last) range of elements from a %multiset.
366 * @param first Iterator pointing to the start of the range to be
368 * @param last Iterator pointing to the end of the range to be erased.
370 * This function erases a sequence of elements from a %multiset.
371 * Note that this function only erases the elements, and that if
372 * the elements themselves are pointers, the pointed-to memory is not
373 * touched in any way. Managing the pointer is the user's responsibilty.
376 erase(iterator __first
, iterator __last
)
377 { _M_t
.erase(__first
, __last
); }
380 * Erases all elements in a %multiset. Note that this function only
381 * erases the elements, and that if the elements themselves are pointers,
382 * the pointed-to memory is not touched in any way. Managing the pointer
383 * is the user's responsibilty.
389 // multiset operations:
392 * @brief Finds the number of elements with given key.
393 * @param x Key of elements to be located.
394 * @return Number of elements with specified key.
397 count(const key_type
& __x
) const
398 { return _M_t
.count(__x
); }
400 // _GLIBCXX_RESOLVE_LIB_DEFECTS
401 // 214. set::find() missing const overload
404 * @brief Tries to locate an element in a %set.
405 * @param x Element to be located.
406 * @return Iterator pointing to sought-after element, or end() if not
409 * This function takes a key and tries to locate the element with which
410 * the key matches. If successful the function returns an iterator
411 * pointing to the sought after element. If unsuccessful it returns the
412 * past-the-end ( @c end() ) iterator.
415 find(const key_type
& __x
)
416 { return _M_t
.find(__x
); }
419 find(const key_type
& __x
) const
420 { return _M_t
.find(__x
); }
425 * @brief Finds the beginning of a subsequence matching given key.
426 * @param x Key to be located.
427 * @return Iterator pointing to first element equal to or greater
428 * than key, or end().
430 * This function returns the first element of a subsequence of elements
431 * that matches the given key. If unsuccessful it returns an iterator
432 * pointing to the first element that has a greater value than given key
433 * or end() if no such element exists.
436 lower_bound(const key_type
& __x
)
437 { return _M_t
.lower_bound(__x
); }
440 lower_bound(const key_type
& __x
) const
441 { return _M_t
.lower_bound(__x
); }
446 * @brief Finds the end of a subsequence matching given key.
447 * @param x Key to be located.
448 * @return Iterator pointing to the first element
449 * greater than key, or end().
452 upper_bound(const key_type
& __x
)
453 { return _M_t
.upper_bound(__x
); }
456 upper_bound(const key_type
& __x
) const
457 { return _M_t
.upper_bound(__x
); }
462 * @brief Finds a subsequence matching given key.
463 * @param x Key to be located.
464 * @return Pair of iterators that possibly points to the subsequence
465 * matching given key.
467 * This function is equivalent to
469 * std::make_pair(c.lower_bound(val),
470 * c.upper_bound(val))
472 * (but is faster than making the calls separately).
474 * This function probably only makes sense for multisets.
476 std::pair
<iterator
, iterator
>
477 equal_range(const key_type
& __x
)
478 { return _M_t
.equal_range(__x
); }
480 std::pair
<const_iterator
, const_iterator
>
481 equal_range(const key_type
& __x
) const
482 { return _M_t
.equal_range(__x
); }
484 template <class _K1
, class _C1
, class _A1
>
486 operator== (const multiset
<_K1
, _C1
, _A1
>&,
487 const multiset
<_K1
, _C1
, _A1
>&);
489 template <class _K1
, class _C1
, class _A1
>
491 operator< (const multiset
<_K1
, _C1
, _A1
>&,
492 const multiset
<_K1
, _C1
, _A1
>&);
496 * @brief Multiset equality comparison.
497 * @param x A %multiset.
498 * @param y A %multiset of the same type as @a x.
499 * @return True iff the size and elements of the multisets are equal.
501 * This is an equivalence relation. It is linear in the size of the
503 * Multisets are considered equivalent if their sizes are equal, and if
504 * corresponding elements compare equal.
506 template <class _Key
, class _Compare
, class _Alloc
>
508 operator==(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
509 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
510 { return __x
._M_t
== __y
._M_t
; }
513 * @brief Multiset ordering relation.
514 * @param x A %multiset.
515 * @param y A %multiset of the same type as @a x.
516 * @return True iff @a x is lexicographically less than @a y.
518 * This is a total ordering relation. It is linear in the size of the
519 * maps. The elements must be comparable with @c <.
521 * See std::lexicographical_compare() for how the determination is made.
523 template <class _Key
, class _Compare
, class _Alloc
>
525 operator<(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
526 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
527 { return __x
._M_t
< __y
._M_t
; }
529 /// Returns !(x == y).
530 template <class _Key
, class _Compare
, class _Alloc
>
532 operator!=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
533 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
534 { return !(__x
== __y
); }
537 template <class _Key
, class _Compare
, class _Alloc
>
539 operator>(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
540 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
541 { return __y
< __x
; }
544 template <class _Key
, class _Compare
, class _Alloc
>
546 operator<=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
547 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
548 { return !(__y
< __x
); }
551 template <class _Key
, class _Compare
, class _Alloc
>
553 operator>=(const multiset
<_Key
, _Compare
, _Alloc
>& __x
,
554 const multiset
<_Key
, _Compare
, _Alloc
>& __y
)
555 { return !(__x
< __y
); }
557 /// See std::multiset::swap().
558 template <class _Key
, class _Compare
, class _Alloc
>
560 swap(multiset
<_Key
, _Compare
, _Alloc
>& __x
,
561 multiset
<_Key
, _Compare
, _Alloc
>& __y
)
564 _GLIBCXX_END_NESTED_NAMESPACE
566 #endif /* _STL_MULTISET_H */