]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_multiset.h
auto_ptr.h: Fix comment typos.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_multiset.h
1 // Multiset implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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,
20 // USA.
21
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.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
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.
43 *
44 *
45 * Copyright (c) 1996
46 * Silicon Graphics Computer Systems, Inc.
47 *
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.
55 */
56
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.
60 */
61
62 #ifndef _STL_MULTISET_H
63 #define _STL_MULTISET_H 1
64
65 #include <bits/concept_check.h>
66
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68
69 /**
70 * @brief A standard container made up of elements, which can be retrieved
71 * in logarithmic time.
72 *
73 * @ingroup Containers
74 * @ingroup Assoc_containers
75 *
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.
80 *
81 * Multisets support bidirectional iterators.
82 *
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 */
87 template <typename _Key, typename _Compare = std::less<_Key>,
88 typename _Alloc = std::allocator<_Key> >
89 class multiset
90 {
91 // concept requirements
92 typedef typename _Alloc::value_type _Alloc_value_type;
93 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
94 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
95 _BinaryFunctionConcept)
96 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
97
98 public:
99 // typedefs:
100 typedef _Key key_type;
101 typedef _Key value_type;
102 typedef _Compare key_compare;
103 typedef _Compare value_compare;
104 typedef _Alloc allocator_type;
105
106 private:
107 /// This turns a red-black tree into a [multi]set.
108 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
109
110 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
111 key_compare, _Key_alloc_type> _Rep_type;
112 /// The actual tree structure.
113 _Rep_type _M_t;
114
115 public:
116 typedef typename _Key_alloc_type::pointer pointer;
117 typedef typename _Key_alloc_type::const_pointer const_pointer;
118 typedef typename _Key_alloc_type::reference reference;
119 typedef typename _Key_alloc_type::const_reference const_reference;
120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
121 // DR 103. set::iterator is required to be modifiable,
122 // but this allows modification of keys.
123 typedef typename _Rep_type::const_iterator iterator;
124 typedef typename _Rep_type::const_iterator const_iterator;
125 typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
126 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
127 typedef typename _Rep_type::size_type size_type;
128 typedef typename _Rep_type::difference_type difference_type;
129
130 // allocation/deallocation
131 /**
132 * @brief Default constructor creates no elements.
133 */
134 multiset()
135 : _M_t() { }
136
137 /**
138 * @brief Creates a %multiset with no elements.
139 * @param comp Comparator to use.
140 * @param a An allocator object.
141 */
142 explicit
143 multiset(const _Compare& __comp,
144 const allocator_type& __a = allocator_type())
145 : _M_t(__comp, __a) { }
146
147 /**
148 * @brief Builds a %multiset from a range.
149 * @param first An input iterator.
150 * @param last An input iterator.
151 *
152 * Create a %multiset consisting of copies of the elements from
153 * [first,last). This is linear in N if the range is already sorted,
154 * and NlogN otherwise (where N is distance(first,last)).
155 */
156 template<typename _InputIterator>
157 multiset(_InputIterator __first, _InputIterator __last)
158 : _M_t()
159 { _M_t._M_insert_equal(__first, __last); }
160
161 /**
162 * @brief Builds a %multiset from a range.
163 * @param first An input iterator.
164 * @param last An input iterator.
165 * @param comp A comparison functor.
166 * @param a An allocator object.
167 *
168 * Create a %multiset consisting of copies of the elements from
169 * [first,last). This is linear in N if the range is already sorted,
170 * and NlogN otherwise (where N is distance(first,last)).
171 */
172 template<typename _InputIterator>
173 multiset(_InputIterator __first, _InputIterator __last,
174 const _Compare& __comp,
175 const allocator_type& __a = allocator_type())
176 : _M_t(__comp, __a)
177 { _M_t._M_insert_equal(__first, __last); }
178
179 /**
180 * @brief %Multiset copy constructor.
181 * @param x A %multiset of identical element and allocator types.
182 *
183 * The newly-created %multiset uses a copy of the allocation object used
184 * by @a x.
185 */
186 multiset(const multiset& __x)
187 : _M_t(__x._M_t) { }
188
189 #ifdef __GXX_EXPERIMENTAL_CXX0X__
190 /**
191 * @brief %Multiset move constructor.
192 * @param x A %multiset of identical element and allocator types.
193 *
194 * The newly-created %multiset contains the exact contents of @a x.
195 * The contents of @a x are a valid, but unspecified %multiset.
196 */
197 multiset(multiset&& __x)
198 : _M_t(std::forward<_Rep_type>(__x._M_t)) { }
199 #endif
200
201 /**
202 * @brief %Multiset assignment operator.
203 * @param x A %multiset of identical element and allocator types.
204 *
205 * All the elements of @a x are copied, but unlike the copy constructor,
206 * the allocator object is not copied.
207 */
208 multiset&
209 operator=(const multiset& __x)
210 {
211 _M_t = __x._M_t;
212 return *this;
213 }
214
215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
216 /**
217 * @brief %Multiset move assignment operator.
218 * @param x A %multiset of identical element and allocator types.
219 *
220 * The contents of @a x are moved into this %multiset (without copying).
221 * @a x is a valid, but unspecified %multiset.
222 */
223 multiset&
224 operator=(multiset&& __x)
225 {
226 // NB: DR 675.
227 this->clear();
228 this->swap(__x);
229 return *this;
230 }
231 #endif
232
233 // accessors:
234
235 /// Returns the comparison object.
236 key_compare
237 key_comp() const
238 { return _M_t.key_comp(); }
239 /// Returns the comparison object.
240 value_compare
241 value_comp() const
242 { return _M_t.key_comp(); }
243 /// Returns the memory allocation object.
244 allocator_type
245 get_allocator() const
246 { return _M_t.get_allocator(); }
247
248 /**
249 * Returns a read-only (constant) iterator that points to the first
250 * element in the %multiset. Iteration is done in ascending order
251 * according to the keys.
252 */
253 iterator
254 begin() const
255 { return _M_t.begin(); }
256
257 /**
258 * Returns a read-only (constant) iterator that points one past the last
259 * element in the %multiset. Iteration is done in ascending order
260 * according to the keys.
261 */
262 iterator
263 end() const
264 { return _M_t.end(); }
265
266 /**
267 * Returns a read-only (constant) reverse iterator that points to the
268 * last element in the %multiset. Iteration is done in descending order
269 * according to the keys.
270 */
271 reverse_iterator
272 rbegin() const
273 { return _M_t.rbegin(); }
274
275 /**
276 * Returns a read-only (constant) reverse iterator that points to the
277 * last element in the %multiset. Iteration is done in descending order
278 * according to the keys.
279 */
280 reverse_iterator
281 rend() const
282 { return _M_t.rend(); }
283
284 #ifdef __GXX_EXPERIMENTAL_CXX0X__
285 /**
286 * Returns a read-only (constant) iterator that points to the first
287 * element in the %multiset. Iteration is done in ascending order
288 * according to the keys.
289 */
290 iterator
291 cbegin() const
292 { return _M_t.begin(); }
293
294 /**
295 * Returns a read-only (constant) iterator that points one past the last
296 * element in the %multiset. Iteration is done in ascending order
297 * according to the keys.
298 */
299 iterator
300 cend() const
301 { return _M_t.end(); }
302
303 /**
304 * Returns a read-only (constant) reverse iterator that points to the
305 * last element in the %multiset. Iteration is done in descending order
306 * according to the keys.
307 */
308 reverse_iterator
309 crbegin() const
310 { return _M_t.rbegin(); }
311
312 /**
313 * Returns a read-only (constant) reverse iterator that points to the
314 * last element in the %multiset. Iteration is done in descending order
315 * according to the keys.
316 */
317 reverse_iterator
318 crend() const
319 { return _M_t.rend(); }
320 #endif
321
322 /// Returns true if the %set is empty.
323 bool
324 empty() const
325 { return _M_t.empty(); }
326
327 /// Returns the size of the %set.
328 size_type
329 size() const
330 { return _M_t.size(); }
331
332 /// Returns the maximum size of the %set.
333 size_type
334 max_size() const
335 { return _M_t.max_size(); }
336
337 /**
338 * @brief Swaps data with another %multiset.
339 * @param x A %multiset of the same element and allocator types.
340 *
341 * This exchanges the elements between two multisets in constant time.
342 * (It is only swapping a pointer, an integer, and an instance of the @c
343 * Compare type (which itself is often stateless and empty), so it should
344 * be quite fast.)
345 * Note that the global std::swap() function is specialized such that
346 * std::swap(s1,s2) will feed to this function.
347 */
348 void
349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
350 swap(multiset&& __x)
351 #else
352 swap(multiset& __x)
353 #endif
354 { _M_t.swap(__x._M_t); }
355
356 // insert/erase
357 /**
358 * @brief Inserts an element into the %multiset.
359 * @param x Element to be inserted.
360 * @return An iterator that points to the inserted element.
361 *
362 * This function inserts an element into the %multiset. Contrary
363 * to a std::set the %multiset does not rely on unique keys and thus
364 * multiple copies of the same element can be inserted.
365 *
366 * Insertion requires logarithmic time.
367 */
368 iterator
369 insert(const value_type& __x)
370 { return _M_t._M_insert_equal(__x); }
371
372 /**
373 * @brief Inserts an element into the %multiset.
374 * @param position An iterator that serves as a hint as to where the
375 * element should be inserted.
376 * @param x Element to be inserted.
377 * @return An iterator that points to the inserted element.
378 *
379 * This function inserts an element into the %multiset. Contrary
380 * to a std::set the %multiset does not rely on unique keys and thus
381 * multiple copies of the same element can be inserted.
382 *
383 * Note that the first parameter is only a hint and can potentially
384 * improve the performance of the insertion process. A bad hint would
385 * cause no gains in efficiency.
386 *
387 * See http://gcc.gnu.org/onlinedocs/libstdc++/23_containers/howto.html#4
388 * for more on "hinting".
389 *
390 * Insertion requires logarithmic time (if the hint is not taken).
391 */
392 iterator
393 insert(iterator __position, const value_type& __x)
394 { return _M_t._M_insert_equal_(__position, __x); }
395
396 /**
397 * @brief A template function that attempts to insert a range of elements.
398 * @param first Iterator pointing to the start of the range to be
399 * inserted.
400 * @param last Iterator pointing to the end of the range.
401 *
402 * Complexity similar to that of the range constructor.
403 */
404 template<typename _InputIterator>
405 void
406 insert(_InputIterator __first, _InputIterator __last)
407 { _M_t._M_insert_equal(__first, __last); }
408
409 /**
410 * @brief Erases an element from a %multiset.
411 * @param position An iterator pointing to the element to be erased.
412 *
413 * This function erases an element, pointed to by the given iterator,
414 * from a %multiset. Note that this function only erases the element,
415 * and that if the element is itself a pointer, the pointed-to memory is
416 * not touched in any way. Managing the pointer is the user's
417 * responsibility.
418 */
419 void
420 erase(iterator __position)
421 { _M_t.erase(__position); }
422
423 /**
424 * @brief Erases elements according to the provided key.
425 * @param x Key of element to be erased.
426 * @return The number of elements erased.
427 *
428 * This function erases all elements located by the given key from a
429 * %multiset.
430 * Note that this function only erases the element, and that if
431 * the element is itself a pointer, the pointed-to memory is not touched
432 * in any way. Managing the pointer is the user's responsibility.
433 */
434 size_type
435 erase(const key_type& __x)
436 { return _M_t.erase(__x); }
437
438 /**
439 * @brief Erases a [first,last) range of elements from a %multiset.
440 * @param first Iterator pointing to the start of the range to be
441 * erased.
442 * @param last Iterator pointing to the end of the range to be erased.
443 *
444 * This function erases a sequence of elements from a %multiset.
445 * Note that this function only erases the elements, and that if
446 * the elements themselves are pointers, the pointed-to memory is not
447 * touched in any way. Managing the pointer is the user's responsibility.
448 */
449 void
450 erase(iterator __first, iterator __last)
451 { _M_t.erase(__first, __last); }
452
453 /**
454 * Erases all elements in a %multiset. Note that this function only
455 * erases the elements, and that if the elements themselves are pointers,
456 * the pointed-to memory is not touched in any way. Managing the pointer
457 * is the user's responsibility.
458 */
459 void
460 clear()
461 { _M_t.clear(); }
462
463 // multiset operations:
464
465 /**
466 * @brief Finds the number of elements with given key.
467 * @param x Key of elements to be located.
468 * @return Number of elements with specified key.
469 */
470 size_type
471 count(const key_type& __x) const
472 { return _M_t.count(__x); }
473
474 // _GLIBCXX_RESOLVE_LIB_DEFECTS
475 // 214. set::find() missing const overload
476 //@{
477 /**
478 * @brief Tries to locate an element in a %set.
479 * @param x Element to be located.
480 * @return Iterator pointing to sought-after element, or end() if not
481 * found.
482 *
483 * This function takes a key and tries to locate the element with which
484 * the key matches. If successful the function returns an iterator
485 * pointing to the sought after element. If unsuccessful it returns the
486 * past-the-end ( @c end() ) iterator.
487 */
488 iterator
489 find(const key_type& __x)
490 { return _M_t.find(__x); }
491
492 const_iterator
493 find(const key_type& __x) const
494 { return _M_t.find(__x); }
495 //@}
496
497 //@{
498 /**
499 * @brief Finds the beginning of a subsequence matching given key.
500 * @param x Key to be located.
501 * @return Iterator pointing to first element equal to or greater
502 * than key, or end().
503 *
504 * This function returns the first element of a subsequence of elements
505 * that matches the given key. If unsuccessful it returns an iterator
506 * pointing to the first element that has a greater value than given key
507 * or end() if no such element exists.
508 */
509 iterator
510 lower_bound(const key_type& __x)
511 { return _M_t.lower_bound(__x); }
512
513 const_iterator
514 lower_bound(const key_type& __x) const
515 { return _M_t.lower_bound(__x); }
516 //@}
517
518 //@{
519 /**
520 * @brief Finds the end of a subsequence matching given key.
521 * @param x Key to be located.
522 * @return Iterator pointing to the first element
523 * greater than key, or end().
524 */
525 iterator
526 upper_bound(const key_type& __x)
527 { return _M_t.upper_bound(__x); }
528
529 const_iterator
530 upper_bound(const key_type& __x) const
531 { return _M_t.upper_bound(__x); }
532 //@}
533
534 //@{
535 /**
536 * @brief Finds a subsequence matching given key.
537 * @param x Key to be located.
538 * @return Pair of iterators that possibly points to the subsequence
539 * matching given key.
540 *
541 * This function is equivalent to
542 * @code
543 * std::make_pair(c.lower_bound(val),
544 * c.upper_bound(val))
545 * @endcode
546 * (but is faster than making the calls separately).
547 *
548 * This function probably only makes sense for multisets.
549 */
550 std::pair<iterator, iterator>
551 equal_range(const key_type& __x)
552 { return _M_t.equal_range(__x); }
553
554 std::pair<const_iterator, const_iterator>
555 equal_range(const key_type& __x) const
556 { return _M_t.equal_range(__x); }
557
558 template<typename _K1, typename _C1, typename _A1>
559 friend bool
560 operator==(const multiset<_K1, _C1, _A1>&,
561 const multiset<_K1, _C1, _A1>&);
562
563 template<typename _K1, typename _C1, typename _A1>
564 friend bool
565 operator< (const multiset<_K1, _C1, _A1>&,
566 const multiset<_K1, _C1, _A1>&);
567 };
568
569 /**
570 * @brief Multiset equality comparison.
571 * @param x A %multiset.
572 * @param y A %multiset of the same type as @a x.
573 * @return True iff the size and elements of the multisets are equal.
574 *
575 * This is an equivalence relation. It is linear in the size of the
576 * multisets.
577 * Multisets are considered equivalent if their sizes are equal, and if
578 * corresponding elements compare equal.
579 */
580 template<typename _Key, typename _Compare, typename _Alloc>
581 inline bool
582 operator==(const multiset<_Key, _Compare, _Alloc>& __x,
583 const multiset<_Key, _Compare, _Alloc>& __y)
584 { return __x._M_t == __y._M_t; }
585
586 /**
587 * @brief Multiset ordering relation.
588 * @param x A %multiset.
589 * @param y A %multiset of the same type as @a x.
590 * @return True iff @a x is lexicographically less than @a y.
591 *
592 * This is a total ordering relation. It is linear in the size of the
593 * maps. The elements must be comparable with @c <.
594 *
595 * See std::lexicographical_compare() for how the determination is made.
596 */
597 template<typename _Key, typename _Compare, typename _Alloc>
598 inline bool
599 operator<(const multiset<_Key, _Compare, _Alloc>& __x,
600 const multiset<_Key, _Compare, _Alloc>& __y)
601 { return __x._M_t < __y._M_t; }
602
603 /// Returns !(x == y).
604 template<typename _Key, typename _Compare, typename _Alloc>
605 inline bool
606 operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
607 const multiset<_Key, _Compare, _Alloc>& __y)
608 { return !(__x == __y); }
609
610 /// Returns y < x.
611 template<typename _Key, typename _Compare, typename _Alloc>
612 inline bool
613 operator>(const multiset<_Key,_Compare,_Alloc>& __x,
614 const multiset<_Key,_Compare,_Alloc>& __y)
615 { return __y < __x; }
616
617 /// Returns !(y < x)
618 template<typename _Key, typename _Compare, typename _Alloc>
619 inline bool
620 operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
621 const multiset<_Key, _Compare, _Alloc>& __y)
622 { return !(__y < __x); }
623
624 /// Returns !(x < y)
625 template<typename _Key, typename _Compare, typename _Alloc>
626 inline bool
627 operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
628 const multiset<_Key, _Compare, _Alloc>& __y)
629 { return !(__x < __y); }
630
631 /// See std::multiset::swap().
632 template<typename _Key, typename _Compare, typename _Alloc>
633 inline void
634 swap(multiset<_Key, _Compare, _Alloc>& __x,
635 multiset<_Key, _Compare, _Alloc>& __y)
636 { __x.swap(__y); }
637
638 #ifdef __GXX_EXPERIMENTAL_CXX0X__
639 template<typename _Key, typename _Compare, typename _Alloc>
640 inline void
641 swap(multiset<_Key, _Compare, _Alloc>&& __x,
642 multiset<_Key, _Compare, _Alloc>& __y)
643 { __x.swap(__y); }
644
645 template<typename _Key, typename _Compare, typename _Alloc>
646 inline void
647 swap(multiset<_Key, _Compare, _Alloc>& __x,
648 multiset<_Key, _Compare, _Alloc>&& __y)
649 { __x.swap(__y); }
650 #endif
651
652 _GLIBCXX_END_NESTED_NAMESPACE
653
654 #endif /* _STL_MULTISET_H */