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