2016-10-26 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_multimap.h
blob7e86b76e9878c8d9b4dfcd26daf1cb2f0724c18d
1 // Multimap implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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_CONTAINER
68 template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
69 class map;
71 /**
72 * @brief A standard container made up of (key,value) pairs, which can be
73 * retrieved based on a key, in logarithmic time.
75 * @ingroup associative_containers
77 * @tparam _Key Type of key objects.
78 * @tparam _Tp Type of mapped objects.
79 * @tparam _Compare Comparison function object type, defaults to less<_Key>.
80 * @tparam _Alloc Allocator type, defaults to
81 * allocator<pair<const _Key, _Tp>.
83 * Meets the requirements of a <a href="tables.html#65">container</a>, a
84 * <a href="tables.html#66">reversible container</a>, and an
85 * <a href="tables.html#69">associative container</a> (using equivalent
86 * keys). For a @c multimap<Key,T> the key_type is Key, the mapped_type
87 * is T, and the value_type is std::pair<const Key,T>.
89 * Multimaps support bidirectional iterators.
91 * The private tree data is declared exactly the same way for map and
92 * multimap; the distinction is made entirely in how the tree functions are
93 * called (*_unique versus *_equal, same as the standard).
95 template <typename _Key, typename _Tp,
96 typename _Compare = std::less<_Key>,
97 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
98 class multimap
100 public:
101 typedef _Key key_type;
102 typedef _Tp mapped_type;
103 typedef std::pair<const _Key, _Tp> value_type;
104 typedef _Compare key_compare;
105 typedef _Alloc allocator_type;
107 private:
108 // concept requirements
109 typedef typename _Alloc::value_type _Alloc_value_type;
110 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
111 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
112 _BinaryFunctionConcept)
113 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
115 public:
116 class value_compare
117 : public std::binary_function<value_type, value_type, bool>
119 friend class multimap<_Key, _Tp, _Compare, _Alloc>;
120 protected:
121 _Compare comp;
123 value_compare(_Compare __c)
124 : comp(__c) { }
126 public:
127 bool operator()(const value_type& __x, const value_type& __y) const
128 { return comp(__x.first, __y.first); }
131 private:
132 /// This turns a red-black tree into a [multi]map.
133 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
134 rebind<value_type>::other _Pair_alloc_type;
136 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
137 key_compare, _Pair_alloc_type> _Rep_type;
138 /// The actual tree structure.
139 _Rep_type _M_t;
141 typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
143 public:
144 // many of these are specified differently in ISO, but the following are
145 // "functionally equivalent"
146 typedef typename _Alloc_traits::pointer pointer;
147 typedef typename _Alloc_traits::const_pointer const_pointer;
148 typedef typename _Alloc_traits::reference reference;
149 typedef typename _Alloc_traits::const_reference const_reference;
150 typedef typename _Rep_type::iterator iterator;
151 typedef typename _Rep_type::const_iterator const_iterator;
152 typedef typename _Rep_type::size_type size_type;
153 typedef typename _Rep_type::difference_type difference_type;
154 typedef typename _Rep_type::reverse_iterator reverse_iterator;
155 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
157 #if __cplusplus > 201402L
158 using node_type = typename _Rep_type::node_type;
159 #endif
161 // [23.3.2] construct/copy/destroy
162 // (get_allocator() is also listed in this section)
165 * @brief Default constructor creates no elements.
167 #if __cplusplus < 201103L
168 multimap() : _M_t() { }
169 #else
170 multimap() = default;
171 #endif
174 * @brief Creates a %multimap with no elements.
175 * @param __comp A comparison object.
176 * @param __a An allocator object.
178 explicit
179 multimap(const _Compare& __comp,
180 const allocator_type& __a = allocator_type())
181 : _M_t(__comp, _Pair_alloc_type(__a)) { }
184 * @brief %Multimap copy constructor.
185 * @param __x A %multimap of identical element and allocator types.
187 * The newly-created %multimap uses a copy of the allocator object used
188 * by @a __x (unless the allocator traits dictate a different object).
190 multimap(const multimap& __x)
191 : _M_t(__x._M_t) { }
193 #if __cplusplus >= 201103L
195 * @brief %Multimap move constructor.
196 * @param __x A %multimap of identical element and allocator types.
198 * The newly-created %multimap contains the exact contents of @a __x.
199 * The contents of @a __x are a valid, but unspecified %multimap.
201 multimap(multimap&& __x)
202 noexcept(is_nothrow_copy_constructible<_Compare>::value)
203 : _M_t(std::move(__x._M_t)) { }
206 * @brief Builds a %multimap from an initializer_list.
207 * @param __l An initializer_list.
208 * @param __comp A comparison functor.
209 * @param __a An allocator object.
211 * Create a %multimap consisting of copies of the elements from
212 * the initializer_list. This is linear in N if the list is already
213 * sorted, and NlogN otherwise (where N is @a __l.size()).
215 multimap(initializer_list<value_type> __l,
216 const _Compare& __comp = _Compare(),
217 const allocator_type& __a = allocator_type())
218 : _M_t(__comp, _Pair_alloc_type(__a))
219 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
221 /// Allocator-extended default constructor.
222 explicit
223 multimap(const allocator_type& __a)
224 : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
226 /// Allocator-extended copy constructor.
227 multimap(const multimap& __m, const allocator_type& __a)
228 : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
230 /// Allocator-extended move constructor.
231 multimap(multimap&& __m, const allocator_type& __a)
232 noexcept(is_nothrow_copy_constructible<_Compare>::value
233 && _Alloc_traits::_S_always_equal())
234 : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
236 /// Allocator-extended initialier-list constructor.
237 multimap(initializer_list<value_type> __l, const allocator_type& __a)
238 : _M_t(_Compare(), _Pair_alloc_type(__a))
239 { _M_t._M_insert_equal(__l.begin(), __l.end()); }
241 /// Allocator-extended range constructor.
242 template<typename _InputIterator>
243 multimap(_InputIterator __first, _InputIterator __last,
244 const allocator_type& __a)
245 : _M_t(_Compare(), _Pair_alloc_type(__a))
246 { _M_t._M_insert_equal(__first, __last); }
247 #endif
250 * @brief Builds a %multimap from a range.
251 * @param __first An input iterator.
252 * @param __last An input iterator.
254 * Create a %multimap consisting of copies of the elements from
255 * [__first,__last). This is linear in N if the range is already sorted,
256 * and NlogN otherwise (where N is distance(__first,__last)).
258 template<typename _InputIterator>
259 multimap(_InputIterator __first, _InputIterator __last)
260 : _M_t()
261 { _M_t._M_insert_equal(__first, __last); }
264 * @brief Builds a %multimap from a range.
265 * @param __first An input iterator.
266 * @param __last An input iterator.
267 * @param __comp A comparison functor.
268 * @param __a An allocator object.
270 * Create a %multimap consisting of copies of the elements from
271 * [__first,__last). This is linear in N if the range is already sorted,
272 * and NlogN otherwise (where N is distance(__first,__last)).
274 template<typename _InputIterator>
275 multimap(_InputIterator __first, _InputIterator __last,
276 const _Compare& __comp,
277 const allocator_type& __a = allocator_type())
278 : _M_t(__comp, _Pair_alloc_type(__a))
279 { _M_t._M_insert_equal(__first, __last); }
281 // FIXME There is no dtor declared, but we should have something generated
282 // by Doxygen. I don't know what tags to add to this paragraph to make
283 // that happen:
285 * The dtor only erases the elements, and note that if the elements
286 * themselves are pointers, the pointed-to memory is not touched in any
287 * way. Managing the pointer is the user's responsibility.
291 * @brief %Multimap assignment operator.
292 * @param __x A %multimap of identical element and allocator types.
294 * All the elements of @a __x are copied.
296 * Whether the allocator is copied depends on the allocator traits.
298 multimap&
299 operator=(const multimap& __x)
301 _M_t = __x._M_t;
302 return *this;
305 #if __cplusplus >= 201103L
306 /// Move assignment operator.
307 multimap&
308 operator=(multimap&&) = default;
311 * @brief %Multimap list assignment operator.
312 * @param __l An initializer_list.
314 * This function fills a %multimap with copies of the elements
315 * in the initializer list @a __l.
317 * Note that the assignment completely changes the %multimap and
318 * that the resulting %multimap's size is the same as the number
319 * of elements assigned.
321 multimap&
322 operator=(initializer_list<value_type> __l)
324 _M_t._M_assign_equal(__l.begin(), __l.end());
325 return *this;
327 #endif
329 /// Get a copy of the memory allocation object.
330 allocator_type
331 get_allocator() const _GLIBCXX_NOEXCEPT
332 { return allocator_type(_M_t.get_allocator()); }
334 // iterators
336 * Returns a read/write iterator that points to the first pair in the
337 * %multimap. Iteration is done in ascending order according to the
338 * keys.
340 iterator
341 begin() _GLIBCXX_NOEXCEPT
342 { return _M_t.begin(); }
345 * Returns a read-only (constant) iterator that points to the first pair
346 * in the %multimap. Iteration is done in ascending order according to
347 * the keys.
349 const_iterator
350 begin() const _GLIBCXX_NOEXCEPT
351 { return _M_t.begin(); }
354 * Returns a read/write iterator that points one past the last pair in
355 * the %multimap. Iteration is done in ascending order according to the
356 * keys.
358 iterator
359 end() _GLIBCXX_NOEXCEPT
360 { return _M_t.end(); }
363 * Returns a read-only (constant) iterator that points one past the last
364 * pair in the %multimap. Iteration is done in ascending order according
365 * to the keys.
367 const_iterator
368 end() const _GLIBCXX_NOEXCEPT
369 { return _M_t.end(); }
372 * Returns a read/write reverse iterator that points to the last pair in
373 * the %multimap. Iteration is done in descending order according to the
374 * keys.
376 reverse_iterator
377 rbegin() _GLIBCXX_NOEXCEPT
378 { return _M_t.rbegin(); }
381 * Returns a read-only (constant) reverse iterator that points to the
382 * last pair in the %multimap. Iteration is done in descending order
383 * according to the keys.
385 const_reverse_iterator
386 rbegin() const _GLIBCXX_NOEXCEPT
387 { return _M_t.rbegin(); }
390 * Returns a read/write reverse iterator that points to one before the
391 * first pair in the %multimap. Iteration is done in descending order
392 * according to the keys.
394 reverse_iterator
395 rend() _GLIBCXX_NOEXCEPT
396 { return _M_t.rend(); }
399 * Returns a read-only (constant) reverse iterator that points to one
400 * before the first pair in the %multimap. Iteration is done in
401 * descending order according to the keys.
403 const_reverse_iterator
404 rend() const _GLIBCXX_NOEXCEPT
405 { return _M_t.rend(); }
407 #if __cplusplus >= 201103L
409 * Returns a read-only (constant) iterator that points to the first pair
410 * in the %multimap. Iteration is done in ascending order according to
411 * the keys.
413 const_iterator
414 cbegin() const noexcept
415 { return _M_t.begin(); }
418 * Returns a read-only (constant) iterator that points one past the last
419 * pair in the %multimap. Iteration is done in ascending order according
420 * to the keys.
422 const_iterator
423 cend() const noexcept
424 { return _M_t.end(); }
427 * Returns a read-only (constant) reverse iterator that points to the
428 * last pair in the %multimap. Iteration is done in descending order
429 * according to the keys.
431 const_reverse_iterator
432 crbegin() const noexcept
433 { return _M_t.rbegin(); }
436 * Returns a read-only (constant) reverse iterator that points to one
437 * before the first pair in the %multimap. Iteration is done in
438 * descending order according to the keys.
440 const_reverse_iterator
441 crend() const noexcept
442 { return _M_t.rend(); }
443 #endif
445 // capacity
446 /** Returns true if the %multimap is empty. */
447 bool
448 empty() const _GLIBCXX_NOEXCEPT
449 { return _M_t.empty(); }
451 /** Returns the size of the %multimap. */
452 size_type
453 size() const _GLIBCXX_NOEXCEPT
454 { return _M_t.size(); }
456 /** Returns the maximum size of the %multimap. */
457 size_type
458 max_size() const _GLIBCXX_NOEXCEPT
459 { return _M_t.max_size(); }
461 // modifiers
462 #if __cplusplus >= 201103L
464 * @brief Build and insert a std::pair into the %multimap.
466 * @param __args Arguments used to generate a new pair instance (see
467 * std::piecewise_contruct for passing arguments to each
468 * part of the pair constructor).
470 * @return An iterator that points to the inserted (key,value) pair.
472 * This function builds and inserts a (key, value) %pair into the
473 * %multimap.
474 * Contrary to a std::map the %multimap does not rely on unique keys and
475 * thus multiple pairs with the same key can be inserted.
477 * Insertion requires logarithmic time.
479 template<typename... _Args>
480 iterator
481 emplace(_Args&&... __args)
482 { return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
485 * @brief Builds and inserts a std::pair into the %multimap.
487 * @param __pos An iterator that serves as a hint as to where the pair
488 * should be inserted.
489 * @param __args Arguments used to generate a new pair instance (see
490 * std::piecewise_contruct for passing arguments to each
491 * part of the pair constructor).
492 * @return An iterator that points to the inserted (key,value) pair.
494 * This function inserts a (key, value) pair into the %multimap.
495 * Contrary to a std::map the %multimap does not rely on unique keys and
496 * thus multiple pairs with the same key can be inserted.
497 * Note that the first parameter is only a hint and can potentially
498 * improve the performance of the insertion process. A bad hint would
499 * cause no gains in efficiency.
501 * For more on @a hinting, see:
502 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
504 * Insertion requires logarithmic time (if the hint is not taken).
506 template<typename... _Args>
507 iterator
508 emplace_hint(const_iterator __pos, _Args&&... __args)
510 return _M_t._M_emplace_hint_equal(__pos,
511 std::forward<_Args>(__args)...);
513 #endif
516 * @brief Inserts a std::pair into the %multimap.
517 * @param __x Pair to be inserted (see std::make_pair for easy creation
518 * of pairs).
519 * @return An iterator that points to the inserted (key,value) pair.
521 * This function inserts a (key, value) pair into the %multimap.
522 * Contrary to a std::map the %multimap does not rely on unique keys and
523 * thus multiple pairs with the same key can be inserted.
525 * Insertion requires logarithmic time.
527 iterator
528 insert(const value_type& __x)
529 { return _M_t._M_insert_equal(__x); }
531 #if __cplusplus >= 201103L
532 template<typename _Pair, typename = typename
533 std::enable_if<std::is_constructible<value_type,
534 _Pair&&>::value>::type>
535 iterator
536 insert(_Pair&& __x)
537 { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
538 #endif
541 * @brief Inserts a std::pair into the %multimap.
542 * @param __position An iterator that serves as a hint as to where the
543 * pair should be inserted.
544 * @param __x Pair to be inserted (see std::make_pair for easy creation
545 * of pairs).
546 * @return An iterator that points to the inserted (key,value) pair.
548 * This function inserts a (key, value) pair into the %multimap.
549 * Contrary to a std::map the %multimap does not rely on unique keys and
550 * thus multiple pairs with the same key can be inserted.
551 * Note that the first parameter is only a hint and can potentially
552 * improve the performance of the insertion process. A bad hint would
553 * cause no gains in efficiency.
555 * For more on @a hinting, see:
556 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints
558 * Insertion requires logarithmic time (if the hint is not taken).
560 iterator
561 #if __cplusplus >= 201103L
562 insert(const_iterator __position, const value_type& __x)
563 #else
564 insert(iterator __position, const value_type& __x)
565 #endif
566 { return _M_t._M_insert_equal_(__position, __x); }
568 #if __cplusplus >= 201103L
569 template<typename _Pair, typename = typename
570 std::enable_if<std::is_constructible<value_type,
571 _Pair&&>::value>::type>
572 iterator
573 insert(const_iterator __position, _Pair&& __x)
574 { return _M_t._M_insert_equal_(__position,
575 std::forward<_Pair>(__x)); }
576 #endif
579 * @brief A template function that attempts to insert a range
580 * of elements.
581 * @param __first Iterator pointing to the start of the range to be
582 * inserted.
583 * @param __last Iterator pointing to the end of the range.
585 * Complexity similar to that of the range constructor.
587 template<typename _InputIterator>
588 void
589 insert(_InputIterator __first, _InputIterator __last)
590 { _M_t._M_insert_equal(__first, __last); }
592 #if __cplusplus >= 201103L
594 * @brief Attempts to insert a list of std::pairs into the %multimap.
595 * @param __l A std::initializer_list<value_type> of pairs to be
596 * inserted.
598 * Complexity similar to that of the range constructor.
600 void
601 insert(initializer_list<value_type> __l)
602 { this->insert(__l.begin(), __l.end()); }
603 #endif
605 #if __cplusplus > 201402L
606 /// Extract a node.
607 node_type
608 extract(const_iterator __pos)
610 __glibcxx_assert(__pos != end());
611 return _M_t.extract(__pos);
614 /// Extract a node.
615 node_type
616 extract(const key_type& __x)
617 { return _M_t.extract(__x); }
619 /// Re-insert an extracted node.
620 iterator
621 insert(node_type&& __nh)
622 { return _M_t._M_reinsert_node_equal(std::move(__nh)); }
624 /// Re-insert an extracted node.
625 iterator
626 insert(const_iterator __hint, node_type&& __nh)
627 { return _M_t._M_reinsert_node_hint_equal(__hint, std::move(__nh)); }
629 template<typename, typename>
630 friend class _Rb_tree_merge_helper;
632 template<typename _C2>
633 void
634 merge(multimap<_Key, _Tp, _C2, _Alloc>& __source)
636 using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
637 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
640 template<typename _C2>
641 void
642 merge(multimap<_Key, _Tp, _C2, _Alloc>&& __source)
643 { merge(__source); }
645 template<typename _C2>
646 void
647 merge(map<_Key, _Tp, _C2, _Alloc>& __source)
649 using _Merge_helper = _Rb_tree_merge_helper<multimap, _C2>;
650 _M_t._M_merge_equal(_Merge_helper::_S_get_tree(__source));
653 template<typename _C2>
654 void
655 merge(map<_Key, _Tp, _C2, _Alloc>&& __source)
656 { merge(__source); }
657 #endif // C++17
659 #if __cplusplus >= 201103L
660 // _GLIBCXX_RESOLVE_LIB_DEFECTS
661 // DR 130. Associative erase should return an iterator.
663 * @brief Erases an element from a %multimap.
664 * @param __position An iterator pointing to the element to be erased.
665 * @return An iterator pointing to the element immediately following
666 * @a position prior to the element being erased. If no such
667 * element exists, end() is returned.
669 * This function erases an element, pointed to by the given iterator,
670 * from a %multimap. Note that this function only erases the element,
671 * and that if the element is itself a pointer, the pointed-to memory is
672 * not touched in any way. Managing the pointer is the user's
673 * responsibility.
675 iterator
676 erase(const_iterator __position)
677 { return _M_t.erase(__position); }
679 // LWG 2059.
680 _GLIBCXX_ABI_TAG_CXX11
681 iterator
682 erase(iterator __position)
683 { return _M_t.erase(__position); }
684 #else
686 * @brief Erases an element from a %multimap.
687 * @param __position An iterator pointing to the element to be erased.
689 * This function erases an element, pointed to by the given iterator,
690 * from a %multimap. Note that this function only erases the element,
691 * and that if the element is itself a pointer, the pointed-to memory is
692 * not touched in any way. Managing the pointer is the user's
693 * responsibility.
695 void
696 erase(iterator __position)
697 { _M_t.erase(__position); }
698 #endif
701 * @brief Erases elements according to the provided key.
702 * @param __x Key of element to be erased.
703 * @return The number of elements erased.
705 * This function erases all elements located by the given key from a
706 * %multimap.
707 * Note that this function only erases the element, and that if
708 * the element is itself a pointer, the pointed-to memory is not touched
709 * in any way. Managing the pointer is the user's responsibility.
711 size_type
712 erase(const key_type& __x)
713 { return _M_t.erase(__x); }
715 #if __cplusplus >= 201103L
716 // _GLIBCXX_RESOLVE_LIB_DEFECTS
717 // DR 130. Associative erase should return an iterator.
719 * @brief Erases a [first,last) range of elements from a %multimap.
720 * @param __first Iterator pointing to the start of the range to be
721 * erased.
722 * @param __last Iterator pointing to the end of the range to be
723 * erased .
724 * @return The iterator @a __last.
726 * This function erases a sequence of elements from a %multimap.
727 * Note that this function only erases the elements, and that if
728 * the elements themselves are pointers, the pointed-to memory is not
729 * touched in any way. Managing the pointer is the user's
730 * responsibility.
732 iterator
733 erase(const_iterator __first, const_iterator __last)
734 { return _M_t.erase(__first, __last); }
735 #else
736 // _GLIBCXX_RESOLVE_LIB_DEFECTS
737 // DR 130. Associative erase should return an iterator.
739 * @brief Erases a [first,last) range of elements from a %multimap.
740 * @param __first Iterator pointing to the start of the range to be
741 * erased.
742 * @param __last Iterator pointing to the end of the range to
743 * be erased.
745 * This function erases a sequence of elements from a %multimap.
746 * Note that this function only erases the elements, and that if
747 * the elements themselves are pointers, the pointed-to memory is not
748 * touched in any way. Managing the pointer is the user's
749 * responsibility.
751 void
752 erase(iterator __first, iterator __last)
753 { _M_t.erase(__first, __last); }
754 #endif
757 * @brief Swaps data with another %multimap.
758 * @param __x A %multimap of the same element and allocator types.
760 * This exchanges the elements between two multimaps in constant time.
761 * (It is only swapping a pointer, an integer, and an instance of
762 * the @c Compare type (which itself is often stateless and empty), so it
763 * should be quite fast.)
764 * Note that the global std::swap() function is specialized such that
765 * std::swap(m1,m2) will feed to this function.
767 * Whether the allocators are swapped depends on the allocator traits.
769 void
770 swap(multimap& __x)
771 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
772 { _M_t.swap(__x._M_t); }
775 * Erases all elements in a %multimap. Note that this function only
776 * erases the elements, and that if the elements themselves are pointers,
777 * the pointed-to memory is not touched in any way. Managing the pointer
778 * is the user's responsibility.
780 void
781 clear() _GLIBCXX_NOEXCEPT
782 { _M_t.clear(); }
784 // observers
786 * Returns the key comparison object out of which the %multimap
787 * was constructed.
789 key_compare
790 key_comp() const
791 { return _M_t.key_comp(); }
794 * Returns a value comparison object, built from the key comparison
795 * object out of which the %multimap was constructed.
797 value_compare
798 value_comp() const
799 { return value_compare(_M_t.key_comp()); }
801 // multimap operations
803 //@{
805 * @brief Tries to locate an element in a %multimap.
806 * @param __x Key of (key, value) pair to be located.
807 * @return Iterator pointing to sought-after element,
808 * or end() if not found.
810 * This function takes a key and tries to locate the element with which
811 * the key matches. If successful the function returns an iterator
812 * pointing to the sought after %pair. If unsuccessful it returns the
813 * past-the-end ( @c end() ) iterator.
815 iterator
816 find(const key_type& __x)
817 { return _M_t.find(__x); }
819 #if __cplusplus > 201103L
820 template<typename _Kt>
821 auto
822 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
823 { return _M_t._M_find_tr(__x); }
824 #endif
825 //@}
827 //@{
829 * @brief Tries to locate an element in a %multimap.
830 * @param __x Key of (key, value) pair to be located.
831 * @return Read-only (constant) iterator pointing to sought-after
832 * element, or end() if not found.
834 * This function takes a key and tries to locate the element with which
835 * the key matches. If successful the function returns a constant
836 * iterator pointing to the sought after %pair. If unsuccessful it
837 * returns the past-the-end ( @c end() ) iterator.
839 const_iterator
840 find(const key_type& __x) const
841 { return _M_t.find(__x); }
843 #if __cplusplus > 201103L
844 template<typename _Kt>
845 auto
846 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
847 { return _M_t._M_find_tr(__x); }
848 #endif
849 //@}
851 //@{
853 * @brief Finds the number of elements with given key.
854 * @param __x Key of (key, value) pairs to be located.
855 * @return Number of elements with specified key.
857 size_type
858 count(const key_type& __x) const
859 { return _M_t.count(__x); }
861 #if __cplusplus > 201103L
862 template<typename _Kt>
863 auto
864 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x))
865 { return _M_t._M_count_tr(__x); }
866 #endif
867 //@}
869 //@{
871 * @brief Finds the beginning of a subsequence matching given key.
872 * @param __x Key of (key, value) pair to be located.
873 * @return Iterator pointing to first element equal to or greater
874 * than key, or end().
876 * This function returns the first element of a subsequence of elements
877 * that matches the given key. If unsuccessful it returns an iterator
878 * pointing to the first element that has a greater value than given key
879 * or end() if no such element exists.
881 iterator
882 lower_bound(const key_type& __x)
883 { return _M_t.lower_bound(__x); }
885 #if __cplusplus > 201103L
886 template<typename _Kt>
887 auto
888 lower_bound(const _Kt& __x)
889 -> decltype(_M_t._M_lower_bound_tr(__x))
890 { return _M_t._M_lower_bound_tr(__x); }
891 #endif
892 //@}
894 //@{
896 * @brief Finds the beginning of a subsequence matching given key.
897 * @param __x Key of (key, value) pair to be located.
898 * @return Read-only (constant) iterator pointing to first element
899 * equal to or greater than key, or end().
901 * This function returns the first element of a subsequence of
902 * elements that matches the given key. If unsuccessful the
903 * iterator will point to the next greatest element or, if no
904 * such greater element exists, to end().
906 const_iterator
907 lower_bound(const key_type& __x) const
908 { return _M_t.lower_bound(__x); }
910 #if __cplusplus > 201103L
911 template<typename _Kt>
912 auto
913 lower_bound(const _Kt& __x) const
914 -> decltype(_M_t._M_lower_bound_tr(__x))
915 { return _M_t._M_lower_bound_tr(__x); }
916 #endif
917 //@}
919 //@{
921 * @brief Finds the end of a subsequence matching given key.
922 * @param __x Key of (key, value) pair to be located.
923 * @return Iterator pointing to the first element
924 * greater than key, or end().
926 iterator
927 upper_bound(const key_type& __x)
928 { return _M_t.upper_bound(__x); }
930 #if __cplusplus > 201103L
931 template<typename _Kt>
932 auto
933 upper_bound(const _Kt& __x)
934 -> decltype(_M_t._M_upper_bound_tr(__x))
935 { return _M_t._M_upper_bound_tr(__x); }
936 #endif
937 //@}
939 //@{
941 * @brief Finds the end of a subsequence matching given key.
942 * @param __x Key of (key, value) pair to be located.
943 * @return Read-only (constant) iterator pointing to first iterator
944 * greater than key, or end().
946 const_iterator
947 upper_bound(const key_type& __x) const
948 { return _M_t.upper_bound(__x); }
950 #if __cplusplus > 201103L
951 template<typename _Kt>
952 auto
953 upper_bound(const _Kt& __x) const
954 -> decltype(_M_t._M_upper_bound_tr(__x))
955 { return _M_t._M_upper_bound_tr(__x); }
956 #endif
957 //@}
959 //@{
961 * @brief Finds a subsequence matching given key.
962 * @param __x Key of (key, value) pairs to be located.
963 * @return Pair of iterators that possibly points to the subsequence
964 * matching given key.
966 * This function is equivalent to
967 * @code
968 * std::make_pair(c.lower_bound(val),
969 * c.upper_bound(val))
970 * @endcode
971 * (but is faster than making the calls separately).
973 std::pair<iterator, iterator>
974 equal_range(const key_type& __x)
975 { return _M_t.equal_range(__x); }
977 #if __cplusplus > 201103L
978 template<typename _Kt>
979 auto
980 equal_range(const _Kt& __x)
981 -> decltype(_M_t._M_equal_range_tr(__x))
982 { return _M_t._M_equal_range_tr(__x); }
983 #endif
984 //@}
986 //@{
988 * @brief Finds a subsequence matching given key.
989 * @param __x Key of (key, value) pairs to be located.
990 * @return Pair of read-only (constant) iterators that possibly points
991 * to the subsequence matching given key.
993 * This function is equivalent to
994 * @code
995 * std::make_pair(c.lower_bound(val),
996 * c.upper_bound(val))
997 * @endcode
998 * (but is faster than making the calls separately).
1000 std::pair<const_iterator, const_iterator>
1001 equal_range(const key_type& __x) const
1002 { return _M_t.equal_range(__x); }
1004 #if __cplusplus > 201103L
1005 template<typename _Kt>
1006 auto
1007 equal_range(const _Kt& __x) const
1008 -> decltype(_M_t._M_equal_range_tr(__x))
1009 { return _M_t._M_equal_range_tr(__x); }
1010 #endif
1011 //@}
1013 template<typename _K1, typename _T1, typename _C1, typename _A1>
1014 friend bool
1015 operator==(const multimap<_K1, _T1, _C1, _A1>&,
1016 const multimap<_K1, _T1, _C1, _A1>&);
1018 template<typename _K1, typename _T1, typename _C1, typename _A1>
1019 friend bool
1020 operator<(const multimap<_K1, _T1, _C1, _A1>&,
1021 const multimap<_K1, _T1, _C1, _A1>&);
1025 * @brief Multimap equality comparison.
1026 * @param __x A %multimap.
1027 * @param __y A %multimap of the same type as @a __x.
1028 * @return True iff the size and elements of the maps are equal.
1030 * This is an equivalence relation. It is linear in the size of the
1031 * multimaps. Multimaps are considered equivalent if their sizes are equal,
1032 * and if corresponding elements compare equal.
1034 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1035 inline bool
1036 operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1037 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1038 { return __x._M_t == __y._M_t; }
1041 * @brief Multimap ordering relation.
1042 * @param __x A %multimap.
1043 * @param __y A %multimap of the same type as @a __x.
1044 * @return True iff @a x is lexicographically less than @a y.
1046 * This is a total ordering relation. It is linear in the size of the
1047 * multimaps. The elements must be comparable with @c <.
1049 * See std::lexicographical_compare() for how the determination is made.
1051 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1052 inline bool
1053 operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1054 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1055 { return __x._M_t < __y._M_t; }
1057 /// Based on operator==
1058 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1059 inline bool
1060 operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1061 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1062 { return !(__x == __y); }
1064 /// Based on operator<
1065 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1066 inline bool
1067 operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1068 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1069 { return __y < __x; }
1071 /// Based on operator<
1072 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1073 inline bool
1074 operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1075 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1076 { return !(__y < __x); }
1078 /// Based on operator<
1079 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1080 inline bool
1081 operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1082 const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1083 { return !(__x < __y); }
1085 /// See std::multimap::swap().
1086 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
1087 inline void
1088 swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
1089 multimap<_Key, _Tp, _Compare, _Alloc>& __y)
1090 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1091 { __x.swap(__y); }
1093 _GLIBCXX_END_NAMESPACE_CONTAINER
1095 #if __cplusplus > 201402L
1096 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1097 // Allow std::multimap access to internals of compatible maps.
1098 template<typename _Key, typename _Val, typename _Cmp1, typename _Alloc,
1099 typename _Cmp2>
1100 struct
1101 _Rb_tree_merge_helper<_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>,
1102 _Cmp2>
1104 private:
1105 friend class _GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp1, _Alloc>;
1107 static auto&
1108 _S_get_tree(_GLIBCXX_STD_C::map<_Key, _Val, _Cmp2, _Alloc>& __map)
1109 { return __map._M_t; }
1111 static auto&
1112 _S_get_tree(_GLIBCXX_STD_C::multimap<_Key, _Val, _Cmp2, _Alloc>& __map)
1113 { return __map._M_t; }
1115 _GLIBCXX_END_NAMESPACE_VERSION
1116 #endif // C++17
1118 } // namespace std
1120 #endif /* _STL_MULTIMAP_H */