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