]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libstdc++/include/bits/stl_multiset.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libstdc++ / include / bits / stl_multiset.h
1 // Multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
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.
29
30 /*
31  *
32  * Copyright (c) 1994
33  * Hewlett-Packard Company
34  *
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.
42  *
43  *
44  * Copyright (c) 1996
45  * Silicon Graphics Computer Systems, Inc.
46  *
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.
54  */
55
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.
59  */
60
61 #ifndef _MULTISET_H
62 #define _MULTISET_H 1
63
64 #include <bits/concept_check.h>
65
66 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
67
68   /**
69    *  @brief A standard container made up of elements, which can be retrieved
70    *  in logarithmic time.
71    *
72    *  @ingroup Containers
73    *  @ingroup Assoc_containers
74    *
75    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
76    *  <a href="tables.html#66">reversible container</a>, and an
77    *  <a href="tables.html#69">associative container</a> (using equivalent
78    *  keys).  For a @c multiset<Key> the key_type and value_type are Key.
79    *
80    *  Multisets support bidirectional iterators.
81    *
82    *  @if maint
83    *  The private tree data is declared exactly the same way for set and
84    *  multiset; the distinction is made entirely in how the tree functions are
85    *  called (*_unique versus *_equal, same as the standard).
86    *  @endif
87   */
88   template <class _Key, class _Compare = std::less<_Key>,
89             class _Alloc = std::allocator<_Key> >
90     class multiset
91     {
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)      
98
99     public:
100       // typedefs:
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;
106
107     private:
108       /// @if maint  This turns a red-black tree into a [multi]set.  @endif
109       typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
110
111       typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
112                        key_compare, _Key_alloc_type> _Rep_type;
113       /// @if maint  The actual tree structure.  @endif
114       _Rep_type _M_t;
115
116     public:
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;
130
131       // allocation/deallocation
132
133       /**
134        *  @brief  Default constructor creates no elements.
135        */
136       multiset()
137       : _M_t() { }
138
139       explicit
140       multiset(const _Compare& __comp,
141                const allocator_type& __a = allocator_type())
142       : _M_t(__comp, __a) { }
143
144       /**
145        *  @brief  Builds a %multiset from a range.
146        *  @param  first  An input iterator.
147        *  @param  last  An input iterator.
148        *
149        *  Create a %multiset consisting of copies of the elements from
150        *  [first,last).  This is linear in N if the range is already sorted,
151        *  and NlogN otherwise (where N is distance(first,last)).
152        */
153       template <class _InputIterator>
154         multiset(_InputIterator __first, _InputIterator __last)
155         : _M_t()
156         { _M_t._M_insert_equal(__first, __last); }
157
158       /**
159        *  @brief  Builds a %multiset from a range.
160        *  @param  first  An input iterator.
161        *  @param  last  An input iterator.
162        *  @param  comp  A comparison functor.
163        *  @param  a  An allocator object.
164        *
165        *  Create a %multiset consisting of copies of the elements from
166        *  [first,last).  This is linear in N if the range is already sorted,
167        *  and NlogN otherwise (where N is distance(first,last)).
168        */
169       template <class _InputIterator>
170         multiset(_InputIterator __first, _InputIterator __last,
171                  const _Compare& __comp,
172                  const allocator_type& __a = allocator_type())
173         : _M_t(__comp, __a)
174         { _M_t._M_insert_equal(__first, __last); }
175
176       /**
177        *  @brief  %Multiset copy constructor.
178        *  @param  x  A %multiset of identical element and allocator types.
179        *
180        *  The newly-created %multiset uses a copy of the allocation object used
181        *  by @a x.
182        */
183       multiset(const multiset& __x)
184       : _M_t(__x._M_t) { }
185
186       /**
187        *  @brief  %Multiset assignment operator.
188        *  @param  x  A %multiset of identical element and allocator types.
189        *
190        *  All the elements of @a x are copied, but unlike the copy constructor,
191        *  the allocator object is not copied.
192        */
193       multiset&
194       operator=(const multiset& __x)
195       {
196         _M_t = __x._M_t;
197         return *this;
198       }
199
200       // accessors:
201
202       ///  Returns the comparison object.
203       key_compare
204       key_comp() const
205       { return _M_t.key_comp(); }
206       ///  Returns the comparison object.
207       value_compare
208       value_comp() const
209       { return _M_t.key_comp(); }
210       ///  Returns the memory allocation object.
211       allocator_type
212       get_allocator() const
213       { return _M_t.get_allocator(); }
214
215       /**
216        *  Returns a read/write iterator that points to the first element in the
217        *  %multiset.  Iteration is done in ascending order according to the
218        *  keys.
219        */
220       iterator
221       begin() const
222       { return _M_t.begin(); }
223
224       /**
225        *  Returns a read/write iterator that points one past the last element in
226        *  the %multiset.  Iteration is done in ascending order according to the
227        *  keys.
228        */
229       iterator
230       end() const
231       { return _M_t.end(); }
232
233       /**
234        *  Returns a read/write reverse iterator that points to the last element
235        *  in the %multiset.  Iteration is done in descending order according to
236        *  the keys.
237        */
238       reverse_iterator
239       rbegin() const
240       { return _M_t.rbegin(); }
241
242       /**
243        *  Returns a read/write reverse iterator that points to the last element
244        *  in the %multiset.  Iteration is done in descending order according to
245        *  the keys.
246        */
247       reverse_iterator
248       rend() const
249       { return _M_t.rend(); }
250
251       ///  Returns true if the %set is empty.
252       bool
253       empty() const
254       { return _M_t.empty(); }
255
256       ///  Returns the size of the %set.
257       size_type
258       size() const
259       { return _M_t.size(); }
260
261       ///  Returns the maximum size of the %set.
262       size_type
263       max_size() const
264       { return _M_t.max_size(); }
265
266       /**
267        *  @brief  Swaps data with another %multiset.
268        *  @param  x  A %multiset of the same element and allocator types.
269        *
270        *  This exchanges the elements between two multisets in constant time.
271        *  (It is only swapping a pointer, an integer, and an instance of the @c
272        *  Compare type (which itself is often stateless and empty), so it should
273        *  be quite fast.)
274        *  Note that the global std::swap() function is specialized such that
275        *  std::swap(s1,s2) will feed to this function.
276        */
277       void
278       swap(multiset& __x)
279       { _M_t.swap(__x._M_t); }
280
281       // insert/erase
282       /**
283        *  @brief Inserts an element into the %multiset.
284        *  @param  x  Element to be inserted.
285        *  @return An iterator that points to the inserted element.
286        *
287        *  This function inserts an element into the %multiset.  Contrary
288        *  to a std::set the %multiset does not rely on unique keys and thus
289        *  multiple copies of the same element can be inserted.
290        *
291        *  Insertion requires logarithmic time.
292        */
293       iterator
294       insert(const value_type& __x)
295       { return _M_t._M_insert_equal(__x); }
296
297       /**
298        *  @brief Inserts an element into the %multiset.
299        *  @param  position  An iterator that serves as a hint as to where the
300        *                    element should be inserted.
301        *  @param  x  Element to be inserted.
302        *  @return An iterator that points to the inserted element.
303        *
304        *  This function inserts an element into the %multiset.  Contrary
305        *  to a std::set the %multiset does not rely on unique keys and thus
306        *  multiple copies of the same element can be inserted.
307        *
308        *  Note that the first parameter is only a hint and can potentially
309        *  improve the performance of the insertion process.  A bad hint would
310        *  cause no gains in efficiency.
311        *
312        *  See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
313        *  for more on "hinting".
314        *
315        *  Insertion requires logarithmic time (if the hint is not taken).
316        */
317       iterator
318       insert(iterator __position, const value_type& __x)
319       { return _M_t._M_insert_equal(__position, __x); }
320
321       /**
322        *  @brief A template function that attemps to insert a range of elements.
323        *  @param  first  Iterator pointing to the start of the range to be
324        *                 inserted.
325        *  @param  last  Iterator pointing to the end of the range.
326        *
327        *  Complexity similar to that of the range constructor.
328        */
329       template <class _InputIterator>
330         void
331         insert(_InputIterator __first, _InputIterator __last)
332         { _M_t._M_insert_equal(__first, __last); }
333
334       /**
335        *  @brief Erases an element from a %multiset.
336        *  @param  position  An iterator pointing to the element to be erased.
337        *
338        *  This function erases an element, pointed to by the given iterator,
339        *  from a %multiset.  Note that this function only erases the element,
340        *  and that if the element is itself a pointer, the pointed-to memory is
341        *  not touched in any way.  Managing the pointer is the user's
342        *  responsibilty.
343        */
344       void
345       erase(iterator __position)
346       { _M_t.erase(__position); }
347
348       /**
349        *  @brief Erases elements according to the provided key.
350        *  @param  x  Key of element to be erased.
351        *  @return  The number of elements erased.
352        *
353        *  This function erases all elements located by the given key from a
354        *  %multiset.
355        *  Note that this function only erases the element, and that if
356        *  the element is itself a pointer, the pointed-to memory is not touched
357        *  in any way.  Managing the pointer is the user's responsibilty.
358        */
359       size_type
360       erase(const key_type& __x)
361       { return _M_t.erase(__x); }
362
363       /**
364        *  @brief Erases a [first,last) range of elements from a %multiset.
365        *  @param  first  Iterator pointing to the start of the range to be
366        *                 erased.
367        *  @param  last  Iterator pointing to the end of the range to be erased.
368        *
369        *  This function erases a sequence of elements from a %multiset.
370        *  Note that this function only erases the elements, and that if
371        *  the elements themselves are pointers, the pointed-to memory is not
372        *  touched in any way.  Managing the pointer is the user's responsibilty.
373        */
374       void
375       erase(iterator __first, iterator __last)
376       { _M_t.erase(__first, __last); }
377
378       /**
379        *  Erases all elements in a %multiset.  Note that this function only
380        *  erases the elements, and that if the elements themselves are pointers,
381        *  the pointed-to memory is not touched in any way.  Managing the pointer
382        *  is the user's responsibilty.
383        */
384       void
385       clear()
386       { _M_t.clear(); }
387
388       // multiset operations:
389
390       /**
391        *  @brief Finds the number of elements with given key.
392        *  @param  x  Key of elements to be located.
393        *  @return Number of elements with specified key.
394        */
395       size_type
396       count(const key_type& __x) const
397       { return _M_t.count(__x); }
398
399       // _GLIBCXX_RESOLVE_LIB_DEFECTS
400       // 214.  set::find() missing const overload
401       //@{
402       /**
403        *  @brief Tries to locate an element in a %set.
404        *  @param  x  Element to be located.
405        *  @return  Iterator pointing to sought-after element, or end() if not
406        *           found.
407        *
408        *  This function takes a key and tries to locate the element with which
409        *  the key matches.  If successful the function returns an iterator
410        *  pointing to the sought after element.  If unsuccessful it returns the
411        *  past-the-end ( @c end() ) iterator.
412        */
413       iterator
414       find(const key_type& __x)
415       { return _M_t.find(__x); }
416
417       const_iterator
418       find(const key_type& __x) const
419       { return _M_t.find(__x); }
420       //@}
421
422       //@{
423       /**
424        *  @brief Finds the beginning of a subsequence matching given key.
425        *  @param  x  Key to be located.
426        *  @return  Iterator pointing to first element equal to or greater
427        *           than key, or end().
428        *
429        *  This function returns the first element of a subsequence of elements
430        *  that matches the given key.  If unsuccessful it returns an iterator
431        *  pointing to the first element that has a greater value than given key
432        *  or end() if no such element exists.
433        */
434       iterator
435       lower_bound(const key_type& __x)
436       { return _M_t.lower_bound(__x); }
437
438       const_iterator
439       lower_bound(const key_type& __x) const
440       { return _M_t.lower_bound(__x); }
441       //@}
442
443       //@{
444       /**
445        *  @brief Finds the end of a subsequence matching given key.
446        *  @param  x  Key to be located.
447        *  @return Iterator pointing to the first element
448        *          greater than key, or end().
449        */
450       iterator
451       upper_bound(const key_type& __x)
452       { return _M_t.upper_bound(__x); }
453
454       const_iterator
455       upper_bound(const key_type& __x) const
456       { return _M_t.upper_bound(__x); }
457       //@}
458
459       //@{
460       /**
461        *  @brief Finds a subsequence matching given key.
462        *  @param  x  Key to be located.
463        *  @return  Pair of iterators that possibly points to the subsequence
464        *           matching given key.
465        *
466        *  This function is equivalent to
467        *  @code
468        *    std::make_pair(c.lower_bound(val),
469        *                   c.upper_bound(val))
470        *  @endcode
471        *  (but is faster than making the calls separately).
472        *
473        *  This function probably only makes sense for multisets.
474        */
475       std::pair<iterator, iterator>
476       equal_range(const key_type& __x)
477       { return _M_t.equal_range(__x); }
478
479       std::pair<const_iterator, const_iterator>
480       equal_range(const key_type& __x) const
481       { return _M_t.equal_range(__x); }
482
483       template <class _K1, class _C1, class _A1>
484         friend bool
485         operator== (const multiset<_K1, _C1, _A1>&,
486                     const multiset<_K1, _C1, _A1>&);
487
488       template <class _K1, class _C1, class _A1>
489         friend bool
490         operator< (const multiset<_K1, _C1, _A1>&,
491                    const multiset<_K1, _C1, _A1>&);
492     };
493
494   /**
495    *  @brief  Multiset equality comparison.
496    *  @param  x  A %multiset.
497    *  @param  y  A %multiset of the same type as @a x.
498    *  @return  True iff the size and elements of the multisets are equal.
499    *
500    *  This is an equivalence relation.  It is linear in the size of the
501    *  multisets.
502    *  Multisets are considered equivalent if their sizes are equal, and if
503    *  corresponding elements compare equal.
504   */
505   template <class _Key, class _Compare, class _Alloc>
506     inline bool
507     operator==(const multiset<_Key, _Compare, _Alloc>& __x,
508                const multiset<_Key, _Compare, _Alloc>& __y)
509     { return __x._M_t == __y._M_t; }
510
511   /**
512    *  @brief  Multiset ordering relation.
513    *  @param  x  A %multiset.
514    *  @param  y  A %multiset of the same type as @a x.
515    *  @return  True iff @a x is lexicographically less than @a y.
516    *
517    *  This is a total ordering relation.  It is linear in the size of the
518    *  maps.  The elements must be comparable with @c <.
519    *
520    *  See std::lexicographical_compare() for how the determination is made.
521   */
522   template <class _Key, class _Compare, class _Alloc>
523     inline bool
524     operator<(const multiset<_Key, _Compare, _Alloc>& __x,
525               const multiset<_Key, _Compare, _Alloc>& __y)
526     { return __x._M_t < __y._M_t; }
527
528   ///  Returns !(x == y).
529   template <class _Key, class _Compare, class _Alloc>
530     inline bool
531     operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
532                const multiset<_Key, _Compare, _Alloc>& __y)
533     { return !(__x == __y); }
534
535   ///  Returns y < x.
536   template <class _Key, class _Compare, class _Alloc>
537     inline bool
538     operator>(const multiset<_Key,_Compare,_Alloc>& __x,
539               const multiset<_Key,_Compare,_Alloc>& __y)
540     { return __y < __x; }
541
542   ///  Returns !(y < x)
543   template <class _Key, class _Compare, class _Alloc>
544     inline bool
545     operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
546                const multiset<_Key, _Compare, _Alloc>& __y)
547     { return !(__y < __x); }
548
549   ///  Returns !(x < y)
550   template <class _Key, class _Compare, class _Alloc>
551     inline bool
552     operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
553                const multiset<_Key, _Compare, _Alloc>& __y)
554     { return !(__x < __y); }
555
556   /// See std::multiset::swap().
557   template <class _Key, class _Compare, class _Alloc>
558     inline void
559     swap(multiset<_Key, _Compare, _Alloc>& __x,
560          multiset<_Key, _Compare, _Alloc>& __y)
561     { __x.swap(__y); }
562
563 _GLIBCXX_END_NESTED_NAMESPACE
564
565 #endif /* _MULTISET_H */