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