1 // Multiset implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file stl_multiset.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
69 // Forward declaration of operators < and ==, needed for friend declaration.
70 template <class _Key
, class _Compare
= less
<_Key
>,
71 class _Alloc
= allocator
<_Key
> >
74 template <class _Key
, class _Compare
, class _Alloc
>
76 operator==(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
77 const multiset
<_Key
,_Compare
,_Alloc
>& __y
);
79 template <class _Key
, class _Compare
, class _Alloc
>
81 operator<(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
82 const multiset
<_Key
,_Compare
,_Alloc
>& __y
);
85 * @brief A standard container made up of elements, which can be retrieved
86 * in logarithmic time.
89 * @ingroup Assoc_containers
91 * Meets the requirements of a <a href="tables.html#65">container</a>, a
92 * <a href="tables.html#66">reversible container</a>, and an
93 * <a href="tables.html#69">associative container</a> (using equivalent
94 * keys). For a @c multiset<Key> the key_type and value_type are Key.
96 * Multisets support bidirectional iterators.
99 * The private tree data is declared exactly the same way for set and
100 * multiset; the distinction is made entirely in how the tree functions are
101 * called (*_unique versus *_equal, same as the standard).
104 template <class _Key
, class _Compare
, class _Alloc
>
107 // concept requirements
108 __glibcxx_class_requires(_Key
, _SGIAssignableConcept
)
109 __glibcxx_class_requires4(_Compare
, bool, _Key
, _Key
,
110 _BinaryFunctionConcept
)
114 typedef _Key key_type
;
115 typedef _Key value_type
;
116 typedef _Compare key_compare
;
117 typedef _Compare value_compare
;
120 /// @if maint This turns a red-black tree into a [multi]set. @endif
121 typedef _Rb_tree
<key_type
, value_type
,
122 _Identity
<value_type
>, key_compare
, _Alloc
> _Rep_type
;
123 /// @if maint The actual tree structure. @endif
127 typedef typename
_Alloc::pointer pointer
;
128 typedef typename
_Alloc::const_pointer const_pointer
;
129 typedef typename
_Alloc::reference reference
;
130 typedef typename
_Alloc::const_reference const_reference
;
131 typedef typename
_Rep_type::const_iterator iterator
;
132 typedef typename
_Rep_type::const_iterator const_iterator
;
133 typedef typename
_Rep_type::const_reverse_iterator reverse_iterator
;
134 typedef typename
_Rep_type::const_reverse_iterator const_reverse_iterator
;
135 typedef typename
_Rep_type::size_type size_type
;
136 typedef typename
_Rep_type::difference_type difference_type
;
137 typedef typename
_Rep_type::allocator_type allocator_type
;
139 // allocation/deallocation
142 * @brief Default constructor creates no elements.
145 : _M_t(_Compare(), allocator_type()) { }
148 multiset(const _Compare
& __comp
,
149 const allocator_type
& __a
= allocator_type())
150 : _M_t(__comp
, __a
) { }
153 * @brief Builds a %multiset from a range.
154 * @param first An input iterator.
155 * @param last An input iterator.
157 * Create a %multiset consisting of copies of the elements from
158 * [first,last). This is linear in N if the range is already sorted,
159 * and NlogN otherwise (where N is distance(first,last)).
161 template <class _InputIterator
>
162 multiset(_InputIterator __first
, _InputIterator __last
)
163 : _M_t(_Compare(), allocator_type())
164 { _M_t
.insert_equal(__first
, __last
); }
167 * @brief Builds a %multiset from a range.
168 * @param first An input iterator.
169 * @param last An input iterator.
170 * @param comp A comparison functor.
171 * @param a An allocator object.
173 * Create a %multiset consisting of copies of the elements from
174 * [first,last). This is linear in N if the range is already sorted,
175 * and NlogN otherwise (where N is distance(first,last)).
177 template <class _InputIterator
>
178 multiset(_InputIterator __first
, _InputIterator __last
,
179 const _Compare
& __comp
,
180 const allocator_type
& __a
= allocator_type())
182 { _M_t
.insert_equal(__first
, __last
); }
185 * @brief %Multiset copy constructor.
186 * @param x A %multiset of identical element and allocator types.
188 * The newly-created %multiset uses a copy of the allocation object used
191 multiset(const multiset
<_Key
,_Compare
,_Alloc
>& __x
)
195 * @brief %Multiset assignment operator.
196 * @param x A %multiset of identical element and allocator types.
198 * All the elements of @a x are copied, but unlike the copy constructor,
199 * the allocator object is not copied.
201 multiset
<_Key
,_Compare
,_Alloc
>&
202 operator=(const multiset
<_Key
,_Compare
,_Alloc
>& __x
)
210 /// Returns the comparison object.
213 { return _M_t
.key_comp(); }
214 /// Returns the comparison object.
217 { return _M_t
.key_comp(); }
218 /// Returns the memory allocation object.
220 get_allocator() const
221 { return _M_t
.get_allocator(); }
224 * Returns a read/write iterator that points to the first element in the
225 * %multiset. Iteration is done in ascending order according to the
230 { return _M_t
.begin(); }
233 * Returns a read/write iterator that points one past the last element in
234 * the %multiset. Iteration is done in ascending order according to the
239 { return _M_t
.end(); }
242 * Returns a read/write reverse iterator that points to the last element
243 * in the %multiset. Iteration is done in descending order according to
248 { return _M_t
.rbegin(); }
251 * Returns a read/write reverse iterator that points to the last element
252 * in the %multiset. Iteration is done in descending order according to
257 { return _M_t
.rend(); }
259 /// Returns true if the %set is empty.
262 { return _M_t
.empty(); }
264 /// Returns the size of the %set.
267 { return _M_t
.size(); }
269 /// Returns the maximum size of the %set.
272 { return _M_t
.max_size(); }
275 * @brief Swaps data with another %multiset.
276 * @param x A %multiset of the same element and allocator types.
278 * This exchanges the elements between two multisets in constant time.
279 * (It is only swapping a pointer, an integer, and an instance of the @c
280 * Compare type (which itself is often stateless and empty), so it should
282 * Note that the global std::swap() function is specialized such that
283 * std::swap(s1,s2) will feed to this function.
286 swap(multiset
<_Key
,_Compare
,_Alloc
>& __x
)
287 { _M_t
.swap(__x
._M_t
); }
291 * @brief Inserts an element into the %multiset.
292 * @param x Element to be inserted.
293 * @return An iterator that points to the inserted element.
295 * This function inserts an element into the %multiset. Contrary
296 * to a std::set the %multiset does not rely on unique keys and thus
297 * multiple copies of the same element can be inserted.
299 * Insertion requires logarithmic time.
302 insert(const value_type
& __x
)
303 { return _M_t
.insert_equal(__x
); }
306 * @brief Inserts an element into the %multiset.
307 * @param position An iterator that serves as a hint as to where the
308 * element should be inserted.
309 * @param x Element to be inserted.
310 * @return An iterator that points to the inserted element.
312 * This function inserts an element into the %multiset. Contrary
313 * to a std::set the %multiset does not rely on unique keys and thus
314 * multiple copies of the same element can be inserted.
316 * Note that the first parameter is only a hint and can potentially
317 * improve the performance of the insertion process. A bad hint would
318 * cause no gains in efficiency.
320 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
321 * for more on "hinting".
323 * Insertion requires logarithmic time (if the hint is not taken).
326 insert(iterator __position
, const value_type
& __x
)
328 typedef typename
_Rep_type::iterator _Rep_iterator
;
329 return _M_t
.insert_equal((_Rep_iterator
&)__position
, __x
);
333 * @brief A template function that attemps to insert a range of elements.
334 * @param first Iterator pointing to the start of the range to be
336 * @param last Iterator pointing to the end of the range.
338 * Complexity similar to that of the range constructor.
340 template <class _InputIterator
>
342 insert(_InputIterator __first
, _InputIterator __last
)
343 { _M_t
.insert_equal(__first
, __last
); }
346 * @brief Erases an element from a %multiset.
347 * @param position An iterator pointing to the element to be erased.
349 * This function erases an element, pointed to by the given iterator,
350 * from a %multiset. Note that this function only erases the element,
351 * and that if the element is itself a pointer, the pointed-to memory is
352 * not touched in any way. Managing the pointer is the user's
356 erase(iterator __position
)
358 typedef typename
_Rep_type::iterator _Rep_iterator
;
359 _M_t
.erase((_Rep_iterator
&)__position
);
363 * @brief Erases elements according to the provided key.
364 * @param x Key of element to be erased.
365 * @return The number of elements erased.
367 * This function erases all elements located by the given key from a
369 * Note that this function only erases the element, and that if
370 * the element is itself a pointer, the pointed-to memory is not touched
371 * in any way. Managing the pointer is the user's responsibilty.
374 erase(const key_type
& __x
)
375 { return _M_t
.erase(__x
); }
378 * @brief Erases a [first,last) range of elements from a %multiset.
379 * @param first Iterator pointing to the start of the range to be
381 * @param last Iterator pointing to the end of the range to be erased.
383 * This function erases a sequence of elements from a %multiset.
384 * Note that this function only erases the elements, and that if
385 * the elements themselves are pointers, the pointed-to memory is not
386 * touched in any way. Managing the pointer is the user's responsibilty.
389 erase(iterator __first
, iterator __last
)
391 typedef typename
_Rep_type::iterator _Rep_iterator
;
392 _M_t
.erase((_Rep_iterator
&)__first
, (_Rep_iterator
&)__last
);
396 * Erases all elements in a %multiset. Note that this function only
397 * erases the elements, and that if the elements themselves are pointers,
398 * the pointed-to memory is not touched in any way. Managing the pointer
399 * is the user's responsibilty.
405 // multiset operations:
408 * @brief Finds the number of elements with given key.
409 * @param x Key of elements to be located.
410 * @return Number of elements with specified key.
413 count(const key_type
& __x
) const
414 { return _M_t
.count(__x
); }
416 // _GLIBCXX_RESOLVE_LIB_DEFECTS
417 // 214. set::find() missing const overload
420 * @brief Tries to locate an element in a %set.
421 * @param x Element to be located.
422 * @return Iterator pointing to sought-after element, or end() if not
425 * This function takes a key and tries to locate the element with which
426 * the key matches. If successful the function returns an iterator
427 * pointing to the sought after element. If unsuccessful it returns the
428 * past-the-end ( @c end() ) iterator.
431 find(const key_type
& __x
)
432 { return _M_t
.find(__x
); }
435 find(const key_type
& __x
) const
436 { return _M_t
.find(__x
); }
441 * @brief Finds the beginning of a subsequence matching given key.
442 * @param x Key to be located.
443 * @return Iterator pointing to first element equal to or greater
444 * than key, or end().
446 * This function returns the first element of a subsequence of elements
447 * that matches the given key. If unsuccessful it returns an iterator
448 * pointing to the first element that has a greater value than given key
449 * or end() if no such element exists.
452 lower_bound(const key_type
& __x
)
453 { return _M_t
.lower_bound(__x
); }
456 lower_bound(const key_type
& __x
) const
457 { return _M_t
.lower_bound(__x
); }
462 * @brief Finds the end of a subsequence matching given key.
463 * @param x Key to be located.
464 * @return Iterator pointing to the first element
465 * greater than key, or end().
468 upper_bound(const key_type
& __x
)
469 { return _M_t
.upper_bound(__x
); }
472 upper_bound(const key_type
& __x
) const
473 { return _M_t
.upper_bound(__x
); }
478 * @brief Finds a subsequence matching given key.
479 * @param x Key to be located.
480 * @return Pair of iterators that possibly points to the subsequence
481 * matching given key.
483 * This function is equivalent to
485 * std::make_pair(c.lower_bound(val),
486 * c.upper_bound(val))
488 * (but is faster than making the calls separately).
490 * This function probably only makes sense for multisets.
492 pair
<iterator
,iterator
>
493 equal_range(const key_type
& __x
)
494 { return _M_t
.equal_range(__x
); }
496 pair
<const_iterator
,const_iterator
>
497 equal_range(const key_type
& __x
) const
498 { return _M_t
.equal_range(__x
); }
500 template <class _K1
, class _C1
, class _A1
>
502 operator== (const multiset
<_K1
,_C1
,_A1
>&,
503 const multiset
<_K1
,_C1
,_A1
>&);
505 template <class _K1
, class _C1
, class _A1
>
507 operator< (const multiset
<_K1
,_C1
,_A1
>&,
508 const multiset
<_K1
,_C1
,_A1
>&);
512 * @brief Multiset equality comparison.
513 * @param x A %multiset.
514 * @param y A %multiset of the same type as @a x.
515 * @return True iff the size and elements of the multisets are equal.
517 * This is an equivalence relation. It is linear in the size of the
519 * Multisets are considered equivalent if their sizes are equal, and if
520 * corresponding elements compare equal.
522 template <class _Key
, class _Compare
, class _Alloc
>
524 operator==(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
525 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
526 { return __x
._M_t
== __y
._M_t
; }
529 * @brief Multiset ordering relation.
530 * @param x A %multiset.
531 * @param y A %multiset of the same type as @a x.
532 * @return True iff @a x is lexicographically less than @a y.
534 * This is a total ordering relation. It is linear in the size of the
535 * maps. The elements must be comparable with @c <.
537 * See std::lexicographical_compare() for how the determination is made.
539 template <class _Key
, class _Compare
, class _Alloc
>
541 operator<(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
542 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
543 { return __x
._M_t
< __y
._M_t
; }
545 /// Returns !(x == y).
546 template <class _Key
, class _Compare
, class _Alloc
>
548 operator!=(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
549 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
550 { return !(__x
== __y
); }
553 template <class _Key
, class _Compare
, class _Alloc
>
555 operator>(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
556 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
557 { return __y
< __x
; }
560 template <class _Key
, class _Compare
, class _Alloc
>
562 operator<=(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
563 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
564 { return !(__y
< __x
); }
567 template <class _Key
, class _Compare
, class _Alloc
>
569 operator>=(const multiset
<_Key
,_Compare
,_Alloc
>& __x
,
570 const multiset
<_Key
,_Compare
,_Alloc
>& __y
)
571 { return !(__x
< __y
); }
573 /// See std::multiset::swap().
574 template <class _Key
, class _Compare
, class _Alloc
>
576 swap(multiset
<_Key
,_Compare
,_Alloc
>& __x
,
577 multiset
<_Key
,_Compare
,_Alloc
>& __y
)
580 } // namespace __gnu_norm
582 #endif /* _MULTISET_H */