xref: /llvm-project-15.0.7/libcxx/include/map (revision ff9e596b)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MAP
11#define _LIBCPP_MAP
12
13/*
14
15    map synopsis
16
17namespace std
18{
19
20template <class Key, class T, class Compare = less<Key>,
21          class Allocator = allocator<pair<const Key, T>>>
22class map
23{
24public:
25    // types:
26    typedef Key                                      key_type;
27    typedef T                                        mapped_type;
28    typedef pair<const key_type, mapped_type>        value_type;
29    typedef Compare                                  key_compare;
30    typedef Allocator                                allocator_type;
31    typedef typename allocator_type::reference       reference;
32    typedef typename allocator_type::const_reference const_reference;
33    typedef typename allocator_type::pointer         pointer;
34    typedef typename allocator_type::const_pointer   const_pointer;
35    typedef typename allocator_type::size_type       size_type;
36    typedef typename allocator_type::difference_type difference_type;
37
38    typedef implementation-defined                   iterator;
39    typedef implementation-defined                   const_iterator;
40    typedef std::reverse_iterator<iterator>          reverse_iterator;
41    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
42    typedef unspecified                              node_type;              // C++17
43    typedef INSERT_RETURN_TYPE<iterator, node_type>  insert_return_type;     // C++17
44
45    class value_compare
46    {
47        friend class map;
48    protected:
49        key_compare comp;
50
51        value_compare(key_compare c);
52    public:
53        typedef bool result_type;  // deprecated in C++17, removed in C++20
54        typedef value_type first_argument_type;  // deprecated in C++17, removed in C++20
55        typedef value_type second_argument_type;  // deprecated in C++17, removed in C++20
56        bool operator()(const value_type& x, const value_type& y) const;
57    };
58
59    // construct/copy/destroy:
60    map()
61        noexcept(
62            is_nothrow_default_constructible<allocator_type>::value &&
63            is_nothrow_default_constructible<key_compare>::value &&
64            is_nothrow_copy_constructible<key_compare>::value);
65    explicit map(const key_compare& comp);
66    map(const key_compare& comp, const allocator_type& a);
67    template <class InputIterator>
68        map(InputIterator first, InputIterator last,
69            const key_compare& comp = key_compare());
70    template <class InputIterator>
71        map(InputIterator first, InputIterator last,
72            const key_compare& comp, const allocator_type& a);
73    map(const map& m);
74    map(map&& m)
75        noexcept(
76            is_nothrow_move_constructible<allocator_type>::value &&
77            is_nothrow_move_constructible<key_compare>::value);
78    explicit map(const allocator_type& a);
79    map(const map& m, const allocator_type& a);
80    map(map&& m, const allocator_type& a);
81    map(initializer_list<value_type> il, const key_compare& comp = key_compare());
82    map(initializer_list<value_type> il, const key_compare& comp, const allocator_type& a);
83    template <class InputIterator>
84        map(InputIterator first, InputIterator last, const allocator_type& a)
85            : map(first, last, Compare(), a) {}  // C++14
86    map(initializer_list<value_type> il, const allocator_type& a)
87        : map(il, Compare(), a) {}  // C++14
88   ~map();
89
90    map& operator=(const map& m);
91    map& operator=(map&& m)
92        noexcept(
93            allocator_type::propagate_on_container_move_assignment::value &&
94            is_nothrow_move_assignable<allocator_type>::value &&
95            is_nothrow_move_assignable<key_compare>::value);
96    map& operator=(initializer_list<value_type> il);
97
98    // iterators:
99          iterator begin() noexcept;
100    const_iterator begin() const noexcept;
101          iterator end() noexcept;
102    const_iterator end()   const noexcept;
103
104          reverse_iterator rbegin() noexcept;
105    const_reverse_iterator rbegin() const noexcept;
106          reverse_iterator rend() noexcept;
107    const_reverse_iterator rend()   const noexcept;
108
109    const_iterator         cbegin()  const noexcept;
110    const_iterator         cend()    const noexcept;
111    const_reverse_iterator crbegin() const noexcept;
112    const_reverse_iterator crend()   const noexcept;
113
114    // capacity:
115    bool      empty()    const noexcept;
116    size_type size()     const noexcept;
117    size_type max_size() const noexcept;
118
119    // element access:
120    mapped_type& operator[](const key_type& k);
121    mapped_type& operator[](key_type&& k);
122
123          mapped_type& at(const key_type& k);
124    const mapped_type& at(const key_type& k) const;
125
126    // modifiers:
127    template <class... Args>
128        pair<iterator, bool> emplace(Args&&... args);
129    template <class... Args>
130        iterator emplace_hint(const_iterator position, Args&&... args);
131    pair<iterator, bool> insert(const value_type& v);
132    pair<iterator, bool> insert(      value_type&& v);                                // C++17
133    template <class P>
134        pair<iterator, bool> insert(P&& p);
135    iterator insert(const_iterator position, const value_type& v);
136    iterator insert(const_iterator position,       value_type&& v);                   // C++17
137    template <class P>
138        iterator insert(const_iterator position, P&& p);
139    template <class InputIterator>
140        void insert(InputIterator first, InputIterator last);
141    void insert(initializer_list<value_type> il);
142
143    node_type extract(const_iterator position);                                       // C++17
144    node_type extract(const key_type& x);                                             // C++17
145    insert_return_type insert(node_type&& nh);                                        // C++17
146    iterator insert(const_iterator hint, node_type&& nh);                             // C++17
147
148    template <class... Args>
149        pair<iterator, bool> try_emplace(const key_type& k, Args&&... args);          // C++17
150    template <class... Args>
151        pair<iterator, bool> try_emplace(key_type&& k, Args&&... args);               // C++17
152    template <class... Args>
153        iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); // C++17
154    template <class... Args>
155        iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args);      // C++17
156    template <class M>
157        pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj);            // C++17
158    template <class M>
159        pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj);                 // C++17
160    template <class M>
161        iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj);   // C++17
162    template <class M>
163        iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);        // C++17
164
165    iterator  erase(const_iterator position);
166    iterator  erase(iterator position); // C++14
167    size_type erase(const key_type& k);
168    iterator  erase(const_iterator first, const_iterator last);
169    void clear() noexcept;
170
171    template<class C2>
172      void merge(map<Key, T, C2, Allocator>& source);         // C++17
173    template<class C2>
174      void merge(map<Key, T, C2, Allocator>&& source);        // C++17
175    template<class C2>
176      void merge(multimap<Key, T, C2, Allocator>& source);    // C++17
177    template<class C2>
178      void merge(multimap<Key, T, C2, Allocator>&& source);   // C++17
179
180    void swap(map& m)
181        noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
182            is_nothrow_swappable<key_compare>::value); // C++17
183
184    // observers:
185    allocator_type get_allocator() const noexcept;
186    key_compare    key_comp()      const;
187    value_compare  value_comp()    const;
188
189    // map operations:
190          iterator find(const key_type& k);
191    const_iterator find(const key_type& k) const;
192    template<typename K>
193        iterator find(const K& x);              // C++14
194    template<typename K>
195        const_iterator find(const K& x) const;  // C++14
196
197    template<typename K>
198      size_type count(const K& x) const;        // C++14
199    size_type      count(const key_type& k) const;
200
201    bool           contains(const key_type& x) const;  // C++20
202    template<class K> bool contains(const K& x) const; // C++20
203
204          iterator lower_bound(const key_type& k);
205    const_iterator lower_bound(const key_type& k) const;
206    template<typename K>
207        iterator lower_bound(const K& x);              // C++14
208    template<typename K>
209        const_iterator lower_bound(const K& x) const;  // C++14
210
211          iterator upper_bound(const key_type& k);
212    const_iterator upper_bound(const key_type& k) const;
213    template<typename K>
214        iterator upper_bound(const K& x);              // C++14
215    template<typename K>
216        const_iterator upper_bound(const K& x) const;  // C++14
217
218    pair<iterator,iterator>             equal_range(const key_type& k);
219    pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
220    template<typename K>
221        pair<iterator,iterator>             equal_range(const K& x);        // C++14
222    template<typename K>
223        pair<const_iterator,const_iterator> equal_range(const K& x) const;  // C++14
224};
225
226template <class InputIterator,
227      class Compare = less<iter_key_t<InputIterator>>,
228      class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
229map(InputIterator, InputIterator, Compare = Compare(), Allocator = Allocator())
230  -> map<iter_key_t<InputIterator>, iter_val_t<InputIterator>, Compare, Allocator>; // C++17
231
232template<class Key, class T, class Compare = less<Key>,
233    class Allocator = allocator<pair<const Key, T>>>
234map(initializer_list<pair<const Key, T>>, Compare = Compare(), Allocator = Allocator())
235  -> map<Key, T, Compare, Allocator>; // C++17
236
237template <class InputIterator, class Allocator>
238map(InputIterator, InputIterator, Allocator)
239  -> map<iter_key_t<InputIterator>, iter_val_t<InputIterator>, less<iter_key_t<InputIterator>>,
240    Allocator>; // C++17
241
242template<class Key, class T, class Allocator>
243map(initializer_list<pair<const Key, T>>, Allocator) -> map<Key, T, less<Key>, Allocator>; // C++17
244
245template <class Key, class T, class Compare, class Allocator>
246bool
247operator==(const map<Key, T, Compare, Allocator>& x,
248           const map<Key, T, Compare, Allocator>& y);
249
250template <class Key, class T, class Compare, class Allocator>
251bool
252operator< (const map<Key, T, Compare, Allocator>& x,
253           const map<Key, T, Compare, Allocator>& y);
254
255template <class Key, class T, class Compare, class Allocator>
256bool
257operator!=(const map<Key, T, Compare, Allocator>& x,
258           const map<Key, T, Compare, Allocator>& y);
259
260template <class Key, class T, class Compare, class Allocator>
261bool
262operator> (const map<Key, T, Compare, Allocator>& x,
263           const map<Key, T, Compare, Allocator>& y);
264
265template <class Key, class T, class Compare, class Allocator>
266bool
267operator>=(const map<Key, T, Compare, Allocator>& x,
268           const map<Key, T, Compare, Allocator>& y);
269
270template <class Key, class T, class Compare, class Allocator>
271bool
272operator<=(const map<Key, T, Compare, Allocator>& x,
273           const map<Key, T, Compare, Allocator>& y);
274
275// specialized algorithms:
276template <class Key, class T, class Compare, class Allocator>
277void
278swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y)
279    noexcept(noexcept(x.swap(y)));
280
281template <class Key, class T, class Compare, class Allocator, class Predicate>
282typename map<Key, T, Compare, Allocator>::size_type
283erase_if(map<Key, T, Compare, Allocator>& c, Predicate pred);  // C++20
284
285
286template <class Key, class T, class Compare = less<Key>,
287          class Allocator = allocator<pair<const Key, T>>>
288class multimap
289{
290public:
291    // types:
292    typedef Key                                      key_type;
293    typedef T                                        mapped_type;
294    typedef pair<const key_type,mapped_type>         value_type;
295    typedef Compare                                  key_compare;
296    typedef Allocator                                allocator_type;
297    typedef typename allocator_type::reference       reference;
298    typedef typename allocator_type::const_reference const_reference;
299    typedef typename allocator_type::size_type       size_type;
300    typedef typename allocator_type::difference_type difference_type;
301    typedef typename allocator_type::pointer         pointer;
302    typedef typename allocator_type::const_pointer   const_pointer;
303
304    typedef implementation-defined                   iterator;
305    typedef implementation-defined                   const_iterator;
306    typedef std::reverse_iterator<iterator>          reverse_iterator;
307    typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
308    typedef unspecified                              node_type;              // C++17
309
310    class value_compare
311    {
312        friend class multimap;
313    protected:
314        key_compare comp;
315        value_compare(key_compare c);
316    public:
317        typedef bool result_type;  // deprecated in C++17, removed in C++20
318        typedef value_type first_argument_type;  // deprecated in C++17, removed in C++20
319        typedef value_type second_argument_type;  // deprecated in C++17, removed in C++20
320        bool operator()(const value_type& x, const value_type& y) const;
321    };
322
323    // construct/copy/destroy:
324    multimap()
325        noexcept(
326            is_nothrow_default_constructible<allocator_type>::value &&
327            is_nothrow_default_constructible<key_compare>::value &&
328            is_nothrow_copy_constructible<key_compare>::value);
329    explicit multimap(const key_compare& comp);
330    multimap(const key_compare& comp, const allocator_type& a);
331    template <class InputIterator>
332        multimap(InputIterator first, InputIterator last, const key_compare& comp);
333    template <class InputIterator>
334        multimap(InputIterator first, InputIterator last, const key_compare& comp,
335                 const allocator_type& a);
336    multimap(const multimap& m);
337    multimap(multimap&& m)
338        noexcept(
339            is_nothrow_move_constructible<allocator_type>::value &&
340            is_nothrow_move_constructible<key_compare>::value);
341    explicit multimap(const allocator_type& a);
342    multimap(const multimap& m, const allocator_type& a);
343    multimap(multimap&& m, const allocator_type& a);
344    multimap(initializer_list<value_type> il, const key_compare& comp = key_compare());
345    multimap(initializer_list<value_type> il, const key_compare& comp,
346             const allocator_type& a);
347    template <class InputIterator>
348        multimap(InputIterator first, InputIterator last, const allocator_type& a)
349            : multimap(first, last, Compare(), a) {} // C++14
350    multimap(initializer_list<value_type> il, const allocator_type& a)
351        : multimap(il, Compare(), a) {} // C++14
352    ~multimap();
353
354    multimap& operator=(const multimap& m);
355    multimap& operator=(multimap&& m)
356        noexcept(
357            allocator_type::propagate_on_container_move_assignment::value &&
358            is_nothrow_move_assignable<allocator_type>::value &&
359            is_nothrow_move_assignable<key_compare>::value);
360    multimap& operator=(initializer_list<value_type> il);
361
362    // iterators:
363          iterator begin() noexcept;
364    const_iterator begin() const noexcept;
365          iterator end() noexcept;
366    const_iterator end()   const noexcept;
367
368          reverse_iterator rbegin() noexcept;
369    const_reverse_iterator rbegin() const noexcept;
370          reverse_iterator rend() noexcept;
371    const_reverse_iterator rend()   const noexcept;
372
373    const_iterator         cbegin()  const noexcept;
374    const_iterator         cend()    const noexcept;
375    const_reverse_iterator crbegin() const noexcept;
376    const_reverse_iterator crend()   const noexcept;
377
378    // capacity:
379    bool      empty()    const noexcept;
380    size_type size()     const noexcept;
381    size_type max_size() const noexcept;
382
383    // modifiers:
384    template <class... Args>
385        iterator emplace(Args&&... args);
386    template <class... Args>
387        iterator emplace_hint(const_iterator position, Args&&... args);
388    iterator insert(const value_type& v);
389    iterator insert(      value_type&& v);                                            // C++17
390    template <class P>
391        iterator insert(P&& p);
392    iterator insert(const_iterator position, const value_type& v);
393    iterator insert(const_iterator position,       value_type&& v);                   // C++17
394    template <class P>
395        iterator insert(const_iterator position, P&& p);
396    template <class InputIterator>
397        void insert(InputIterator first, InputIterator last);
398    void insert(initializer_list<value_type> il);
399
400    node_type extract(const_iterator position);                                       // C++17
401    node_type extract(const key_type& x);                                             // C++17
402    iterator insert(node_type&& nh);                                                  // C++17
403    iterator insert(const_iterator hint, node_type&& nh);                             // C++17
404
405    iterator  erase(const_iterator position);
406    iterator  erase(iterator position); // C++14
407    size_type erase(const key_type& k);
408    iterator  erase(const_iterator first, const_iterator last);
409    void clear() noexcept;
410
411    template<class C2>
412      void merge(multimap<Key, T, C2, Allocator>& source);    // C++17
413    template<class C2>
414      void merge(multimap<Key, T, C2, Allocator>&& source);   // C++17
415    template<class C2>
416      void merge(map<Key, T, C2, Allocator>& source);         // C++17
417    template<class C2>
418      void merge(map<Key, T, C2, Allocator>&& source);        // C++17
419
420    void swap(multimap& m)
421        noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
422            is_nothrow_swappable<key_compare>::value); // C++17
423
424    // observers:
425    allocator_type get_allocator() const noexcept;
426    key_compare    key_comp()      const;
427    value_compare  value_comp()    const;
428
429    // map operations:
430          iterator find(const key_type& k);
431    const_iterator find(const key_type& k) const;
432    template<typename K>
433        iterator find(const K& x);              // C++14
434    template<typename K>
435        const_iterator find(const K& x) const;  // C++14
436
437    template<typename K>
438      size_type count(const K& x) const;        // C++14
439    size_type      count(const key_type& k) const;
440
441    bool           contains(const key_type& x) const;  // C++20
442    template<class K> bool contains(const K& x) const; // C++20
443
444          iterator lower_bound(const key_type& k);
445    const_iterator lower_bound(const key_type& k) const;
446    template<typename K>
447        iterator lower_bound(const K& x);              // C++14
448    template<typename K>
449        const_iterator lower_bound(const K& x) const;  // C++14
450
451          iterator upper_bound(const key_type& k);
452    const_iterator upper_bound(const key_type& k) const;
453    template<typename K>
454        iterator upper_bound(const K& x);              // C++14
455    template<typename K>
456        const_iterator upper_bound(const K& x) const;  // C++14
457
458    pair<iterator,iterator>             equal_range(const key_type& k);
459    pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
460    template<typename K>
461        pair<iterator,iterator>             equal_range(const K& x);        // C++14
462    template<typename K>
463        pair<const_iterator,const_iterator> equal_range(const K& x) const;  // C++14
464};
465
466template <class InputIterator,
467      class Compare = less<iter_key_t<InputIterator>>,
468      class Allocator = allocator<iter_to_alloc_t<InputIterator>>>
469multimap(InputIterator, InputIterator, Compare = Compare(), Allocator = Allocator())
470  -> multimap<iter_key_t<InputIterator>, iter_val_t<InputIterator>, Compare, Allocator>; // C++17
471
472template<class Key, class T, class Compare = less<Key>,
473    class Allocator = allocator<pair<const Key, T>>>
474multimap(initializer_list<pair<const Key, T>>, Compare = Compare(), Allocator = Allocator())
475  -> multimap<Key, T, Compare, Allocator>; // C++17
476
477template <class InputIterator, class Allocator>
478multimap(InputIterator, InputIterator, Allocator)
479  -> multimap<iter_key_t<InputIterator>, iter_val_t<InputIterator>,
480    less<iter_key_t<InputIterator>>, Allocator>; // C++17
481
482template<class Key, class T, class Allocator>
483multimap(initializer_list<pair<const Key, T>>, Allocator)
484  -> multimap<Key, T, less<Key>, Allocator>; // C++17
485
486template <class Key, class T, class Compare, class Allocator>
487bool
488operator==(const multimap<Key, T, Compare, Allocator>& x,
489           const multimap<Key, T, Compare, Allocator>& y);
490
491template <class Key, class T, class Compare, class Allocator>
492bool
493operator< (const multimap<Key, T, Compare, Allocator>& x,
494           const multimap<Key, T, Compare, Allocator>& y);
495
496template <class Key, class T, class Compare, class Allocator>
497bool
498operator!=(const multimap<Key, T, Compare, Allocator>& x,
499           const multimap<Key, T, Compare, Allocator>& y);
500
501template <class Key, class T, class Compare, class Allocator>
502bool
503operator> (const multimap<Key, T, Compare, Allocator>& x,
504           const multimap<Key, T, Compare, Allocator>& y);
505
506template <class Key, class T, class Compare, class Allocator>
507bool
508operator>=(const multimap<Key, T, Compare, Allocator>& x,
509           const multimap<Key, T, Compare, Allocator>& y);
510
511template <class Key, class T, class Compare, class Allocator>
512bool
513operator<=(const multimap<Key, T, Compare, Allocator>& x,
514           const multimap<Key, T, Compare, Allocator>& y);
515
516// specialized algorithms:
517template <class Key, class T, class Compare, class Allocator>
518void
519swap(multimap<Key, T, Compare, Allocator>& x,
520     multimap<Key, T, Compare, Allocator>& y)
521    noexcept(noexcept(x.swap(y)));
522
523template <class Key, class T, class Compare, class Allocator, class Predicate>
524typename multimap<Key, T, Compare, Allocator>::size_type
525erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred);  // C++20
526
527}  // std
528
529*/
530
531#include <__algorithm/equal.h>
532#include <__algorithm/lexicographical_compare.h>
533#include <__assert>
534#include <__config>
535#include <__functional/is_transparent.h>
536#include <__iterator/iterator_traits.h>
537#include <__node_handle>
538#include <__tree>
539#include <__utility/forward.h>
540#include <__utility/swap.h>
541#include <compare>
542#include <functional>
543#include <initializer_list>
544#include <iterator> // __libcpp_erase_if_container
545#include <memory>
546#include <type_traits>
547#include <version>
548
549#include <utility> // TODO: Remove this
550
551#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
552#  pragma GCC system_header
553#endif
554
555_LIBCPP_BEGIN_NAMESPACE_STD
556
557template <class _Key, class _CP, class _Compare,
558          bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
559class __map_value_compare
560    : private _Compare
561{
562public:
563    _LIBCPP_INLINE_VISIBILITY
564    __map_value_compare()
565        _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
566        : _Compare() {}
567    _LIBCPP_INLINE_VISIBILITY
568    __map_value_compare(_Compare c)
569        _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
570        : _Compare(c) {}
571    _LIBCPP_INLINE_VISIBILITY
572    const _Compare& key_comp() const _NOEXCEPT {return *this;}
573    _LIBCPP_INLINE_VISIBILITY
574    bool operator()(const _CP& __x, const _CP& __y) const
575        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y.__get_value().first);}
576    _LIBCPP_INLINE_VISIBILITY
577    bool operator()(const _CP& __x, const _Key& __y) const
578        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
579    _LIBCPP_INLINE_VISIBILITY
580    bool operator()(const _Key& __x, const _CP& __y) const
581        {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
582    void swap(__map_value_compare& __y)
583        _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
584    {
585      using _VSTD::swap;
586      swap(static_cast<_Compare&>(*this), static_cast<_Compare&>(__y));
587    }
588
589#if _LIBCPP_STD_VER > 11
590    template <typename _K2>
591    _LIBCPP_INLINE_VISIBILITY
592    bool operator()(const _K2& __x, const _CP& __y) const
593        {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
594
595    template <typename _K2>
596    _LIBCPP_INLINE_VISIBILITY
597    bool operator()(const _CP& __x, const _K2& __y) const
598        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
599#endif
600};
601
602template <class _Key, class _CP, class _Compare>
603class __map_value_compare<_Key, _CP, _Compare, false>
604{
605    _Compare comp;
606
607public:
608    _LIBCPP_INLINE_VISIBILITY
609    __map_value_compare()
610        _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
611        : comp() {}
612    _LIBCPP_INLINE_VISIBILITY
613    __map_value_compare(_Compare c)
614        _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
615        : comp(c) {}
616    _LIBCPP_INLINE_VISIBILITY
617    const _Compare& key_comp() const _NOEXCEPT {return comp;}
618
619    _LIBCPP_INLINE_VISIBILITY
620    bool operator()(const _CP& __x, const _CP& __y) const
621        {return comp(__x.__get_value().first, __y.__get_value().first);}
622    _LIBCPP_INLINE_VISIBILITY
623    bool operator()(const _CP& __x, const _Key& __y) const
624        {return comp(__x.__get_value().first, __y);}
625    _LIBCPP_INLINE_VISIBILITY
626    bool operator()(const _Key& __x, const _CP& __y) const
627        {return comp(__x, __y.__get_value().first);}
628    void swap(__map_value_compare& __y)
629        _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
630    {
631        using _VSTD::swap;
632        swap(comp, __y.comp);
633    }
634
635#if _LIBCPP_STD_VER > 11
636    template <typename _K2>
637    _LIBCPP_INLINE_VISIBILITY
638    bool operator()(const _K2& __x, const _CP& __y) const
639        {return comp(__x, __y.__get_value().first);}
640
641    template <typename _K2>
642    _LIBCPP_INLINE_VISIBILITY
643    bool operator()(const _CP& __x, const _K2& __y) const
644        {return comp(__x.__get_value().first, __y);}
645#endif
646};
647
648template <class _Key, class _CP, class _Compare, bool __b>
649inline _LIBCPP_INLINE_VISIBILITY
650void
651swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x,
652     __map_value_compare<_Key, _CP, _Compare, __b>& __y)
653    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
654{
655    __x.swap(__y);
656}
657
658template <class _Allocator>
659class __map_node_destructor
660{
661    typedef _Allocator                          allocator_type;
662    typedef allocator_traits<allocator_type>    __alloc_traits;
663
664public:
665    typedef typename __alloc_traits::pointer    pointer;
666
667private:
668    allocator_type& __na_;
669
670    __map_node_destructor& operator=(const __map_node_destructor&);
671
672public:
673    bool __first_constructed;
674    bool __second_constructed;
675
676    _LIBCPP_INLINE_VISIBILITY
677    explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
678        : __na_(__na),
679          __first_constructed(false),
680          __second_constructed(false)
681        {}
682
683#ifndef _LIBCPP_CXX03_LANG
684    _LIBCPP_INLINE_VISIBILITY
685    __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
686        : __na_(__x.__na_),
687          __first_constructed(__x.__value_constructed),
688          __second_constructed(__x.__value_constructed)
689        {
690            __x.__value_constructed = false;
691        }
692#endif // _LIBCPP_CXX03_LANG
693
694    _LIBCPP_INLINE_VISIBILITY
695    void operator()(pointer __p) _NOEXCEPT
696    {
697        if (__second_constructed)
698            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().second));
699        if (__first_constructed)
700            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().first));
701        if (__p)
702            __alloc_traits::deallocate(__na_, __p, 1);
703    }
704};
705
706template <class _Key, class _Tp, class _Compare, class _Allocator>
707    class map;
708template <class _Key, class _Tp, class _Compare, class _Allocator>
709    class multimap;
710template <class _TreeIterator> class __map_const_iterator;
711
712#ifndef _LIBCPP_CXX03_LANG
713
714template <class _Key, class _Tp>
715struct _LIBCPP_STANDALONE_DEBUG __value_type
716{
717    typedef _Key                                     key_type;
718    typedef _Tp                                      mapped_type;
719    typedef pair<const key_type, mapped_type>        value_type;
720    typedef pair<key_type&, mapped_type&>            __nc_ref_pair_type;
721    typedef pair<key_type&&, mapped_type&&>          __nc_rref_pair_type;
722
723private:
724    value_type __cc;
725
726public:
727    _LIBCPP_INLINE_VISIBILITY
728    value_type& __get_value()
729    {
730#if _LIBCPP_STD_VER > 14
731        return *_VSTD::launder(_VSTD::addressof(__cc));
732#else
733        return __cc;
734#endif
735    }
736
737    _LIBCPP_INLINE_VISIBILITY
738    const value_type& __get_value() const
739    {
740#if _LIBCPP_STD_VER > 14
741        return *_VSTD::launder(_VSTD::addressof(__cc));
742#else
743        return __cc;
744#endif
745    }
746
747    _LIBCPP_INLINE_VISIBILITY
748    __nc_ref_pair_type __ref()
749    {
750        value_type& __v = __get_value();
751        return __nc_ref_pair_type(const_cast<key_type&>(__v.first), __v.second);
752    }
753
754    _LIBCPP_INLINE_VISIBILITY
755    __nc_rref_pair_type __move()
756    {
757        value_type& __v = __get_value();
758        return __nc_rref_pair_type(
759            _VSTD::move(const_cast<key_type&>(__v.first)),
760            _VSTD::move(__v.second));
761    }
762
763    _LIBCPP_INLINE_VISIBILITY
764    __value_type& operator=(const __value_type& __v)
765    {
766        __ref() = __v.__get_value();
767        return *this;
768    }
769
770    _LIBCPP_INLINE_VISIBILITY
771    __value_type& operator=(__value_type&& __v)
772    {
773        __ref() = __v.__move();
774        return *this;
775    }
776
777    template <class _ValueTp,
778              class = typename enable_if<
779                    __is_same_uncvref<_ValueTp, value_type>::value
780                 >::type
781             >
782    _LIBCPP_INLINE_VISIBILITY
783    __value_type& operator=(_ValueTp&& __v)
784    {
785        __ref() = _VSTD::forward<_ValueTp>(__v);
786        return *this;
787    }
788
789private:
790    __value_type() = delete;
791    ~__value_type() = delete;
792    __value_type(const __value_type&) = delete;
793    __value_type(__value_type&&) = delete;
794};
795
796#else
797
798template <class _Key, class _Tp>
799struct __value_type
800{
801    typedef _Key                                     key_type;
802    typedef _Tp                                      mapped_type;
803    typedef pair<const key_type, mapped_type>        value_type;
804
805private:
806    value_type __cc;
807
808public:
809    _LIBCPP_INLINE_VISIBILITY
810    value_type& __get_value() { return __cc; }
811    _LIBCPP_INLINE_VISIBILITY
812    const value_type& __get_value() const { return __cc; }
813
814private:
815   __value_type();
816   __value_type(__value_type const&);
817   __value_type& operator=(__value_type const&);
818   ~__value_type();
819};
820
821#endif // _LIBCPP_CXX03_LANG
822
823template <class _Tp>
824struct __extract_key_value_types;
825
826template <class _Key, class _Tp>
827struct __extract_key_value_types<__value_type<_Key, _Tp> >
828{
829  typedef _Key const __key_type;
830  typedef _Tp        __mapped_type;
831};
832
833template <class _TreeIterator>
834class _LIBCPP_TEMPLATE_VIS __map_iterator
835{
836    typedef typename _TreeIterator::_NodeTypes                   _NodeTypes;
837    typedef typename _TreeIterator::__pointer_traits             __pointer_traits;
838
839    _TreeIterator __i_;
840
841public:
842    typedef bidirectional_iterator_tag                           iterator_category;
843    typedef typename _NodeTypes::__map_value_type                value_type;
844    typedef typename _TreeIterator::difference_type              difference_type;
845    typedef value_type&                                          reference;
846    typedef typename _NodeTypes::__map_value_type_pointer        pointer;
847
848    _LIBCPP_INLINE_VISIBILITY
849    __map_iterator() _NOEXCEPT {}
850
851    _LIBCPP_INLINE_VISIBILITY
852    __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
853
854    _LIBCPP_INLINE_VISIBILITY
855    reference operator*() const {return __i_->__get_value();}
856    _LIBCPP_INLINE_VISIBILITY
857    pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
858
859    _LIBCPP_INLINE_VISIBILITY
860    __map_iterator& operator++() {++__i_; return *this;}
861    _LIBCPP_INLINE_VISIBILITY
862    __map_iterator operator++(int)
863    {
864        __map_iterator __t(*this);
865        ++(*this);
866        return __t;
867    }
868
869    _LIBCPP_INLINE_VISIBILITY
870    __map_iterator& operator--() {--__i_; return *this;}
871    _LIBCPP_INLINE_VISIBILITY
872    __map_iterator operator--(int)
873    {
874        __map_iterator __t(*this);
875        --(*this);
876        return __t;
877    }
878
879    friend _LIBCPP_INLINE_VISIBILITY
880    bool operator==(const __map_iterator& __x, const __map_iterator& __y)
881        {return __x.__i_ == __y.__i_;}
882    friend
883    _LIBCPP_INLINE_VISIBILITY
884    bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
885        {return __x.__i_ != __y.__i_;}
886
887    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
888    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
889    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
890};
891
892template <class _TreeIterator>
893class _LIBCPP_TEMPLATE_VIS __map_const_iterator
894{
895    typedef typename _TreeIterator::_NodeTypes                   _NodeTypes;
896    typedef typename _TreeIterator::__pointer_traits             __pointer_traits;
897
898    _TreeIterator __i_;
899
900public:
901    typedef bidirectional_iterator_tag                           iterator_category;
902    typedef typename _NodeTypes::__map_value_type                value_type;
903    typedef typename _TreeIterator::difference_type              difference_type;
904    typedef const value_type&                                    reference;
905    typedef typename _NodeTypes::__const_map_value_type_pointer  pointer;
906
907    _LIBCPP_INLINE_VISIBILITY
908    __map_const_iterator() _NOEXCEPT {}
909
910    _LIBCPP_INLINE_VISIBILITY
911    __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
912    _LIBCPP_INLINE_VISIBILITY
913    __map_const_iterator(__map_iterator<
914        typename _TreeIterator::__non_const_iterator> __i) _NOEXCEPT
915        : __i_(__i.__i_) {}
916
917    _LIBCPP_INLINE_VISIBILITY
918    reference operator*() const {return __i_->__get_value();}
919    _LIBCPP_INLINE_VISIBILITY
920    pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
921
922    _LIBCPP_INLINE_VISIBILITY
923    __map_const_iterator& operator++() {++__i_; return *this;}
924    _LIBCPP_INLINE_VISIBILITY
925    __map_const_iterator operator++(int)
926    {
927        __map_const_iterator __t(*this);
928        ++(*this);
929        return __t;
930    }
931
932    _LIBCPP_INLINE_VISIBILITY
933    __map_const_iterator& operator--() {--__i_; return *this;}
934    _LIBCPP_INLINE_VISIBILITY
935    __map_const_iterator operator--(int)
936    {
937        __map_const_iterator __t(*this);
938        --(*this);
939        return __t;
940    }
941
942    friend _LIBCPP_INLINE_VISIBILITY
943    bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
944        {return __x.__i_ == __y.__i_;}
945    friend _LIBCPP_INLINE_VISIBILITY
946    bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
947        {return __x.__i_ != __y.__i_;}
948
949    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
950    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
951    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
952};
953
954template <class _Key, class _Tp, class _Compare = less<_Key>,
955          class _Allocator = allocator<pair<const _Key, _Tp> > >
956class _LIBCPP_TEMPLATE_VIS map
957{
958public:
959    // types:
960    typedef _Key                                     key_type;
961    typedef _Tp                                      mapped_type;
962    typedef pair<const key_type, mapped_type>        value_type;
963    typedef __identity_t<_Compare>                   key_compare;
964    typedef __identity_t<_Allocator>                 allocator_type;
965    typedef value_type&                              reference;
966    typedef const value_type&                        const_reference;
967
968    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
969                  "Allocator::value_type must be same type as value_type");
970
971_LIBCPP_SUPPRESS_DEPRECATED_PUSH
972    class _LIBCPP_TEMPLATE_VIS value_compare
973#if defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
974        : public binary_function<value_type, value_type, bool>
975#endif
976    {
977_LIBCPP_SUPPRESS_DEPRECATED_POP
978        friend class map;
979    protected:
980        key_compare comp;
981
982        _LIBCPP_INLINE_VISIBILITY value_compare(key_compare c) : comp(c) {}
983    public:
984#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
985        _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
986        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type first_argument_type;
987        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type second_argument_type;
988#endif
989        _LIBCPP_INLINE_VISIBILITY
990        bool operator()(const value_type& __x, const value_type& __y) const
991            {return comp(__x.first, __y.first);}
992    };
993
994private:
995
996    typedef _VSTD::__value_type<key_type, mapped_type>             __value_type;
997    typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
998    typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
999                                                 __value_type>::type __allocator_type;
1000    typedef __tree<__value_type, __vc, __allocator_type>   __base;
1001    typedef typename __base::__node_traits                 __node_traits;
1002    typedef allocator_traits<allocator_type>               __alloc_traits;
1003
1004    __base __tree_;
1005
1006public:
1007    typedef typename __alloc_traits::pointer               pointer;
1008    typedef typename __alloc_traits::const_pointer         const_pointer;
1009    typedef typename __alloc_traits::size_type             size_type;
1010    typedef typename __alloc_traits::difference_type       difference_type;
1011    typedef __map_iterator<typename __base::iterator>             iterator;
1012    typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1013    typedef _VSTD::reverse_iterator<iterator>               reverse_iterator;
1014    typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
1015
1016#if _LIBCPP_STD_VER > 14
1017    typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
1018    typedef __insert_return_type<iterator, node_type> insert_return_type;
1019#endif
1020
1021    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1022        friend class _LIBCPP_TEMPLATE_VIS map;
1023    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1024        friend class _LIBCPP_TEMPLATE_VIS multimap;
1025
1026    _LIBCPP_INLINE_VISIBILITY
1027    map()
1028        _NOEXCEPT_(
1029            is_nothrow_default_constructible<allocator_type>::value &&
1030            is_nothrow_default_constructible<key_compare>::value &&
1031            is_nothrow_copy_constructible<key_compare>::value)
1032        : __tree_(__vc(key_compare())) {}
1033
1034    _LIBCPP_INLINE_VISIBILITY
1035    explicit map(const key_compare& __comp)
1036        _NOEXCEPT_(
1037            is_nothrow_default_constructible<allocator_type>::value &&
1038            is_nothrow_copy_constructible<key_compare>::value)
1039        : __tree_(__vc(__comp)) {}
1040
1041    _LIBCPP_INLINE_VISIBILITY
1042    explicit map(const key_compare& __comp, const allocator_type& __a)
1043        : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
1044
1045    template <class _InputIterator>
1046    _LIBCPP_INLINE_VISIBILITY
1047        map(_InputIterator __f, _InputIterator __l,
1048            const key_compare& __comp = key_compare())
1049        : __tree_(__vc(__comp))
1050        {
1051            insert(__f, __l);
1052        }
1053
1054    template <class _InputIterator>
1055    _LIBCPP_INLINE_VISIBILITY
1056        map(_InputIterator __f, _InputIterator __l,
1057            const key_compare& __comp, const allocator_type& __a)
1058        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1059        {
1060            insert(__f, __l);
1061        }
1062
1063#if _LIBCPP_STD_VER > 11
1064    template <class _InputIterator>
1065    _LIBCPP_INLINE_VISIBILITY
1066    map(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
1067        : map(__f, __l, key_compare(), __a) {}
1068#endif
1069
1070    _LIBCPP_INLINE_VISIBILITY
1071    map(const map& __m)
1072        : __tree_(__m.__tree_)
1073        {
1074            insert(__m.begin(), __m.end());
1075        }
1076
1077    _LIBCPP_INLINE_VISIBILITY
1078    map& operator=(const map& __m)
1079        {
1080#ifndef _LIBCPP_CXX03_LANG
1081            __tree_ = __m.__tree_;
1082#else
1083            if (this != _VSTD::addressof(__m)) {
1084                __tree_.clear();
1085                __tree_.value_comp() = __m.__tree_.value_comp();
1086                __tree_.__copy_assign_alloc(__m.__tree_);
1087                insert(__m.begin(), __m.end());
1088            }
1089#endif
1090            return *this;
1091        }
1092
1093#ifndef _LIBCPP_CXX03_LANG
1094
1095    _LIBCPP_INLINE_VISIBILITY
1096    map(map&& __m)
1097        _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1098        : __tree_(_VSTD::move(__m.__tree_))
1099        {
1100        }
1101
1102    map(map&& __m, const allocator_type& __a);
1103
1104    _LIBCPP_INLINE_VISIBILITY
1105    map& operator=(map&& __m)
1106        _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
1107        {
1108            __tree_ = _VSTD::move(__m.__tree_);
1109            return *this;
1110        }
1111
1112    _LIBCPP_INLINE_VISIBILITY
1113    map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1114        : __tree_(__vc(__comp))
1115        {
1116            insert(__il.begin(), __il.end());
1117        }
1118
1119    _LIBCPP_INLINE_VISIBILITY
1120    map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1121        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1122        {
1123            insert(__il.begin(), __il.end());
1124        }
1125
1126#if _LIBCPP_STD_VER > 11
1127    _LIBCPP_INLINE_VISIBILITY
1128    map(initializer_list<value_type> __il, const allocator_type& __a)
1129        : map(__il, key_compare(), __a) {}
1130#endif
1131
1132    _LIBCPP_INLINE_VISIBILITY
1133    map& operator=(initializer_list<value_type> __il)
1134        {
1135            __tree_.__assign_unique(__il.begin(), __il.end());
1136            return *this;
1137        }
1138
1139#endif // _LIBCPP_CXX03_LANG
1140
1141    _LIBCPP_INLINE_VISIBILITY
1142    explicit map(const allocator_type& __a)
1143        : __tree_(typename __base::allocator_type(__a))
1144        {
1145        }
1146
1147    _LIBCPP_INLINE_VISIBILITY
1148    map(const map& __m, const allocator_type& __a)
1149        : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
1150        {
1151            insert(__m.begin(), __m.end());
1152        }
1153
1154    _LIBCPP_INLINE_VISIBILITY
1155    ~map() {
1156        static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
1157    }
1158
1159    _LIBCPP_INLINE_VISIBILITY
1160          iterator begin() _NOEXCEPT {return __tree_.begin();}
1161    _LIBCPP_INLINE_VISIBILITY
1162    const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
1163    _LIBCPP_INLINE_VISIBILITY
1164          iterator end() _NOEXCEPT {return __tree_.end();}
1165    _LIBCPP_INLINE_VISIBILITY
1166    const_iterator end() const _NOEXCEPT {return __tree_.end();}
1167
1168    _LIBCPP_INLINE_VISIBILITY
1169          reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
1170    _LIBCPP_INLINE_VISIBILITY
1171    const_reverse_iterator rbegin() const _NOEXCEPT
1172        {return const_reverse_iterator(end());}
1173    _LIBCPP_INLINE_VISIBILITY
1174          reverse_iterator rend() _NOEXCEPT
1175            {return       reverse_iterator(begin());}
1176    _LIBCPP_INLINE_VISIBILITY
1177    const_reverse_iterator rend() const _NOEXCEPT
1178        {return const_reverse_iterator(begin());}
1179
1180    _LIBCPP_INLINE_VISIBILITY
1181    const_iterator cbegin() const _NOEXCEPT {return begin();}
1182    _LIBCPP_INLINE_VISIBILITY
1183    const_iterator cend() const _NOEXCEPT {return end();}
1184    _LIBCPP_INLINE_VISIBILITY
1185    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
1186    _LIBCPP_INLINE_VISIBILITY
1187    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
1188
1189    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1190    bool      empty() const _NOEXCEPT {return __tree_.size() == 0;}
1191    _LIBCPP_INLINE_VISIBILITY
1192    size_type size() const _NOEXCEPT {return __tree_.size();}
1193    _LIBCPP_INLINE_VISIBILITY
1194    size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
1195
1196    mapped_type& operator[](const key_type& __k);
1197#ifndef _LIBCPP_CXX03_LANG
1198    mapped_type& operator[](key_type&& __k);
1199#endif
1200
1201          mapped_type& at(const key_type& __k);
1202    const mapped_type& at(const key_type& __k) const;
1203
1204    _LIBCPP_INLINE_VISIBILITY
1205    allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
1206    _LIBCPP_INLINE_VISIBILITY
1207    key_compare    key_comp()      const {return __tree_.value_comp().key_comp();}
1208    _LIBCPP_INLINE_VISIBILITY
1209    value_compare  value_comp()    const {return value_compare(__tree_.value_comp().key_comp());}
1210
1211#ifndef _LIBCPP_CXX03_LANG
1212    template <class ..._Args>
1213    _LIBCPP_INLINE_VISIBILITY
1214    pair<iterator, bool> emplace(_Args&& ...__args) {
1215        return __tree_.__emplace_unique(_VSTD::forward<_Args>(__args)...);
1216    }
1217
1218    template <class ..._Args>
1219    _LIBCPP_INLINE_VISIBILITY
1220    iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
1221        return __tree_.__emplace_hint_unique(__p.__i_, _VSTD::forward<_Args>(__args)...);
1222    }
1223
1224    template <class _Pp,
1225              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
1226        _LIBCPP_INLINE_VISIBILITY
1227        pair<iterator, bool> insert(_Pp&& __p)
1228            {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}
1229
1230    template <class _Pp,
1231              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
1232        _LIBCPP_INLINE_VISIBILITY
1233        iterator insert(const_iterator __pos, _Pp&& __p)
1234            {return __tree_.__insert_unique(__pos.__i_, _VSTD::forward<_Pp>(__p));}
1235
1236#endif // _LIBCPP_CXX03_LANG
1237
1238    _LIBCPP_INLINE_VISIBILITY
1239    pair<iterator, bool>
1240        insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
1241
1242    _LIBCPP_INLINE_VISIBILITY
1243    iterator
1244        insert(const_iterator __p, const value_type& __v)
1245            {return __tree_.__insert_unique(__p.__i_, __v);}
1246
1247#ifndef _LIBCPP_CXX03_LANG
1248    _LIBCPP_INLINE_VISIBILITY
1249    pair<iterator, bool>
1250    insert(value_type&& __v) {return __tree_.__insert_unique(_VSTD::move(__v));}
1251
1252    _LIBCPP_INLINE_VISIBILITY
1253    iterator insert(const_iterator __p,  value_type&& __v)
1254    {return __tree_.__insert_unique(__p.__i_, _VSTD::move(__v));}
1255
1256    _LIBCPP_INLINE_VISIBILITY
1257    void insert(initializer_list<value_type> __il)
1258        {insert(__il.begin(), __il.end());}
1259#endif
1260
1261    template <class _InputIterator>
1262        _LIBCPP_INLINE_VISIBILITY
1263        void insert(_InputIterator __f, _InputIterator __l)
1264        {
1265            for (const_iterator __e = cend(); __f != __l; ++__f)
1266                insert(__e.__i_, *__f);
1267        }
1268
1269#if _LIBCPP_STD_VER > 14
1270
1271    template <class... _Args>
1272        _LIBCPP_INLINE_VISIBILITY
1273        pair<iterator, bool> try_emplace(const key_type& __k, _Args&&... __args)
1274    {
1275        return __tree_.__emplace_unique_key_args(__k,
1276            _VSTD::piecewise_construct,
1277            _VSTD::forward_as_tuple(__k),
1278            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
1279    }
1280
1281    template <class... _Args>
1282        _LIBCPP_INLINE_VISIBILITY
1283        pair<iterator, bool> try_emplace(key_type&& __k, _Args&&... __args)
1284    {
1285        return __tree_.__emplace_unique_key_args(__k,
1286            _VSTD::piecewise_construct,
1287            _VSTD::forward_as_tuple(_VSTD::move(__k)),
1288            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
1289    }
1290
1291    template <class... _Args>
1292        _LIBCPP_INLINE_VISIBILITY
1293        iterator try_emplace(const_iterator __h, const key_type& __k, _Args&&... __args)
1294    {
1295        return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
1296            _VSTD::piecewise_construct,
1297            _VSTD::forward_as_tuple(__k),
1298            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
1299    }
1300
1301    template <class... _Args>
1302        _LIBCPP_INLINE_VISIBILITY
1303        iterator try_emplace(const_iterator __h, key_type&& __k, _Args&&... __args)
1304    {
1305        return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
1306            _VSTD::piecewise_construct,
1307            _VSTD::forward_as_tuple(_VSTD::move(__k)),
1308            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
1309    }
1310
1311    template <class _Vp>
1312        _LIBCPP_INLINE_VISIBILITY
1313        pair<iterator, bool> insert_or_assign(const key_type& __k, _Vp&& __v)
1314    {
1315        iterator __p = lower_bound(__k);
1316        if ( __p != end() && !key_comp()(__k, __p->first))
1317        {
1318            __p->second = _VSTD::forward<_Vp>(__v);
1319            return _VSTD::make_pair(__p, false);
1320        }
1321        return _VSTD::make_pair(emplace_hint(__p, __k, _VSTD::forward<_Vp>(__v)), true);
1322    }
1323
1324    template <class _Vp>
1325        _LIBCPP_INLINE_VISIBILITY
1326        pair<iterator, bool> insert_or_assign(key_type&& __k, _Vp&& __v)
1327    {
1328        iterator __p = lower_bound(__k);
1329        if ( __p != end() && !key_comp()(__k, __p->first))
1330        {
1331            __p->second = _VSTD::forward<_Vp>(__v);
1332            return _VSTD::make_pair(__p, false);
1333        }
1334        return _VSTD::make_pair(emplace_hint(__p, _VSTD::move(__k), _VSTD::forward<_Vp>(__v)), true);
1335    }
1336
1337    template <class _Vp>
1338    _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
1339                                                        const key_type& __k,
1340                                                        _Vp&& __v) {
1341      auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
1342          __h.__i_, __k, __k, _VSTD::forward<_Vp>(__v));
1343
1344      if (!__inserted)
1345        __r->__get_value().second = _VSTD::forward<_Vp>(__v);
1346
1347      return __r;
1348    }
1349
1350    template <class _Vp>
1351    _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
1352                                                        key_type&& __k,
1353                                                        _Vp&& __v) {
1354      auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
1355          __h.__i_, __k, _VSTD::move(__k), _VSTD::forward<_Vp>(__v));
1356
1357      if (!__inserted)
1358        __r->__get_value().second = _VSTD::forward<_Vp>(__v);
1359
1360      return __r;
1361    }
1362
1363#endif // _LIBCPP_STD_VER > 14
1364
1365    _LIBCPP_INLINE_VISIBILITY
1366    iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
1367    _LIBCPP_INLINE_VISIBILITY
1368    iterator erase(iterator __p)       {return __tree_.erase(__p.__i_);}
1369    _LIBCPP_INLINE_VISIBILITY
1370    size_type erase(const key_type& __k)
1371        {return __tree_.__erase_unique(__k);}
1372    _LIBCPP_INLINE_VISIBILITY
1373    iterator  erase(const_iterator __f, const_iterator __l)
1374        {return __tree_.erase(__f.__i_, __l.__i_);}
1375    _LIBCPP_INLINE_VISIBILITY
1376    void clear() _NOEXCEPT {__tree_.clear();}
1377
1378#if _LIBCPP_STD_VER > 14
1379    _LIBCPP_INLINE_VISIBILITY
1380    insert_return_type insert(node_type&& __nh)
1381    {
1382        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
1383            "node_type with incompatible allocator passed to map::insert()");
1384        return __tree_.template __node_handle_insert_unique<
1385            node_type, insert_return_type>(_VSTD::move(__nh));
1386    }
1387    _LIBCPP_INLINE_VISIBILITY
1388    iterator insert(const_iterator __hint, node_type&& __nh)
1389    {
1390        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
1391            "node_type with incompatible allocator passed to map::insert()");
1392        return __tree_.template __node_handle_insert_unique<node_type>(
1393            __hint.__i_, _VSTD::move(__nh));
1394    }
1395    _LIBCPP_INLINE_VISIBILITY
1396    node_type extract(key_type const& __key)
1397    {
1398        return __tree_.template __node_handle_extract<node_type>(__key);
1399    }
1400    _LIBCPP_INLINE_VISIBILITY
1401    node_type extract(const_iterator __it)
1402    {
1403        return __tree_.template __node_handle_extract<node_type>(__it.__i_);
1404    }
1405    template <class _Compare2>
1406    _LIBCPP_INLINE_VISIBILITY
1407    void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
1408    {
1409        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1410                       "merging container with incompatible allocator");
1411        __tree_.__node_handle_merge_unique(__source.__tree_);
1412    }
1413    template <class _Compare2>
1414    _LIBCPP_INLINE_VISIBILITY
1415    void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
1416    {
1417        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1418                       "merging container with incompatible allocator");
1419        __tree_.__node_handle_merge_unique(__source.__tree_);
1420    }
1421    template <class _Compare2>
1422    _LIBCPP_INLINE_VISIBILITY
1423    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
1424    {
1425        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1426                       "merging container with incompatible allocator");
1427        __tree_.__node_handle_merge_unique(__source.__tree_);
1428    }
1429    template <class _Compare2>
1430    _LIBCPP_INLINE_VISIBILITY
1431    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
1432    {
1433        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1434                       "merging container with incompatible allocator");
1435        __tree_.__node_handle_merge_unique(__source.__tree_);
1436    }
1437#endif
1438
1439    _LIBCPP_INLINE_VISIBILITY
1440    void swap(map& __m)
1441        _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
1442        {__tree_.swap(__m.__tree_);}
1443
1444    _LIBCPP_INLINE_VISIBILITY
1445    iterator find(const key_type& __k)             {return __tree_.find(__k);}
1446    _LIBCPP_INLINE_VISIBILITY
1447    const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
1448#if _LIBCPP_STD_VER > 11
1449    template <typename _K2>
1450    _LIBCPP_INLINE_VISIBILITY
1451    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1452    find(const _K2& __k)                           {return __tree_.find(__k);}
1453    template <typename _K2>
1454    _LIBCPP_INLINE_VISIBILITY
1455    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1456    find(const _K2& __k) const                     {return __tree_.find(__k);}
1457#endif
1458
1459    _LIBCPP_INLINE_VISIBILITY
1460    size_type      count(const key_type& __k) const
1461        {return __tree_.__count_unique(__k);}
1462#if _LIBCPP_STD_VER > 11
1463    template <typename _K2>
1464    _LIBCPP_INLINE_VISIBILITY
1465    typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
1466    count(const _K2& __k) const {return __tree_.__count_multi(__k);}
1467#endif
1468
1469#if _LIBCPP_STD_VER > 17
1470    _LIBCPP_INLINE_VISIBILITY
1471    bool contains(const key_type& __k) const {return find(__k) != end();}
1472    template <typename _K2>
1473    _LIBCPP_INLINE_VISIBILITY
1474    typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
1475    contains(const _K2& __k) const { return find(__k) != end(); }
1476#endif // _LIBCPP_STD_VER > 17
1477
1478    _LIBCPP_INLINE_VISIBILITY
1479    iterator lower_bound(const key_type& __k)
1480        {return __tree_.lower_bound(__k);}
1481    _LIBCPP_INLINE_VISIBILITY
1482    const_iterator lower_bound(const key_type& __k) const
1483        {return __tree_.lower_bound(__k);}
1484#if _LIBCPP_STD_VER > 11
1485    template <typename _K2>
1486    _LIBCPP_INLINE_VISIBILITY
1487    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1488    lower_bound(const _K2& __k)       {return __tree_.lower_bound(__k);}
1489
1490    template <typename _K2>
1491    _LIBCPP_INLINE_VISIBILITY
1492    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1493    lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
1494#endif
1495
1496    _LIBCPP_INLINE_VISIBILITY
1497    iterator upper_bound(const key_type& __k)
1498        {return __tree_.upper_bound(__k);}
1499    _LIBCPP_INLINE_VISIBILITY
1500    const_iterator upper_bound(const key_type& __k) const
1501        {return __tree_.upper_bound(__k);}
1502#if _LIBCPP_STD_VER > 11
1503    template <typename _K2>
1504    _LIBCPP_INLINE_VISIBILITY
1505    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1506    upper_bound(const _K2& __k)       {return __tree_.upper_bound(__k);}
1507    template <typename _K2>
1508    _LIBCPP_INLINE_VISIBILITY
1509    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1510    upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
1511#endif
1512
1513    _LIBCPP_INLINE_VISIBILITY
1514    pair<iterator,iterator> equal_range(const key_type& __k)
1515        {return __tree_.__equal_range_unique(__k);}
1516    _LIBCPP_INLINE_VISIBILITY
1517    pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1518        {return __tree_.__equal_range_unique(__k);}
1519#if _LIBCPP_STD_VER > 11
1520    template <typename _K2>
1521    _LIBCPP_INLINE_VISIBILITY
1522    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
1523    equal_range(const _K2& __k)       {return __tree_.__equal_range_multi(__k);}
1524    template <typename _K2>
1525    _LIBCPP_INLINE_VISIBILITY
1526    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
1527    equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
1528#endif
1529
1530private:
1531    typedef typename __base::__node                    __node;
1532    typedef typename __base::__node_allocator          __node_allocator;
1533    typedef typename __base::__node_pointer            __node_pointer;
1534    typedef typename __base::__node_base_pointer       __node_base_pointer;
1535    typedef typename __base::__parent_pointer          __parent_pointer;
1536
1537    typedef __map_node_destructor<__node_allocator> _Dp;
1538    typedef unique_ptr<__node, _Dp> __node_holder;
1539
1540#ifdef _LIBCPP_CXX03_LANG
1541    __node_holder __construct_node_with_key(const key_type& __k);
1542#endif
1543};
1544
1545#if _LIBCPP_STD_VER >= 17
1546template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
1547         class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
1548         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
1549         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
1550         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1551map(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
1552  -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
1553
1554template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
1555         class _Allocator = allocator<pair<const _Key, _Tp>>,
1556         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
1557         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1558map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
1559  -> map<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
1560
1561template<class _InputIterator, class _Allocator,
1562         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
1563         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1564map(_InputIterator, _InputIterator, _Allocator)
1565  -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
1566         less<__iter_key_type<_InputIterator>>, _Allocator>;
1567
1568template<class _Key, class _Tp, class _Allocator,
1569         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1570map(initializer_list<pair<_Key, _Tp>>, _Allocator)
1571  -> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
1572#endif
1573
1574#ifndef _LIBCPP_CXX03_LANG
1575template <class _Key, class _Tp, class _Compare, class _Allocator>
1576map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
1577    : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
1578{
1579    if (__a != __m.get_allocator())
1580    {
1581        const_iterator __e = cend();
1582        while (!__m.empty())
1583            __tree_.__insert_unique(__e.__i_,
1584                    __m.__tree_.remove(__m.begin().__i_)->__value_.__move());
1585    }
1586}
1587
1588template <class _Key, class _Tp, class _Compare, class _Allocator>
1589_Tp&
1590map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1591{
1592    return __tree_.__emplace_unique_key_args(__k,
1593        _VSTD::piecewise_construct,
1594        _VSTD::forward_as_tuple(__k),
1595        _VSTD::forward_as_tuple()).first->__get_value().second;
1596}
1597
1598template <class _Key, class _Tp, class _Compare, class _Allocator>
1599_Tp&
1600map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1601{
1602    return __tree_.__emplace_unique_key_args(__k,
1603        _VSTD::piecewise_construct,
1604        _VSTD::forward_as_tuple(_VSTD::move(__k)),
1605        _VSTD::forward_as_tuple()).first->__get_value().second;
1606}
1607
1608#else // _LIBCPP_CXX03_LANG
1609
1610template <class _Key, class _Tp, class _Compare, class _Allocator>
1611typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1612map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(const key_type& __k)
1613{
1614    __node_allocator& __na = __tree_.__node_alloc();
1615    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1616    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().first), __k);
1617    __h.get_deleter().__first_constructed = true;
1618    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().second));
1619    __h.get_deleter().__second_constructed = true;
1620    return __h;
1621}
1622
1623template <class _Key, class _Tp, class _Compare, class _Allocator>
1624_Tp&
1625map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1626{
1627    __parent_pointer __parent;
1628    __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
1629    __node_pointer __r = static_cast<__node_pointer>(__child);
1630    if (__child == nullptr)
1631    {
1632        __node_holder __h = __construct_node_with_key(__k);
1633        __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1634        __r = __h.release();
1635    }
1636    return __r->__value_.__get_value().second;
1637}
1638
1639#endif // _LIBCPP_CXX03_LANG
1640
1641template <class _Key, class _Tp, class _Compare, class _Allocator>
1642_Tp&
1643map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1644{
1645    __parent_pointer __parent;
1646    __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
1647    if (__child == nullptr)
1648        __throw_out_of_range("map::at:  key not found");
1649    return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
1650}
1651
1652template <class _Key, class _Tp, class _Compare, class _Allocator>
1653const _Tp&
1654map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1655{
1656    __parent_pointer __parent;
1657    __node_base_pointer __child = __tree_.__find_equal(__parent, __k);
1658    if (__child == nullptr)
1659        __throw_out_of_range("map::at:  key not found");
1660    return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
1661}
1662
1663
1664template <class _Key, class _Tp, class _Compare, class _Allocator>
1665inline _LIBCPP_INLINE_VISIBILITY
1666bool
1667operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1668           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1669{
1670    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
1671}
1672
1673template <class _Key, class _Tp, class _Compare, class _Allocator>
1674inline _LIBCPP_INLINE_VISIBILITY
1675bool
1676operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1677           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1678{
1679    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1680}
1681
1682template <class _Key, class _Tp, class _Compare, class _Allocator>
1683inline _LIBCPP_INLINE_VISIBILITY
1684bool
1685operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1686           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1687{
1688    return !(__x == __y);
1689}
1690
1691template <class _Key, class _Tp, class _Compare, class _Allocator>
1692inline _LIBCPP_INLINE_VISIBILITY
1693bool
1694operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1695           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1696{
1697    return __y < __x;
1698}
1699
1700template <class _Key, class _Tp, class _Compare, class _Allocator>
1701inline _LIBCPP_INLINE_VISIBILITY
1702bool
1703operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1704           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1705{
1706    return !(__x < __y);
1707}
1708
1709template <class _Key, class _Tp, class _Compare, class _Allocator>
1710inline _LIBCPP_INLINE_VISIBILITY
1711bool
1712operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1713           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1714{
1715    return !(__y < __x);
1716}
1717
1718template <class _Key, class _Tp, class _Compare, class _Allocator>
1719inline _LIBCPP_INLINE_VISIBILITY
1720void
1721swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1722     map<_Key, _Tp, _Compare, _Allocator>& __y)
1723    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
1724{
1725    __x.swap(__y);
1726}
1727
1728#if _LIBCPP_STD_VER > 17
1729template <class _Key, class _Tp, class _Compare, class _Allocator,
1730          class _Predicate>
1731inline _LIBCPP_INLINE_VISIBILITY
1732    typename map<_Key, _Tp, _Compare, _Allocator>::size_type
1733    erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
1734  return _VSTD::__libcpp_erase_if_container(__c, __pred);
1735}
1736#endif
1737
1738
1739template <class _Key, class _Tp, class _Compare = less<_Key>,
1740          class _Allocator = allocator<pair<const _Key, _Tp> > >
1741class _LIBCPP_TEMPLATE_VIS multimap
1742{
1743public:
1744    // types:
1745    typedef _Key                                     key_type;
1746    typedef _Tp                                      mapped_type;
1747    typedef pair<const key_type, mapped_type>        value_type;
1748    typedef __identity_t<_Compare>                   key_compare;
1749    typedef __identity_t<_Allocator>                 allocator_type;
1750    typedef value_type&                              reference;
1751    typedef const value_type&                        const_reference;
1752
1753    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1754                  "Allocator::value_type must be same type as value_type");
1755
1756_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1757    class _LIBCPP_TEMPLATE_VIS value_compare
1758#if defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1759        : public binary_function<value_type, value_type, bool>
1760#endif
1761    {
1762_LIBCPP_SUPPRESS_DEPRECATED_POP
1763        friend class multimap;
1764    protected:
1765        key_compare comp;
1766
1767        _LIBCPP_INLINE_VISIBILITY
1768        value_compare(key_compare c) : comp(c) {}
1769    public:
1770#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1771        _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
1772        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type first_argument_type;
1773        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type second_argument_type;
1774#endif
1775        _LIBCPP_INLINE_VISIBILITY
1776        bool operator()(const value_type& __x, const value_type& __y) const
1777            {return comp(__x.first, __y.first);}
1778    };
1779
1780private:
1781
1782    typedef _VSTD::__value_type<key_type, mapped_type>             __value_type;
1783    typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
1784    typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
1785                                                 __value_type>::type __allocator_type;
1786    typedef __tree<__value_type, __vc, __allocator_type>            __base;
1787    typedef typename __base::__node_traits                          __node_traits;
1788    typedef allocator_traits<allocator_type>                        __alloc_traits;
1789
1790    __base __tree_;
1791
1792public:
1793    typedef typename __alloc_traits::pointer               pointer;
1794    typedef typename __alloc_traits::const_pointer         const_pointer;
1795    typedef typename __alloc_traits::size_type             size_type;
1796    typedef typename __alloc_traits::difference_type       difference_type;
1797    typedef __map_iterator<typename __base::iterator>      iterator;
1798    typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1799    typedef _VSTD::reverse_iterator<iterator>               reverse_iterator;
1800    typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
1801
1802#if _LIBCPP_STD_VER > 14
1803    typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
1804#endif
1805
1806    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1807        friend class _LIBCPP_TEMPLATE_VIS map;
1808    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1809        friend class _LIBCPP_TEMPLATE_VIS multimap;
1810
1811    _LIBCPP_INLINE_VISIBILITY
1812    multimap()
1813        _NOEXCEPT_(
1814            is_nothrow_default_constructible<allocator_type>::value &&
1815            is_nothrow_default_constructible<key_compare>::value &&
1816            is_nothrow_copy_constructible<key_compare>::value)
1817        : __tree_(__vc(key_compare())) {}
1818
1819    _LIBCPP_INLINE_VISIBILITY
1820    explicit multimap(const key_compare& __comp)
1821        _NOEXCEPT_(
1822            is_nothrow_default_constructible<allocator_type>::value &&
1823            is_nothrow_copy_constructible<key_compare>::value)
1824        : __tree_(__vc(__comp)) {}
1825
1826    _LIBCPP_INLINE_VISIBILITY
1827    explicit multimap(const key_compare& __comp, const allocator_type& __a)
1828        : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
1829
1830    template <class _InputIterator>
1831        _LIBCPP_INLINE_VISIBILITY
1832        multimap(_InputIterator __f, _InputIterator __l,
1833            const key_compare& __comp = key_compare())
1834        : __tree_(__vc(__comp))
1835        {
1836            insert(__f, __l);
1837        }
1838
1839    template <class _InputIterator>
1840        _LIBCPP_INLINE_VISIBILITY
1841        multimap(_InputIterator __f, _InputIterator __l,
1842            const key_compare& __comp, const allocator_type& __a)
1843        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1844        {
1845            insert(__f, __l);
1846        }
1847
1848#if _LIBCPP_STD_VER > 11
1849    template <class _InputIterator>
1850    _LIBCPP_INLINE_VISIBILITY
1851    multimap(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
1852        : multimap(__f, __l, key_compare(), __a) {}
1853#endif
1854
1855    _LIBCPP_INLINE_VISIBILITY
1856    multimap(const multimap& __m)
1857        : __tree_(__m.__tree_.value_comp(),
1858          __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1859        {
1860            insert(__m.begin(), __m.end());
1861        }
1862
1863    _LIBCPP_INLINE_VISIBILITY
1864    multimap& operator=(const multimap& __m)
1865        {
1866#ifndef _LIBCPP_CXX03_LANG
1867            __tree_ = __m.__tree_;
1868#else
1869            if (this != _VSTD::addressof(__m)) {
1870                __tree_.clear();
1871                __tree_.value_comp() = __m.__tree_.value_comp();
1872                __tree_.__copy_assign_alloc(__m.__tree_);
1873                insert(__m.begin(), __m.end());
1874            }
1875#endif
1876            return *this;
1877        }
1878
1879#ifndef _LIBCPP_CXX03_LANG
1880
1881    _LIBCPP_INLINE_VISIBILITY
1882    multimap(multimap&& __m)
1883        _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1884        : __tree_(_VSTD::move(__m.__tree_))
1885        {
1886        }
1887
1888    multimap(multimap&& __m, const allocator_type& __a);
1889
1890    _LIBCPP_INLINE_VISIBILITY
1891    multimap& operator=(multimap&& __m)
1892        _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
1893        {
1894            __tree_ = _VSTD::move(__m.__tree_);
1895            return *this;
1896        }
1897
1898    _LIBCPP_INLINE_VISIBILITY
1899    multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1900        : __tree_(__vc(__comp))
1901        {
1902            insert(__il.begin(), __il.end());
1903        }
1904
1905    _LIBCPP_INLINE_VISIBILITY
1906    multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1907        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1908        {
1909            insert(__il.begin(), __il.end());
1910        }
1911
1912#if _LIBCPP_STD_VER > 11
1913    _LIBCPP_INLINE_VISIBILITY
1914    multimap(initializer_list<value_type> __il, const allocator_type& __a)
1915        : multimap(__il, key_compare(), __a) {}
1916#endif
1917
1918    _LIBCPP_INLINE_VISIBILITY
1919    multimap& operator=(initializer_list<value_type> __il)
1920        {
1921            __tree_.__assign_multi(__il.begin(), __il.end());
1922            return *this;
1923        }
1924
1925#endif // _LIBCPP_CXX03_LANG
1926
1927    _LIBCPP_INLINE_VISIBILITY
1928    explicit multimap(const allocator_type& __a)
1929        : __tree_(typename __base::allocator_type(__a))
1930        {
1931        }
1932
1933    _LIBCPP_INLINE_VISIBILITY
1934    multimap(const multimap& __m, const allocator_type& __a)
1935        : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
1936        {
1937            insert(__m.begin(), __m.end());
1938        }
1939
1940    _LIBCPP_INLINE_VISIBILITY
1941    ~multimap() {
1942        static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
1943    }
1944
1945    _LIBCPP_INLINE_VISIBILITY
1946          iterator begin() _NOEXCEPT {return __tree_.begin();}
1947    _LIBCPP_INLINE_VISIBILITY
1948    const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
1949    _LIBCPP_INLINE_VISIBILITY
1950          iterator end() _NOEXCEPT {return __tree_.end();}
1951    _LIBCPP_INLINE_VISIBILITY
1952    const_iterator end() const _NOEXCEPT {return __tree_.end();}
1953
1954    _LIBCPP_INLINE_VISIBILITY
1955          reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
1956    _LIBCPP_INLINE_VISIBILITY
1957    const_reverse_iterator rbegin() const _NOEXCEPT
1958        {return const_reverse_iterator(end());}
1959    _LIBCPP_INLINE_VISIBILITY
1960          reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
1961    _LIBCPP_INLINE_VISIBILITY
1962    const_reverse_iterator rend() const _NOEXCEPT
1963        {return const_reverse_iterator(begin());}
1964
1965    _LIBCPP_INLINE_VISIBILITY
1966    const_iterator cbegin()  const _NOEXCEPT {return begin();}
1967    _LIBCPP_INLINE_VISIBILITY
1968    const_iterator cend() const _NOEXCEPT {return end();}
1969    _LIBCPP_INLINE_VISIBILITY
1970    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
1971    _LIBCPP_INLINE_VISIBILITY
1972    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
1973
1974    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1975    bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
1976    _LIBCPP_INLINE_VISIBILITY
1977    size_type size() const _NOEXCEPT {return __tree_.size();}
1978    _LIBCPP_INLINE_VISIBILITY
1979    size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
1980
1981    _LIBCPP_INLINE_VISIBILITY
1982    allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
1983    _LIBCPP_INLINE_VISIBILITY
1984    key_compare    key_comp() const {return __tree_.value_comp().key_comp();}
1985    _LIBCPP_INLINE_VISIBILITY
1986    value_compare  value_comp() const
1987        {return value_compare(__tree_.value_comp().key_comp());}
1988
1989#ifndef _LIBCPP_CXX03_LANG
1990
1991    template <class ..._Args>
1992    _LIBCPP_INLINE_VISIBILITY
1993    iterator emplace(_Args&& ...__args) {
1994        return __tree_.__emplace_multi(_VSTD::forward<_Args>(__args)...);
1995    }
1996
1997    template <class ..._Args>
1998    _LIBCPP_INLINE_VISIBILITY
1999    iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
2000        return __tree_.__emplace_hint_multi(__p.__i_, _VSTD::forward<_Args>(__args)...);
2001    }
2002
2003    template <class _Pp,
2004              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
2005        _LIBCPP_INLINE_VISIBILITY
2006        iterator insert(_Pp&& __p)
2007            {return __tree_.__insert_multi(_VSTD::forward<_Pp>(__p));}
2008
2009    template <class _Pp,
2010              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
2011        _LIBCPP_INLINE_VISIBILITY
2012        iterator insert(const_iterator __pos, _Pp&& __p)
2013            {return __tree_.__insert_multi(__pos.__i_, _VSTD::forward<_Pp>(__p));}
2014
2015    _LIBCPP_INLINE_VISIBILITY
2016    iterator insert(value_type&& __v)
2017        {return __tree_.__insert_multi(_VSTD::move(__v));}
2018
2019    _LIBCPP_INLINE_VISIBILITY
2020    iterator insert(const_iterator __p, value_type&& __v)
2021        {return __tree_.__insert_multi(__p.__i_, _VSTD::move(__v));}
2022
2023
2024    _LIBCPP_INLINE_VISIBILITY
2025    void insert(initializer_list<value_type> __il)
2026        {insert(__il.begin(), __il.end());}
2027
2028#endif // _LIBCPP_CXX03_LANG
2029
2030    _LIBCPP_INLINE_VISIBILITY
2031    iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
2032
2033    _LIBCPP_INLINE_VISIBILITY
2034    iterator insert(const_iterator __p, const value_type& __v)
2035            {return __tree_.__insert_multi(__p.__i_, __v);}
2036
2037    template <class _InputIterator>
2038        _LIBCPP_INLINE_VISIBILITY
2039        void insert(_InputIterator __f, _InputIterator __l)
2040        {
2041            for (const_iterator __e = cend(); __f != __l; ++__f)
2042                __tree_.__insert_multi(__e.__i_, *__f);
2043        }
2044
2045    _LIBCPP_INLINE_VISIBILITY
2046    iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
2047    _LIBCPP_INLINE_VISIBILITY
2048    iterator erase(iterator __p)       {return __tree_.erase(__p.__i_);}
2049    _LIBCPP_INLINE_VISIBILITY
2050    size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
2051    _LIBCPP_INLINE_VISIBILITY
2052    iterator  erase(const_iterator __f, const_iterator __l)
2053        {return __tree_.erase(__f.__i_, __l.__i_);}
2054
2055#if _LIBCPP_STD_VER > 14
2056    _LIBCPP_INLINE_VISIBILITY
2057    iterator insert(node_type&& __nh)
2058    {
2059        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
2060            "node_type with incompatible allocator passed to multimap::insert()");
2061        return __tree_.template __node_handle_insert_multi<node_type>(
2062            _VSTD::move(__nh));
2063    }
2064    _LIBCPP_INLINE_VISIBILITY
2065    iterator insert(const_iterator __hint, node_type&& __nh)
2066    {
2067        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
2068            "node_type with incompatible allocator passed to multimap::insert()");
2069        return __tree_.template __node_handle_insert_multi<node_type>(
2070            __hint.__i_, _VSTD::move(__nh));
2071    }
2072    _LIBCPP_INLINE_VISIBILITY
2073    node_type extract(key_type const& __key)
2074    {
2075        return __tree_.template __node_handle_extract<node_type>(__key);
2076    }
2077    _LIBCPP_INLINE_VISIBILITY
2078    node_type extract(const_iterator __it)
2079    {
2080        return __tree_.template __node_handle_extract<node_type>(
2081            __it.__i_);
2082    }
2083    template <class _Compare2>
2084    _LIBCPP_INLINE_VISIBILITY
2085    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
2086    {
2087        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2088                       "merging container with incompatible allocator");
2089        return __tree_.__node_handle_merge_multi(__source.__tree_);
2090    }
2091    template <class _Compare2>
2092    _LIBCPP_INLINE_VISIBILITY
2093    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
2094    {
2095        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2096                       "merging container with incompatible allocator");
2097        return __tree_.__node_handle_merge_multi(__source.__tree_);
2098    }
2099    template <class _Compare2>
2100    _LIBCPP_INLINE_VISIBILITY
2101    void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
2102    {
2103        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2104                       "merging container with incompatible allocator");
2105        return __tree_.__node_handle_merge_multi(__source.__tree_);
2106    }
2107    template <class _Compare2>
2108    _LIBCPP_INLINE_VISIBILITY
2109    void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
2110    {
2111        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2112                       "merging container with incompatible allocator");
2113        return __tree_.__node_handle_merge_multi(__source.__tree_);
2114    }
2115#endif
2116
2117    _LIBCPP_INLINE_VISIBILITY
2118    void clear() _NOEXCEPT {__tree_.clear();}
2119
2120    _LIBCPP_INLINE_VISIBILITY
2121    void swap(multimap& __m)
2122        _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
2123        {__tree_.swap(__m.__tree_);}
2124
2125    _LIBCPP_INLINE_VISIBILITY
2126    iterator find(const key_type& __k)             {return __tree_.find(__k);}
2127    _LIBCPP_INLINE_VISIBILITY
2128    const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
2129#if _LIBCPP_STD_VER > 11
2130    template <typename _K2>
2131    _LIBCPP_INLINE_VISIBILITY
2132    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2133    find(const _K2& __k)                           {return __tree_.find(__k);}
2134    template <typename _K2>
2135    _LIBCPP_INLINE_VISIBILITY
2136    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2137    find(const _K2& __k) const                     {return __tree_.find(__k);}
2138#endif
2139
2140    _LIBCPP_INLINE_VISIBILITY
2141    size_type      count(const key_type& __k) const
2142        {return __tree_.__count_multi(__k);}
2143#if _LIBCPP_STD_VER > 11
2144    template <typename _K2>
2145    _LIBCPP_INLINE_VISIBILITY
2146    typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
2147    count(const _K2& __k) const {return __tree_.__count_multi(__k);}
2148#endif
2149
2150#if _LIBCPP_STD_VER > 17
2151    _LIBCPP_INLINE_VISIBILITY
2152    bool contains(const key_type& __k) const {return find(__k) != end();}
2153    template <typename _K2>
2154    _LIBCPP_INLINE_VISIBILITY
2155    typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
2156    contains(const _K2& __k) const { return find(__k) != end(); }
2157#endif // _LIBCPP_STD_VER > 17
2158
2159    _LIBCPP_INLINE_VISIBILITY
2160    iterator lower_bound(const key_type& __k)
2161        {return __tree_.lower_bound(__k);}
2162    _LIBCPP_INLINE_VISIBILITY
2163    const_iterator lower_bound(const key_type& __k) const
2164            {return __tree_.lower_bound(__k);}
2165#if _LIBCPP_STD_VER > 11
2166    template <typename _K2>
2167    _LIBCPP_INLINE_VISIBILITY
2168    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2169    lower_bound(const _K2& __k)       {return __tree_.lower_bound(__k);}
2170
2171    template <typename _K2>
2172    _LIBCPP_INLINE_VISIBILITY
2173    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2174    lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
2175#endif
2176
2177    _LIBCPP_INLINE_VISIBILITY
2178    iterator upper_bound(const key_type& __k)
2179            {return __tree_.upper_bound(__k);}
2180    _LIBCPP_INLINE_VISIBILITY
2181    const_iterator upper_bound(const key_type& __k) const
2182            {return __tree_.upper_bound(__k);}
2183#if _LIBCPP_STD_VER > 11
2184    template <typename _K2>
2185    _LIBCPP_INLINE_VISIBILITY
2186    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2187    upper_bound(const _K2& __k)       {return __tree_.upper_bound(__k);}
2188    template <typename _K2>
2189    _LIBCPP_INLINE_VISIBILITY
2190    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2191    upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
2192#endif
2193
2194    _LIBCPP_INLINE_VISIBILITY
2195    pair<iterator,iterator>             equal_range(const key_type& __k)
2196            {return __tree_.__equal_range_multi(__k);}
2197    _LIBCPP_INLINE_VISIBILITY
2198    pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
2199            {return __tree_.__equal_range_multi(__k);}
2200#if _LIBCPP_STD_VER > 11
2201    template <typename _K2>
2202    _LIBCPP_INLINE_VISIBILITY
2203    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
2204    equal_range(const _K2& __k)       {return __tree_.__equal_range_multi(__k);}
2205    template <typename _K2>
2206    _LIBCPP_INLINE_VISIBILITY
2207    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
2208    equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
2209#endif
2210
2211private:
2212    typedef typename __base::__node                    __node;
2213    typedef typename __base::__node_allocator          __node_allocator;
2214    typedef typename __base::__node_pointer            __node_pointer;
2215
2216    typedef __map_node_destructor<__node_allocator> _Dp;
2217    typedef unique_ptr<__node, _Dp> __node_holder;
2218};
2219
2220#if _LIBCPP_STD_VER >= 17
2221template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
2222         class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
2223         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
2224         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
2225         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2226multimap(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
2227  -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
2228
2229template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
2230         class _Allocator = allocator<pair<const _Key, _Tp>>,
2231         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
2232         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2233multimap(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
2234  -> multimap<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
2235
2236template<class _InputIterator, class _Allocator,
2237         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
2238         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2239multimap(_InputIterator, _InputIterator, _Allocator)
2240  -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
2241         less<__iter_key_type<_InputIterator>>, _Allocator>;
2242
2243template<class _Key, class _Tp, class _Allocator,
2244         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2245multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
2246  -> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
2247#endif
2248
2249#ifndef _LIBCPP_CXX03_LANG
2250template <class _Key, class _Tp, class _Compare, class _Allocator>
2251multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
2252    : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
2253{
2254    if (__a != __m.get_allocator())
2255    {
2256        const_iterator __e = cend();
2257        while (!__m.empty())
2258            __tree_.__insert_multi(__e.__i_,
2259                    _VSTD::move(__m.__tree_.remove(__m.begin().__i_)->__value_.__move()));
2260    }
2261}
2262#endif
2263
2264template <class _Key, class _Tp, class _Compare, class _Allocator>
2265inline _LIBCPP_INLINE_VISIBILITY
2266bool
2267operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2268           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2269{
2270    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2271}
2272
2273template <class _Key, class _Tp, class _Compare, class _Allocator>
2274inline _LIBCPP_INLINE_VISIBILITY
2275bool
2276operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2277           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2278{
2279    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2280}
2281
2282template <class _Key, class _Tp, class _Compare, class _Allocator>
2283inline _LIBCPP_INLINE_VISIBILITY
2284bool
2285operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2286           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2287{
2288    return !(__x == __y);
2289}
2290
2291template <class _Key, class _Tp, class _Compare, class _Allocator>
2292inline _LIBCPP_INLINE_VISIBILITY
2293bool
2294operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2295           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2296{
2297    return __y < __x;
2298}
2299
2300template <class _Key, class _Tp, class _Compare, class _Allocator>
2301inline _LIBCPP_INLINE_VISIBILITY
2302bool
2303operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2304           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2305{
2306    return !(__x < __y);
2307}
2308
2309template <class _Key, class _Tp, class _Compare, class _Allocator>
2310inline _LIBCPP_INLINE_VISIBILITY
2311bool
2312operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2313           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2314{
2315    return !(__y < __x);
2316}
2317
2318template <class _Key, class _Tp, class _Compare, class _Allocator>
2319inline _LIBCPP_INLINE_VISIBILITY
2320void
2321swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2322     multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2323    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2324{
2325    __x.swap(__y);
2326}
2327
2328#if _LIBCPP_STD_VER > 17
2329template <class _Key, class _Tp, class _Compare, class _Allocator,
2330          class _Predicate>
2331inline _LIBCPP_INLINE_VISIBILITY
2332    typename multimap<_Key, _Tp, _Compare, _Allocator>::size_type
2333    erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c,
2334             _Predicate __pred) {
2335  return _VSTD::__libcpp_erase_if_container(__c, __pred);
2336}
2337#endif
2338
2339_LIBCPP_END_NAMESPACE_STD
2340
2341#endif // _LIBCPP_MAP
2342