]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_multiset.h
user.cfg.in: Tweaks.
[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 #include <initializer_list>
67
68 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
69
70 /**
71 * @brief A standard container made up of elements, which can be retrieved
72 * in logarithmic time.
73 *
74 * @ingroup associative_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
200 /**
201 * @brief Builds a %multiset from an initializer_list.
202 * @param l An initializer_list.
203 * @param comp A comparison functor.
204 * @param a An allocator object.
205 *
206 * Create a %multiset consisting of copies of the elements from
207 * the list. This is linear in N if the list is already sorted,
208 * and NlogN otherwise (where N is @a l.size()).
209 */
210 multiset(initializer_list<value_type> __l,
211 const _Compare& __comp = _Compare(),
212 const allocator_type& __a = allocator_type())
213 : _M_t(__comp, __a)
214 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
215 #endif
216
217 /**
218 * @brief %Multiset assignment operator.
219 * @param x A %multiset of identical element and allocator types.
220 *
221 * All the elements of @a x are copied, but unlike the copy constructor,
222 * the allocator object is not copied.
223 */
224 multiset&
225 operator=(const multiset& __x)
226 {
227 _M_t = __x._M_t;
228 return *this;
229 }
230
231 #ifdef __GXX_EXPERIMENTAL_CXX0X__
232 /**
233 * @brief %Multiset move assignment operator.
234 * @param x A %multiset of identical element and allocator types.
235 *
236 * The contents of @a x are moved into this %multiset (without copying).
237 * @a x is a valid, but unspecified %multiset.
238 */
239 multiset&
240 operator=(multiset&& __x)
241 {
242 // NB: DR 675.
243 this->clear();
244 this->swap(__x);
245 return *this;
246 }
247
248 /**
249 * @brief %Multiset list assignment operator.
250 * @param l An initializer_list.
251 *
252 * This function fills a %multiset with copies of the elements in the
253 * initializer list @a l.
254 *
255 * Note that the assignment completely changes the %multiset and
256 * that the resulting %multiset's size is the same as the number
257 * of elements assigned. Old data may be lost.
258 */
259 multiset&
260 operator=(initializer_list<value_type> __l)
261 {
262 this->clear();
263 this->insert(__l.begin(), __l.end());
264 return *this;
265 }
266 #endif
267
268 // accessors:
269
270 /// Returns the comparison object.
271 key_compare
272 key_comp() const
273 { return _M_t.key_comp(); }
274 /// Returns the comparison object.
275 value_compare
276 value_comp() const
277 { return _M_t.key_comp(); }
278 /// Returns the memory allocation object.
279 allocator_type
280 get_allocator() const
281 { return _M_t.get_allocator(); }
282
283 /**
284 * Returns a read-only (constant) iterator that points to the first
285 * element in the %multiset. Iteration is done in ascending order
286 * according to the keys.
287 */
288 iterator
289 begin() const
290 { return _M_t.begin(); }
291
292 /**
293 * Returns a read-only (constant) iterator that points one past the last
294 * element in the %multiset. Iteration is done in ascending order
295 * according to the keys.
296 */
297 iterator
298 end() const
299 { return _M_t.end(); }
300
301 /**
302 * Returns a read-only (constant) reverse iterator that points to the
303 * last element in the %multiset. Iteration is done in descending order
304 * according to the keys.
305 */
306 reverse_iterator
307 rbegin() const
308 { return _M_t.rbegin(); }
309
310 /**
311 * Returns a read-only (constant) reverse iterator that points to the
312 * last element in the %multiset. Iteration is done in descending order
313 * according to the keys.
314 */
315 reverse_iterator
316 rend() const
317 { return _M_t.rend(); }
318
319 #ifdef __GXX_EXPERIMENTAL_CXX0X__
320 /**
321 * Returns a read-only (constant) iterator that points to the first
322 * element in the %multiset. Iteration is done in ascending order
323 * according to the keys.
324 */
325 iterator
326 cbegin() const
327 { return _M_t.begin(); }
328
329 /**
330 * Returns a read-only (constant) iterator that points one past the last
331 * element in the %multiset. Iteration is done in ascending order
332 * according to the keys.
333 */
334 iterator
335 cend() const
336 { return _M_t.end(); }
337
338 /**
339 * Returns a read-only (constant) reverse iterator that points to the
340 * last element in the %multiset. Iteration is done in descending order
341 * according to the keys.
342 */
343 reverse_iterator
344 crbegin() const
345 { return _M_t.rbegin(); }
346
347 /**
348 * Returns a read-only (constant) reverse iterator that points to the
349 * last element in the %multiset. Iteration is done in descending order
350 * according to the keys.
351 */
352 reverse_iterator
353 crend() const
354 { return _M_t.rend(); }
355 #endif
356
357 /// Returns true if the %set is empty.
358 bool
359 empty() const
360 { return _M_t.empty(); }
361
362 /// Returns the size of the %set.
363 size_type
364 size() const
365 { return _M_t.size(); }
366
367 /// Returns the maximum size of the %set.
368 size_type
369 max_size() const
370 { return _M_t.max_size(); }
371
372 /**
373 * @brief Swaps data with another %multiset.
374 * @param x A %multiset of the same element and allocator types.
375 *
376 * This exchanges the elements between two multisets in constant time.
377 * (It is only swapping a pointer, an integer, and an instance of the @c
378 * Compare type (which itself is often stateless and empty), so it should
379 * be quite fast.)
380 * Note that the global std::swap() function is specialized such that
381 * std::swap(s1,s2) will feed to this function.
382 */
383 void
384 #ifdef __GXX_EXPERIMENTAL_CXX0X__
385 swap(multiset&& __x)
386 #else
387 swap(multiset& __x)
388 #endif
389 { _M_t.swap(__x._M_t); }
390
391 // insert/erase
392 /**
393 * @brief Inserts an element into the %multiset.
394 * @param x Element to be inserted.
395 * @return An iterator that points to the inserted element.
396 *
397 * This function inserts an element into the %multiset. Contrary
398 * to a std::set the %multiset does not rely on unique keys and thus
399 * multiple copies of the same element can be inserted.
400 *
401 * Insertion requires logarithmic time.
402 */
403 iterator
404 insert(const value_type& __x)
405 { return _M_t._M_insert_equal(__x); }
406
407 /**
408 * @brief Inserts an element into the %multiset.
409 * @param position An iterator that serves as a hint as to where the
410 * element should be inserted.
411 * @param x Element to be inserted.
412 * @return An iterator that points to the inserted element.
413 *
414 * This function inserts an element into the %multiset. Contrary
415 * to a std::set the %multiset does not rely on unique keys and thus
416 * multiple copies of the same element can be inserted.
417 *
418 * Note that the first parameter is only a hint and can potentially
419 * improve the performance of the insertion process. A bad hint would
420 * cause no gains in efficiency.
421 *
422 * See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
423 * for more on "hinting".
424 *
425 * Insertion requires logarithmic time (if the hint is not taken).
426 */
427 iterator
428 insert(iterator __position, const value_type& __x)
429 { return _M_t._M_insert_equal_(__position, __x); }
430
431 /**
432 * @brief A template function that attempts to insert a range of elements.
433 * @param first Iterator pointing to the start of the range to be
434 * inserted.
435 * @param last Iterator pointing to the end of the range.
436 *
437 * Complexity similar to that of the range constructor.
438 */
439 template<typename _InputIterator>
440 void
441 insert(_InputIterator __first, _InputIterator __last)
442 { _M_t._M_insert_equal(__first, __last); }
443
444 #ifdef __GXX_EXPERIMENTAL_CXX0X__
445 /**
446 * @brief Attempts to insert a list of elements into the %multiset.
447 * @param list A std::initializer_list<value_type> of elements
448 * to be inserted.
449 *
450 * Complexity similar to that of the range constructor.
451 */
452 void
453 insert(initializer_list<value_type> __l)
454 { this->insert(__l.begin(), __l.end()); }
455 #endif
456
457 /**
458 * @brief Erases an element from a %multiset.
459 * @param position An iterator pointing to the element to be erased.
460 *
461 * This function erases an element, pointed to by the given iterator,
462 * from a %multiset. Note that this function only erases the element,
463 * and that if the element is itself a pointer, the pointed-to memory is
464 * not touched in any way. Managing the pointer is the user's
465 * responsibility.
466 */
467 void
468 erase(iterator __position)
469 { _M_t.erase(__position); }
470
471 /**
472 * @brief Erases elements according to the provided key.
473 * @param x Key of element to be erased.
474 * @return The number of elements erased.
475 *
476 * This function erases all elements located by the given key from a
477 * %multiset.
478 * Note that this function only erases the element, and that if
479 * the element is itself a pointer, the pointed-to memory is not touched
480 * in any way. Managing the pointer is the user's responsibility.
481 */
482 size_type
483 erase(const key_type& __x)
484 { return _M_t.erase(__x); }
485
486 /**
487 * @brief Erases a [first,last) range of elements from a %multiset.
488 * @param first Iterator pointing to the start of the range to be
489 * erased.
490 * @param last Iterator pointing to the end of the range to be erased.
491 *
492 * This function erases a sequence of elements from a %multiset.
493 * Note that this function only erases the elements, and that if
494 * the elements themselves are pointers, the pointed-to memory is not
495 * touched in any way. Managing the pointer is the user's responsibility.
496 */
497 void
498 erase(iterator __first, iterator __last)
499 { _M_t.erase(__first, __last); }
500
501 /**
502 * Erases all elements in a %multiset. Note that this function only
503 * erases the elements, and that if the elements themselves are pointers,
504 * the pointed-to memory is not touched in any way. Managing the pointer
505 * is the user's responsibility.
506 */
507 void
508 clear()
509 { _M_t.clear(); }
510
511 // multiset operations:
512
513 /**
514 * @brief Finds the number of elements with given key.
515 * @param x Key of elements to be located.
516 * @return Number of elements with specified key.
517 */
518 size_type
519 count(const key_type& __x) const
520 { return _M_t.count(__x); }
521
522 // _GLIBCXX_RESOLVE_LIB_DEFECTS
523 // 214. set::find() missing const overload
524 //@{
525 /**
526 * @brief Tries to locate an element in a %set.
527 * @param x Element to be located.
528 * @return Iterator pointing to sought-after element, or end() if not
529 * found.
530 *
531 * This function takes a key and tries to locate the element with which
532 * the key matches. If successful the function returns an iterator
533 * pointing to the sought after element. If unsuccessful it returns the
534 * past-the-end ( @c end() ) iterator.
535 */
536 iterator
537 find(const key_type& __x)
538 { return _M_t.find(__x); }
539
540 const_iterator
541 find(const key_type& __x) const
542 { return _M_t.find(__x); }
543 //@}
544
545 //@{
546 /**
547 * @brief Finds the beginning of a subsequence matching given key.
548 * @param x Key to be located.
549 * @return Iterator pointing to first element equal to or greater
550 * than key, or end().
551 *
552 * This function returns the first element of a subsequence of elements
553 * that matches the given key. If unsuccessful it returns an iterator
554 * pointing to the first element that has a greater value than given key
555 * or end() if no such element exists.
556 */
557 iterator
558 lower_bound(const key_type& __x)
559 { return _M_t.lower_bound(__x); }
560
561 const_iterator
562 lower_bound(const key_type& __x) const
563 { return _M_t.lower_bound(__x); }
564 //@}
565
566 //@{
567 /**
568 * @brief Finds the end of a subsequence matching given key.
569 * @param x Key to be located.
570 * @return Iterator pointing to the first element
571 * greater than key, or end().
572 */
573 iterator
574 upper_bound(const key_type& __x)
575 { return _M_t.upper_bound(__x); }
576
577 const_iterator
578 upper_bound(const key_type& __x) const
579 { return _M_t.upper_bound(__x); }
580 //@}
581
582 //@{
583 /**
584 * @brief Finds a subsequence matching given key.
585 * @param x Key to be located.
586 * @return Pair of iterators that possibly points to the subsequence
587 * matching given key.
588 *
589 * This function is equivalent to
590 * @code
591 * std::make_pair(c.lower_bound(val),
592 * c.upper_bound(val))
593 * @endcode
594 * (but is faster than making the calls separately).
595 *
596 * This function probably only makes sense for multisets.
597 */
598 std::pair<iterator, iterator>
599 equal_range(const key_type& __x)
600 { return _M_t.equal_range(__x); }
601
602 std::pair<const_iterator, const_iterator>
603 equal_range(const key_type& __x) const
604 { return _M_t.equal_range(__x); }
605
606 template<typename _K1, typename _C1, typename _A1>
607 friend bool
608 operator==(const multiset<_K1, _C1, _A1>&,
609 const multiset<_K1, _C1, _A1>&);
610
611 template<typename _K1, typename _C1, typename _A1>
612 friend bool
613 operator< (const multiset<_K1, _C1, _A1>&,
614 const multiset<_K1, _C1, _A1>&);
615 };
616
617 /**
618 * @brief Multiset equality comparison.
619 * @param x A %multiset.
620 * @param y A %multiset of the same type as @a x.
621 * @return True iff the size and elements of the multisets are equal.
622 *
623 * This is an equivalence relation. It is linear in the size of the
624 * multisets.
625 * Multisets are considered equivalent if their sizes are equal, and if
626 * corresponding elements compare equal.
627 */
628 template<typename _Key, typename _Compare, typename _Alloc>
629 inline bool
630 operator==(const multiset<_Key, _Compare, _Alloc>& __x,
631 const multiset<_Key, _Compare, _Alloc>& __y)
632 { return __x._M_t == __y._M_t; }
633
634 /**
635 * @brief Multiset ordering relation.
636 * @param x A %multiset.
637 * @param y A %multiset of the same type as @a x.
638 * @return True iff @a x is lexicographically less than @a y.
639 *
640 * This is a total ordering relation. It is linear in the size of the
641 * maps. The elements must be comparable with @c <.
642 *
643 * See std::lexicographical_compare() for how the determination is made.
644 */
645 template<typename _Key, typename _Compare, typename _Alloc>
646 inline bool
647 operator<(const multiset<_Key, _Compare, _Alloc>& __x,
648 const multiset<_Key, _Compare, _Alloc>& __y)
649 { return __x._M_t < __y._M_t; }
650
651 /// Returns !(x == y).
652 template<typename _Key, typename _Compare, typename _Alloc>
653 inline bool
654 operator!=(const multiset<_Key, _Compare, _Alloc>& __x,
655 const multiset<_Key, _Compare, _Alloc>& __y)
656 { return !(__x == __y); }
657
658 /// Returns y < x.
659 template<typename _Key, typename _Compare, typename _Alloc>
660 inline bool
661 operator>(const multiset<_Key,_Compare,_Alloc>& __x,
662 const multiset<_Key,_Compare,_Alloc>& __y)
663 { return __y < __x; }
664
665 /// Returns !(y < x)
666 template<typename _Key, typename _Compare, typename _Alloc>
667 inline bool
668 operator<=(const multiset<_Key, _Compare, _Alloc>& __x,
669 const multiset<_Key, _Compare, _Alloc>& __y)
670 { return !(__y < __x); }
671
672 /// Returns !(x < y)
673 template<typename _Key, typename _Compare, typename _Alloc>
674 inline bool
675 operator>=(const multiset<_Key, _Compare, _Alloc>& __x,
676 const multiset<_Key, _Compare, _Alloc>& __y)
677 { return !(__x < __y); }
678
679 /// See std::multiset::swap().
680 template<typename _Key, typename _Compare, typename _Alloc>
681 inline void
682 swap(multiset<_Key, _Compare, _Alloc>& __x,
683 multiset<_Key, _Compare, _Alloc>& __y)
684 { __x.swap(__y); }
685
686 #ifdef __GXX_EXPERIMENTAL_CXX0X__
687 template<typename _Key, typename _Compare, typename _Alloc>
688 inline void
689 swap(multiset<_Key, _Compare, _Alloc>&& __x,
690 multiset<_Key, _Compare, _Alloc>& __y)
691 { __x.swap(__y); }
692
693 template<typename _Key, typename _Compare, typename _Alloc>
694 inline void
695 swap(multiset<_Key, _Compare, _Alloc>& __x,
696 multiset<_Key, _Compare, _Alloc>&& __y)
697 { __x.swap(__y); }
698 #endif
699
700 _GLIBCXX_END_NESTED_NAMESPACE
701
702 #endif /* _STL_MULTISET_H */