1// Multimap implementation -*- C++ -*-
2
3// Copyright (C) 2001-2014 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.
10
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.
15
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.
19
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/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
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.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
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.
49 */
50
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}
54 */
55
56#ifndef _STL_MULTIMAP_H
57#define _STL_MULTIMAP_H 1
58
59#include <bits/concept_check.h>
60#if __cplusplus >= 201103L
61#include <initializer_list>
62#endif
63
64namespace std _GLIBCXX_VISIBILITY(default)
65{
66_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
67
68  /**
69   *  @brief A standard container made up of (key,value) pairs, which can be
70   *  retrieved based on a key, in logarithmic time.
71   *
72   *  @ingroup associative_containers
73   *
74   *  @tparam _Key  Type of key objects.
75   *  @tparam  _Tp  Type of mapped objects.
76   *  @tparam _Compare  Comparison function object type, defaults to less<_Key>.
77   *  @tparam _Alloc  Allocator type, defaults to
78   *                  allocator<pair<const _Key, _Tp>.
79   *
80   *  Meets the requirements of a <a href="tables.html#65">container</a>, a
81   *  <a href="tables.html#66">reversible container</a>, and an
82   *  <a href="tables.html#69">associative container</a> (using equivalent
83   *  keys).  For a @c multimap<Key,T> the key_type is Key, the mapped_type
84   *  is T, and the value_type is std::pair<const Key,T>.
85   *
86   *  Multimaps support bidirectional iterators.
87   *
88   *  The private tree data is declared exactly the same way for map and
89   *  multimap; the distinction is made entirely in how the tree functions are
90   *  called (*_unique versus *_equal, same as the standard).
91  */
92  template <typename _Key, typename _Tp,
93	    typename _Compare = std::less<_Key>,
94	    typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
95    class multimap
96    {
97    public:
98      typedef _Key                                          key_type;
99      typedef _Tp                                           mapped_type;
100      typedef std::pair<const _Key, _Tp>                    value_type;
101      typedef _Compare                                      key_compare;
102      typedef _Alloc                                        allocator_type;
103
104    private:
105      // concept requirements
106      typedef typename _Alloc::value_type                   _Alloc_value_type;
107      __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
108      __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
109				_BinaryFunctionConcept)
110      __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
111
112    public:
113      class value_compare
114      : public std::binary_function<value_type, value_type, bool>
115      {
116	friend class multimap<_Key, _Tp, _Compare, _Alloc>;
117      protected:
118	_Compare comp;
119
120	value_compare(_Compare __c)
121	: comp(__c) { }
122
123      public:
124	bool operator()(const value_type& __x, const value_type& __y) const
125	{ return comp(__x.first, __y.first); }
126      };
127
128    private:
129      /// This turns a red-black tree into a [multi]map.
130      typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
131	rebind<value_type>::other _Pair_alloc_type;
132
133      typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
134		       key_compare, _Pair_alloc_type> _Rep_type;
135      /// The actual tree structure.
136      _Rep_type _M_t;
137
138      typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
139
140    public:
141      // many of these are specified differently in ISO, but the following are
142      // "functionally equivalent"
143      typedef typename _Alloc_traits::pointer            pointer;
144      typedef typename _Alloc_traits::const_pointer      const_pointer;
145      typedef typename _Alloc_traits::reference          reference;
146      typedef typename _Alloc_traits::const_reference    const_reference;
147      typedef typename _Rep_type::iterator               iterator;
148      typedef typename _Rep_type::const_iterator         const_iterator;
149      typedef typename _Rep_type::size_type              size_type;
150      typedef typename _Rep_type::difference_type        difference_type;
151      typedef typename _Rep_type::reverse_iterator       reverse_iterator;
152      typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
153
154      // [23.3.2] construct/copy/destroy
155      // (get_allocator() is also listed in this section)
156
157      /**
158       *  @brief  Default constructor creates no elements.
159       */
160      multimap()
161      : _M_t() { }
162
163      /**
164       *  @brief  Creates a %multimap with no elements.
165       *  @param  __comp  A comparison object.
166       *  @param  __a  An allocator object.
167       */
168      explicit
169      multimap(const _Compare& __comp,
170	       const allocator_type& __a = allocator_type())
171      : _M_t(__comp, _Pair_alloc_type(__a)) { }
172
173      /**
174       *  @brief  %Multimap copy constructor.
175       *  @param  __x  A %multimap of identical element and allocator types.
176       *
177       *  The newly-created %multimap uses a copy of the allocation object
178       *  used by @a __x.
179       */
180      multimap(const multimap& __x)
181      : _M_t(__x._M_t) { }
182
183#if __cplusplus >= 201103L
184      /**
185       *  @brief  %Multimap move constructor.
186       *  @param   __x  A %multimap of identical element and allocator types.
187       *
188       *  The newly-created %multimap contains the exact contents of @a __x.
189       *  The contents of @a __x are a valid, but unspecified %multimap.
190       */
191      multimap(multimap&& __x)
192      noexcept(is_nothrow_copy_constructible<_Compare>::value)
193      : _M_t(std::move(__x._M_t)) { }
194
195      /**
196       *  @brief  Builds a %multimap from an initializer_list.
197       *  @param  __l  An initializer_list.
198       *  @param  __comp  A comparison functor.
199       *  @param  __a  An allocator object.
200       *
201       *  Create a %multimap consisting of copies of the elements from
202       *  the initializer_list.  This is linear in N if the list is already
203       *  sorted, and NlogN otherwise (where N is @a __l.size()).
204       */
205      multimap(initializer_list<value_type> __l,
206	       const _Compare& __comp = _Compare(),
207	       const allocator_type& __a = allocator_type())
208      : _M_t(__comp, _Pair_alloc_type(__a))
209      { _M_t._M_insert_equal(__l.begin(), __l.end()); }
210
211      /// Allocator-extended default constructor.
212      explicit
213      multimap(const allocator_type& __a)
214      : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
215
216      /// Allocator-extended copy constructor.
217      multimap(const multimap& __m, const allocator_type& __a)
218      : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
219
220      /// Allocator-extended move constructor.
221      multimap(multimap&& __m, const allocator_type& __a)
222      noexcept(is_nothrow_copy_constructible<_Compare>::value
223	       && _Alloc_traits::_S_always_equal())
224      : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
225
226      /// Allocator-extended initialier-list constructor.
227      multimap(initializer_list<value_type> __l, const allocator_type& __a)
228      : _M_t(_Compare(), _Pair_alloc_type(__a))
229      { _M_t._M_insert_equal(__l.begin(), __l.end()); }
230
231      /// Allocator-extended range constructor.
232      template<typename _InputIterator>
233        multimap(_InputIterator __first, _InputIterator __last,
234		 const allocator_type& __a)
235	: _M_t(_Compare(), _Pair_alloc_type(__a))
236        { _M_t._M_insert_equal(__first, __last); }
237#endif
238
239      /**
240       *  @brief  Builds a %multimap from a range.
241       *  @param  __first  An input iterator.
242       *  @param  __last  An input iterator.
243       *
244       *  Create a %multimap consisting of copies of the elements from
245       *  [__first,__last).  This is linear in N if the range is already sorted,
246       *  and NlogN otherwise (where N is distance(__first,__last)).
247       */
248      template<typename _InputIterator>
249        multimap(_InputIterator __first, _InputIterator __last)
250	: _M_t()
251        { _M_t._M_insert_equal(__first, __last); }
252
253      /**
254       *  @brief  Builds a %multimap from a range.
255       *  @param  __first  An input iterator.
256       *  @param  __last  An input iterator.
257       *  @param  __comp  A comparison functor.
258       *  @param  __a  An allocator object.
259       *
260       *  Create a %multimap consisting of copies of the elements from
261       *  [__first,__last).  This is linear in N if the range is already sorted,
262       *  and NlogN otherwise (where N is distance(__first,__last)).
263       */
264      template<typename _InputIterator>
265        multimap(_InputIterator __first, _InputIterator __last,
266		 const _Compare& __comp,
267		 const allocator_type& __a = allocator_type())
268	: _M_t(__comp, _Pair_alloc_type(__a))
269        { _M_t._M_insert_equal(__first, __last); }
270
271      // FIXME There is no dtor declared, but we should have something generated
272      // by Doxygen.  I don't know what tags to add to this paragraph to make
273      // that happen:
274      /**
275       *  The dtor only erases the elements, and note that if the elements
276       *  themselves are pointers, the pointed-to memory is not touched in any
277       *  way.  Managing the pointer is the user's responsibility.
278       */
279
280      /**
281       *  @brief  %Multimap assignment operator.
282       *  @param  __x  A %multimap of identical element and allocator types.
283       *
284       *  All the elements of @a __x are copied, but unlike the copy
285       *  constructor, the allocator object is not copied.
286       */
287      multimap&
288      operator=(const multimap& __x)
289      {
290	_M_t = __x._M_t;
291	return *this;
292      }
293
294#if __cplusplus >= 201103L
295      /**
296       *  @brief  %Multimap move assignment operator.
297       *  @param  __x  A %multimap of identical element and allocator types.
298       *
299       *  The contents of @a __x are moved into this multimap (without copying
300       *  if the allocators compare equal or get moved on assignment).
301       *  Afterwards @a __x is in a valid, but unspecified state.
302       */
303      multimap&
304      operator=(multimap&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
305      {
306	if (!_M_t._M_move_assign(__x._M_t))
307	  {
308	    // The rvalue's allocator cannot be moved and is not equal,
309	    // so we need to individually move each element.
310	    clear();
311	    insert(std::__make_move_if_noexcept_iterator(__x.begin()),
312		   std::__make_move_if_noexcept_iterator(__x.end()));
313	    __x.clear();
314	  }
315	return *this;
316      }
317
318      /**
319       *  @brief  %Multimap list assignment operator.
320       *  @param  __l  An initializer_list.
321       *
322       *  This function fills a %multimap with copies of the elements
323       *  in the initializer list @a __l.
324       *
325       *  Note that the assignment completely changes the %multimap and
326       *  that the resulting %multimap's size is the same as the number
327       *  of elements assigned.  Old data may be lost.
328       */
329      multimap&
330      operator=(initializer_list<value_type> __l)
331      {
332	this->clear();
333	this->insert(__l.begin(), __l.end());
334	return *this;
335      }
336#endif
337
338      /// Get a copy of the memory allocation object.
339      allocator_type
340      get_allocator() const _GLIBCXX_NOEXCEPT
341      { return allocator_type(_M_t.get_allocator()); }
342
343      // iterators
344      /**
345       *  Returns a read/write iterator that points to the first pair in the
346       *  %multimap.  Iteration is done in ascending order according to the
347       *  keys.
348       */
349      iterator
350      begin() _GLIBCXX_NOEXCEPT
351      { return _M_t.begin(); }
352
353      /**
354       *  Returns a read-only (constant) iterator that points to the first pair
355       *  in the %multimap.  Iteration is done in ascending order according to
356       *  the keys.
357       */
358      const_iterator
359      begin() const _GLIBCXX_NOEXCEPT
360      { return _M_t.begin(); }
361
362      /**
363       *  Returns a read/write iterator that points one past the last pair in
364       *  the %multimap.  Iteration is done in ascending order according to the
365       *  keys.
366       */
367      iterator
368      end() _GLIBCXX_NOEXCEPT
369      { return _M_t.end(); }
370
371      /**
372       *  Returns a read-only (constant) iterator that points one past the last
373       *  pair in the %multimap.  Iteration is done in ascending order according
374       *  to the keys.
375       */
376      const_iterator
377      end() const _GLIBCXX_NOEXCEPT
378      { return _M_t.end(); }
379
380      /**
381       *  Returns a read/write reverse iterator that points to the last pair in
382       *  the %multimap.  Iteration is done in descending order according to the
383       *  keys.
384       */
385      reverse_iterator
386      rbegin() _GLIBCXX_NOEXCEPT
387      { return _M_t.rbegin(); }
388
389      /**
390       *  Returns a read-only (constant) reverse iterator that points to the
391       *  last pair in the %multimap.  Iteration is done in descending order
392       *  according to the keys.
393       */
394      const_reverse_iterator
395      rbegin() const _GLIBCXX_NOEXCEPT
396      { return _M_t.rbegin(); }
397
398      /**
399       *  Returns a read/write reverse iterator that points to one before the
400       *  first pair in the %multimap.  Iteration is done in descending order
401       *  according to the keys.
402       */
403      reverse_iterator
404      rend() _GLIBCXX_NOEXCEPT
405      { return _M_t.rend(); }
406
407      /**
408       *  Returns a read-only (constant) reverse iterator that points to one
409       *  before the first pair in the %multimap.  Iteration is done in
410       *  descending order according to the keys.
411       */
412      const_reverse_iterator
413      rend() const _GLIBCXX_NOEXCEPT
414      { return _M_t.rend(); }
415
416#if __cplusplus >= 201103L
417      /**
418       *  Returns a read-only (constant) iterator that points to the first pair
419       *  in the %multimap.  Iteration is done in ascending order according to
420       *  the keys.
421       */
422      const_iterator
423      cbegin() const noexcept
424      { return _M_t.begin(); }
425
426      /**
427       *  Returns a read-only (constant) iterator that points one past the last
428       *  pair in the %multimap.  Iteration is done in ascending order according
429       *  to the keys.
430       */
431      const_iterator
432      cend() const noexcept
433      { return _M_t.end(); }
434
435      /**
436       *  Returns a read-only (constant) reverse iterator that points to the
437       *  last pair in the %multimap.  Iteration is done in descending order
438       *  according to the keys.
439       */
440      const_reverse_iterator
441      crbegin() const noexcept
442      { return _M_t.rbegin(); }
443
444      /**
445       *  Returns a read-only (constant) reverse iterator that points to one
446       *  before the first pair in the %multimap.  Iteration is done in
447       *  descending order according to the keys.
448       */
449      const_reverse_iterator
450      crend() const noexcept
451      { return _M_t.rend(); }
452#endif
453
454      // capacity
455      /** Returns true if the %multimap is empty.  */
456      bool
457      empty() const _GLIBCXX_NOEXCEPT
458      { return _M_t.empty(); }
459
460      /** Returns the size of the %multimap.  */
461      size_type
462      size() const _GLIBCXX_NOEXCEPT
463      { return _M_t.size(); }
464
465      /** Returns the maximum size of the %multimap.  */
466      size_type
467      max_size() const _GLIBCXX_NOEXCEPT
468      { return _M_t.max_size(); }
469
470      // modifiers
471#if __cplusplus >= 201103L
472      /**
473       *  @brief Build and insert a std::pair into the %multimap.
474       *
475       *  @param __args  Arguments used to generate a new pair instance (see
476       *	        std::piecewise_contruct for passing arguments to each
477       *	        part of the pair constructor).
478       *
479       *  @return An iterator that points to the inserted (key,value) pair.
480       *
481       *  This function builds and inserts a (key, value) %pair into the
482       *  %multimap.
483       *  Contrary to a std::map the %multimap does not rely on unique keys and
484       *  thus multiple pairs with the same key can be inserted.
485       *
486       *  Insertion requires logarithmic time.
487       */
488      template<typename... _Args>
489	iterator
490	emplace(_Args&&... __args)
491	{ return _M_t._M_emplace_equal(std::forward<_Args>(__args)...); }
492
493      /**
494       *  @brief Builds and inserts a std::pair into the %multimap.
495       *
496       *  @param  __pos  An iterator that serves as a hint as to where the pair
497       *                should be inserted.
498       *  @param  __args  Arguments used to generate a new pair instance (see
499       *	         std::piecewise_contruct for passing arguments to each
500       *	         part of the pair constructor).
501       *  @return An iterator that points to the inserted (key,value) pair.
502       *
503       *  This function inserts a (key, value) pair into the %multimap.
504       *  Contrary to a std::map the %multimap does not rely on unique keys and
505       *  thus multiple pairs with the same key can be inserted.
506       *  Note that the first parameter is only a hint and can potentially
507       *  improve the performance of the insertion process.  A bad hint would
508       *  cause no gains in efficiency.
509       *
510       *  For more on @a hinting, see:
511       *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
512       *
513       *  Insertion requires logarithmic time (if the hint is not taken).
514       */
515      template<typename... _Args>
516	iterator
517	emplace_hint(const_iterator __pos, _Args&&... __args)
518	{
519	  return _M_t._M_emplace_hint_equal(__pos,
520					    std::forward<_Args>(__args)...);
521	}
522#endif
523
524      /**
525       *  @brief Inserts a std::pair into the %multimap.
526       *  @param  __x  Pair to be inserted (see std::make_pair for easy creation
527       *             of pairs).
528       *  @return An iterator that points to the inserted (key,value) pair.
529       *
530       *  This function inserts a (key, value) pair into the %multimap.
531       *  Contrary to a std::map the %multimap does not rely on unique keys and
532       *  thus multiple pairs with the same key can be inserted.
533       *
534       *  Insertion requires logarithmic time.
535       */
536      iterator
537      insert(const value_type& __x)
538      { return _M_t._M_insert_equal(__x); }
539
540#if __cplusplus >= 201103L
541      template<typename _Pair, typename = typename
542	       std::enable_if<std::is_constructible<value_type,
543						    _Pair&&>::value>::type>
544        iterator
545        insert(_Pair&& __x)
546        { return _M_t._M_insert_equal(std::forward<_Pair>(__x)); }
547#endif
548
549      /**
550       *  @brief Inserts a std::pair into the %multimap.
551       *  @param  __position  An iterator that serves as a hint as to where the
552       *                      pair should be inserted.
553       *  @param  __x  Pair to be inserted (see std::make_pair for easy creation
554       *               of pairs).
555       *  @return An iterator that points to the inserted (key,value) pair.
556       *
557       *  This function inserts a (key, value) pair into the %multimap.
558       *  Contrary to a std::map the %multimap does not rely on unique keys and
559       *  thus multiple pairs with the same key can be inserted.
560       *  Note that the first parameter is only a hint and can potentially
561       *  improve the performance of the insertion process.  A bad hint would
562       *  cause no gains in efficiency.
563       *
564       *  For more on @a hinting, see:
565       *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
566       *
567       *  Insertion requires logarithmic time (if the hint is not taken).
568       */
569      iterator
570#if __cplusplus >= 201103L
571      insert(const_iterator __position, const value_type& __x)
572#else
573      insert(iterator __position, const value_type& __x)
574#endif
575      { return _M_t._M_insert_equal_(__position, __x); }
576
577#if __cplusplus >= 201103L
578      template<typename _Pair, typename = typename
579	       std::enable_if<std::is_constructible<value_type,
580						    _Pair&&>::value>::type>
581        iterator
582        insert(const_iterator __position, _Pair&& __x)
583        { return _M_t._M_insert_equal_(__position,
584				       std::forward<_Pair>(__x)); }
585#endif
586
587      /**
588       *  @brief A template function that attempts to insert a range
589       *  of elements.
590       *  @param  __first  Iterator pointing to the start of the range to be
591       *                   inserted.
592       *  @param  __last  Iterator pointing to the end of the range.
593       *
594       *  Complexity similar to that of the range constructor.
595       */
596      template<typename _InputIterator>
597        void
598        insert(_InputIterator __first, _InputIterator __last)
599        { _M_t._M_insert_equal(__first, __last); }
600
601#if __cplusplus >= 201103L
602      /**
603       *  @brief Attempts to insert a list of std::pairs into the %multimap.
604       *  @param  __l  A std::initializer_list<value_type> of pairs to be
605       *               inserted.
606       *
607       *  Complexity similar to that of the range constructor.
608       */
609      void
610      insert(initializer_list<value_type> __l)
611      { this->insert(__l.begin(), __l.end()); }
612#endif
613
614#if __cplusplus >= 201103L
615      // _GLIBCXX_RESOLVE_LIB_DEFECTS
616      // DR 130. Associative erase should return an iterator.
617      /**
618       *  @brief Erases an element from a %multimap.
619       *  @param  __position  An iterator pointing to the element to be erased.
620       *  @return An iterator pointing to the element immediately following
621       *          @a position prior to the element being erased. If no such
622       *          element exists, end() is returned.
623       *
624       *  This function erases an element, pointed to by the given iterator,
625       *  from a %multimap.  Note that this function only erases the element,
626       *  and that if the element is itself a pointer, the pointed-to memory is
627       *  not touched in any way.  Managing the pointer is the user's
628       *  responsibility.
629       */
630      iterator
631      erase(const_iterator __position)
632      { return _M_t.erase(__position); }
633
634      // LWG 2059.
635      _GLIBCXX_ABI_TAG_CXX11
636      iterator
637      erase(iterator __position)
638      { return _M_t.erase(__position); }
639#else
640      /**
641       *  @brief Erases an element from a %multimap.
642       *  @param  __position  An iterator pointing to the element to be erased.
643       *
644       *  This function erases an element, pointed to by the given iterator,
645       *  from a %multimap.  Note that this function only erases the element,
646       *  and that if the element is itself a pointer, the pointed-to memory is
647       *  not touched in any way.  Managing the pointer is the user's
648       *  responsibility.
649       */
650      void
651      erase(iterator __position)
652      { _M_t.erase(__position); }
653#endif
654
655      /**
656       *  @brief Erases elements according to the provided key.
657       *  @param  __x  Key of element to be erased.
658       *  @return  The number of elements erased.
659       *
660       *  This function erases all elements located by the given key from a
661       *  %multimap.
662       *  Note that this function only erases the element, and that if
663       *  the element is itself a pointer, the pointed-to memory is not touched
664       *  in any way.  Managing the pointer is the user's responsibility.
665       */
666      size_type
667      erase(const key_type& __x)
668      { return _M_t.erase(__x); }
669
670#if __cplusplus >= 201103L
671      // _GLIBCXX_RESOLVE_LIB_DEFECTS
672      // DR 130. Associative erase should return an iterator.
673      /**
674       *  @brief Erases a [first,last) range of elements from a %multimap.
675       *  @param  __first  Iterator pointing to the start of the range to be
676       *                   erased.
677       *  @param __last Iterator pointing to the end of the range to be
678       *                erased .
679       *  @return The iterator @a __last.
680       *
681       *  This function erases a sequence of elements from a %multimap.
682       *  Note that this function only erases the elements, and that if
683       *  the elements themselves are pointers, the pointed-to memory is not
684       *  touched in any way.  Managing the pointer is the user's
685       *  responsibility.
686       */
687      iterator
688      erase(const_iterator __first, const_iterator __last)
689      { return _M_t.erase(__first, __last); }
690#else
691      // _GLIBCXX_RESOLVE_LIB_DEFECTS
692      // DR 130. Associative erase should return an iterator.
693      /**
694       *  @brief Erases a [first,last) range of elements from a %multimap.
695       *  @param  __first  Iterator pointing to the start of the range to be
696       *                 erased.
697       *  @param __last Iterator pointing to the end of the range to
698       *                be erased.
699       *
700       *  This function erases a sequence of elements from a %multimap.
701       *  Note that this function only erases the elements, and that if
702       *  the elements themselves are pointers, the pointed-to memory is not
703       *  touched in any way.  Managing the pointer is the user's
704       *  responsibility.
705       */
706      void
707      erase(iterator __first, iterator __last)
708      { _M_t.erase(__first, __last); }
709#endif
710
711      /**
712       *  @brief  Swaps data with another %multimap.
713       *  @param  __x  A %multimap of the same element and allocator types.
714       *
715       *  This exchanges the elements between two multimaps in constant time.
716       *  (It is only swapping a pointer, an integer, and an instance of
717       *  the @c Compare type (which itself is often stateless and empty), so it
718       *  should be quite fast.)
719       *  Note that the global std::swap() function is specialized such that
720       *  std::swap(m1,m2) will feed to this function.
721       */
722      void
723      swap(multimap& __x)
724#if __cplusplus >= 201103L
725      noexcept(_Alloc_traits::_S_nothrow_swap())
726#endif
727      { _M_t.swap(__x._M_t); }
728
729      /**
730       *  Erases all elements in a %multimap.  Note that this function only
731       *  erases the elements, and that if the elements themselves are pointers,
732       *  the pointed-to memory is not touched in any way.  Managing the pointer
733       *  is the user's responsibility.
734       */
735      void
736      clear() _GLIBCXX_NOEXCEPT
737      { _M_t.clear(); }
738
739      // observers
740      /**
741       *  Returns the key comparison object out of which the %multimap
742       *  was constructed.
743       */
744      key_compare
745      key_comp() const
746      { return _M_t.key_comp(); }
747
748      /**
749       *  Returns a value comparison object, built from the key comparison
750       *  object out of which the %multimap was constructed.
751       */
752      value_compare
753      value_comp() const
754      { return value_compare(_M_t.key_comp()); }
755
756      // multimap operations
757      /**
758       *  @brief Tries to locate an element in a %multimap.
759       *  @param  __x  Key of (key, value) pair to be located.
760       *  @return  Iterator pointing to sought-after element,
761       *           or end() if not found.
762       *
763       *  This function takes a key and tries to locate the element with which
764       *  the key matches.  If successful the function returns an iterator
765       *  pointing to the sought after %pair.  If unsuccessful it returns the
766       *  past-the-end ( @c end() ) iterator.
767       */
768      iterator
769      find(const key_type& __x)
770      { return _M_t.find(__x); }
771
772      /**
773       *  @brief Tries to locate an element in a %multimap.
774       *  @param  __x  Key of (key, value) pair to be located.
775       *  @return  Read-only (constant) iterator pointing to sought-after
776       *           element, or end() if not found.
777       *
778       *  This function takes a key and tries to locate the element with which
779       *  the key matches.  If successful the function returns a constant
780       *  iterator pointing to the sought after %pair.  If unsuccessful it
781       *  returns the past-the-end ( @c end() ) iterator.
782       */
783      const_iterator
784      find(const key_type& __x) const
785      { return _M_t.find(__x); }
786
787      /**
788       *  @brief Finds the number of elements with given key.
789       *  @param  __x  Key of (key, value) pairs to be located.
790       *  @return Number of elements with specified key.
791       */
792      size_type
793      count(const key_type& __x) const
794      { return _M_t.count(__x); }
795
796      /**
797       *  @brief Finds the beginning of a subsequence matching given key.
798       *  @param  __x  Key of (key, value) pair to be located.
799       *  @return  Iterator pointing to first element equal to or greater
800       *           than key, or end().
801       *
802       *  This function returns the first element of a subsequence of elements
803       *  that matches the given key.  If unsuccessful it returns an iterator
804       *  pointing to the first element that has a greater value than given key
805       *  or end() if no such element exists.
806       */
807      iterator
808      lower_bound(const key_type& __x)
809      { return _M_t.lower_bound(__x); }
810
811      /**
812       *  @brief Finds the beginning of a subsequence matching given key.
813       *  @param  __x  Key of (key, value) pair to be located.
814       *  @return  Read-only (constant) iterator pointing to first element
815       *           equal to or greater than key, or end().
816       *
817       *  This function returns the first element of a subsequence of
818       *  elements that matches the given key.  If unsuccessful the
819       *  iterator will point to the next greatest element or, if no
820       *  such greater element exists, to end().
821       */
822      const_iterator
823      lower_bound(const key_type& __x) const
824      { return _M_t.lower_bound(__x); }
825
826      /**
827       *  @brief Finds the end of a subsequence matching given key.
828       *  @param  __x  Key of (key, value) pair to be located.
829       *  @return Iterator pointing to the first element
830       *          greater than key, or end().
831       */
832      iterator
833      upper_bound(const key_type& __x)
834      { return _M_t.upper_bound(__x); }
835
836      /**
837       *  @brief Finds the end of a subsequence matching given key.
838       *  @param  __x  Key of (key, value) pair to be located.
839       *  @return  Read-only (constant) iterator pointing to first iterator
840       *           greater than key, or end().
841       */
842      const_iterator
843      upper_bound(const key_type& __x) const
844      { return _M_t.upper_bound(__x); }
845
846      /**
847       *  @brief Finds a subsequence matching given key.
848       *  @param  __x  Key of (key, value) pairs to be located.
849       *  @return  Pair of iterators that possibly points to the subsequence
850       *           matching given key.
851       *
852       *  This function is equivalent to
853       *  @code
854       *    std::make_pair(c.lower_bound(val),
855       *                   c.upper_bound(val))
856       *  @endcode
857       *  (but is faster than making the calls separately).
858       */
859      std::pair<iterator, iterator>
860      equal_range(const key_type& __x)
861      { return _M_t.equal_range(__x); }
862
863      /**
864       *  @brief Finds a subsequence matching given key.
865       *  @param  __x  Key of (key, value) pairs to be located.
866       *  @return  Pair of read-only (constant) iterators that possibly points
867       *           to the subsequence matching given key.
868       *
869       *  This function is equivalent to
870       *  @code
871       *    std::make_pair(c.lower_bound(val),
872       *                   c.upper_bound(val))
873       *  @endcode
874       *  (but is faster than making the calls separately).
875       */
876      std::pair<const_iterator, const_iterator>
877      equal_range(const key_type& __x) const
878      { return _M_t.equal_range(__x); }
879
880      template<typename _K1, typename _T1, typename _C1, typename _A1>
881        friend bool
882        operator==(const multimap<_K1, _T1, _C1, _A1>&,
883		   const multimap<_K1, _T1, _C1, _A1>&);
884
885      template<typename _K1, typename _T1, typename _C1, typename _A1>
886        friend bool
887        operator<(const multimap<_K1, _T1, _C1, _A1>&,
888		  const multimap<_K1, _T1, _C1, _A1>&);
889  };
890
891  /**
892   *  @brief  Multimap equality comparison.
893   *  @param  __x  A %multimap.
894   *  @param  __y  A %multimap of the same type as @a __x.
895   *  @return  True iff the size and elements of the maps are equal.
896   *
897   *  This is an equivalence relation.  It is linear in the size of the
898   *  multimaps.  Multimaps are considered equivalent if their sizes are equal,
899   *  and if corresponding elements compare equal.
900  */
901  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
902    inline bool
903    operator==(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
904               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
905    { return __x._M_t == __y._M_t; }
906
907  /**
908   *  @brief  Multimap ordering relation.
909   *  @param  __x  A %multimap.
910   *  @param  __y  A %multimap of the same type as @a __x.
911   *  @return  True iff @a x is lexicographically less than @a y.
912   *
913   *  This is a total ordering relation.  It is linear in the size of the
914   *  multimaps.  The elements must be comparable with @c <.
915   *
916   *  See std::lexicographical_compare() for how the determination is made.
917  */
918  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
919    inline bool
920    operator<(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
921              const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
922    { return __x._M_t < __y._M_t; }
923
924  /// Based on operator==
925  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
926    inline bool
927    operator!=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
928               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
929    { return !(__x == __y); }
930
931  /// Based on operator<
932  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
933    inline bool
934    operator>(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
935              const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
936    { return __y < __x; }
937
938  /// Based on operator<
939  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
940    inline bool
941    operator<=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
942               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
943    { return !(__y < __x); }
944
945  /// Based on operator<
946  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
947    inline bool
948    operator>=(const multimap<_Key, _Tp, _Compare, _Alloc>& __x,
949               const multimap<_Key, _Tp, _Compare, _Alloc>& __y)
950    { return !(__x < __y); }
951
952  /// See std::multimap::swap().
953  template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
954    inline void
955    swap(multimap<_Key, _Tp, _Compare, _Alloc>& __x,
956         multimap<_Key, _Tp, _Compare, _Alloc>& __y)
957    { __x.swap(__y); }
958
959_GLIBCXX_END_NAMESPACE_CONTAINER
960} // namespace std
961
962#endif /* _STL_MULTIMAP_H */
963