2017-10-17 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_multimap.h
blob6f5cb7a47db7f6a58c9250f0bd48e965e6ff0293
1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_multimap.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{map}
56 #ifndef _STL_MULTIMAP_H
57 #define _STL_MULTIMAP_H 1
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
64 namespace std _GLIBCXX_VISIBILITY(default)
66 _GLIBCXX_BEGIN_NAMESPACE_VERSION
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
70 class map;
72 /**
73 * @brief A standard container made up of (key,value) pairs, which can be
74 * retrieved based on a key, in logarithmic time.
76 * @ingroup associative_containers
78 * @tparam _Key Type of key objects.
79 * @tparam _Tp Type of mapped objects.
80 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
81 * @tparam _Alloc Allocator type, defaults to
82 * allocator<pair<const _Key, _Tp>.
84 * Meets the requirements of a <a href="tables.html#65">container</a>, a
85 * <a href="tables.html#66">reversible container</a>, and an
86 * <a href="tables.html#69">associative container</a> (using equivalent
87 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
88 * is T, and the value_type is std::pair<const Key,T>.
90 * Multimaps support bidirectional iterators.
92 * The private tree data is declared exactly the same way for map and
93 * multimap; the distinction is made entirely in how the tree functions are
94 * called (*_unique versus *_equal, same as the standard).
96 template <typename _Key, typename _Tp,
97 typename _Compare = std::less<_Key>,
98 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
99 class multimap
101 public:
102 typedef _Key key_type;
103 typedef _Tp mapped_type;
104 typedef std::pair<const _Key, _Tp> value_type;
105 typedef _Compare key_compare;
106 typedef _Alloc allocator_type;
108 private:
109 #ifdef _GLIBCXX_CONCEPT_CHECKS
110 // concept requirements
111 typedef typename _Alloc::value_type _Alloc_value_type;
112 # if __cplusplus < 201103L
113 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
114 # endif
115 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
116 _BinaryFunctionConcept)
117 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
118 #endif
120 public:
121 class value_compare
122 : public std::binary_function<value_type, value_type, bool>
124 friend class multimap<_Key, _Tp, _Compare, _Alloc>;
125 protected:
126 _Compare comp;
128 value_compare(_Compare __c)
129 : comp(__c) { }
131 public:
132 bool operator()(const value_type& __x, const value_type& __y) const
133 { return comp(__x.first, __y.first); }
136 private:
137 /// This turns a red-black tree into a [multi]map.
138 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
139 rebind<value_type>::other _Pair_alloc_type;
141 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
142 key_compare, _Pair_alloc_type> _Rep_type;
143 /// The actual tree structure.
144 _Rep_type _M_t;
146 typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
148 public:
149 // many of these are specified differently in ISO, but the following are
150 // "functionally equivalent"
151 typedef typename _Alloc_traits::pointer pointer;
152 typedef typename _Alloc_traits::const_pointer const_pointer;
153 typedef typename _Alloc_traits::reference reference;
154 typedef typename _Alloc_traits::const_reference const_reference;
155 typedef typename _Rep_type::iterator iterator;
156 typedef typename _Rep_type::const_iterator const_iterator;
157 typedef typename _Rep_type::size_type size_type;
158 typedef typename _Rep_type::difference_type difference_type;
159 typedef typename _Rep_type::reverse_iterator reverse_iterator;
160 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
162 #if __cplusplus > 201402L
163 using node_type = typename _Rep_type::node_type;
164 #endif
166 // [23.3.2] construct/copy/destroy
167 // (get_allocator() is also listed in this section)
170 * @brief Default constructor creates no elements.
172 #if __cplusplus < 201103L
173 multimap() : _M_t() { }
174 #else
175 multimap() = default;
176 #endif
179 * @brief Creates a %multimap with no elements.
180 * @param __comp A comparison object.
181 * @param __a An allocator object.
183 explicit
184 multimap(const _Compare& __comp,
185 const allocator_type& __a = allocator_type())
186 : _M_t(__comp, _Pair_alloc_type(__a)) { }
189 * @brief %Multimap copy constructor.
191 * Whether the allocator is copied depends on the allocator traits.
193 #if __cplusplus < 201103L
194 multimap(const multimap& __x)
195 : _M_t(__x._M_t) { }
196 #else
197 multimap(const multimap&) = default;
200 * @brief %Multimap move constructor.
202 * The newly-created %multimap contains the exact contents of the
203 * moved instance. The moved instance is a valid, but unspecified
204 * %multimap.
206 multimap(multimap&&) = default;
209 * @brief Builds a %multimap from an initializer_list.
210 * @param __l An initializer_list.
211 * @param __comp A comparison functor.
212 * @param __a An allocator object.
214 * Create a %multimap consisting of copies of the elements from
215 * the initializer_list. This is linear in N if the list is already
216 * sorted, and NlogN otherwise (where N is @a __l.size()).
218 multimap(initializer_list<value_type> __l,
219 const _Compare& __comp = _Compare(),
220 const allocator_type& __a = allocator_type())
221 : _M_t(__comp, _Pair_alloc_type(__a))
222 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
224 /// Allocator-extended default constructor.
225 explicit
226 multimap(const allocator_type& __a)
227 : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
229 /// Allocator-extended copy constructor.
230 multimap(const multimap& __m, const allocator_type& __a)
231 : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
233 /// Allocator-extended move constructor.
234 multimap(multimap&& __m, const allocator_type& __a)
235 noexcept(is_nothrow_copy_constructible<_Compare>::value
236 && _Alloc_traits::_S_always_equal())
237 : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
239 /// Allocator-extended initialier-list constructor.
240 multimap(initializer_list<value_type> __l, const allocator_type& __a)
241 : _M_t(_Compare(), _Pair_alloc_type(__a))
242 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
244 /// Allocator-extended range constructor.
245 template<typename _InputIterator>
246 multimap(_InputIterator __first, _InputIterator __last,
247 const allocator_type& __a)
248 : _M_t(_Compare(), _Pair_alloc_type(__a))
249 { _M_t._M_insert_equal(__first, __last); }
250 #endif
253 * @brief Builds a %multimap from a range.
254 * @param __first An input iterator.
255 * @param __last An input iterator.
257 * Create a %multimap consisting of copies of the elements from
258 * [__first,__last). This is linear in N if the range is already sorted,
259 * and NlogN otherwise (where N is distance(__first,__last)).
261 template<typename _InputIterator>
262 multimap(_InputIterator __first, _InputIterator __last)
263 : _M_t()
264 { _M_t._M_insert_equal(__first, __last); }
267 * @brief Builds a %multimap from a range.
268 * @param __first An input iterator.
269 * @param __last An input iterator.
270 * @param __comp A comparison functor.
271 * @param __a An allocator object.
273 * Create a %multimap consisting of copies of the elements from
274 * [__first,__last). This is linear in N if the range is already sorted,
275 * and NlogN otherwise (where N is distance(__first,__last)).
277 template<typename _InputIterator>
278 multimap(_InputIterator __first, _InputIterator __last,
279 const _Compare& __comp,
280 const allocator_type& __a = allocator_type())
281 : _M_t(__comp, _Pair_alloc_type(__a))
282 { _M_t._M_insert_equal(__first, __last); }
284 #if __cplusplus >= 201103L
286 * The dtor only erases the elements, and note that if the elements
287 * themselves are pointers, the pointed-to memory is not touched in any
288 * way. Managing the pointer is the user's responsibility.
290 ~multimap() = default;
291 #endif
294 * @brief %Multimap assignment operator.
296 * Whether the allocator is copied depends on the allocator traits.
298 #if __cplusplus < 201103L
299 multimap&
300 operator=(const multimap& __x)
302 _M_t = __x._M_t;
303 return *this;
305 #else
306 multimap&
307 operator=(const multimap&) = default;
309 /// Move assignment operator.
310 multimap&
311 operator=(multimap&&) = default;
314 * @brief %Multimap list assignment operator.
315 * @param __l An initializer_list.
317 * This function fills a %multimap with copies of the elements
318 * in the initializer list @a __l.
320 * Note that the assignment completely changes the %multimap and
321 * that the resulting %multimap's size is the same as the number
322 * of elements assigned.
324 multimap&
325 operator=(initializer_list<value_type> __l)
327 _M_t._M_assign_equal(__l.begin(), __l.end());
328 return *this;
330 #endif
332 /// Get a copy of the memory allocation object.
333 allocator_type
334 get_allocator() const _GLIBCXX_NOEXCEPT
335 { return allocator_type(_M_t.get_allocator()); }
337 // iterators
339 * Returns a read/write iterator that points to the first pair in the
340 * %multimap. Iteration is done in ascending order according to the
341 * keys.
343 iterator
344 begin() _GLIBCXX_NOEXCEPT
345 { return _M_t.begin(); }
348 * Returns a read-only (constant) iterator that points to the first pair
349 * in the %multimap. Iteration is done in ascending order according to
350 * the keys.
352 const_iterator
353 begin() const _GLIBCXX_NOEXCEPT
354 { return _M_t.begin(); }
357 * Returns a read/write iterator that points one past the last pair in
358 * the %multimap. Iteration is done in ascending order according to the
359 * keys.
361 iterator
362 end() _GLIBCXX_NOEXCEPT
363 { return _M_t.end(); }
366 * Returns a read-only (constant) iterator that points one past the last
367 * pair in the %multimap. Iteration is done in ascending order according
368 * to the keys.
370 const_iterator
371 end() const _GLIBCXX_NOEXCEPT
372 { return _M_t.end(); }
375 * Returns a read/write reverse iterator that points to the last pair in
376 * the %multimap. Iteration is done in descending order according to the
377 * keys.
379 reverse_iterator
380 rbegin() _GLIBCXX_NOEXCEPT
381 { return _M_t.rbegin(); }
384 * Returns a read-only (constant) reverse iterator that points to the
385 * last pair in the %multimap. Iteration is done in descending order
386 * according to the keys.
388 const_reverse_iterator
389 rbegin() const _GLIBCXX_NOEXCEPT
390 { return _M_t.rbegin(); }
393 * Returns a read/write reverse iterator that points to one before the
394 * first pair in the %multimap. Iteration is done in descending order
395 * according to the keys.
397 reverse_iterator
398 rend() _GLIBCXX_NOEXCEPT
399 { return _M_t.rend(); }
402 * Returns a read-only (constant) reverse iterator that points to one
403 * before the first pair in the %multimap. Iteration is done in
404 * descending order according to the keys.
406 const_reverse_iterator
407 rend() const _GLIBCXX_NOEXCEPT
408 { return _M_t.rend(); }
410 #if __cplusplus >= 201103L
412 * Returns a read-only (constant) iterator that points to the first pair
413 * in the %multimap. Iteration is done in ascending order according to
414 * the keys.
416 const_iterator
417 cbegin() const noexcept
418 { return _M_t.begin(); }
421 * Returns a read-only (constant) iterator that points one past the last
422 * pair in the %multimap. Iteration is done in ascending order according
423 * to the keys.
425 const_iterator
426 cend() const noexcept
427 { return _M_t.end(); }
430 * Returns a read-only (constant) reverse iterator that points to the
431 * last pair in the %multimap. Iteration is done in descending order
432 * according to the keys.
434 const_reverse_iterator
435 crbegin() const noexcept
436 { return _M_t.rbegin(); }
439 * Returns a read-only (constant) reverse iterator that points to one
440 * before the first pair in the %multimap. Iteration is done in
441 * descending order according to the keys.
443 const_reverse_iterator
444 crend() const noexcept
445 { return _M_t.rend(); }
446 #endif
448 // capacity
449 /** Returns true if the %multimap is empty. */
450 bool
451 empty() const _GLIBCXX_NOEXCEPT
452 { return _M_t.empty(); }
454 /** Returns the size of the %multimap. */
455 size_type
456 size() const _GLIBCXX_NOEXCEPT
457 { return _M_t.size(); }
459 /** Returns the maximum size of the %multimap. */
460 size_type
461 max_size() const _GLIBCXX_NOEXCEPT
462 { return _M_t.max_size(); }
464 // modifiers
465 #if __cplusplus >= 201103L
467 * @brief Build and insert a std::pair into the %multimap.
469 * @param __args Arguments used to generate a new pair instance (see
470 * std::piecewise_contruct for passing arguments to each
471 * part of the pair constructor).
473 * @return An iterator that points to the inserted (key,value) pair.
475 * This function builds and inserts a (key, value) %pair into the
476 * %multimap.
477 * Contrary to a std::map the %multimap does not rely on unique keys and
478 * thus multiple pairs with the same key can be inserted.
480 * Insertion requires logarithmic time.
482 template<typename... _Args>
483 iterator
484 emplace(_Args&&... __args)
485 { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
488 * @brief Builds and inserts a std::pair into the %multimap.
490 * @param __pos An iterator that serves as a hint as to where the pair
491 * should be inserted.
492 * @param __args Arguments used to generate a new pair instance (see
493 * std::piecewise_contruct for passing arguments to each
494 * part of the pair constructor).
495 * @return An iterator that points to the inserted (key,value) pair.
497 * This function inserts a (key, value) pair into the %multimap.
498 * Contrary to a std::map the %multimap does not rely on unique keys and
499 * thus multiple pairs with the same key can be inserted.
500 * Note that the first parameter is only a hint and can potentially
501 * improve the performance of the insertion process. A bad hint would
502 * cause no gains in efficiency.
504 * For more on @a hinting, see:
505 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
507 * Insertion requires logarithmic time (if the hint is not taken).
509 template<typename... _Args>
510 iterator
511 emplace_hint(const_iterator __pos, _Args&&... __args)
513 return _M_t._M_emplace_hint_equal(__pos,
514 std::forward<_Args>(__args)...);
516 #endif
519 * @brief Inserts a std::pair into the %multimap.
520 * @param __x Pair to be inserted (see std::make_pair for easy creation
521 * of pairs).
522 * @return An iterator that points to the inserted (key,value) pair.
524 * This function inserts a (key, value) pair into the %multimap.
525 * Contrary to a std::map the %multimap does not rely on unique keys and
526 * thus multiple pairs with the same key can be inserted.
528 * Insertion requires logarithmic time.
529 * @{
531 iterator
532 insert(const value_type& __x)
533 { return _M_t._M_insert_equal(__x); }
535 #if __cplusplus >= 201103L
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 2354. Unnecessary copying when inserting into maps with braced-init
538 iterator
539 insert(value_type&& __x)
540 { return _M_t._M_insert_equal(std::move(__x)); }
542 template<typename _Pair, typename = typename
543 std::enable_if<std::is_constructible<value_type,
544 _Pair&&>::value>::type>
545 iterator
546 insert(_Pair&& __x)
547 { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
548 #endif
549 // @}
552 * @brief Inserts a std::pair into the %multimap.
553 * @param __position An iterator that serves as a hint as to where the
554 * pair should be inserted.
555 * @param __x Pair to be inserted (see std::make_pair for easy creation
556 * of pairs).
557 * @return An iterator that points to the inserted (key,value) pair.
559 * This function inserts a (key, value) pair into the %multimap.
560 * Contrary to a std::map the %multimap does not rely on unique keys and
561 * thus multiple pairs with the same key can be inserted.
562 * Note that the first parameter is only a hint and can potentially
563 * improve the performance of the insertion process. A bad hint would
564 * cause no gains in efficiency.
566 * For more on @a hinting, see:
567 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
569 * Insertion requires logarithmic time (if the hint is not taken).
570 * @{
572 iterator
573 #if __cplusplus >= 201103L
574 insert(const_iterator __position, const value_type& __x)
575 #else
576 insert(iterator __position, const value_type& __x)
577 #endif
578 { return _M_t._M_insert_equal_(__position, __x); }
580 #if __cplusplus >= 201103L
581 // _GLIBCXX_RESOLVE_LIB_DEFECTS
582 // 2354. Unnecessary copying when inserting into maps with braced-init
583 iterator
584 insert(const_iterator __position, value_type&& __x)
585 { return _M_t._M_insert_equal_(__position, std::move(__x)); }
587 template<typename _Pair, typename = typename
588 std::enable_if<std::is_constructible<value_type,
589 _Pair&&>::value>::type>
590 iterator
591 insert(const_iterator __position, _Pair&& __x)
592 { return _M_t._M_insert_equal_(__position,
593 std::forward<_Pair>(__x)); }
594 #endif
595 // @}
598 * @brief A template function that attempts to insert a range
599 * of elements.
600 * @param __first Iterator pointing to the start of the range to be
601 * inserted.
602 * @param __last Iterator pointing to the end of the range.
604 * Complexity similar to that of the range constructor.
606 template<typename _InputIterator>
607 void
608 insert(_InputIterator __first, _InputIterator __last)
609 { _M_t._M_insert_equal(__first, __last); }
611 #if __cplusplus >= 201103L
613 * @brief Attempts to insert a list of std::pairs into the %multimap.
614 * @param __l A std::initializer_list<value_type> of pairs to be
615 * inserted.
617 * Complexity similar to that of the range constructor.
619 void
620 insert(initializer_list<value_type> __l)
621 { this->insert(__l.begin(), __l.end()); }
622 #endif
624 #if __cplusplus > 201402L
625 /// Extract a node.
626 node_type
627 extract(const_iterator __pos)
629 __glibcxx_assert(__pos != end());
630 return _M_t.extract(__pos);
633 /// Extract a node.
634 node_type
635 extract(const key_type& __x)
636 { return _M_t.extract(__x); }
638 /// Re-insert an extracted node.
639 iterator
640 insert(node_type&& __nh)
641 { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
643 /// Re-insert an extracted node.
644 iterator
645 insert(const_iterator __hint, node_type&& __nh)
646 { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
648 template<typename, typename>
649 friend class _Rb_tree_merge_helper;
651 template<typename _C2>
652 void
653 merge(multimap<_Key, _Tp, _C2, _Alloc>& __source)
655 using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
656 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
659 template<typename _C2>
660 void
661 merge(multimap<_Key, _Tp, _C2, _Alloc>&& __source)
662 { merge(__source); }
664 template<typename _C2>
665 void
666 merge(map<_Key, _Tp, _C2, _Alloc>& __source)
668 using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
669 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
672 template<typename _C2>
673 void
674 merge(map<_Key, _Tp, _C2, _Alloc>&& __source)
675 { merge(__source); }
676 #endif // C++17
678 #if __cplusplus >= 201103L
679 // _GLIBCXX_RESOLVE_LIB_DEFECTS
680 // DR 130. Associative erase should return an iterator.
682 * @brief Erases an element from a %multimap.
683 * @param __position An iterator pointing to the element to be erased.
684 * @return An iterator pointing to the element immediately following
685 * @a position prior to the element being erased. If no such
686 * element exists, end() is returned.
688 * This function erases an element, pointed to by the given iterator,
689 * from a %multimap. Note that this function only erases the element,
690 * and that if the element is itself a pointer, the pointed-to memory is
691 * not touched in any way. Managing the pointer is the user's
692 * responsibility.
694 * @{
696 iterator
697 erase(const_iterator __position)
698 { return _M_t.erase(__position); }
700 // LWG 2059.
701 _GLIBCXX_ABI_TAG_CXX11
702 iterator
703 erase(iterator __position)
704 { return _M_t.erase(__position); }
705 // @}
706 #else
708 * @brief Erases an element from a %multimap.
709 * @param __position An iterator pointing to the element to be erased.
711 * This function erases an element, pointed to by the given iterator,
712 * from a %multimap. Note that this function only erases the element,
713 * and that if the element is itself a pointer, the pointed-to memory is
714 * not touched in any way. Managing the pointer is the user's
715 * responsibility.
717 void
718 erase(iterator __position)
719 { _M_t.erase(__position); }
720 #endif
723 * @brief Erases elements according to the provided key.
724 * @param __x Key of element to be erased.
725 * @return The number of elements erased.
727 * This function erases all elements located by the given key from a
728 * %multimap.
729 * Note that this function only erases the element, and that if
730 * the element is itself a pointer, the pointed-to memory is not touched
731 * in any way. Managing the pointer is the user's responsibility.
733 size_type
734 erase(const key_type& __x)
735 { return _M_t.erase(__x); }
737 #if __cplusplus >= 201103L
738 // _GLIBCXX_RESOLVE_LIB_DEFECTS
739 // DR 130. Associative erase should return an iterator.
741 * @brief Erases a [first,last) range of elements from a %multimap.
742 * @param __first Iterator pointing to the start of the range to be
743 * erased.
744 * @param __last Iterator pointing to the end of the range to be
745 * erased .
746 * @return The iterator @a __last.
748 * This function erases a sequence of elements from a %multimap.
749 * Note that this function only erases the elements, and that if
750 * the elements themselves are pointers, the pointed-to memory is not
751 * touched in any way. Managing the pointer is the user's
752 * responsibility.
754 iterator
755 erase(const_iterator __first, const_iterator __last)
756 { return _M_t.erase(__first, __last); }
757 #else
758 // _GLIBCXX_RESOLVE_LIB_DEFECTS
759 // DR 130. Associative erase should return an iterator.
761 * @brief Erases a [first,last) range of elements from a %multimap.
762 * @param __first Iterator pointing to the start of the range to be
763 * erased.
764 * @param __last Iterator pointing to the end of the range to
765 * be erased.
767 * This function erases a sequence of elements from a %multimap.
768 * Note that this function only erases the elements, and that if
769 * the elements themselves are pointers, the pointed-to memory is not
770 * touched in any way. Managing the pointer is the user's
771 * responsibility.
773 void
774 erase(iterator __first, iterator __last)
775 { _M_t.erase(__first, __last); }
776 #endif
779 * @brief Swaps data with another %multimap.
780 * @param __x A %multimap of the same element and allocator types.
782 * This exchanges the elements between two multimaps in constant time.
783 * (It is only swapping a pointer, an integer, and an instance of
784 * the @c Compare type (which itself is often stateless and empty), so it
785 * should be quite fast.)
786 * Note that the global std::swap() function is specialized such that
787 * std::swap(m1,m2) will feed to this function.
789 * Whether the allocators are swapped depends on the allocator traits.
791 void
792 swap(multimap& __x)
793 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
794 { _M_t.swap(__x._M_t); }
797 * Erases all elements in a %multimap. Note that this function only
798 * erases the elements, and that if the elements themselves are pointers,
799 * the pointed-to memory is not touched in any way. Managing the pointer
800 * is the user's responsibility.
802 void
803 clear() _GLIBCXX_NOEXCEPT
804 { _M_t.clear(); }
806 // observers
808 * Returns the key comparison object out of which the %multimap
809 * was constructed.
811 key_compare
812 key_comp() const
813 { return _M_t.key_comp(); }
816 * Returns a value comparison object, built from the key comparison
817 * object out of which the %multimap was constructed.
819 value_compare
820 value_comp() const
821 { return value_compare(_M_t.key_comp()); }
823 // multimap operations
825 //@{
827 * @brief Tries to locate an element in a %multimap.
828 * @param __x Key of (key, value) pair to be located.
829 * @return Iterator pointing to sought-after element,
830 * or end() if not found.
832 * This function takes a key and tries to locate the element with which
833 * the key matches. If successful the function returns an iterator
834 * pointing to the sought after %pair. If unsuccessful it returns the
835 * past-the-end ( @c end() ) iterator.
837 iterator
838 find(const key_type& __x)
839 { return _M_t.find(__x); }
841 #if __cplusplus > 201103L
842 template<typename _Kt>
843 auto
844 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
845 { return _M_t._M_find_tr(__x); }
846 #endif
847 //@}
849 //@{
851 * @brief Tries to locate an element in a %multimap.
852 * @param __x Key of (key, value) pair to be located.
853 * @return Read-only (constant) iterator pointing to sought-after
854 * element, or end() if not found.
856 * This function takes a key and tries to locate the element with which
857 * the key matches. If successful the function returns a constant
858 * iterator pointing to the sought after %pair. If unsuccessful it
859 * returns the past-the-end ( @c end() ) iterator.
861 const_iterator
862 find(const key_type& __x) const
863 { return _M_t.find(__x); }
865 #if __cplusplus > 201103L
866 template<typename _Kt>
867 auto
868 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
869 { return _M_t._M_find_tr(__x); }
870 #endif
871 //@}
873 //@{
875 * @brief Finds the number of elements with given key.
876 * @param __x Key of (key, value) pairs to be located.
877 * @return Number of elements with specified key.
879 size_type
880 count(const key_type& __x) const
881 { return _M_t.count(__x); }
883 #if __cplusplus > 201103L
884 template<typename _Kt>
885 auto
886 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
887 { return _M_t._M_count_tr(__x); }
888 #endif
889 //@}
891 //@{
893 * @brief Finds the beginning of a subsequence matching given key.
894 * @param __x Key of (key, value) pair to be located.
895 * @return Iterator pointing to first element equal to or greater
896 * than key, or end().
898 * This function returns the first element of a subsequence of elements
899 * that matches the given key. If unsuccessful it returns an iterator
900 * pointing to the first element that has a greater value than given key
901 * or end() if no such element exists.
903 iterator
904 lower_bound(const key_type& __x)
905 { return _M_t.lower_bound(__x); }
907 #if __cplusplus > 201103L
908 template<typename _Kt>
909 auto
910 lower_bound(const _Kt& __x)
911 -> decltype(iterator(_M_t._M_lower_bound_tr(__x)))
912 { return iterator(_M_t._M_lower_bound_tr(__x)); }
913 #endif
914 //@}
916 //@{
918 * @brief Finds the beginning of a subsequence matching given key.
919 * @param __x Key of (key, value) pair to be located.
920 * @return Read-only (constant) iterator pointing to first element
921 * equal to or greater than key, or end().
923 * This function returns the first element of a subsequence of
924 * elements that matches the given key. If unsuccessful the
925 * iterator will point to the next greatest element or, if no
926 * such greater element exists, to end().
928 const_iterator
929 lower_bound(const key_type& __x) const
930 { return _M_t.lower_bound(__x); }
932 #if __cplusplus > 201103L
933 template<typename _Kt>
934 auto
935 lower_bound(const _Kt& __x) const
936 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x)))
937 { return const_iterator(_M_t._M_lower_bound_tr(__x)); }
938 #endif
939 //@}
941 //@{
943 * @brief Finds the end of a subsequence matching given key.
944 * @param __x Key of (key, value) pair to be located.
945 * @return Iterator pointing to the first element
946 * greater than key, or end().
948 iterator
949 upper_bound(const key_type& __x)
950 { return _M_t.upper_bound(__x); }
952 #if __cplusplus > 201103L
953 template<typename _Kt>
954 auto
955 upper_bound(const _Kt& __x)
956 -> decltype(iterator(_M_t._M_upper_bound_tr(__x)))
957 { return iterator(_M_t._M_upper_bound_tr(__x)); }
958 #endif
959 //@}
961 //@{
963 * @brief Finds the end of a subsequence matching given key.
964 * @param __x Key of (key, value) pair to be located.
965 * @return Read-only (constant) iterator pointing to first iterator
966 * greater than key, or end().
968 const_iterator
969 upper_bound(const key_type& __x) const
970 { return _M_t.upper_bound(__x); }
972 #if __cplusplus > 201103L
973 template<typename _Kt>
974 auto
975 upper_bound(const _Kt& __x) const
976 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x)))
977 { return const_iterator(_M_t._M_upper_bound_tr(__x)); }
978 #endif
979 //@}
981 //@{
983 * @brief Finds a subsequence matching given key.
984 * @param __x Key of (key, value) pairs to be located.
985 * @return Pair of iterators that possibly points to the subsequence
986 * matching given key.
988 * This function is equivalent to
989 * @code
990 * std::make_pair(c.lower_bound(val),
991 * c.upper_bound(val))
992 * @endcode
993 * (but is faster than making the calls separately).
995 std::pair<iterator, iterator>
996 equal_range(const key_type& __x)
997 { return _M_t.equal_range(__x); }
999 #if __cplusplus > 201103L
1000 template<typename _Kt>
1001 auto
1002 equal_range(const _Kt& __x)
1003 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)))
1004 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); }
1005 #endif
1006 //@}
1008 //@{
1010 * @brief Finds a subsequence matching given key.
1011 * @param __x Key of (key, value) pairs to be located.
1012 * @return Pair of read-only (constant) iterators that possibly points
1013 * to the subsequence matching given key.
1015 * This function is equivalent to
1016 * @code
1017 * std::make_pair(c.lower_bound(val),
1018 * c.upper_bound(val))
1019 * @endcode
1020 * (but is faster than making the calls separately).
1022 std::pair<const_iterator, const_iterator>
1023 equal_range(const key_type& __x) const
1024 { return _M_t.equal_range(__x); }
1026 #if __cplusplus > 201103L
1027 template<typename _Kt>
1028 auto
1029 equal_range(const _Kt& __x) const
1030 -> decltype(pair<const_iterator, const_iterator>(
1031 _M_t._M_equal_range_tr(__x)))
1033 return pair<const_iterator, const_iterator>(
1034 _M_t._M_equal_range_tr(__x));
1036 #endif
1037 //@}
1039 template<typename _K1, typename _T1, typename _C1, typename _A1>
1040 friend bool
1041 operator==(const multimap<_K1, _T1, _C1, _A1>&,
1042 const multimap<_K1, _T1, _C1, _A1>&);
1044 template<typename _K1, typename _T1, typename _C1, typename _A1>
1045 friend bool
1046 operator<(const multimap<_K1, _T1, _C1, _A1>&,
1047 const multimap<_K1, _T1, _C1, _A1>&);
1051 * @brief Multimap equality comparison.
1052 * @param __x A %multimap.
1053 * @param __y A %multimap of the same type as @a __x.
1054 * @return True iff the size and elements of the maps are equal.
1056 * This is an equivalence relation. It is linear in the size of the
1057 * multimaps. Multimaps are considered equivalent if their sizes are equal,
1058 * and if corresponding elements compare equal.
1060 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1061 inline bool
1062 operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1063 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1064 { return __x._M_t == __y._M_t; }
1067 * @brief Multimap ordering relation.
1068 * @param __x A %multimap.
1069 * @param __y A %multimap of the same type as @a __x.
1070 * @return True iff @a x is lexicographically less than @a y.
1072 * This is a total ordering relation. It is linear in the size of the
1073 * multimaps. The elements must be comparable with @c <.
1075 * See std::lexicographical_compare() for how the determination is made.
1077 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1078 inline bool
1079 operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1080 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1081 { return __x._M_t < __y._M_t; }
1083 /// Based on operator==
1084 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1085 inline bool
1086 operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1087 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1088 { return !(__x == __y); }
1090 /// Based on operator<
1091 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1092 inline bool
1093 operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1094 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1095 { return __y < __x; }
1097 /// Based on operator<
1098 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1099 inline bool
1100 operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1101 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1102 { return !(__y < __x); }
1104 /// Based on operator<
1105 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1106 inline bool
1107 operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1108 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1109 { return !(__x < __y); }
1111 /// See std::multimap::swap().
1112 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1113 inline void
1114 swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1115 multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1116 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1117 { __x.swap(__y); }
1119 _GLIBCXX_END_NAMESPACE_CONTAINER
1121 #if __cplusplus > 201402L
1122 // Allow std::multimap access to internals of compatible maps.
1123 template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
1124 typename _Cmp2>
1125 struct
1126 _Rb_tree_merge_helper<_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>,
1127 _Cmp2>
1129 private:
1130 friend class _GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>;
1132 static auto&
1133 _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
1134 { return __map._M_t; }
1136 static auto&
1137 _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
1138 { return __map._M_t; }
1140 #endif // C++17
1142 _GLIBCXX_END_NAMESPACE_VERSION
1143 } // namespace std
1145 #endif /* _STL_MULTIMAP_H */