xref: /freebsd/contrib/llvm-project/libcxx/include/map (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 <__config>
532#include <__debug>
533#include <__functional/is_transparent.h>
534#include <__iterator/iterator_traits.h>
535#include <__node_handle>
536#include <__tree>
537#include <__utility/forward.h>
538#include <compare>
539#include <functional>
540#include <initializer_list>
541#include <iterator> // __libcpp_erase_if_container
542#include <memory>
543#include <type_traits>
544#include <utility>
545#include <version>
546
547#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
548#pragma GCC system_header
549#endif
550
551_LIBCPP_BEGIN_NAMESPACE_STD
552
553template <class _Key, class _CP, class _Compare,
554          bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
555class __map_value_compare
556    : private _Compare
557{
558public:
559    _LIBCPP_INLINE_VISIBILITY
560    __map_value_compare()
561        _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
562        : _Compare() {}
563    _LIBCPP_INLINE_VISIBILITY
564    __map_value_compare(_Compare c)
565        _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
566        : _Compare(c) {}
567    _LIBCPP_INLINE_VISIBILITY
568    const _Compare& key_comp() const _NOEXCEPT {return *this;}
569    _LIBCPP_INLINE_VISIBILITY
570    bool operator()(const _CP& __x, const _CP& __y) const
571        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y.__get_value().first);}
572    _LIBCPP_INLINE_VISIBILITY
573    bool operator()(const _CP& __x, const _Key& __y) const
574        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
575    _LIBCPP_INLINE_VISIBILITY
576    bool operator()(const _Key& __x, const _CP& __y) const
577        {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
578    void swap(__map_value_compare& __y)
579        _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
580    {
581      using _VSTD::swap;
582      swap(static_cast<_Compare&>(*this), static_cast<_Compare&>(__y));
583    }
584
585#if _LIBCPP_STD_VER > 11
586    template <typename _K2>
587    _LIBCPP_INLINE_VISIBILITY
588    bool operator()(const _K2& __x, const _CP& __y) const
589        {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);}
590
591    template <typename _K2>
592    _LIBCPP_INLINE_VISIBILITY
593    bool operator()(const _CP& __x, const _K2& __y) const
594        {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);}
595#endif
596};
597
598template <class _Key, class _CP, class _Compare>
599class __map_value_compare<_Key, _CP, _Compare, false>
600{
601    _Compare comp;
602
603public:
604    _LIBCPP_INLINE_VISIBILITY
605    __map_value_compare()
606        _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
607        : comp() {}
608    _LIBCPP_INLINE_VISIBILITY
609    __map_value_compare(_Compare c)
610        _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
611        : comp(c) {}
612    _LIBCPP_INLINE_VISIBILITY
613    const _Compare& key_comp() const _NOEXCEPT {return comp;}
614
615    _LIBCPP_INLINE_VISIBILITY
616    bool operator()(const _CP& __x, const _CP& __y) const
617        {return comp(__x.__get_value().first, __y.__get_value().first);}
618    _LIBCPP_INLINE_VISIBILITY
619    bool operator()(const _CP& __x, const _Key& __y) const
620        {return comp(__x.__get_value().first, __y);}
621    _LIBCPP_INLINE_VISIBILITY
622    bool operator()(const _Key& __x, const _CP& __y) const
623        {return comp(__x, __y.__get_value().first);}
624    void swap(__map_value_compare& __y)
625        _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
626    {
627        using _VSTD::swap;
628        swap(comp, __y.comp);
629    }
630
631#if _LIBCPP_STD_VER > 11
632    template <typename _K2>
633    _LIBCPP_INLINE_VISIBILITY
634    bool operator()(const _K2& __x, const _CP& __y) const
635        {return comp(__x, __y.__get_value().first);}
636
637    template <typename _K2>
638    _LIBCPP_INLINE_VISIBILITY
639    bool operator()(const _CP& __x, const _K2& __y) const
640        {return comp(__x.__get_value().first, __y);}
641#endif
642};
643
644template <class _Key, class _CP, class _Compare, bool __b>
645inline _LIBCPP_INLINE_VISIBILITY
646void
647swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x,
648     __map_value_compare<_Key, _CP, _Compare, __b>& __y)
649    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
650{
651    __x.swap(__y);
652}
653
654template <class _Allocator>
655class __map_node_destructor
656{
657    typedef _Allocator                          allocator_type;
658    typedef allocator_traits<allocator_type>    __alloc_traits;
659
660public:
661    typedef typename __alloc_traits::pointer    pointer;
662
663private:
664    allocator_type& __na_;
665
666    __map_node_destructor& operator=(const __map_node_destructor&);
667
668public:
669    bool __first_constructed;
670    bool __second_constructed;
671
672    _LIBCPP_INLINE_VISIBILITY
673    explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
674        : __na_(__na),
675          __first_constructed(false),
676          __second_constructed(false)
677        {}
678
679#ifndef _LIBCPP_CXX03_LANG
680    _LIBCPP_INLINE_VISIBILITY
681    __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
682        : __na_(__x.__na_),
683          __first_constructed(__x.__value_constructed),
684          __second_constructed(__x.__value_constructed)
685        {
686            __x.__value_constructed = false;
687        }
688#endif // _LIBCPP_CXX03_LANG
689
690    _LIBCPP_INLINE_VISIBILITY
691    void operator()(pointer __p) _NOEXCEPT
692    {
693        if (__second_constructed)
694            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().second));
695        if (__first_constructed)
696            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__get_value().first));
697        if (__p)
698            __alloc_traits::deallocate(__na_, __p, 1);
699    }
700};
701
702template <class _Key, class _Tp, class _Compare, class _Allocator>
703    class map;
704template <class _Key, class _Tp, class _Compare, class _Allocator>
705    class multimap;
706template <class _TreeIterator> class __map_const_iterator;
707
708#ifndef _LIBCPP_CXX03_LANG
709
710template <class _Key, class _Tp>
711struct _LIBCPP_STANDALONE_DEBUG __value_type
712{
713    typedef _Key                                     key_type;
714    typedef _Tp                                      mapped_type;
715    typedef pair<const key_type, mapped_type>        value_type;
716    typedef pair<key_type&, mapped_type&>            __nc_ref_pair_type;
717    typedef pair<key_type&&, mapped_type&&>          __nc_rref_pair_type;
718
719private:
720    value_type __cc;
721
722public:
723    _LIBCPP_INLINE_VISIBILITY
724    value_type& __get_value()
725    {
726#if _LIBCPP_STD_VER > 14
727        return *_VSTD::launder(_VSTD::addressof(__cc));
728#else
729        return __cc;
730#endif
731    }
732
733    _LIBCPP_INLINE_VISIBILITY
734    const value_type& __get_value() const
735    {
736#if _LIBCPP_STD_VER > 14
737        return *_VSTD::launder(_VSTD::addressof(__cc));
738#else
739        return __cc;
740#endif
741    }
742
743    _LIBCPP_INLINE_VISIBILITY
744    __nc_ref_pair_type __ref()
745    {
746        value_type& __v = __get_value();
747        return __nc_ref_pair_type(const_cast<key_type&>(__v.first), __v.second);
748    }
749
750    _LIBCPP_INLINE_VISIBILITY
751    __nc_rref_pair_type __move()
752    {
753        value_type& __v = __get_value();
754        return __nc_rref_pair_type(
755            _VSTD::move(const_cast<key_type&>(__v.first)),
756            _VSTD::move(__v.second));
757    }
758
759    _LIBCPP_INLINE_VISIBILITY
760    __value_type& operator=(const __value_type& __v)
761    {
762        __ref() = __v.__get_value();
763        return *this;
764    }
765
766    _LIBCPP_INLINE_VISIBILITY
767    __value_type& operator=(__value_type&& __v)
768    {
769        __ref() = __v.__move();
770        return *this;
771    }
772
773    template <class _ValueTp,
774              class = typename enable_if<
775                    __is_same_uncvref<_ValueTp, value_type>::value
776                 >::type
777             >
778    _LIBCPP_INLINE_VISIBILITY
779    __value_type& operator=(_ValueTp&& __v)
780    {
781        __ref() = _VSTD::forward<_ValueTp>(__v);
782        return *this;
783    }
784
785private:
786    __value_type() = delete;
787    ~__value_type() = delete;
788    __value_type(const __value_type&) = delete;
789    __value_type(__value_type&&) = delete;
790};
791
792#else
793
794template <class _Key, class _Tp>
795struct __value_type
796{
797    typedef _Key                                     key_type;
798    typedef _Tp                                      mapped_type;
799    typedef pair<const key_type, mapped_type>        value_type;
800
801private:
802    value_type __cc;
803
804public:
805    _LIBCPP_INLINE_VISIBILITY
806    value_type& __get_value() { return __cc; }
807    _LIBCPP_INLINE_VISIBILITY
808    const value_type& __get_value() const { return __cc; }
809
810private:
811   __value_type();
812   __value_type(__value_type const&);
813   __value_type& operator=(__value_type const&);
814   ~__value_type();
815};
816
817#endif // _LIBCPP_CXX03_LANG
818
819template <class _Tp>
820struct __extract_key_value_types;
821
822template <class _Key, class _Tp>
823struct __extract_key_value_types<__value_type<_Key, _Tp> >
824{
825  typedef _Key const __key_type;
826  typedef _Tp        __mapped_type;
827};
828
829template <class _TreeIterator>
830class _LIBCPP_TEMPLATE_VIS __map_iterator
831{
832    typedef typename _TreeIterator::_NodeTypes                   _NodeTypes;
833    typedef typename _TreeIterator::__pointer_traits             __pointer_traits;
834
835    _TreeIterator __i_;
836
837public:
838    typedef bidirectional_iterator_tag                           iterator_category;
839    typedef typename _NodeTypes::__map_value_type                value_type;
840    typedef typename _TreeIterator::difference_type              difference_type;
841    typedef value_type&                                          reference;
842    typedef typename _NodeTypes::__map_value_type_pointer        pointer;
843
844    _LIBCPP_INLINE_VISIBILITY
845    __map_iterator() _NOEXCEPT {}
846
847    _LIBCPP_INLINE_VISIBILITY
848    __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
849
850    _LIBCPP_INLINE_VISIBILITY
851    reference operator*() const {return __i_->__get_value();}
852    _LIBCPP_INLINE_VISIBILITY
853    pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
854
855    _LIBCPP_INLINE_VISIBILITY
856    __map_iterator& operator++() {++__i_; return *this;}
857    _LIBCPP_INLINE_VISIBILITY
858    __map_iterator operator++(int)
859    {
860        __map_iterator __t(*this);
861        ++(*this);
862        return __t;
863    }
864
865    _LIBCPP_INLINE_VISIBILITY
866    __map_iterator& operator--() {--__i_; return *this;}
867    _LIBCPP_INLINE_VISIBILITY
868    __map_iterator operator--(int)
869    {
870        __map_iterator __t(*this);
871        --(*this);
872        return __t;
873    }
874
875    friend _LIBCPP_INLINE_VISIBILITY
876    bool operator==(const __map_iterator& __x, const __map_iterator& __y)
877        {return __x.__i_ == __y.__i_;}
878    friend
879    _LIBCPP_INLINE_VISIBILITY
880    bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
881        {return __x.__i_ != __y.__i_;}
882
883    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
884    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
885    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
886};
887
888template <class _TreeIterator>
889class _LIBCPP_TEMPLATE_VIS __map_const_iterator
890{
891    typedef typename _TreeIterator::_NodeTypes                   _NodeTypes;
892    typedef typename _TreeIterator::__pointer_traits             __pointer_traits;
893
894    _TreeIterator __i_;
895
896public:
897    typedef bidirectional_iterator_tag                           iterator_category;
898    typedef typename _NodeTypes::__map_value_type                value_type;
899    typedef typename _TreeIterator::difference_type              difference_type;
900    typedef const value_type&                                    reference;
901    typedef typename _NodeTypes::__const_map_value_type_pointer  pointer;
902
903    _LIBCPP_INLINE_VISIBILITY
904    __map_const_iterator() _NOEXCEPT {}
905
906    _LIBCPP_INLINE_VISIBILITY
907    __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
908    _LIBCPP_INLINE_VISIBILITY
909    __map_const_iterator(__map_iterator<
910        typename _TreeIterator::__non_const_iterator> __i) _NOEXCEPT
911        : __i_(__i.__i_) {}
912
913    _LIBCPP_INLINE_VISIBILITY
914    reference operator*() const {return __i_->__get_value();}
915    _LIBCPP_INLINE_VISIBILITY
916    pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__get_value());}
917
918    _LIBCPP_INLINE_VISIBILITY
919    __map_const_iterator& operator++() {++__i_; return *this;}
920    _LIBCPP_INLINE_VISIBILITY
921    __map_const_iterator operator++(int)
922    {
923        __map_const_iterator __t(*this);
924        ++(*this);
925        return __t;
926    }
927
928    _LIBCPP_INLINE_VISIBILITY
929    __map_const_iterator& operator--() {--__i_; return *this;}
930    _LIBCPP_INLINE_VISIBILITY
931    __map_const_iterator operator--(int)
932    {
933        __map_const_iterator __t(*this);
934        --(*this);
935        return __t;
936    }
937
938    friend _LIBCPP_INLINE_VISIBILITY
939    bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
940        {return __x.__i_ == __y.__i_;}
941    friend _LIBCPP_INLINE_VISIBILITY
942    bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
943        {return __x.__i_ != __y.__i_;}
944
945    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
946    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
947    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
948};
949
950template <class _Key, class _Tp, class _Compare = less<_Key>,
951          class _Allocator = allocator<pair<const _Key, _Tp> > >
952class _LIBCPP_TEMPLATE_VIS map
953{
954public:
955    // types:
956    typedef _Key                                     key_type;
957    typedef _Tp                                      mapped_type;
958    typedef pair<const key_type, mapped_type>        value_type;
959    typedef __identity_t<_Compare>                   key_compare;
960    typedef __identity_t<_Allocator>                 allocator_type;
961    typedef value_type&                              reference;
962    typedef const value_type&                        const_reference;
963
964    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
965                  "Allocator::value_type must be same type as value_type");
966
967_LIBCPP_SUPPRESS_DEPRECATED_PUSH
968    class _LIBCPP_TEMPLATE_VIS value_compare
969#if defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
970        : public binary_function<value_type, value_type, bool>
971#endif
972    {
973_LIBCPP_SUPPRESS_DEPRECATED_POP
974        friend class map;
975    protected:
976        key_compare comp;
977
978        _LIBCPP_INLINE_VISIBILITY value_compare(key_compare c) : comp(c) {}
979    public:
980#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
981        _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
982        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type first_argument_type;
983        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type second_argument_type;
984#endif
985        _LIBCPP_INLINE_VISIBILITY
986        bool operator()(const value_type& __x, const value_type& __y) const
987            {return comp(__x.first, __y.first);}
988    };
989
990private:
991
992    typedef _VSTD::__value_type<key_type, mapped_type>             __value_type;
993    typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
994    typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
995                                                 __value_type>::type __allocator_type;
996    typedef __tree<__value_type, __vc, __allocator_type>   __base;
997    typedef typename __base::__node_traits                 __node_traits;
998    typedef allocator_traits<allocator_type>               __alloc_traits;
999
1000    __base __tree_;
1001
1002public:
1003    typedef typename __alloc_traits::pointer               pointer;
1004    typedef typename __alloc_traits::const_pointer         const_pointer;
1005    typedef typename __alloc_traits::size_type             size_type;
1006    typedef typename __alloc_traits::difference_type       difference_type;
1007    typedef __map_iterator<typename __base::iterator>             iterator;
1008    typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1009    typedef _VSTD::reverse_iterator<iterator>               reverse_iterator;
1010    typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
1011
1012#if _LIBCPP_STD_VER > 14
1013    typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
1014    typedef __insert_return_type<iterator, node_type> insert_return_type;
1015#endif
1016
1017    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1018        friend class _LIBCPP_TEMPLATE_VIS map;
1019    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1020        friend class _LIBCPP_TEMPLATE_VIS multimap;
1021
1022    _LIBCPP_INLINE_VISIBILITY
1023    map()
1024        _NOEXCEPT_(
1025            is_nothrow_default_constructible<allocator_type>::value &&
1026            is_nothrow_default_constructible<key_compare>::value &&
1027            is_nothrow_copy_constructible<key_compare>::value)
1028        : __tree_(__vc(key_compare())) {}
1029
1030    _LIBCPP_INLINE_VISIBILITY
1031    explicit map(const key_compare& __comp)
1032        _NOEXCEPT_(
1033            is_nothrow_default_constructible<allocator_type>::value &&
1034            is_nothrow_copy_constructible<key_compare>::value)
1035        : __tree_(__vc(__comp)) {}
1036
1037    _LIBCPP_INLINE_VISIBILITY
1038    explicit map(const key_compare& __comp, const allocator_type& __a)
1039        : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
1040
1041    template <class _InputIterator>
1042    _LIBCPP_INLINE_VISIBILITY
1043        map(_InputIterator __f, _InputIterator __l,
1044            const key_compare& __comp = key_compare())
1045        : __tree_(__vc(__comp))
1046        {
1047            insert(__f, __l);
1048        }
1049
1050    template <class _InputIterator>
1051    _LIBCPP_INLINE_VISIBILITY
1052        map(_InputIterator __f, _InputIterator __l,
1053            const key_compare& __comp, const allocator_type& __a)
1054        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1055        {
1056            insert(__f, __l);
1057        }
1058
1059#if _LIBCPP_STD_VER > 11
1060    template <class _InputIterator>
1061    _LIBCPP_INLINE_VISIBILITY
1062    map(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
1063        : map(__f, __l, key_compare(), __a) {}
1064#endif
1065
1066    _LIBCPP_INLINE_VISIBILITY
1067    map(const map& __m)
1068        : __tree_(__m.__tree_)
1069        {
1070            insert(__m.begin(), __m.end());
1071        }
1072
1073    _LIBCPP_INLINE_VISIBILITY
1074    map& operator=(const map& __m)
1075        {
1076#ifndef _LIBCPP_CXX03_LANG
1077            __tree_ = __m.__tree_;
1078#else
1079            if (this != _VSTD::addressof(__m)) {
1080                __tree_.clear();
1081                __tree_.value_comp() = __m.__tree_.value_comp();
1082                __tree_.__copy_assign_alloc(__m.__tree_);
1083                insert(__m.begin(), __m.end());
1084            }
1085#endif
1086            return *this;
1087        }
1088
1089#ifndef _LIBCPP_CXX03_LANG
1090
1091    _LIBCPP_INLINE_VISIBILITY
1092    map(map&& __m)
1093        _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1094        : __tree_(_VSTD::move(__m.__tree_))
1095        {
1096        }
1097
1098    map(map&& __m, const allocator_type& __a);
1099
1100    _LIBCPP_INLINE_VISIBILITY
1101    map& operator=(map&& __m)
1102        _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
1103        {
1104            __tree_ = _VSTD::move(__m.__tree_);
1105            return *this;
1106        }
1107
1108    _LIBCPP_INLINE_VISIBILITY
1109    map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1110        : __tree_(__vc(__comp))
1111        {
1112            insert(__il.begin(), __il.end());
1113        }
1114
1115    _LIBCPP_INLINE_VISIBILITY
1116    map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1117        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1118        {
1119            insert(__il.begin(), __il.end());
1120        }
1121
1122#if _LIBCPP_STD_VER > 11
1123    _LIBCPP_INLINE_VISIBILITY
1124    map(initializer_list<value_type> __il, const allocator_type& __a)
1125        : map(__il, key_compare(), __a) {}
1126#endif
1127
1128    _LIBCPP_INLINE_VISIBILITY
1129    map& operator=(initializer_list<value_type> __il)
1130        {
1131            __tree_.__assign_unique(__il.begin(), __il.end());
1132            return *this;
1133        }
1134
1135#endif // _LIBCPP_CXX03_LANG
1136
1137    _LIBCPP_INLINE_VISIBILITY
1138    explicit map(const allocator_type& __a)
1139        : __tree_(typename __base::allocator_type(__a))
1140        {
1141        }
1142
1143    _LIBCPP_INLINE_VISIBILITY
1144    map(const map& __m, const allocator_type& __a)
1145        : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
1146        {
1147            insert(__m.begin(), __m.end());
1148        }
1149
1150    _LIBCPP_INLINE_VISIBILITY
1151    ~map() {
1152        static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
1153    }
1154
1155    _LIBCPP_INLINE_VISIBILITY
1156          iterator begin() _NOEXCEPT {return __tree_.begin();}
1157    _LIBCPP_INLINE_VISIBILITY
1158    const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
1159    _LIBCPP_INLINE_VISIBILITY
1160          iterator end() _NOEXCEPT {return __tree_.end();}
1161    _LIBCPP_INLINE_VISIBILITY
1162    const_iterator end() const _NOEXCEPT {return __tree_.end();}
1163
1164    _LIBCPP_INLINE_VISIBILITY
1165          reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
1166    _LIBCPP_INLINE_VISIBILITY
1167    const_reverse_iterator rbegin() const _NOEXCEPT
1168        {return const_reverse_iterator(end());}
1169    _LIBCPP_INLINE_VISIBILITY
1170          reverse_iterator rend() _NOEXCEPT
1171            {return       reverse_iterator(begin());}
1172    _LIBCPP_INLINE_VISIBILITY
1173    const_reverse_iterator rend() const _NOEXCEPT
1174        {return const_reverse_iterator(begin());}
1175
1176    _LIBCPP_INLINE_VISIBILITY
1177    const_iterator cbegin() const _NOEXCEPT {return begin();}
1178    _LIBCPP_INLINE_VISIBILITY
1179    const_iterator cend() const _NOEXCEPT {return end();}
1180    _LIBCPP_INLINE_VISIBILITY
1181    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
1182    _LIBCPP_INLINE_VISIBILITY
1183    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
1184
1185    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1186    bool      empty() const _NOEXCEPT {return __tree_.size() == 0;}
1187    _LIBCPP_INLINE_VISIBILITY
1188    size_type size() const _NOEXCEPT {return __tree_.size();}
1189    _LIBCPP_INLINE_VISIBILITY
1190    size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
1191
1192    mapped_type& operator[](const key_type& __k);
1193#ifndef _LIBCPP_CXX03_LANG
1194    mapped_type& operator[](key_type&& __k);
1195#endif
1196
1197          mapped_type& at(const key_type& __k);
1198    const mapped_type& at(const key_type& __k) const;
1199
1200    _LIBCPP_INLINE_VISIBILITY
1201    allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
1202    _LIBCPP_INLINE_VISIBILITY
1203    key_compare    key_comp()      const {return __tree_.value_comp().key_comp();}
1204    _LIBCPP_INLINE_VISIBILITY
1205    value_compare  value_comp()    const {return value_compare(__tree_.value_comp().key_comp());}
1206
1207#ifndef _LIBCPP_CXX03_LANG
1208    template <class ..._Args>
1209    _LIBCPP_INLINE_VISIBILITY
1210    pair<iterator, bool> emplace(_Args&& ...__args) {
1211        return __tree_.__emplace_unique(_VSTD::forward<_Args>(__args)...);
1212    }
1213
1214    template <class ..._Args>
1215    _LIBCPP_INLINE_VISIBILITY
1216    iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
1217        return __tree_.__emplace_hint_unique(__p.__i_, _VSTD::forward<_Args>(__args)...);
1218    }
1219
1220    template <class _Pp,
1221              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
1222        _LIBCPP_INLINE_VISIBILITY
1223        pair<iterator, bool> insert(_Pp&& __p)
1224            {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}
1225
1226    template <class _Pp,
1227              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
1228        _LIBCPP_INLINE_VISIBILITY
1229        iterator insert(const_iterator __pos, _Pp&& __p)
1230            {return __tree_.__insert_unique(__pos.__i_, _VSTD::forward<_Pp>(__p));}
1231
1232#endif // _LIBCPP_CXX03_LANG
1233
1234    _LIBCPP_INLINE_VISIBILITY
1235    pair<iterator, bool>
1236        insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
1237
1238    _LIBCPP_INLINE_VISIBILITY
1239    iterator
1240        insert(const_iterator __p, const value_type& __v)
1241            {return __tree_.__insert_unique(__p.__i_, __v);}
1242
1243#ifndef _LIBCPP_CXX03_LANG
1244    _LIBCPP_INLINE_VISIBILITY
1245    pair<iterator, bool>
1246    insert(value_type&& __v) {return __tree_.__insert_unique(_VSTD::move(__v));}
1247
1248    _LIBCPP_INLINE_VISIBILITY
1249    iterator insert(const_iterator __p,  value_type&& __v)
1250    {return __tree_.__insert_unique(__p.__i_, _VSTD::move(__v));}
1251
1252    _LIBCPP_INLINE_VISIBILITY
1253    void insert(initializer_list<value_type> __il)
1254        {insert(__il.begin(), __il.end());}
1255#endif
1256
1257    template <class _InputIterator>
1258        _LIBCPP_INLINE_VISIBILITY
1259        void insert(_InputIterator __f, _InputIterator __l)
1260        {
1261            for (const_iterator __e = cend(); __f != __l; ++__f)
1262                insert(__e.__i_, *__f);
1263        }
1264
1265#if _LIBCPP_STD_VER > 14
1266
1267    template <class... _Args>
1268        _LIBCPP_INLINE_VISIBILITY
1269        pair<iterator, bool> try_emplace(const key_type& __k, _Args&&... __args)
1270    {
1271        return __tree_.__emplace_unique_key_args(__k,
1272            _VSTD::piecewise_construct,
1273            _VSTD::forward_as_tuple(__k),
1274            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
1275    }
1276
1277    template <class... _Args>
1278        _LIBCPP_INLINE_VISIBILITY
1279        pair<iterator, bool> try_emplace(key_type&& __k, _Args&&... __args)
1280    {
1281        return __tree_.__emplace_unique_key_args(__k,
1282            _VSTD::piecewise_construct,
1283            _VSTD::forward_as_tuple(_VSTD::move(__k)),
1284            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
1285    }
1286
1287    template <class... _Args>
1288        _LIBCPP_INLINE_VISIBILITY
1289        iterator try_emplace(const_iterator __h, const key_type& __k, _Args&&... __args)
1290    {
1291        return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
1292            _VSTD::piecewise_construct,
1293            _VSTD::forward_as_tuple(__k),
1294            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
1295    }
1296
1297    template <class... _Args>
1298        _LIBCPP_INLINE_VISIBILITY
1299        iterator try_emplace(const_iterator __h, key_type&& __k, _Args&&... __args)
1300    {
1301        return __tree_.__emplace_hint_unique_key_args(__h.__i_, __k,
1302            _VSTD::piecewise_construct,
1303            _VSTD::forward_as_tuple(_VSTD::move(__k)),
1304            _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)).first;
1305    }
1306
1307    template <class _Vp>
1308        _LIBCPP_INLINE_VISIBILITY
1309        pair<iterator, bool> insert_or_assign(const key_type& __k, _Vp&& __v)
1310    {
1311        iterator __p = lower_bound(__k);
1312        if ( __p != end() && !key_comp()(__k, __p->first))
1313        {
1314            __p->second = _VSTD::forward<_Vp>(__v);
1315            return _VSTD::make_pair(__p, false);
1316        }
1317        return _VSTD::make_pair(emplace_hint(__p, __k, _VSTD::forward<_Vp>(__v)), true);
1318    }
1319
1320    template <class _Vp>
1321        _LIBCPP_INLINE_VISIBILITY
1322        pair<iterator, bool> insert_or_assign(key_type&& __k, _Vp&& __v)
1323    {
1324        iterator __p = lower_bound(__k);
1325        if ( __p != end() && !key_comp()(__k, __p->first))
1326        {
1327            __p->second = _VSTD::forward<_Vp>(__v);
1328            return _VSTD::make_pair(__p, false);
1329        }
1330        return _VSTD::make_pair(emplace_hint(__p, _VSTD::move(__k), _VSTD::forward<_Vp>(__v)), true);
1331    }
1332
1333    template <class _Vp>
1334    _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
1335                                                        const key_type& __k,
1336                                                        _Vp&& __v) {
1337      auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
1338          __h.__i_, __k, __k, _VSTD::forward<_Vp>(__v));
1339
1340      if (!__inserted)
1341        __r->__get_value().second = _VSTD::forward<_Vp>(__v);
1342
1343      return __r;
1344    }
1345
1346    template <class _Vp>
1347    _LIBCPP_INLINE_VISIBILITY iterator insert_or_assign(const_iterator __h,
1348                                                        key_type&& __k,
1349                                                        _Vp&& __v) {
1350      auto [__r, __inserted] = __tree_.__emplace_hint_unique_key_args(
1351          __h.__i_, __k, _VSTD::move(__k), _VSTD::forward<_Vp>(__v));
1352
1353      if (!__inserted)
1354        __r->__get_value().second = _VSTD::forward<_Vp>(__v);
1355
1356      return __r;
1357    }
1358
1359#endif // _LIBCPP_STD_VER > 14
1360
1361    _LIBCPP_INLINE_VISIBILITY
1362    iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
1363    _LIBCPP_INLINE_VISIBILITY
1364    iterator erase(iterator __p)       {return __tree_.erase(__p.__i_);}
1365    _LIBCPP_INLINE_VISIBILITY
1366    size_type erase(const key_type& __k)
1367        {return __tree_.__erase_unique(__k);}
1368    _LIBCPP_INLINE_VISIBILITY
1369    iterator  erase(const_iterator __f, const_iterator __l)
1370        {return __tree_.erase(__f.__i_, __l.__i_);}
1371    _LIBCPP_INLINE_VISIBILITY
1372    void clear() _NOEXCEPT {__tree_.clear();}
1373
1374#if _LIBCPP_STD_VER > 14
1375    _LIBCPP_INLINE_VISIBILITY
1376    insert_return_type insert(node_type&& __nh)
1377    {
1378        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
1379            "node_type with incompatible allocator passed to map::insert()");
1380        return __tree_.template __node_handle_insert_unique<
1381            node_type, insert_return_type>(_VSTD::move(__nh));
1382    }
1383    _LIBCPP_INLINE_VISIBILITY
1384    iterator insert(const_iterator __hint, node_type&& __nh)
1385    {
1386        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
1387            "node_type with incompatible allocator passed to map::insert()");
1388        return __tree_.template __node_handle_insert_unique<node_type>(
1389            __hint.__i_, _VSTD::move(__nh));
1390    }
1391    _LIBCPP_INLINE_VISIBILITY
1392    node_type extract(key_type const& __key)
1393    {
1394        return __tree_.template __node_handle_extract<node_type>(__key);
1395    }
1396    _LIBCPP_INLINE_VISIBILITY
1397    node_type extract(const_iterator __it)
1398    {
1399        return __tree_.template __node_handle_extract<node_type>(__it.__i_);
1400    }
1401    template <class _Compare2>
1402    _LIBCPP_INLINE_VISIBILITY
1403    void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
1404    {
1405        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1406                       "merging container with incompatible allocator");
1407        __tree_.__node_handle_merge_unique(__source.__tree_);
1408    }
1409    template <class _Compare2>
1410    _LIBCPP_INLINE_VISIBILITY
1411    void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
1412    {
1413        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1414                       "merging container with incompatible allocator");
1415        __tree_.__node_handle_merge_unique(__source.__tree_);
1416    }
1417    template <class _Compare2>
1418    _LIBCPP_INLINE_VISIBILITY
1419    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
1420    {
1421        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1422                       "merging container with incompatible allocator");
1423        __tree_.__node_handle_merge_unique(__source.__tree_);
1424    }
1425    template <class _Compare2>
1426    _LIBCPP_INLINE_VISIBILITY
1427    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
1428    {
1429        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
1430                       "merging container with incompatible allocator");
1431        __tree_.__node_handle_merge_unique(__source.__tree_);
1432    }
1433#endif
1434
1435    _LIBCPP_INLINE_VISIBILITY
1436    void swap(map& __m)
1437        _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
1438        {__tree_.swap(__m.__tree_);}
1439
1440    _LIBCPP_INLINE_VISIBILITY
1441    iterator find(const key_type& __k)             {return __tree_.find(__k);}
1442    _LIBCPP_INLINE_VISIBILITY
1443    const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
1444#if _LIBCPP_STD_VER > 11
1445    template <typename _K2>
1446    _LIBCPP_INLINE_VISIBILITY
1447    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1448    find(const _K2& __k)                           {return __tree_.find(__k);}
1449    template <typename _K2>
1450    _LIBCPP_INLINE_VISIBILITY
1451    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1452    find(const _K2& __k) const                     {return __tree_.find(__k);}
1453#endif
1454
1455    _LIBCPP_INLINE_VISIBILITY
1456    size_type      count(const key_type& __k) const
1457        {return __tree_.__count_unique(__k);}
1458#if _LIBCPP_STD_VER > 11
1459    template <typename _K2>
1460    _LIBCPP_INLINE_VISIBILITY
1461    typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
1462    count(const _K2& __k) const {return __tree_.__count_multi(__k);}
1463#endif
1464
1465#if _LIBCPP_STD_VER > 17
1466    _LIBCPP_INLINE_VISIBILITY
1467    bool contains(const key_type& __k) const {return find(__k) != end();}
1468    template <typename _K2>
1469    _LIBCPP_INLINE_VISIBILITY
1470    typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
1471    contains(const _K2& __k) const { return find(__k) != end(); }
1472#endif // _LIBCPP_STD_VER > 17
1473
1474    _LIBCPP_INLINE_VISIBILITY
1475    iterator lower_bound(const key_type& __k)
1476        {return __tree_.lower_bound(__k);}
1477    _LIBCPP_INLINE_VISIBILITY
1478    const_iterator lower_bound(const key_type& __k) const
1479        {return __tree_.lower_bound(__k);}
1480#if _LIBCPP_STD_VER > 11
1481    template <typename _K2>
1482    _LIBCPP_INLINE_VISIBILITY
1483    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1484    lower_bound(const _K2& __k)       {return __tree_.lower_bound(__k);}
1485
1486    template <typename _K2>
1487    _LIBCPP_INLINE_VISIBILITY
1488    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1489    lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
1490#endif
1491
1492    _LIBCPP_INLINE_VISIBILITY
1493    iterator upper_bound(const key_type& __k)
1494        {return __tree_.upper_bound(__k);}
1495    _LIBCPP_INLINE_VISIBILITY
1496    const_iterator upper_bound(const key_type& __k) const
1497        {return __tree_.upper_bound(__k);}
1498#if _LIBCPP_STD_VER > 11
1499    template <typename _K2>
1500    _LIBCPP_INLINE_VISIBILITY
1501    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1502    upper_bound(const _K2& __k)       {return __tree_.upper_bound(__k);}
1503    template <typename _K2>
1504    _LIBCPP_INLINE_VISIBILITY
1505    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1506    upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
1507#endif
1508
1509    _LIBCPP_INLINE_VISIBILITY
1510    pair<iterator,iterator> equal_range(const key_type& __k)
1511        {return __tree_.__equal_range_unique(__k);}
1512    _LIBCPP_INLINE_VISIBILITY
1513    pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1514        {return __tree_.__equal_range_unique(__k);}
1515#if _LIBCPP_STD_VER > 11
1516    template <typename _K2>
1517    _LIBCPP_INLINE_VISIBILITY
1518    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
1519    equal_range(const _K2& __k)       {return __tree_.__equal_range_multi(__k);}
1520    template <typename _K2>
1521    _LIBCPP_INLINE_VISIBILITY
1522    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
1523    equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
1524#endif
1525
1526private:
1527    typedef typename __base::__node                    __node;
1528    typedef typename __base::__node_allocator          __node_allocator;
1529    typedef typename __base::__node_pointer            __node_pointer;
1530    typedef typename __base::__node_base_pointer       __node_base_pointer;
1531    typedef typename __base::__parent_pointer          __parent_pointer;
1532
1533    typedef __map_node_destructor<__node_allocator> _Dp;
1534    typedef unique_ptr<__node, _Dp> __node_holder;
1535
1536#ifdef _LIBCPP_CXX03_LANG
1537    __node_holder __construct_node_with_key(const key_type& __k);
1538#endif
1539};
1540
1541#if _LIBCPP_STD_VER >= 17
1542template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
1543         class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
1544         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
1545         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
1546         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1547map(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
1548  -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
1549
1550template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
1551         class _Allocator = allocator<pair<const _Key, _Tp>>,
1552         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
1553         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1554map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
1555  -> map<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
1556
1557template<class _InputIterator, class _Allocator,
1558         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
1559         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1560map(_InputIterator, _InputIterator, _Allocator)
1561  -> map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
1562         less<__iter_key_type<_InputIterator>>, _Allocator>;
1563
1564template<class _Key, class _Tp, class _Allocator,
1565         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
1566map(initializer_list<pair<_Key, _Tp>>, _Allocator)
1567  -> map<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
1568#endif
1569
1570#ifndef _LIBCPP_CXX03_LANG
1571template <class _Key, class _Tp, class _Compare, class _Allocator>
1572map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
1573    : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
1574{
1575    if (__a != __m.get_allocator())
1576    {
1577        const_iterator __e = cend();
1578        while (!__m.empty())
1579            __tree_.__insert_unique(__e.__i_,
1580                    __m.__tree_.remove(__m.begin().__i_)->__value_.__move());
1581    }
1582}
1583
1584template <class _Key, class _Tp, class _Compare, class _Allocator>
1585_Tp&
1586map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1587{
1588    return __tree_.__emplace_unique_key_args(__k,
1589        _VSTD::piecewise_construct,
1590        _VSTD::forward_as_tuple(__k),
1591        _VSTD::forward_as_tuple()).first->__get_value().second;
1592}
1593
1594template <class _Key, class _Tp, class _Compare, class _Allocator>
1595_Tp&
1596map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1597{
1598    return __tree_.__emplace_unique_key_args(__k,
1599        _VSTD::piecewise_construct,
1600        _VSTD::forward_as_tuple(_VSTD::move(__k)),
1601        _VSTD::forward_as_tuple()).first->__get_value().second;
1602}
1603
1604#else // _LIBCPP_CXX03_LANG
1605
1606template <class _Key, class _Tp, class _Compare, class _Allocator>
1607typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1608map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(const key_type& __k)
1609{
1610    __node_allocator& __na = __tree_.__node_alloc();
1611    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1612    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().first), __k);
1613    __h.get_deleter().__first_constructed = true;
1614    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__get_value().second));
1615    __h.get_deleter().__second_constructed = true;
1616    return __h;
1617}
1618
1619template <class _Key, class _Tp, class _Compare, class _Allocator>
1620_Tp&
1621map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1622{
1623    __parent_pointer __parent;
1624    __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
1625    __node_pointer __r = static_cast<__node_pointer>(__child);
1626    if (__child == nullptr)
1627    {
1628        __node_holder __h = __construct_node_with_key(__k);
1629        __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1630        __r = __h.release();
1631    }
1632    return __r->__value_.__get_value().second;
1633}
1634
1635#endif // _LIBCPP_CXX03_LANG
1636
1637template <class _Key, class _Tp, class _Compare, class _Allocator>
1638_Tp&
1639map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1640{
1641    __parent_pointer __parent;
1642    __node_base_pointer& __child = __tree_.__find_equal(__parent, __k);
1643    if (__child == nullptr)
1644        __throw_out_of_range("map::at:  key not found");
1645    return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
1646}
1647
1648template <class _Key, class _Tp, class _Compare, class _Allocator>
1649const _Tp&
1650map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1651{
1652    __parent_pointer __parent;
1653    __node_base_pointer __child = __tree_.__find_equal(__parent, __k);
1654    if (__child == nullptr)
1655        __throw_out_of_range("map::at:  key not found");
1656    return static_cast<__node_pointer>(__child)->__value_.__get_value().second;
1657}
1658
1659
1660template <class _Key, class _Tp, class _Compare, class _Allocator>
1661inline _LIBCPP_INLINE_VISIBILITY
1662bool
1663operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1664           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1665{
1666    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
1667}
1668
1669template <class _Key, class _Tp, class _Compare, class _Allocator>
1670inline _LIBCPP_INLINE_VISIBILITY
1671bool
1672operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1673           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1674{
1675    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1676}
1677
1678template <class _Key, class _Tp, class _Compare, class _Allocator>
1679inline _LIBCPP_INLINE_VISIBILITY
1680bool
1681operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1682           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1683{
1684    return !(__x == __y);
1685}
1686
1687template <class _Key, class _Tp, class _Compare, class _Allocator>
1688inline _LIBCPP_INLINE_VISIBILITY
1689bool
1690operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1691           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1692{
1693    return __y < __x;
1694}
1695
1696template <class _Key, class _Tp, class _Compare, class _Allocator>
1697inline _LIBCPP_INLINE_VISIBILITY
1698bool
1699operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1700           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1701{
1702    return !(__x < __y);
1703}
1704
1705template <class _Key, class _Tp, class _Compare, class _Allocator>
1706inline _LIBCPP_INLINE_VISIBILITY
1707bool
1708operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1709           const map<_Key, _Tp, _Compare, _Allocator>& __y)
1710{
1711    return !(__y < __x);
1712}
1713
1714template <class _Key, class _Tp, class _Compare, class _Allocator>
1715inline _LIBCPP_INLINE_VISIBILITY
1716void
1717swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1718     map<_Key, _Tp, _Compare, _Allocator>& __y)
1719    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
1720{
1721    __x.swap(__y);
1722}
1723
1724#if _LIBCPP_STD_VER > 17
1725template <class _Key, class _Tp, class _Compare, class _Allocator,
1726          class _Predicate>
1727inline _LIBCPP_INLINE_VISIBILITY
1728    typename map<_Key, _Tp, _Compare, _Allocator>::size_type
1729    erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
1730  return _VSTD::__libcpp_erase_if_container(__c, __pred);
1731}
1732#endif
1733
1734
1735template <class _Key, class _Tp, class _Compare = less<_Key>,
1736          class _Allocator = allocator<pair<const _Key, _Tp> > >
1737class _LIBCPP_TEMPLATE_VIS multimap
1738{
1739public:
1740    // types:
1741    typedef _Key                                     key_type;
1742    typedef _Tp                                      mapped_type;
1743    typedef pair<const key_type, mapped_type>        value_type;
1744    typedef __identity_t<_Compare>                   key_compare;
1745    typedef __identity_t<_Allocator>                 allocator_type;
1746    typedef value_type&                              reference;
1747    typedef const value_type&                        const_reference;
1748
1749    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1750                  "Allocator::value_type must be same type as value_type");
1751
1752_LIBCPP_SUPPRESS_DEPRECATED_PUSH
1753    class _LIBCPP_TEMPLATE_VIS value_compare
1754#if defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1755        : public binary_function<value_type, value_type, bool>
1756#endif
1757    {
1758_LIBCPP_SUPPRESS_DEPRECATED_POP
1759        friend class multimap;
1760    protected:
1761        key_compare comp;
1762
1763        _LIBCPP_INLINE_VISIBILITY
1764        value_compare(key_compare c) : comp(c) {}
1765    public:
1766#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1767        _LIBCPP_DEPRECATED_IN_CXX17 typedef bool result_type;
1768        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type first_argument_type;
1769        _LIBCPP_DEPRECATED_IN_CXX17 typedef value_type second_argument_type;
1770#endif
1771        _LIBCPP_INLINE_VISIBILITY
1772        bool operator()(const value_type& __x, const value_type& __y) const
1773            {return comp(__x.first, __y.first);}
1774    };
1775
1776private:
1777
1778    typedef _VSTD::__value_type<key_type, mapped_type>             __value_type;
1779    typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
1780    typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
1781                                                 __value_type>::type __allocator_type;
1782    typedef __tree<__value_type, __vc, __allocator_type>            __base;
1783    typedef typename __base::__node_traits                          __node_traits;
1784    typedef allocator_traits<allocator_type>                        __alloc_traits;
1785
1786    __base __tree_;
1787
1788public:
1789    typedef typename __alloc_traits::pointer               pointer;
1790    typedef typename __alloc_traits::const_pointer         const_pointer;
1791    typedef typename __alloc_traits::size_type             size_type;
1792    typedef typename __alloc_traits::difference_type       difference_type;
1793    typedef __map_iterator<typename __base::iterator>      iterator;
1794    typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1795    typedef _VSTD::reverse_iterator<iterator>               reverse_iterator;
1796    typedef _VSTD::reverse_iterator<const_iterator>         const_reverse_iterator;
1797
1798#if _LIBCPP_STD_VER > 14
1799    typedef __map_node_handle<typename __base::__node, allocator_type> node_type;
1800#endif
1801
1802    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1803        friend class _LIBCPP_TEMPLATE_VIS map;
1804    template <class _Key2, class _Value2, class _Comp2, class _Alloc2>
1805        friend class _LIBCPP_TEMPLATE_VIS multimap;
1806
1807    _LIBCPP_INLINE_VISIBILITY
1808    multimap()
1809        _NOEXCEPT_(
1810            is_nothrow_default_constructible<allocator_type>::value &&
1811            is_nothrow_default_constructible<key_compare>::value &&
1812            is_nothrow_copy_constructible<key_compare>::value)
1813        : __tree_(__vc(key_compare())) {}
1814
1815    _LIBCPP_INLINE_VISIBILITY
1816    explicit multimap(const key_compare& __comp)
1817        _NOEXCEPT_(
1818            is_nothrow_default_constructible<allocator_type>::value &&
1819            is_nothrow_copy_constructible<key_compare>::value)
1820        : __tree_(__vc(__comp)) {}
1821
1822    _LIBCPP_INLINE_VISIBILITY
1823    explicit multimap(const key_compare& __comp, const allocator_type& __a)
1824        : __tree_(__vc(__comp), typename __base::allocator_type(__a)) {}
1825
1826    template <class _InputIterator>
1827        _LIBCPP_INLINE_VISIBILITY
1828        multimap(_InputIterator __f, _InputIterator __l,
1829            const key_compare& __comp = key_compare())
1830        : __tree_(__vc(__comp))
1831        {
1832            insert(__f, __l);
1833        }
1834
1835    template <class _InputIterator>
1836        _LIBCPP_INLINE_VISIBILITY
1837        multimap(_InputIterator __f, _InputIterator __l,
1838            const key_compare& __comp, const allocator_type& __a)
1839        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1840        {
1841            insert(__f, __l);
1842        }
1843
1844#if _LIBCPP_STD_VER > 11
1845    template <class _InputIterator>
1846    _LIBCPP_INLINE_VISIBILITY
1847    multimap(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
1848        : multimap(__f, __l, key_compare(), __a) {}
1849#endif
1850
1851    _LIBCPP_INLINE_VISIBILITY
1852    multimap(const multimap& __m)
1853        : __tree_(__m.__tree_.value_comp(),
1854          __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1855        {
1856            insert(__m.begin(), __m.end());
1857        }
1858
1859    _LIBCPP_INLINE_VISIBILITY
1860    multimap& operator=(const multimap& __m)
1861        {
1862#ifndef _LIBCPP_CXX03_LANG
1863            __tree_ = __m.__tree_;
1864#else
1865            if (this != _VSTD::addressof(__m)) {
1866                __tree_.clear();
1867                __tree_.value_comp() = __m.__tree_.value_comp();
1868                __tree_.__copy_assign_alloc(__m.__tree_);
1869                insert(__m.begin(), __m.end());
1870            }
1871#endif
1872            return *this;
1873        }
1874
1875#ifndef _LIBCPP_CXX03_LANG
1876
1877    _LIBCPP_INLINE_VISIBILITY
1878    multimap(multimap&& __m)
1879        _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
1880        : __tree_(_VSTD::move(__m.__tree_))
1881        {
1882        }
1883
1884    multimap(multimap&& __m, const allocator_type& __a);
1885
1886    _LIBCPP_INLINE_VISIBILITY
1887    multimap& operator=(multimap&& __m)
1888        _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
1889        {
1890            __tree_ = _VSTD::move(__m.__tree_);
1891            return *this;
1892        }
1893
1894    _LIBCPP_INLINE_VISIBILITY
1895    multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1896        : __tree_(__vc(__comp))
1897        {
1898            insert(__il.begin(), __il.end());
1899        }
1900
1901    _LIBCPP_INLINE_VISIBILITY
1902    multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1903        : __tree_(__vc(__comp), typename __base::allocator_type(__a))
1904        {
1905            insert(__il.begin(), __il.end());
1906        }
1907
1908#if _LIBCPP_STD_VER > 11
1909    _LIBCPP_INLINE_VISIBILITY
1910    multimap(initializer_list<value_type> __il, const allocator_type& __a)
1911        : multimap(__il, key_compare(), __a) {}
1912#endif
1913
1914    _LIBCPP_INLINE_VISIBILITY
1915    multimap& operator=(initializer_list<value_type> __il)
1916        {
1917            __tree_.__assign_multi(__il.begin(), __il.end());
1918            return *this;
1919        }
1920
1921#endif // _LIBCPP_CXX03_LANG
1922
1923    _LIBCPP_INLINE_VISIBILITY
1924    explicit multimap(const allocator_type& __a)
1925        : __tree_(typename __base::allocator_type(__a))
1926        {
1927        }
1928
1929    _LIBCPP_INLINE_VISIBILITY
1930    multimap(const multimap& __m, const allocator_type& __a)
1931        : __tree_(__m.__tree_.value_comp(), typename __base::allocator_type(__a))
1932        {
1933            insert(__m.begin(), __m.end());
1934        }
1935
1936    _LIBCPP_INLINE_VISIBILITY
1937    ~multimap() {
1938        static_assert(sizeof(__diagnose_non_const_comparator<_Key, _Compare>()), "");
1939    }
1940
1941    _LIBCPP_INLINE_VISIBILITY
1942          iterator begin() _NOEXCEPT {return __tree_.begin();}
1943    _LIBCPP_INLINE_VISIBILITY
1944    const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
1945    _LIBCPP_INLINE_VISIBILITY
1946          iterator end() _NOEXCEPT {return __tree_.end();}
1947    _LIBCPP_INLINE_VISIBILITY
1948    const_iterator end() const _NOEXCEPT {return __tree_.end();}
1949
1950    _LIBCPP_INLINE_VISIBILITY
1951          reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
1952    _LIBCPP_INLINE_VISIBILITY
1953    const_reverse_iterator rbegin() const _NOEXCEPT
1954        {return const_reverse_iterator(end());}
1955    _LIBCPP_INLINE_VISIBILITY
1956          reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
1957    _LIBCPP_INLINE_VISIBILITY
1958    const_reverse_iterator rend() const _NOEXCEPT
1959        {return const_reverse_iterator(begin());}
1960
1961    _LIBCPP_INLINE_VISIBILITY
1962    const_iterator cbegin()  const _NOEXCEPT {return begin();}
1963    _LIBCPP_INLINE_VISIBILITY
1964    const_iterator cend() const _NOEXCEPT {return end();}
1965    _LIBCPP_INLINE_VISIBILITY
1966    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
1967    _LIBCPP_INLINE_VISIBILITY
1968    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
1969
1970    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
1971    bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
1972    _LIBCPP_INLINE_VISIBILITY
1973    size_type size() const _NOEXCEPT {return __tree_.size();}
1974    _LIBCPP_INLINE_VISIBILITY
1975    size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
1976
1977    _LIBCPP_INLINE_VISIBILITY
1978    allocator_type get_allocator() const _NOEXCEPT {return allocator_type(__tree_.__alloc());}
1979    _LIBCPP_INLINE_VISIBILITY
1980    key_compare    key_comp() const {return __tree_.value_comp().key_comp();}
1981    _LIBCPP_INLINE_VISIBILITY
1982    value_compare  value_comp() const
1983        {return value_compare(__tree_.value_comp().key_comp());}
1984
1985#ifndef _LIBCPP_CXX03_LANG
1986
1987    template <class ..._Args>
1988    _LIBCPP_INLINE_VISIBILITY
1989    iterator emplace(_Args&& ...__args) {
1990        return __tree_.__emplace_multi(_VSTD::forward<_Args>(__args)...);
1991    }
1992
1993    template <class ..._Args>
1994    _LIBCPP_INLINE_VISIBILITY
1995    iterator emplace_hint(const_iterator __p, _Args&& ...__args) {
1996        return __tree_.__emplace_hint_multi(__p.__i_, _VSTD::forward<_Args>(__args)...);
1997    }
1998
1999    template <class _Pp,
2000              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
2001        _LIBCPP_INLINE_VISIBILITY
2002        iterator insert(_Pp&& __p)
2003            {return __tree_.__insert_multi(_VSTD::forward<_Pp>(__p));}
2004
2005    template <class _Pp,
2006              class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
2007        _LIBCPP_INLINE_VISIBILITY
2008        iterator insert(const_iterator __pos, _Pp&& __p)
2009            {return __tree_.__insert_multi(__pos.__i_, _VSTD::forward<_Pp>(__p));}
2010
2011    _LIBCPP_INLINE_VISIBILITY
2012    iterator insert(value_type&& __v)
2013        {return __tree_.__insert_multi(_VSTD::move(__v));}
2014
2015    _LIBCPP_INLINE_VISIBILITY
2016    iterator insert(const_iterator __p, value_type&& __v)
2017        {return __tree_.__insert_multi(__p.__i_, _VSTD::move(__v));}
2018
2019
2020    _LIBCPP_INLINE_VISIBILITY
2021    void insert(initializer_list<value_type> __il)
2022        {insert(__il.begin(), __il.end());}
2023
2024#endif // _LIBCPP_CXX03_LANG
2025
2026    _LIBCPP_INLINE_VISIBILITY
2027    iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
2028
2029    _LIBCPP_INLINE_VISIBILITY
2030    iterator insert(const_iterator __p, const value_type& __v)
2031            {return __tree_.__insert_multi(__p.__i_, __v);}
2032
2033    template <class _InputIterator>
2034        _LIBCPP_INLINE_VISIBILITY
2035        void insert(_InputIterator __f, _InputIterator __l)
2036        {
2037            for (const_iterator __e = cend(); __f != __l; ++__f)
2038                __tree_.__insert_multi(__e.__i_, *__f);
2039        }
2040
2041    _LIBCPP_INLINE_VISIBILITY
2042    iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
2043    _LIBCPP_INLINE_VISIBILITY
2044    iterator erase(iterator __p)       {return __tree_.erase(__p.__i_);}
2045    _LIBCPP_INLINE_VISIBILITY
2046    size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
2047    _LIBCPP_INLINE_VISIBILITY
2048    iterator  erase(const_iterator __f, const_iterator __l)
2049        {return __tree_.erase(__f.__i_, __l.__i_);}
2050
2051#if _LIBCPP_STD_VER > 14
2052    _LIBCPP_INLINE_VISIBILITY
2053    iterator insert(node_type&& __nh)
2054    {
2055        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
2056            "node_type with incompatible allocator passed to multimap::insert()");
2057        return __tree_.template __node_handle_insert_multi<node_type>(
2058            _VSTD::move(__nh));
2059    }
2060    _LIBCPP_INLINE_VISIBILITY
2061    iterator insert(const_iterator __hint, node_type&& __nh)
2062    {
2063        _LIBCPP_ASSERT(__nh.empty() || __nh.get_allocator() == get_allocator(),
2064            "node_type with incompatible allocator passed to multimap::insert()");
2065        return __tree_.template __node_handle_insert_multi<node_type>(
2066            __hint.__i_, _VSTD::move(__nh));
2067    }
2068    _LIBCPP_INLINE_VISIBILITY
2069    node_type extract(key_type const& __key)
2070    {
2071        return __tree_.template __node_handle_extract<node_type>(__key);
2072    }
2073    _LIBCPP_INLINE_VISIBILITY
2074    node_type extract(const_iterator __it)
2075    {
2076        return __tree_.template __node_handle_extract<node_type>(
2077            __it.__i_);
2078    }
2079    template <class _Compare2>
2080    _LIBCPP_INLINE_VISIBILITY
2081    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>& __source)
2082    {
2083        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2084                       "merging container with incompatible allocator");
2085        return __tree_.__node_handle_merge_multi(__source.__tree_);
2086    }
2087    template <class _Compare2>
2088    _LIBCPP_INLINE_VISIBILITY
2089    void merge(multimap<key_type, mapped_type, _Compare2, allocator_type>&& __source)
2090    {
2091        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2092                       "merging container with incompatible allocator");
2093        return __tree_.__node_handle_merge_multi(__source.__tree_);
2094    }
2095    template <class _Compare2>
2096    _LIBCPP_INLINE_VISIBILITY
2097    void merge(map<key_type, mapped_type, _Compare2, allocator_type>& __source)
2098    {
2099        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2100                       "merging container with incompatible allocator");
2101        return __tree_.__node_handle_merge_multi(__source.__tree_);
2102    }
2103    template <class _Compare2>
2104    _LIBCPP_INLINE_VISIBILITY
2105    void merge(map<key_type, mapped_type, _Compare2, allocator_type>&& __source)
2106    {
2107        _LIBCPP_ASSERT(__source.get_allocator() == get_allocator(),
2108                       "merging container with incompatible allocator");
2109        return __tree_.__node_handle_merge_multi(__source.__tree_);
2110    }
2111#endif
2112
2113    _LIBCPP_INLINE_VISIBILITY
2114    void clear() _NOEXCEPT {__tree_.clear();}
2115
2116    _LIBCPP_INLINE_VISIBILITY
2117    void swap(multimap& __m)
2118        _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
2119        {__tree_.swap(__m.__tree_);}
2120
2121    _LIBCPP_INLINE_VISIBILITY
2122    iterator find(const key_type& __k)             {return __tree_.find(__k);}
2123    _LIBCPP_INLINE_VISIBILITY
2124    const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
2125#if _LIBCPP_STD_VER > 11
2126    template <typename _K2>
2127    _LIBCPP_INLINE_VISIBILITY
2128    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2129    find(const _K2& __k)                           {return __tree_.find(__k);}
2130    template <typename _K2>
2131    _LIBCPP_INLINE_VISIBILITY
2132    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2133    find(const _K2& __k) const                     {return __tree_.find(__k);}
2134#endif
2135
2136    _LIBCPP_INLINE_VISIBILITY
2137    size_type      count(const key_type& __k) const
2138        {return __tree_.__count_multi(__k);}
2139#if _LIBCPP_STD_VER > 11
2140    template <typename _K2>
2141    _LIBCPP_INLINE_VISIBILITY
2142    typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
2143    count(const _K2& __k) const {return __tree_.__count_multi(__k);}
2144#endif
2145
2146#if _LIBCPP_STD_VER > 17
2147    _LIBCPP_INLINE_VISIBILITY
2148    bool contains(const key_type& __k) const {return find(__k) != end();}
2149    template <typename _K2>
2150    _LIBCPP_INLINE_VISIBILITY
2151    typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
2152    contains(const _K2& __k) const { return find(__k) != end(); }
2153#endif // _LIBCPP_STD_VER > 17
2154
2155    _LIBCPP_INLINE_VISIBILITY
2156    iterator lower_bound(const key_type& __k)
2157        {return __tree_.lower_bound(__k);}
2158    _LIBCPP_INLINE_VISIBILITY
2159    const_iterator lower_bound(const key_type& __k) const
2160            {return __tree_.lower_bound(__k);}
2161#if _LIBCPP_STD_VER > 11
2162    template <typename _K2>
2163    _LIBCPP_INLINE_VISIBILITY
2164    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2165    lower_bound(const _K2& __k)       {return __tree_.lower_bound(__k);}
2166
2167    template <typename _K2>
2168    _LIBCPP_INLINE_VISIBILITY
2169    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2170    lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
2171#endif
2172
2173    _LIBCPP_INLINE_VISIBILITY
2174    iterator upper_bound(const key_type& __k)
2175            {return __tree_.upper_bound(__k);}
2176    _LIBCPP_INLINE_VISIBILITY
2177    const_iterator upper_bound(const key_type& __k) const
2178            {return __tree_.upper_bound(__k);}
2179#if _LIBCPP_STD_VER > 11
2180    template <typename _K2>
2181    _LIBCPP_INLINE_VISIBILITY
2182    typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2183    upper_bound(const _K2& __k)       {return __tree_.upper_bound(__k);}
2184    template <typename _K2>
2185    _LIBCPP_INLINE_VISIBILITY
2186    typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2187    upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
2188#endif
2189
2190    _LIBCPP_INLINE_VISIBILITY
2191    pair<iterator,iterator>             equal_range(const key_type& __k)
2192            {return __tree_.__equal_range_multi(__k);}
2193    _LIBCPP_INLINE_VISIBILITY
2194    pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
2195            {return __tree_.__equal_range_multi(__k);}
2196#if _LIBCPP_STD_VER > 11
2197    template <typename _K2>
2198    _LIBCPP_INLINE_VISIBILITY
2199    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
2200    equal_range(const _K2& __k)       {return __tree_.__equal_range_multi(__k);}
2201    template <typename _K2>
2202    _LIBCPP_INLINE_VISIBILITY
2203    typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
2204    equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
2205#endif
2206
2207private:
2208    typedef typename __base::__node                    __node;
2209    typedef typename __base::__node_allocator          __node_allocator;
2210    typedef typename __base::__node_pointer            __node_pointer;
2211
2212    typedef __map_node_destructor<__node_allocator> _Dp;
2213    typedef unique_ptr<__node, _Dp> __node_holder;
2214};
2215
2216#if _LIBCPP_STD_VER >= 17
2217template<class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>,
2218         class _Allocator = allocator<__iter_to_alloc_type<_InputIterator>>,
2219         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
2220         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
2221         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2222multimap(_InputIterator, _InputIterator, _Compare = _Compare(), _Allocator = _Allocator())
2223  -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare, _Allocator>;
2224
2225template<class _Key, class _Tp, class _Compare = less<remove_const_t<_Key>>,
2226         class _Allocator = allocator<pair<const _Key, _Tp>>,
2227         class = enable_if_t<!__is_allocator<_Compare>::value, void>,
2228         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2229multimap(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare(), _Allocator = _Allocator())
2230  -> multimap<remove_const_t<_Key>, _Tp, _Compare, _Allocator>;
2231
2232template<class _InputIterator, class _Allocator,
2233         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value, void>,
2234         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2235multimap(_InputIterator, _InputIterator, _Allocator)
2236  -> multimap<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>,
2237         less<__iter_key_type<_InputIterator>>, _Allocator>;
2238
2239template<class _Key, class _Tp, class _Allocator,
2240         class = enable_if_t<__is_allocator<_Allocator>::value, void>>
2241multimap(initializer_list<pair<_Key, _Tp>>, _Allocator)
2242  -> multimap<remove_const_t<_Key>, _Tp, less<remove_const_t<_Key>>, _Allocator>;
2243#endif
2244
2245#ifndef _LIBCPP_CXX03_LANG
2246template <class _Key, class _Tp, class _Compare, class _Allocator>
2247multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
2248    : __tree_(_VSTD::move(__m.__tree_), typename __base::allocator_type(__a))
2249{
2250    if (__a != __m.get_allocator())
2251    {
2252        const_iterator __e = cend();
2253        while (!__m.empty())
2254            __tree_.__insert_multi(__e.__i_,
2255                    _VSTD::move(__m.__tree_.remove(__m.begin().__i_)->__value_.__move()));
2256    }
2257}
2258#endif
2259
2260template <class _Key, class _Tp, class _Compare, class _Allocator>
2261inline _LIBCPP_INLINE_VISIBILITY
2262bool
2263operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2264           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2265{
2266    return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
2267}
2268
2269template <class _Key, class _Tp, class _Compare, class _Allocator>
2270inline _LIBCPP_INLINE_VISIBILITY
2271bool
2272operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2273           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2274{
2275    return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
2276}
2277
2278template <class _Key, class _Tp, class _Compare, class _Allocator>
2279inline _LIBCPP_INLINE_VISIBILITY
2280bool
2281operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2282           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2283{
2284    return !(__x == __y);
2285}
2286
2287template <class _Key, class _Tp, class _Compare, class _Allocator>
2288inline _LIBCPP_INLINE_VISIBILITY
2289bool
2290operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2291           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2292{
2293    return __y < __x;
2294}
2295
2296template <class _Key, class _Tp, class _Compare, class _Allocator>
2297inline _LIBCPP_INLINE_VISIBILITY
2298bool
2299operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2300           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2301{
2302    return !(__x < __y);
2303}
2304
2305template <class _Key, class _Tp, class _Compare, class _Allocator>
2306inline _LIBCPP_INLINE_VISIBILITY
2307bool
2308operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2309           const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2310{
2311    return !(__y < __x);
2312}
2313
2314template <class _Key, class _Tp, class _Compare, class _Allocator>
2315inline _LIBCPP_INLINE_VISIBILITY
2316void
2317swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2318     multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2319    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2320{
2321    __x.swap(__y);
2322}
2323
2324#if _LIBCPP_STD_VER > 17
2325template <class _Key, class _Tp, class _Compare, class _Allocator,
2326          class _Predicate>
2327inline _LIBCPP_INLINE_VISIBILITY
2328    typename multimap<_Key, _Tp, _Compare, _Allocator>::size_type
2329    erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c,
2330             _Predicate __pred) {
2331  return _VSTD::__libcpp_erase_if_container(__c, __pred);
2332}
2333#endif
2334
2335_LIBCPP_END_NAMESPACE_STD
2336
2337#endif // _LIBCPP_MAP
2338