blob: eb6b8ed0530877de27de33c8115e46f37fe6138a [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------- map ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MAP
12#define _LIBCPP_MAP
13
14/*
15
16 map synopsis
17
18namespace std
19{
20
21template <class Key, class T, class Compare = less<Key>,
22 class Allocator = allocator<pair<const Key, T>>>
23class map
24{
25public:
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:
Howard Hinnant7686add2011-06-04 14:31:57 +000057 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062 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);
Howard Hinnant7686add2011-06-04 14:31:57 +000071 map(map&& m)
72 noexcept(
73 is_nothrow_move_constructible<allocator_type>::value &&
74 is_nothrow_move_constructible<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075 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);
Marshall Clow49d596d2013-09-11 01:15:47 +000080 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();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87 map& operator=(const map& m);
Howard Hinnant7686add2011-06-04 14:31:57 +000088 map& operator=(map&& m)
89 noexcept(
90 allocator_type::propagate_on_container_move_assignment::value &&
91 is_nothrow_move_assignable<allocator_type>::value &&
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +000092 is_nothrow_move_assignable<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093 map& operator=(initializer_list<value_type> il);
94
95 // iterators:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +000096 iterator begin() noexcept;
97 const_iterator begin() const noexcept;
98 iterator end() noexcept;
99 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000101 reverse_iterator rbegin() noexcept;
102 const_reverse_iterator rbegin() const noexcept;
103 reverse_iterator rend() noexcept;
104 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000106 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110
111 // capacity:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000112 bool empty() const noexcept;
113 size_type size() const noexcept;
114 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115
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
Marshall Clowf3a1a182015-07-07 03:37:33 +0000138 template <class... Args>
139 pair<iterator, bool> try_emplace(const key_type& k, Args&&... args); // C++17
140 template <class... Args>
141 pair<iterator, bool> try_emplace(key_type&& k, Args&&... args); // C++17
142 template <class... Args>
143 iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); // C++17
144 template <class... Args>
145 iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); // C++17
146 template <class M>
147 pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj); // C++17
148 template <class M>
149 pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj); // C++17
150 template <class M>
151 iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); // C++17
152 template <class M>
153 iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); // C++17
154
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000155 iterator erase(const_iterator position);
Marshall Clow488025c2015-05-10 13:35:00 +0000156 iterator erase(iterator position); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157 size_type erase(const key_type& k);
158 iterator erase(const_iterator first, const_iterator last);
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000159 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160
Howard Hinnant7686add2011-06-04 14:31:57 +0000161 void swap(map& m)
Marshall Clow7d914d12015-07-13 20:04:56 +0000162 noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700163 __is_nothrow_swappable<key_compare>::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164
165 // observers:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000166 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 key_compare key_comp() const;
168 value_compare value_comp() const;
169
170 // map operations:
171 iterator find(const key_type& k);
172 const_iterator find(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000173 template<typename K>
174 iterator find(const K& x); // C++14
175 template<typename K>
176 const_iterator find(const K& x) const; // C++14
177 template<typename K>
Marshall Clow4de32042014-08-24 23:54:16 +0000178 size_type count(const K& x) const; // C++14
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000179
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180 size_type count(const key_type& k) const;
181 iterator lower_bound(const key_type& k);
182 const_iterator lower_bound(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000183 template<typename K>
184 iterator lower_bound(const K& x); // C++14
185 template<typename K>
186 const_iterator lower_bound(const K& x) const; // C++14
187
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188 iterator upper_bound(const key_type& k);
189 const_iterator upper_bound(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000190 template<typename K>
191 iterator upper_bound(const K& x); // C++14
192 template<typename K>
193 const_iterator upper_bound(const K& x) const; // C++14
194
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 pair<iterator,iterator> equal_range(const key_type& k);
196 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000197 template<typename K>
198 pair<iterator,iterator> equal_range(const K& x); // C++14
199 template<typename K>
200 pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201};
202
203template <class Key, class T, class Compare, class Allocator>
204bool
205operator==(const map<Key, T, Compare, Allocator>& x,
206 const map<Key, T, Compare, Allocator>& y);
207
208template <class Key, class T, class Compare, class Allocator>
209bool
210operator< (const map<Key, T, Compare, Allocator>& x,
211 const map<Key, T, Compare, Allocator>& y);
212
213template <class Key, class T, class Compare, class Allocator>
214bool
215operator!=(const map<Key, T, Compare, Allocator>& x,
216 const map<Key, T, Compare, Allocator>& y);
217
218template <class Key, class T, class Compare, class Allocator>
219bool
220operator> (const map<Key, T, Compare, Allocator>& x,
221 const map<Key, T, Compare, Allocator>& y);
222
223template <class Key, class T, class Compare, class Allocator>
224bool
225operator>=(const map<Key, T, Compare, Allocator>& x,
226 const map<Key, T, Compare, Allocator>& y);
227
228template <class Key, class T, class Compare, class Allocator>
229bool
230operator<=(const map<Key, T, Compare, Allocator>& x,
231 const map<Key, T, Compare, Allocator>& y);
232
233// specialized algorithms:
234template <class Key, class T, class Compare, class Allocator>
235void
Howard Hinnant7686add2011-06-04 14:31:57 +0000236swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y)
237 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238
239template <class Key, class T, class Compare = less<Key>,
240 class Allocator = allocator<pair<const Key, T>>>
241class multimap
242{
243public:
244 // types:
245 typedef Key key_type;
246 typedef T mapped_type;
247 typedef pair<const key_type,mapped_type> value_type;
248 typedef Compare key_compare;
249 typedef Allocator allocator_type;
250 typedef typename allocator_type::reference reference;
251 typedef typename allocator_type::const_reference const_reference;
252 typedef typename allocator_type::size_type size_type;
253 typedef typename allocator_type::difference_type difference_type;
254 typedef typename allocator_type::pointer pointer;
255 typedef typename allocator_type::const_pointer const_pointer;
256
257 typedef implementation-defined iterator;
258 typedef implementation-defined const_iterator;
259 typedef std::reverse_iterator<iterator> reverse_iterator;
260 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
261
262 class value_compare
263 : public binary_function<value_type,value_type,bool>
264 {
265 friend class multimap;
266 protected:
267 key_compare comp;
268 value_compare(key_compare c);
269 public:
270 bool operator()(const value_type& x, const value_type& y) const;
271 };
272
273 // construct/copy/destroy:
Howard Hinnant7686add2011-06-04 14:31:57 +0000274 multimap()
275 noexcept(
276 is_nothrow_default_constructible<allocator_type>::value &&
277 is_nothrow_default_constructible<key_compare>::value &&
278 is_nothrow_copy_constructible<key_compare>::value);
279 explicit multimap(const key_compare& comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280 multimap(const key_compare& comp, const allocator_type& a);
281 template <class InputIterator>
282 multimap(InputIterator first, InputIterator last, const key_compare& comp);
283 template <class InputIterator>
284 multimap(InputIterator first, InputIterator last, const key_compare& comp,
285 const allocator_type& a);
286 multimap(const multimap& m);
Howard Hinnant7686add2011-06-04 14:31:57 +0000287 multimap(multimap&& m)
288 noexcept(
289 is_nothrow_move_constructible<allocator_type>::value &&
290 is_nothrow_move_constructible<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 explicit multimap(const allocator_type& a);
292 multimap(const multimap& m, const allocator_type& a);
293 multimap(multimap&& m, const allocator_type& a);
294 multimap(initializer_list<value_type> il, const key_compare& comp = key_compare());
295 multimap(initializer_list<value_type> il, const key_compare& comp,
296 const allocator_type& a);
Marshall Clow49d596d2013-09-11 01:15:47 +0000297 template <class InputIterator>
298 multimap(InputIterator first, InputIterator last, const allocator_type& a)
299 : multimap(first, last, Compare(), a) {} // C++14
300 multimap(initializer_list<value_type> il, const allocator_type& a)
301 : multimap(il, Compare(), a) {} // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 ~multimap();
303
304 multimap& operator=(const multimap& m);
Howard Hinnant7686add2011-06-04 14:31:57 +0000305 multimap& operator=(multimap&& m)
306 noexcept(
307 allocator_type::propagate_on_container_move_assignment::value &&
308 is_nothrow_move_assignable<allocator_type>::value &&
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000309 is_nothrow_move_assignable<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 multimap& operator=(initializer_list<value_type> il);
311
312 // iterators:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000313 iterator begin() noexcept;
314 const_iterator begin() const noexcept;
315 iterator end() noexcept;
316 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000317
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000318 reverse_iterator rbegin() noexcept;
319 const_reverse_iterator rbegin() const noexcept;
320 reverse_iterator rend() noexcept;
321 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000322
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000323 const_iterator cbegin() const noexcept;
324 const_iterator cend() const noexcept;
325 const_reverse_iterator crbegin() const noexcept;
326 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327
328 // capacity:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000329 bool empty() const noexcept;
330 size_type size() const noexcept;
331 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332
333 // modifiers:
334 template <class... Args>
335 iterator emplace(Args&&... args);
336 template <class... Args>
337 iterator emplace_hint(const_iterator position, Args&&... args);
338 iterator insert(const value_type& v);
339 template <class P>
340 iterator insert(P&& p);
341 iterator insert(const_iterator position, const value_type& v);
342 template <class P>
343 iterator insert(const_iterator position, P&& p);
344 template <class InputIterator>
345 void insert(InputIterator first, InputIterator last);
346 void insert(initializer_list<value_type> il);
347
348 iterator erase(const_iterator position);
Marshall Clow488025c2015-05-10 13:35:00 +0000349 iterator erase(iterator position); // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350 size_type erase(const key_type& k);
351 iterator erase(const_iterator first, const_iterator last);
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000352 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000353
Howard Hinnant7686add2011-06-04 14:31:57 +0000354 void swap(multimap& m)
Marshall Clow7d914d12015-07-13 20:04:56 +0000355 noexcept(allocator_traits<allocator_type>::is_always_equal::value &&
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700356 __is_nothrow_swappable<key_compare>::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358 // observers:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000359 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000360 key_compare key_comp() const;
361 value_compare value_comp() const;
362
363 // map operations:
364 iterator find(const key_type& k);
365 const_iterator find(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000366 template<typename K>
367 iterator find(const K& x); // C++14
368 template<typename K>
369 const_iterator find(const K& x) const; // C++14
370 template<typename K>
Marshall Clow4de32042014-08-24 23:54:16 +0000371 size_type count(const K& x) const; // C++14
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000372
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 size_type count(const key_type& k) const;
374 iterator lower_bound(const key_type& k);
375 const_iterator lower_bound(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000376 template<typename K>
377 iterator lower_bound(const K& x); // C++14
378 template<typename K>
379 const_iterator lower_bound(const K& x) const; // C++14
380
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 iterator upper_bound(const key_type& k);
382 const_iterator upper_bound(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000383 template<typename K>
384 iterator upper_bound(const K& x); // C++14
385 template<typename K>
386 const_iterator upper_bound(const K& x) const; // C++14
387
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388 pair<iterator,iterator> equal_range(const key_type& k);
389 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000390 template<typename K>
391 pair<iterator,iterator> equal_range(const K& x); // C++14
392 template<typename K>
393 pair<const_iterator,const_iterator> equal_range(const K& x) const; // C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394};
395
396template <class Key, class T, class Compare, class Allocator>
397bool
398operator==(const multimap<Key, T, Compare, Allocator>& x,
399 const multimap<Key, T, Compare, Allocator>& y);
400
401template <class Key, class T, class Compare, class Allocator>
402bool
403operator< (const multimap<Key, T, Compare, Allocator>& x,
404 const multimap<Key, T, Compare, Allocator>& y);
405
406template <class Key, class T, class Compare, class Allocator>
407bool
408operator!=(const multimap<Key, T, Compare, Allocator>& x,
409 const multimap<Key, T, Compare, Allocator>& y);
410
411template <class Key, class T, class Compare, class Allocator>
412bool
413operator> (const multimap<Key, T, Compare, Allocator>& x,
414 const multimap<Key, T, Compare, Allocator>& y);
415
416template <class Key, class T, class Compare, class Allocator>
417bool
418operator>=(const multimap<Key, T, Compare, Allocator>& x,
419 const multimap<Key, T, Compare, Allocator>& y);
420
421template <class Key, class T, class Compare, class Allocator>
422bool
423operator<=(const multimap<Key, T, Compare, Allocator>& x,
424 const multimap<Key, T, Compare, Allocator>& y);
425
426// specialized algorithms:
427template <class Key, class T, class Compare, class Allocator>
428void
429swap(multimap<Key, T, Compare, Allocator>& x,
Howard Hinnant7686add2011-06-04 14:31:57 +0000430 multimap<Key, T, Compare, Allocator>& y)
431 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432
433} // std
434
435*/
436
437#include <__config>
438#include <__tree>
439#include <iterator>
440#include <memory>
441#include <utility>
442#include <functional>
443#include <initializer_list>
Eric Fiselier3a0e4302015-06-13 07:08:02 +0000444#include <type_traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445
Howard Hinnant08e17472011-10-17 20:05:10 +0000446#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000448#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449
450_LIBCPP_BEGIN_NAMESPACE_STD
451
Eric Fiselier3a0e4302015-06-13 07:08:02 +0000452template <class _Key, class _CP, class _Compare,
453 bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value
Howard Hinnantd4cf2152011-12-11 20:31:33 +0000454 >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455class __map_value_compare
456 : private _Compare
457{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458public:
Howard Hinnant82894812010-09-22 16:48:34 +0000459 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000460 __map_value_compare()
461 _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
462 : _Compare() {}
Howard Hinnant82894812010-09-22 16:48:34 +0000463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000464 __map_value_compare(_Compare c)
465 _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
466 : _Compare(c) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000467 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000468 const _Compare& key_comp() const _NOEXCEPT {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 bool operator()(const _CP& __x, const _CP& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000471 {return static_cast<const _Compare&>(*this)(__x.__cc.first, __y.__cc.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 bool operator()(const _CP& __x, const _Key& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000474 {return static_cast<const _Compare&>(*this)(__x.__cc.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476 bool operator()(const _Key& __x, const _CP& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000477 {return static_cast<const _Compare&>(*this)(__x, __y.__cc.first);}
Marshall Clow7d914d12015-07-13 20:04:56 +0000478 void swap(__map_value_compare&__y)
479 _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
480 {
481 using _VSTD::swap;
482 swap(static_cast<const _Compare&>(*this), static_cast<const _Compare&>(__y));
483 }
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000484
485#if _LIBCPP_STD_VER > 11
486 template <typename _K2>
487 _LIBCPP_INLINE_VISIBILITY
488 typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
489 operator () ( const _K2& __x, const _CP& __y ) const
490 {return static_cast<const _Compare&>(*this) (__x, __y.__cc.first);}
491
492 template <typename _K2>
493 _LIBCPP_INLINE_VISIBILITY
494 typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
495 operator () (const _CP& __x, const _K2& __y) const
496 {return static_cast<const _Compare&>(*this) (__x.__cc.first, __y);}
497#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498};
499
Howard Hinnant9b128e02013-07-05 18:06:00 +0000500template <class _Key, class _CP, class _Compare>
501class __map_value_compare<_Key, _CP, _Compare, false>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000502{
503 _Compare comp;
504
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505public:
Howard Hinnant82894812010-09-22 16:48:34 +0000506 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000507 __map_value_compare()
508 _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
509 : comp() {}
Howard Hinnant82894812010-09-22 16:48:34 +0000510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000511 __map_value_compare(_Compare c)
512 _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
513 : comp(c) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000514 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000515 const _Compare& key_comp() const _NOEXCEPT {return comp;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516
Howard Hinnant82894812010-09-22 16:48:34 +0000517 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518 bool operator()(const _CP& __x, const _CP& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000519 {return comp(__x.__cc.first, __y.__cc.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000520 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521 bool operator()(const _CP& __x, const _Key& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000522 {return comp(__x.__cc.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 bool operator()(const _Key& __x, const _CP& __y) const
Howard Hinnant9b128e02013-07-05 18:06:00 +0000525 {return comp(__x, __y.__cc.first);}
Marshall Clow7d914d12015-07-13 20:04:56 +0000526 void swap(__map_value_compare&__y)
527 _NOEXCEPT_(__is_nothrow_swappable<_Compare>::value)
528 {
529 using _VSTD::swap;
530 swap(comp, __y.comp);
531 }
532
Marshall Clow5cfc6ab2013-08-13 22:18:47 +0000533#if _LIBCPP_STD_VER > 11
534 template <typename _K2>
535 _LIBCPP_INLINE_VISIBILITY
536 typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
537 operator () ( const _K2& __x, const _CP& __y ) const
538 {return comp (__x, __y.__cc.first);}
539
540 template <typename _K2>
541 _LIBCPP_INLINE_VISIBILITY
542 typename enable_if<__is_transparent<_Compare, _K2>::value, bool>::type
543 operator () (const _CP& __x, const _K2& __y) const
544 {return comp (__x.__cc.first, __y);}
545#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546};
547
Marshall Clow7d914d12015-07-13 20:04:56 +0000548template <class _Key, class _CP, class _Compare, bool __b>
549inline _LIBCPP_INLINE_VISIBILITY
550void
551swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x,
552 __map_value_compare<_Key, _CP, _Compare, __b>& __y)
553 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
554{
555 __x.swap(__y);
556}
557
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558template <class _Allocator>
559class __map_node_destructor
560{
561 typedef _Allocator allocator_type;
562 typedef allocator_traits<allocator_type> __alloc_traits;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700563 typedef typename __alloc_traits::value_type::value_type value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564public:
565 typedef typename __alloc_traits::pointer pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000566private:
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700567 typedef typename value_type::value_type::first_type first_type;
568 typedef typename value_type::value_type::second_type second_type;
569
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 allocator_type& __na_;
571
572 __map_node_destructor& operator=(const __map_node_destructor&);
573
574public:
575 bool __first_constructed;
576 bool __second_constructed;
577
Howard Hinnant82894812010-09-22 16:48:34 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000579 explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000580 : __na_(__na),
581 __first_constructed(false),
582 __second_constructed(false)
583 {}
584
Howard Hinnant73d21a42010-09-04 23:28:19 +0000585#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000587 __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 : __na_(__x.__na_),
589 __first_constructed(__x.__value_constructed),
590 __second_constructed(__x.__value_constructed)
591 {
592 __x.__value_constructed = false;
593 }
Howard Hinnantbfd55302010-09-04 23:46:48 +0000594#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595
Howard Hinnant82894812010-09-22 16:48:34 +0000596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000597 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 {
599 if (__second_constructed)
Howard Hinnant70342b92013-06-19 21:29:40 +0000600 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__cc.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601 if (__first_constructed)
Howard Hinnant70342b92013-06-19 21:29:40 +0000602 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.__cc.first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 if (__p)
604 __alloc_traits::deallocate(__na_, __p, 1);
605 }
606};
607
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000608template <class _Key, class _Tp, class _Compare, class _Allocator>
609 class map;
610template <class _Key, class _Tp, class _Compare, class _Allocator>
611 class multimap;
612template <class _TreeIterator> class __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700614#if __cplusplus >= 201103L
Howard Hinnantff7546e2013-09-30 19:08:22 +0000615
616template <class _Key, class _Tp>
617union __value_type
618{
619 typedef _Key key_type;
620 typedef _Tp mapped_type;
621 typedef pair<const key_type, mapped_type> value_type;
622 typedef pair<key_type, mapped_type> __nc_value_type;
623
624 value_type __cc;
625 __nc_value_type __nc;
626
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700627 template <class ..._Args>
628 _LIBCPP_INLINE_VISIBILITY
629 __value_type(_Args&& ...__args)
630 : __cc(std::forward<_Args>(__args)...) {}
631
632 _LIBCPP_INLINE_VISIBILITY
633 __value_type(const __value_type& __v)
634 : __cc(__v.__cc) {}
635
636 _LIBCPP_INLINE_VISIBILITY
637 __value_type(__value_type& __v)
638 : __cc(__v.__cc) {}
639
640 _LIBCPP_INLINE_VISIBILITY
641 __value_type(__value_type&& __v)
642 : __nc(std::move(__v.__nc)) {}
643
Howard Hinnantff7546e2013-09-30 19:08:22 +0000644 _LIBCPP_INLINE_VISIBILITY
645 __value_type& operator=(const __value_type& __v)
646 {__nc = __v.__cc; return *this;}
647
648 _LIBCPP_INLINE_VISIBILITY
649 __value_type& operator=(__value_type&& __v)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700650 {__nc = std::move(__v.__nc); return *this;}
Howard Hinnantff7546e2013-09-30 19:08:22 +0000651
652 _LIBCPP_INLINE_VISIBILITY
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700653 ~__value_type() {__cc.~value_type();}
Howard Hinnantff7546e2013-09-30 19:08:22 +0000654};
655
656#else
657
658template <class _Key, class _Tp>
659struct __value_type
660{
661 typedef _Key key_type;
662 typedef _Tp mapped_type;
663 typedef pair<const key_type, mapped_type> value_type;
664
665 value_type __cc;
666
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700667 _LIBCPP_INLINE_VISIBILITY
668 __value_type() {}
669
670 template <class _A0>
671 _LIBCPP_INLINE_VISIBILITY
672 __value_type(const _A0& __a0)
673 : __cc(__a0) {}
674
675 template <class _A0, class _A1>
676 _LIBCPP_INLINE_VISIBILITY
677 __value_type(const _A0& __a0, const _A1& __a1)
678 : __cc(__a0, __a1) {}
Howard Hinnantff7546e2013-09-30 19:08:22 +0000679};
680
681#endif
682
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000683template <class _Tp>
684struct __extract_key_value_types;
685
686template <class _Key, class _Tp>
687struct __extract_key_value_types<__value_type<_Key, _Tp> >
688{
689 typedef _Key const __key_type;
690 typedef _Tp __mapped_type;
691};
692
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693template <class _TreeIterator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000694class _LIBCPP_TYPE_VIS_ONLY __map_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695{
696 _TreeIterator __i_;
697
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700698 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
699 typedef typename _TreeIterator::value_type __value_type;
700 typedef typename __extract_key_value_types<__value_type>::__key_type __key_type;
701 typedef typename __extract_key_value_types<__value_type>::__mapped_type __mapped_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702public:
703 typedef bidirectional_iterator_tag iterator_category;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700704 typedef pair<__key_type, __mapped_type> value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 typedef typename _TreeIterator::difference_type difference_type;
706 typedef value_type& reference;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700707 typedef typename __pointer_traits::template
708#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
709 rebind<value_type>
710#else
711 rebind<value_type>::other
712#endif
713 pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714
Howard Hinnant82894812010-09-22 16:48:34 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000716 __map_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717
Howard Hinnant82894812010-09-22 16:48:34 +0000718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000719 __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720
Howard Hinnant82894812010-09-22 16:48:34 +0000721 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70342b92013-06-19 21:29:40 +0000722 reference operator*() const {return __i_->__cc;}
Howard Hinnant82894812010-09-22 16:48:34 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70342b92013-06-19 21:29:40 +0000724 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__cc);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725
Howard Hinnant82894812010-09-22 16:48:34 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727 __map_iterator& operator++() {++__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000728 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729 __map_iterator operator++(int)
730 {
731 __map_iterator __t(*this);
732 ++(*this);
733 return __t;
734 }
735
Howard Hinnant82894812010-09-22 16:48:34 +0000736 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 __map_iterator& operator--() {--__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739 __map_iterator operator--(int)
740 {
741 __map_iterator __t(*this);
742 --(*this);
743 return __t;
744 }
745
Howard Hinnant82894812010-09-22 16:48:34 +0000746 friend _LIBCPP_INLINE_VISIBILITY
747 bool operator==(const __map_iterator& __x, const __map_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748 {return __x.__i_ == __y.__i_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000749 friend
750 _LIBCPP_INLINE_VISIBILITY
751 bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 {return __x.__i_ != __y.__i_;}
753
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000754 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
755 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
756 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757};
758
759template <class _TreeIterator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000760class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761{
762 _TreeIterator __i_;
763
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700764 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
765 typedef typename _TreeIterator::value_type __value_type;
766 typedef typename __extract_key_value_types<__value_type>::__key_type __key_type;
767 typedef typename __extract_key_value_types<__value_type>::__mapped_type __mapped_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768public:
769 typedef bidirectional_iterator_tag iterator_category;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700770 typedef pair<__key_type, __mapped_type> value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 typedef typename _TreeIterator::difference_type difference_type;
772 typedef const value_type& reference;
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700773 typedef typename __pointer_traits::template
774#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
775 rebind<const value_type>
776#else
777 rebind<const value_type>::other
778#endif
779 pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780
Howard Hinnant82894812010-09-22 16:48:34 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000782 __map_const_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783
Howard Hinnant82894812010-09-22 16:48:34 +0000784 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000785 __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000786 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000787 __map_const_iterator(__map_iterator<
788 typename _TreeIterator::__non_const_iterator> __i) _NOEXCEPT
789 : __i_(__i.__i_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790
Howard Hinnant82894812010-09-22 16:48:34 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70342b92013-06-19 21:29:40 +0000792 reference operator*() const {return __i_->__cc;}
Howard Hinnant82894812010-09-22 16:48:34 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant70342b92013-06-19 21:29:40 +0000794 pointer operator->() const {return pointer_traits<pointer>::pointer_to(__i_->__cc);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795
Howard Hinnant82894812010-09-22 16:48:34 +0000796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797 __map_const_iterator& operator++() {++__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000799 __map_const_iterator operator++(int)
800 {
801 __map_const_iterator __t(*this);
802 ++(*this);
803 return __t;
804 }
805
Howard Hinnant82894812010-09-22 16:48:34 +0000806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 __map_const_iterator& operator--() {--__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809 __map_const_iterator operator--(int)
810 {
811 __map_const_iterator __t(*this);
812 --(*this);
813 return __t;
814 }
815
Howard Hinnant82894812010-09-22 16:48:34 +0000816 friend _LIBCPP_INLINE_VISIBILITY
817 bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818 {return __x.__i_ == __y.__i_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000819 friend _LIBCPP_INLINE_VISIBILITY
820 bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 {return __x.__i_ != __y.__i_;}
822
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000823 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
824 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
825 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826};
827
828template <class _Key, class _Tp, class _Compare = less<_Key>,
829 class _Allocator = allocator<pair<const _Key, _Tp> > >
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000830class _LIBCPP_TYPE_VIS_ONLY map
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831{
832public:
833 // types:
834 typedef _Key key_type;
835 typedef _Tp mapped_type;
836 typedef pair<const key_type, mapped_type> value_type;
Howard Hinnant70342b92013-06-19 21:29:40 +0000837 typedef pair<key_type, mapped_type> __nc_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000838 typedef _Compare key_compare;
839 typedef _Allocator allocator_type;
840 typedef value_type& reference;
841 typedef const value_type& const_reference;
842
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000843 class _LIBCPP_TYPE_VIS_ONLY value_compare
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844 : public binary_function<value_type, value_type, bool>
845 {
846 friend class map;
847 protected:
848 key_compare comp;
849
Howard Hinnant82894812010-09-22 16:48:34 +0000850 _LIBCPP_INLINE_VISIBILITY value_compare(key_compare c) : comp(c) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851 public:
Howard Hinnant82894812010-09-22 16:48:34 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 bool operator()(const value_type& __x, const value_type& __y) const
854 {return comp(__x.first, __y.first);}
855 };
856
857private:
Howard Hinnant70342b92013-06-19 21:29:40 +0000858
Howard Hinnantff7546e2013-09-30 19:08:22 +0000859 typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
Howard Hinnant9b128e02013-07-05 18:06:00 +0000860 typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
Marshall Clow66302c62015-04-07 05:21:38 +0000861 typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
862 __value_type>::type __allocator_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863 typedef __tree<__value_type, __vc, __allocator_type> __base;
864 typedef typename __base::__node_traits __node_traits;
865 typedef allocator_traits<allocator_type> __alloc_traits;
866
867 __base __tree_;
868
869public:
870 typedef typename __alloc_traits::pointer pointer;
871 typedef typename __alloc_traits::const_pointer const_pointer;
872 typedef typename __alloc_traits::size_type size_type;
873 typedef typename __alloc_traits::difference_type difference_type;
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000874 typedef __map_iterator<typename __base::iterator> iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000876 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
877 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878
Howard Hinnant82894812010-09-22 16:48:34 +0000879 _LIBCPP_INLINE_VISIBILITY
Marshall Clowcaaa1412014-03-10 04:50:10 +0000880 map()
881 _NOEXCEPT_(
882 is_nothrow_default_constructible<allocator_type>::value &&
883 is_nothrow_default_constructible<key_compare>::value &&
884 is_nothrow_copy_constructible<key_compare>::value)
885 : __tree_(__vc(key_compare())) {}
886
887 _LIBCPP_INLINE_VISIBILITY
888 explicit map(const key_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +0000889 _NOEXCEPT_(
890 is_nothrow_default_constructible<allocator_type>::value &&
Howard Hinnant7686add2011-06-04 14:31:57 +0000891 is_nothrow_copy_constructible<key_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 : __tree_(__vc(__comp)) {}
893
Howard Hinnant82894812010-09-22 16:48:34 +0000894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895 explicit map(const key_compare& __comp, const allocator_type& __a)
896 : __tree_(__vc(__comp), __a) {}
897
898 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900 map(_InputIterator __f, _InputIterator __l,
901 const key_compare& __comp = key_compare())
902 : __tree_(__vc(__comp))
903 {
904 insert(__f, __l);
905 }
906
907 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 map(_InputIterator __f, _InputIterator __l,
910 const key_compare& __comp, const allocator_type& __a)
911 : __tree_(__vc(__comp), __a)
912 {
913 insert(__f, __l);
914 }
915
Marshall Clow49d596d2013-09-11 01:15:47 +0000916#if _LIBCPP_STD_VER > 11
917 template <class _InputIterator>
918 _LIBCPP_INLINE_VISIBILITY
919 map(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
920 : map(__f, __l, key_compare(), __a) {}
921#endif
922
Howard Hinnant82894812010-09-22 16:48:34 +0000923 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924 map(const map& __m)
925 : __tree_(__m.__tree_)
926 {
927 insert(__m.begin(), __m.end());
928 }
929
Howard Hinnant61aa6012011-07-01 19:24:36 +0000930 _LIBCPP_INLINE_VISIBILITY
931 map& operator=(const map& __m)
932 {
Howard Hinnant70342b92013-06-19 21:29:40 +0000933#if __cplusplus >= 201103L
Howard Hinnant61aa6012011-07-01 19:24:36 +0000934 __tree_ = __m.__tree_;
Howard Hinnant70342b92013-06-19 21:29:40 +0000935#else
Marshall Clowebfc50e2014-02-08 04:03:14 +0000936 if (this != &__m) {
937 __tree_.clear();
938 __tree_.value_comp() = __m.__tree_.value_comp();
939 __tree_.__copy_assign_alloc(__m.__tree_);
940 insert(__m.begin(), __m.end());
941 }
Howard Hinnant70342b92013-06-19 21:29:40 +0000942#endif
Howard Hinnant61aa6012011-07-01 19:24:36 +0000943 return *this;
944 }
945
Howard Hinnant73d21a42010-09-04 23:28:19 +0000946#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947
Howard Hinnant82894812010-09-22 16:48:34 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949 map(map&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +0000950 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000951 : __tree_(_VSTD::move(__m.__tree_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952 {
953 }
954
955 map(map&& __m, const allocator_type& __a);
956
Howard Hinnant82894812010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante3e32912011-08-12 21:56:02 +0000958 map& operator=(map&& __m)
959 _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
960 {
961 __tree_ = _VSTD::move(__m.__tree_);
962 return *this;
963 }
964
965#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
966
967#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
968
969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970 map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
971 : __tree_(__vc(__comp))
972 {
973 insert(__il.begin(), __il.end());
974 }
975
Howard Hinnant82894812010-09-22 16:48:34 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
978 : __tree_(__vc(__comp), __a)
979 {
980 insert(__il.begin(), __il.end());
981 }
982
Marshall Clow49d596d2013-09-11 01:15:47 +0000983#if _LIBCPP_STD_VER > 11
984 _LIBCPP_INLINE_VISIBILITY
985 map(initializer_list<value_type> __il, const allocator_type& __a)
986 : map(__il, key_compare(), __a) {}
987#endif
988
Howard Hinnant82894812010-09-22 16:48:34 +0000989 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 map& operator=(initializer_list<value_type> __il)
991 {
992 __tree_.__assign_unique(__il.begin(), __il.end());
993 return *this;
994 }
995
Howard Hinnante3e32912011-08-12 21:56:02 +0000996#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997
Howard Hinnant82894812010-09-22 16:48:34 +0000998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 explicit map(const allocator_type& __a)
1000 : __tree_(__a)
1001 {
1002 }
1003
Howard Hinnant82894812010-09-22 16:48:34 +00001004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005 map(const map& __m, const allocator_type& __a)
1006 : __tree_(__m.__tree_.value_comp(), __a)
1007 {
1008 insert(__m.begin(), __m.end());
1009 }
1010
Howard Hinnant82894812010-09-22 16:48:34 +00001011 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001012 iterator begin() _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001014 const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001016 iterator end() _NOEXCEPT {return __tree_.end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001017 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001018 const_iterator end() const _NOEXCEPT {return __tree_.end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019
Howard Hinnant82894812010-09-22 16:48:34 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001021 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001023 const_reverse_iterator rbegin() const _NOEXCEPT
1024 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001026 reverse_iterator rend() _NOEXCEPT
1027 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +00001028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001029 const_reverse_iterator rend() const _NOEXCEPT
1030 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031
Howard Hinnant82894812010-09-22 16:48:34 +00001032 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001033 const_iterator cbegin() const _NOEXCEPT {return begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001034 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001035 const_iterator cend() const _NOEXCEPT {return end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001037 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001039 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040
Howard Hinnant82894812010-09-22 16:48:34 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001042 bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001044 size_type size() const _NOEXCEPT {return __tree_.size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001046 size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047
1048 mapped_type& operator[](const key_type& __k);
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 mapped_type& operator[](key_type&& __k);
1051#endif
1052
1053 mapped_type& at(const key_type& __k);
1054 const mapped_type& at(const key_type& __k) const;
1055
Howard Hinnant82894812010-09-22 16:48:34 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001057 allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
Howard Hinnant82894812010-09-22 16:48:34 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
Howard Hinnant82894812010-09-22 16:48:34 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
1062
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001063#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1064#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +00001065
Howard Hinnant635ce1d2012-05-25 22:04:21 +00001066 template <class ..._Args>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001067 pair<iterator, bool>
1068 emplace(_Args&& ...__args);
1069
1070 template <class ..._Args>
1071 iterator
1072 emplace_hint(const_iterator __p, _Args&& ...__args);
1073
1074#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +00001075
Howard Hinnant99968442011-11-29 18:15:50 +00001076 template <class _Pp,
1077 class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001079 pair<iterator, bool> insert(_Pp&& __p)
1080 {return __tree_.__insert_unique(_VSTD::forward<_Pp>(__p));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001081
Howard Hinnant99968442011-11-29 18:15:50 +00001082 template <class _Pp,
1083 class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001085 iterator insert(const_iterator __pos, _Pp&& __p)
1086 {return __tree_.__insert_unique(__pos.__i_, _VSTD::forward<_Pp>(__p));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001087
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001088#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089
Howard Hinnant82894812010-09-22 16:48:34 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091 pair<iterator, bool>
1092 insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
1093
Howard Hinnant82894812010-09-22 16:48:34 +00001094 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 iterator
1096 insert(const_iterator __p, const value_type& __v)
1097 {return __tree_.__insert_unique(__p.__i_, __v);}
1098
1099 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101 void insert(_InputIterator __f, _InputIterator __l)
1102 {
1103 for (const_iterator __e = cend(); __f != __l; ++__f)
1104 insert(__e.__i_, *__f);
1105 }
1106
Howard Hinnante3e32912011-08-12 21:56:02 +00001107#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1108
Howard Hinnant82894812010-09-22 16:48:34 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 void insert(initializer_list<value_type> __il)
1111 {insert(__il.begin(), __il.end());}
1112
Howard Hinnante3e32912011-08-12 21:56:02 +00001113#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1114
Marshall Clowf3a1a182015-07-07 03:37:33 +00001115#if _LIBCPP_STD_VER > 14
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001116#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1117#ifndef _LIBCPP_HAS_NO_VARIADICS
Marshall Clowf3a1a182015-07-07 03:37:33 +00001118 template <class... _Args>
1119 _LIBCPP_INLINE_VISIBILITY
1120 pair<iterator, bool> try_emplace(const key_type& __k, _Args&&... __args)
1121 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001122 iterator __p = lower_bound(__k);
1123 if ( __p != end() && !key_comp()(__k, __p->first))
1124 return _VSTD::make_pair(__p, false);
1125 else
1126 return _VSTD::make_pair(
1127 emplace_hint(__p,
1128 _VSTD::piecewise_construct, _VSTD::forward_as_tuple(__k),
1129 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)),
1130 true);
Marshall Clowf3a1a182015-07-07 03:37:33 +00001131 }
1132
1133 template <class... _Args>
1134 _LIBCPP_INLINE_VISIBILITY
1135 pair<iterator, bool> try_emplace(key_type&& __k, _Args&&... __args)
1136 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001137 iterator __p = lower_bound(__k);
1138 if ( __p != end() && !key_comp()(__k, __p->first))
1139 return _VSTD::make_pair(__p, false);
1140 else
1141 return _VSTD::make_pair(
1142 emplace_hint(__p,
1143 _VSTD::piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__k)),
1144 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)),
1145 true);
Marshall Clowf3a1a182015-07-07 03:37:33 +00001146 }
1147
1148 template <class... _Args>
1149 _LIBCPP_INLINE_VISIBILITY
1150 iterator try_emplace(const_iterator __h, const key_type& __k, _Args&&... __args)
1151 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001152 iterator __p = lower_bound(__k);
1153 if ( __p != end() && !key_comp()(__k, __p->first))
1154 return __p;
1155 else
1156 return emplace_hint(__p,
1157 _VSTD::piecewise_construct, _VSTD::forward_as_tuple(__k),
1158 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
Marshall Clowf3a1a182015-07-07 03:37:33 +00001159 }
1160
1161 template <class... _Args>
1162 _LIBCPP_INLINE_VISIBILITY
1163 iterator try_emplace(const_iterator __h, key_type&& __k, _Args&&... __args)
1164 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001165 iterator __p = lower_bound(__k);
1166 if ( __p != end() && !key_comp()(__k, __p->first))
1167 return __p;
1168 else
1169 return emplace_hint(__p,
1170 _VSTD::piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__k)),
1171 _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...));
Marshall Clowf3a1a182015-07-07 03:37:33 +00001172 }
1173
1174 template <class _Vp>
1175 _LIBCPP_INLINE_VISIBILITY
1176 pair<iterator, bool> insert_or_assign(const key_type& __k, _Vp&& __v)
1177 {
1178 iterator __p = lower_bound(__k);
1179 if ( __p != end() && !key_comp()(__k, __p->first))
1180 {
1181 __p->second = _VSTD::forward<_Vp>(__v);
1182 return _VSTD::make_pair(__p, false);
1183 }
1184 return _VSTD::make_pair(emplace_hint(__p, __k, _VSTD::forward<_Vp>(__v)), true);
1185 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001186
Marshall Clowf3a1a182015-07-07 03:37:33 +00001187 template <class _Vp>
1188 _LIBCPP_INLINE_VISIBILITY
1189 pair<iterator, bool> insert_or_assign(key_type&& __k, _Vp&& __v)
1190 {
1191 iterator __p = lower_bound(__k);
1192 if ( __p != end() && !key_comp()(__k, __p->first))
1193 {
1194 __p->second = _VSTD::forward<_Vp>(__v);
1195 return _VSTD::make_pair(__p, false);
1196 }
1197 return _VSTD::make_pair(emplace_hint(__p, _VSTD::move(__k), _VSTD::forward<_Vp>(__v)), true);
1198 }
1199
1200 template <class _Vp>
1201 _LIBCPP_INLINE_VISIBILITY
1202 iterator insert_or_assign(const_iterator __h, const key_type& __k, _Vp&& __v)
1203 {
1204 iterator __p = lower_bound(__k);
1205 if ( __p != end() && !key_comp()(__k, __p->first))
1206 {
1207 __p->second = _VSTD::forward<_Vp>(__v);
1208 return __p;
1209 }
1210 return emplace_hint(__h, __k, _VSTD::forward<_Vp>(__v));
1211 }
1212
1213 template <class _Vp>
1214 _LIBCPP_INLINE_VISIBILITY
1215 iterator insert_or_assign(const_iterator __h, key_type&& __k, _Vp&& __v)
1216 {
1217 iterator __p = lower_bound(__k);
1218 if ( __p != end() && !key_comp()(__k, __p->first))
1219 {
1220 __p->second = _VSTD::forward<_Vp>(__v);
1221 return __p;
1222 }
1223 return emplace_hint(__h, _VSTD::move(__k), _VSTD::forward<_Vp>(__v));
1224 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001225#endif
1226#endif
Marshall Clowf3a1a182015-07-07 03:37:33 +00001227#endif
1228
Howard Hinnant82894812010-09-22 16:48:34 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001231 _LIBCPP_INLINE_VISIBILITY
Marshall Clow488025c2015-05-10 13:35:00 +00001232 iterator erase(iterator __p) {return __tree_.erase(__p.__i_);}
1233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234 size_type erase(const key_type& __k)
1235 {return __tree_.__erase_unique(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 iterator erase(const_iterator __f, const_iterator __l)
1238 {return __tree_.erase(__f.__i_, __l.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001240 void clear() _NOEXCEPT {__tree_.clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241
Howard Hinnant82894812010-09-22 16:48:34 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001243 void swap(map& __m)
1244 _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
1245 {__tree_.swap(__m.__tree_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246
Howard Hinnant82894812010-09-22 16:48:34 +00001247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 iterator find(const key_type& __k) {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00001251#if _LIBCPP_STD_VER > 11
1252 template <typename _K2>
1253 _LIBCPP_INLINE_VISIBILITY
1254 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1255 find(const _K2& __k) {return __tree_.find(__k);}
1256 template <typename _K2>
1257 _LIBCPP_INLINE_VISIBILITY
1258 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1259 find(const _K2& __k) const {return __tree_.find(__k);}
1260#endif
1261
Howard Hinnant82894812010-09-22 16:48:34 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263 size_type count(const key_type& __k) const
1264 {return __tree_.__count_unique(__k);}
Marshall Clow4de32042014-08-24 23:54:16 +00001265#if _LIBCPP_STD_VER > 11
1266 template <typename _K2>
1267 _LIBCPP_INLINE_VISIBILITY
1268 typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
Marshall Clow58113db2015-06-30 18:15:41 +00001269 count(const _K2& __k) const {return __tree_.__count_unique(__k);}
Marshall Clow4de32042014-08-24 23:54:16 +00001270#endif
Howard Hinnant82894812010-09-22 16:48:34 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001272 iterator lower_bound(const key_type& __k)
1273 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001274 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275 const_iterator lower_bound(const key_type& __k) const
1276 {return __tree_.lower_bound(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00001277#if _LIBCPP_STD_VER > 11
1278 template <typename _K2>
1279 _LIBCPP_INLINE_VISIBILITY
1280 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1281 lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
1282
1283 template <typename _K2>
1284 _LIBCPP_INLINE_VISIBILITY
1285 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1286 lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
1287#endif
1288
Howard Hinnant82894812010-09-22 16:48:34 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 iterator upper_bound(const key_type& __k)
1291 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 const_iterator upper_bound(const key_type& __k) const
1294 {return __tree_.upper_bound(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00001295#if _LIBCPP_STD_VER > 11
1296 template <typename _K2>
1297 _LIBCPP_INLINE_VISIBILITY
1298 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1299 upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
1300 template <typename _K2>
1301 _LIBCPP_INLINE_VISIBILITY
1302 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1303 upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
1304#endif
1305
Howard Hinnant82894812010-09-22 16:48:34 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 pair<iterator,iterator> equal_range(const key_type& __k)
1308 {return __tree_.__equal_range_unique(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1311 {return __tree_.__equal_range_unique(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00001312#if _LIBCPP_STD_VER > 11
1313 template <typename _K2>
1314 _LIBCPP_INLINE_VISIBILITY
1315 typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
1316 equal_range(const _K2& __k) {return __tree_.__equal_range_unique(__k);}
1317 template <typename _K2>
1318 _LIBCPP_INLINE_VISIBILITY
1319 typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
1320 equal_range(const _K2& __k) const {return __tree_.__equal_range_unique(__k);}
1321#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001322
1323private:
1324 typedef typename __base::__node __node;
1325 typedef typename __base::__node_allocator __node_allocator;
1326 typedef typename __base::__node_pointer __node_pointer;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001327 typedef typename __base::__node_const_pointer __node_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001328 typedef typename __base::__node_base_pointer __node_base_pointer;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001329 typedef typename __base::__node_base_const_pointer __node_base_const_pointer;
Howard Hinnant99968442011-11-29 18:15:50 +00001330 typedef __map_node_destructor<__node_allocator> _Dp;
1331 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001333#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1334 __node_holder __construct_node();
1335 template <class _A0>
1336 __node_holder __construct_node(_A0&& __a0);
1337 __node_holder __construct_node_with_key(key_type&& __k);
1338#ifndef _LIBCPP_HAS_NO_VARIADICS
1339 template <class _A0, class _A1, class ..._Args>
1340 __node_holder __construct_node(_A0&& __a0, _A1&& __a1, _Args&& ...__args);
1341#endif // _LIBCPP_HAS_NO_VARIADICS
Eric Fiselierd2864672016-03-31 03:13:37 +00001342#endif
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001343 __node_holder __construct_node_with_key(const key_type& __k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344
1345 __node_base_pointer&
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001346 __find_equal_key(__node_base_pointer& __parent, const key_type& __k);
1347 __node_base_const_pointer
1348 __find_equal_key(__node_base_const_pointer& __parent, const key_type& __k) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349};
1350
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001351// Find place to insert if __k doesn't exist
1352// Set __parent to parent of null leaf
1353// Return reference to null leaf
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354// If __k exists, set parent to node of __k and return reference to node of __k
1355template <class _Key, class _Tp, class _Compare, class _Allocator>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001356typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_pointer& __parent,
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001358 const key_type& __k)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359{
1360 __node_pointer __nd = __tree_.__root();
1361 if (__nd != nullptr)
1362 {
1363 while (true)
1364 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001365 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.__cc.first))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 {
1367 if (__nd->__left_ != nullptr)
1368 __nd = static_cast<__node_pointer>(__nd->__left_);
1369 else
1370 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001371 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372 return __parent->__left_;
1373 }
1374 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001375 else if (__tree_.value_comp().key_comp()(__nd->__value_.__cc.first, __k))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 {
1377 if (__nd->__right_ != nullptr)
1378 __nd = static_cast<__node_pointer>(__nd->__right_);
1379 else
1380 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001381 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382 return __parent->__right_;
1383 }
1384 }
1385 else
1386 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001387 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 return __parent;
1389 }
1390 }
1391 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001392 __parent = static_cast<__node_base_pointer>(__tree_.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 return __parent->__left_;
1394}
1395
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001396// Find __k
1397// Set __parent to parent of null leaf and
1398// return reference to null leaf iv __k does not exist.
1399// If __k exists, set parent to node of __k and return reference to node of __k
1400template <class _Key, class _Tp, class _Compare, class _Allocator>
1401typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_const_pointer
1402map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_const_pointer& __parent,
1403 const key_type& __k) const
1404{
1405 __node_const_pointer __nd = __tree_.__root();
1406 if (__nd != nullptr)
1407 {
1408 while (true)
1409 {
1410 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.__cc.first))
1411 {
1412 if (__nd->__left_ != nullptr)
1413 __nd = static_cast<__node_pointer>(__nd->__left_);
1414 else
1415 {
1416 __parent = static_cast<__node_base_pointer>(__nd);
1417 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1418 }
1419 }
1420 else if (__tree_.value_comp().key_comp()(__nd->__value_.__cc.first, __k))
1421 {
1422 if (__nd->__right_ != nullptr)
1423 __nd = static_cast<__node_pointer>(__nd->__right_);
1424 else
1425 {
1426 __parent = static_cast<__node_base_pointer>(__nd);
1427 return const_cast<const __node_base_const_pointer&>(__parent->__right_);
1428 }
1429 }
1430 else
1431 {
1432 __parent = static_cast<__node_base_pointer>(__nd);
1433 return __parent;
1434 }
1435 }
1436 }
1437 __parent = static_cast<__node_base_pointer>(__tree_.__end_node());
1438 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1439}
1440
1441#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442
1443template <class _Key, class _Tp, class _Compare, class _Allocator>
1444map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001445 : __tree_(_VSTD::move(__m.__tree_), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446{
1447 if (__a != __m.get_allocator())
1448 {
1449 const_iterator __e = cend();
1450 while (!__m.empty())
1451 __tree_.__insert_unique(__e.__i_,
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001452 _VSTD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 }
1454}
1455
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001456template <class _Key, class _Tp, class _Compare, class _Allocator>
1457typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1458map<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1459{
1460 __node_allocator& __na = __tree_.__node_alloc();
1461 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1462 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.first));
1463 __h.get_deleter().__first_constructed = true;
1464 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
1465 __h.get_deleter().__second_constructed = true;
1466 return __h;
1467}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001469template <class _Key, class _Tp, class _Compare, class _Allocator>
1470template <class _A0>
1471typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1472map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1473{
1474 __node_allocator& __na = __tree_.__node_alloc();
1475 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1476 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_A0>(__a0));
1477 __h.get_deleter().__first_constructed = true;
1478 __h.get_deleter().__second_constructed = true;
1479 return __h;
1480}
Eric Fiselierd2864672016-03-31 03:13:37 +00001481
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001482template <class _Key, class _Tp, class _Compare, class _Allocator>
1483typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1484map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(key_type&& __k)
1485{
1486 __node_allocator& __na = __tree_.__node_alloc();
1487 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1488 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.first), _VSTD::move(__k));
1489 __h.get_deleter().__first_constructed = true;
1490 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
1491 __h.get_deleter().__second_constructed = true;
1492 return __h;
1493}
1494
1495#ifndef _LIBCPP_HAS_NO_VARIADICS
1496
1497template <class _Key, class _Tp, class _Compare, class _Allocator>
1498template <class _A0, class _A1, class ..._Args>
1499typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1500map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _A1&& __a1, _Args&& ...__args)
1501{
1502 __node_allocator& __na = __tree_.__node_alloc();
1503 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1504 __node_traits::construct(__na, _VSTD::addressof(__h->__value_),
1505 _VSTD::forward<_A0>(__a0), _VSTD::forward<_A1>(__a1),
1506 _VSTD::forward<_Args>(__args)...);
1507 __h.get_deleter().__first_constructed = true;
1508 __h.get_deleter().__second_constructed = true;
1509 return __h;
1510}
1511
1512#endif // _LIBCPP_HAS_NO_VARIADICS
1513
1514#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Eric Fiselierd2864672016-03-31 03:13:37 +00001515
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516template <class _Key, class _Tp, class _Compare, class _Allocator>
1517typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
Howard Hinnantb66e1c32013-07-04 20:59:16 +00001518map<_Key, _Tp, _Compare, _Allocator>::__construct_node_with_key(const key_type& __k)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519{
1520 __node_allocator& __na = __tree_.__node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001521 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant70342b92013-06-19 21:29:40 +00001522 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.first), __k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 __h.get_deleter().__first_constructed = true;
Howard Hinnant70342b92013-06-19 21:29:40 +00001524 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 __h.get_deleter().__second_constructed = true;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001526 return _VSTD::move(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001527}
1528
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529template <class _Key, class _Tp, class _Compare, class _Allocator>
1530_Tp&
1531map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1532{
1533 __node_base_pointer __parent;
1534 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1535 __node_pointer __r = static_cast<__node_pointer>(__child);
1536 if (__child == nullptr)
1537 {
Howard Hinnantb66e1c32013-07-04 20:59:16 +00001538 __node_holder __h = __construct_node_with_key(__k);
Howard Hinnant70342b92013-06-19 21:29:40 +00001539 __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 __r = __h.release();
1541 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001542 return __r->__value_.__cc.second;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543}
1544
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001545#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546
1547template <class _Key, class _Tp, class _Compare, class _Allocator>
1548_Tp&
1549map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1550{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001551 __node_base_pointer __parent;
1552 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1553 __node_pointer __r = static_cast<__node_pointer>(__child);
1554 if (__child == nullptr)
1555 {
1556 __node_holder __h = __construct_node_with_key(_VSTD::move(__k));
1557 __tree_.__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1558 __r = __h.release();
1559 }
1560 return __r->__value_.__cc.second;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561}
1562
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001563#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001564
1565template <class _Key, class _Tp, class _Compare, class _Allocator>
1566_Tp&
1567map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1568{
1569 __node_base_pointer __parent;
1570 __node_base_pointer& __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001571#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572 if (__child == nullptr)
1573 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001574#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant70342b92013-06-19 21:29:40 +00001575 return static_cast<__node_pointer>(__child)->__value_.__cc.second;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576}
1577
1578template <class _Key, class _Tp, class _Compare, class _Allocator>
1579const _Tp&
1580map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1581{
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001582 __node_base_const_pointer __parent;
1583 __node_base_const_pointer __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001584#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 if (__child == nullptr)
1586 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001587#endif // _LIBCPP_NO_EXCEPTIONS
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001588 return static_cast<__node_const_pointer>(__child)->__value_.__cc.second;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589}
1590
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001591#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1592
1593template <class _Key, class _Tp, class _Compare, class _Allocator>
1594template <class ..._Args>
1595pair<typename map<_Key, _Tp, _Compare, _Allocator>::iterator, bool>
1596map<_Key, _Tp, _Compare, _Allocator>::emplace(_Args&& ...__args)
1597{
1598 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1599 pair<iterator, bool> __r = __tree_.__node_insert_unique(__h.get());
1600 if (__r.second)
1601 __h.release();
1602 return __r;
1603}
1604
1605template <class _Key, class _Tp, class _Compare, class _Allocator>
1606template <class ..._Args>
1607typename map<_Key, _Tp, _Compare, _Allocator>::iterator
1608map<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1609 _Args&& ...__args)
1610{
1611 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1612 iterator __r = __tree_.__node_insert_unique(__p.__i_, __h.get());
1613 if (__r.__i_.__ptr_ == __h.get())
1614 __h.release();
1615 return __r;
1616}
1617
1618#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619
1620template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001621inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622bool
1623operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1624 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1625{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001626 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627}
1628
1629template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001630inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631bool
1632operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1633 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1634{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001635 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636}
1637
1638template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001639inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640bool
1641operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1642 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1643{
1644 return !(__x == __y);
1645}
1646
1647template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001648inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001649bool
1650operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1651 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1652{
1653 return __y < __x;
1654}
1655
1656template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001657inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658bool
1659operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1660 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1661{
1662 return !(__x < __y);
1663}
1664
1665template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667bool
1668operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1669 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1670{
1671 return !(__y < __x);
1672}
1673
1674template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001675inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676void
1677swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1678 map<_Key, _Tp, _Compare, _Allocator>& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001679 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001680{
1681 __x.swap(__y);
1682}
1683
1684template <class _Key, class _Tp, class _Compare = less<_Key>,
1685 class _Allocator = allocator<pair<const _Key, _Tp> > >
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001686class _LIBCPP_TYPE_VIS_ONLY multimap
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001687{
1688public:
1689 // types:
1690 typedef _Key key_type;
1691 typedef _Tp mapped_type;
1692 typedef pair<const key_type, mapped_type> value_type;
Howard Hinnant70342b92013-06-19 21:29:40 +00001693 typedef pair<key_type, mapped_type> __nc_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 typedef _Compare key_compare;
1695 typedef _Allocator allocator_type;
1696 typedef value_type& reference;
1697 typedef const value_type& const_reference;
1698
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001699 class _LIBCPP_TYPE_VIS_ONLY value_compare
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 : public binary_function<value_type, value_type, bool>
1701 {
1702 friend class multimap;
1703 protected:
1704 key_compare comp;
1705
Howard Hinnant82894812010-09-22 16:48:34 +00001706 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 value_compare(key_compare c) : comp(c) {}
1708 public:
Howard Hinnant82894812010-09-22 16:48:34 +00001709 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001710 bool operator()(const value_type& __x, const value_type& __y) const
1711 {return comp(__x.first, __y.first);}
1712 };
1713
1714private:
Howard Hinnant70342b92013-06-19 21:29:40 +00001715
Howard Hinnantff7546e2013-09-30 19:08:22 +00001716 typedef _VSTD::__value_type<key_type, mapped_type> __value_type;
Howard Hinnant9b128e02013-07-05 18:06:00 +00001717 typedef __map_value_compare<key_type, __value_type, key_compare> __vc;
Marshall Clow66302c62015-04-07 05:21:38 +00001718 typedef typename __rebind_alloc_helper<allocator_traits<allocator_type>,
1719 __value_type>::type __allocator_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 typedef __tree<__value_type, __vc, __allocator_type> __base;
1721 typedef typename __base::__node_traits __node_traits;
1722 typedef allocator_traits<allocator_type> __alloc_traits;
1723
1724 __base __tree_;
1725
1726public:
1727 typedef typename __alloc_traits::pointer pointer;
1728 typedef typename __alloc_traits::const_pointer const_pointer;
1729 typedef typename __alloc_traits::size_type size_type;
1730 typedef typename __alloc_traits::difference_type difference_type;
1731 typedef __map_iterator<typename __base::iterator> iterator;
1732 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001733 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1734 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735
Howard Hinnant82894812010-09-22 16:48:34 +00001736 _LIBCPP_INLINE_VISIBILITY
Marshall Clowcaaa1412014-03-10 04:50:10 +00001737 multimap()
1738 _NOEXCEPT_(
1739 is_nothrow_default_constructible<allocator_type>::value &&
1740 is_nothrow_default_constructible<key_compare>::value &&
1741 is_nothrow_copy_constructible<key_compare>::value)
1742 : __tree_(__vc(key_compare())) {}
1743
1744 _LIBCPP_INLINE_VISIBILITY
1745 explicit multimap(const key_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001746 _NOEXCEPT_(
1747 is_nothrow_default_constructible<allocator_type>::value &&
Howard Hinnant7686add2011-06-04 14:31:57 +00001748 is_nothrow_copy_constructible<key_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001749 : __tree_(__vc(__comp)) {}
1750
Howard Hinnant82894812010-09-22 16:48:34 +00001751 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001752 explicit multimap(const key_compare& __comp, const allocator_type& __a)
1753 : __tree_(__vc(__comp), __a) {}
1754
1755 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001756 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 multimap(_InputIterator __f, _InputIterator __l,
1758 const key_compare& __comp = key_compare())
1759 : __tree_(__vc(__comp))
1760 {
1761 insert(__f, __l);
1762 }
1763
1764 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 multimap(_InputIterator __f, _InputIterator __l,
1767 const key_compare& __comp, const allocator_type& __a)
1768 : __tree_(__vc(__comp), __a)
1769 {
1770 insert(__f, __l);
1771 }
1772
Marshall Clow49d596d2013-09-11 01:15:47 +00001773#if _LIBCPP_STD_VER > 11
1774 template <class _InputIterator>
1775 _LIBCPP_INLINE_VISIBILITY
1776 multimap(_InputIterator __f, _InputIterator __l, const allocator_type& __a)
1777 : multimap(__f, __l, key_compare(), __a) {}
1778#endif
1779
Howard Hinnant82894812010-09-22 16:48:34 +00001780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 multimap(const multimap& __m)
1782 : __tree_(__m.__tree_.value_comp(),
1783 __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1784 {
1785 insert(__m.begin(), __m.end());
1786 }
1787
Howard Hinnant61aa6012011-07-01 19:24:36 +00001788 _LIBCPP_INLINE_VISIBILITY
1789 multimap& operator=(const multimap& __m)
1790 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001791#if __cplusplus >= 201103L
Howard Hinnant61aa6012011-07-01 19:24:36 +00001792 __tree_ = __m.__tree_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001793#else
Marshall Clowebfc50e2014-02-08 04:03:14 +00001794 if (this != &__m) {
1795 __tree_.clear();
1796 __tree_.value_comp() = __m.__tree_.value_comp();
1797 __tree_.__copy_assign_alloc(__m.__tree_);
1798 insert(__m.begin(), __m.end());
1799 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001800#endif
Howard Hinnant61aa6012011-07-01 19:24:36 +00001801 return *this;
1802 }
1803
Howard Hinnant73d21a42010-09-04 23:28:19 +00001804#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805
Howard Hinnant82894812010-09-22 16:48:34 +00001806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 multimap(multimap&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +00001808 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001809 : __tree_(_VSTD::move(__m.__tree_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001810 {
1811 }
1812
1813 multimap(multimap&& __m, const allocator_type& __a);
1814
Howard Hinnant82894812010-09-22 16:48:34 +00001815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante3e32912011-08-12 21:56:02 +00001816 multimap& operator=(multimap&& __m)
1817 _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
1818 {
1819 __tree_ = _VSTD::move(__m.__tree_);
1820 return *this;
1821 }
1822
1823#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1824
1825#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1826
1827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1829 : __tree_(__vc(__comp))
1830 {
1831 insert(__il.begin(), __il.end());
1832 }
1833
Howard Hinnant82894812010-09-22 16:48:34 +00001834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001835 multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1836 : __tree_(__vc(__comp), __a)
1837 {
1838 insert(__il.begin(), __il.end());
1839 }
1840
Marshall Clow49d596d2013-09-11 01:15:47 +00001841#if _LIBCPP_STD_VER > 11
1842 _LIBCPP_INLINE_VISIBILITY
1843 multimap(initializer_list<value_type> __il, const allocator_type& __a)
1844 : multimap(__il, key_compare(), __a) {}
1845#endif
1846
Howard Hinnant82894812010-09-22 16:48:34 +00001847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 multimap& operator=(initializer_list<value_type> __il)
1849 {
1850 __tree_.__assign_multi(__il.begin(), __il.end());
1851 return *this;
1852 }
Howard Hinnante3e32912011-08-12 21:56:02 +00001853
1854#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855
Howard Hinnant82894812010-09-22 16:48:34 +00001856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857 explicit multimap(const allocator_type& __a)
1858 : __tree_(__a)
1859 {
1860 }
1861
Howard Hinnant82894812010-09-22 16:48:34 +00001862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 multimap(const multimap& __m, const allocator_type& __a)
1864 : __tree_(__m.__tree_.value_comp(), __a)
1865 {
1866 insert(__m.begin(), __m.end());
1867 }
1868
Howard Hinnant82894812010-09-22 16:48:34 +00001869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001870 iterator begin() _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001872 const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001874 iterator end() _NOEXCEPT {return __tree_.end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001876 const_iterator end() const _NOEXCEPT {return __tree_.end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877
Howard Hinnant82894812010-09-22 16:48:34 +00001878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001879 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001880 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001881 const_reverse_iterator rbegin() const _NOEXCEPT
1882 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001883 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001884 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +00001885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001886 const_reverse_iterator rend() const _NOEXCEPT
1887 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888
Howard Hinnant82894812010-09-22 16:48:34 +00001889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001890 const_iterator cbegin() const _NOEXCEPT {return begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001892 const_iterator cend() const _NOEXCEPT {return end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001894 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001896 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897
Howard Hinnant82894812010-09-22 16:48:34 +00001898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001899 bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00001900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001901 size_type size() const _NOEXCEPT {return __tree_.size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001903 size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904
Howard Hinnant82894812010-09-22 16:48:34 +00001905 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001906 allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
Howard Hinnant82894812010-09-22 16:48:34 +00001907 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001908 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
Howard Hinnant82894812010-09-22 16:48:34 +00001909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001910 value_compare value_comp() const
1911 {return value_compare(__tree_.value_comp().key_comp());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001913#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1914#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +00001915
Howard Hinnant635ce1d2012-05-25 22:04:21 +00001916 template <class ..._Args>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001917 iterator
1918 emplace(_Args&& ...__args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919
Howard Hinnant635ce1d2012-05-25 22:04:21 +00001920 template <class ..._Args>
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001921 iterator
1922 emplace_hint(const_iterator __p, _Args&& ...__args);
1923
1924#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnant73d21a42010-09-04 23:28:19 +00001925
Howard Hinnant99968442011-11-29 18:15:50 +00001926 template <class _Pp,
1927 class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001929 iterator insert(_Pp&& __p)
1930 {return __tree_.__insert_multi(_VSTD::forward<_Pp>(__p));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931
Howard Hinnant99968442011-11-29 18:15:50 +00001932 template <class _Pp,
1933 class = typename enable_if<is_constructible<value_type, _Pp>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001934 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant99968442011-11-29 18:15:50 +00001935 iterator insert(const_iterator __pos, _Pp&& __p)
1936 {return __tree_.__insert_multi(__pos.__i_, _VSTD::forward<_Pp>(__p));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001937
Dan Albert1d4a1ed2016-05-25 22:36:09 -07001938#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939
Howard Hinnant82894812010-09-22 16:48:34 +00001940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001941 iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
1942
Howard Hinnant82894812010-09-22 16:48:34 +00001943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001944 iterator insert(const_iterator __p, const value_type& __v)
1945 {return __tree_.__insert_multi(__p.__i_, __v);}
1946
1947 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001949 void insert(_InputIterator __f, _InputIterator __l)
1950 {
1951 for (const_iterator __e = cend(); __f != __l; ++__f)
1952 __tree_.__insert_multi(__e.__i_, *__f);
1953 }
1954
Howard Hinnante3e32912011-08-12 21:56:02 +00001955#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1956
Howard Hinnant82894812010-09-22 16:48:34 +00001957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001958 void insert(initializer_list<value_type> __il)
1959 {insert(__il.begin(), __il.end());}
1960
Howard Hinnante3e32912011-08-12 21:56:02 +00001961#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1962
Howard Hinnant82894812010-09-22 16:48:34 +00001963 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001965 _LIBCPP_INLINE_VISIBILITY
Marshall Clow488025c2015-05-10 13:35:00 +00001966 iterator erase(iterator __p) {return __tree_.erase(__p.__i_);}
1967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001969 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001970 iterator erase(const_iterator __f, const_iterator __l)
1971 {return __tree_.erase(__f.__i_, __l.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973 void clear() {__tree_.clear();}
1974
Howard Hinnant82894812010-09-22 16:48:34 +00001975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001976 void swap(multimap& __m)
1977 _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
1978 {__tree_.swap(__m.__tree_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979
Howard Hinnant82894812010-09-22 16:48:34 +00001980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 iterator find(const key_type& __k) {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00001984#if _LIBCPP_STD_VER > 11
1985 template <typename _K2>
1986 _LIBCPP_INLINE_VISIBILITY
1987 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
1988 find(const _K2& __k) {return __tree_.find(__k);}
1989 template <typename _K2>
1990 _LIBCPP_INLINE_VISIBILITY
1991 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
1992 find(const _K2& __k) const {return __tree_.find(__k);}
1993#endif
1994
Howard Hinnant82894812010-09-22 16:48:34 +00001995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001996 size_type count(const key_type& __k) const
1997 {return __tree_.__count_multi(__k);}
Marshall Clow4de32042014-08-24 23:54:16 +00001998#if _LIBCPP_STD_VER > 11
1999 template <typename _K2>
2000 _LIBCPP_INLINE_VISIBILITY
2001 typename enable_if<__is_transparent<_Compare, _K2>::value,size_type>::type
Marshall Clow58113db2015-06-30 18:15:41 +00002002 count(const _K2& __k) const {return __tree_.__count_multi(__k);}
Marshall Clow4de32042014-08-24 23:54:16 +00002003#endif
Howard Hinnant82894812010-09-22 16:48:34 +00002004 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 iterator lower_bound(const key_type& __k)
2006 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00002007 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008 const_iterator lower_bound(const key_type& __k) const
2009 {return __tree_.lower_bound(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00002010#if _LIBCPP_STD_VER > 11
2011 template <typename _K2>
2012 _LIBCPP_INLINE_VISIBILITY
2013 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2014 lower_bound(const _K2& __k) {return __tree_.lower_bound(__k);}
2015
2016 template <typename _K2>
2017 _LIBCPP_INLINE_VISIBILITY
2018 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2019 lower_bound(const _K2& __k) const {return __tree_.lower_bound(__k);}
2020#endif
2021
Howard Hinnant82894812010-09-22 16:48:34 +00002022 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 iterator upper_bound(const key_type& __k)
2024 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00002025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002026 const_iterator upper_bound(const key_type& __k) const
2027 {return __tree_.upper_bound(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00002028#if _LIBCPP_STD_VER > 11
2029 template <typename _K2>
2030 _LIBCPP_INLINE_VISIBILITY
2031 typename enable_if<__is_transparent<_Compare, _K2>::value,iterator>::type
2032 upper_bound(const _K2& __k) {return __tree_.upper_bound(__k);}
2033 template <typename _K2>
2034 _LIBCPP_INLINE_VISIBILITY
2035 typename enable_if<__is_transparent<_Compare, _K2>::value,const_iterator>::type
2036 upper_bound(const _K2& __k) const {return __tree_.upper_bound(__k);}
2037#endif
2038
Howard Hinnant82894812010-09-22 16:48:34 +00002039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040 pair<iterator,iterator> equal_range(const key_type& __k)
2041 {return __tree_.__equal_range_multi(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00002042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002043 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
2044 {return __tree_.__equal_range_multi(__k);}
Marshall Clow5cfc6ab2013-08-13 22:18:47 +00002045#if _LIBCPP_STD_VER > 11
2046 template <typename _K2>
2047 _LIBCPP_INLINE_VISIBILITY
2048 typename enable_if<__is_transparent<_Compare, _K2>::value,pair<iterator,iterator>>::type
2049 equal_range(const _K2& __k) {return __tree_.__equal_range_multi(__k);}
2050 template <typename _K2>
2051 _LIBCPP_INLINE_VISIBILITY
2052 typename enable_if<__is_transparent<_Compare, _K2>::value,pair<const_iterator,const_iterator>>::type
2053 equal_range(const _K2& __k) const {return __tree_.__equal_range_multi(__k);}
2054#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055
2056private:
2057 typedef typename __base::__node __node;
2058 typedef typename __base::__node_allocator __node_allocator;
2059 typedef typename __base::__node_pointer __node_pointer;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002060 typedef typename __base::__node_const_pointer __node_const_pointer;
Howard Hinnant99968442011-11-29 18:15:50 +00002061 typedef __map_node_destructor<__node_allocator> _Dp;
2062 typedef unique_ptr<__node, _Dp> __node_holder;
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002063
2064#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2065 __node_holder __construct_node();
2066 template <class _A0>
2067 __node_holder
2068 __construct_node(_A0&& __a0);
2069#ifndef _LIBCPP_HAS_NO_VARIADICS
2070 template <class _A0, class _A1, class ..._Args>
2071 __node_holder __construct_node(_A0&& __a0, _A1&& __a1, _Args&& ...__args);
2072#endif // _LIBCPP_HAS_NO_VARIADICS
2073#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074};
2075
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002076#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2077
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078template <class _Key, class _Tp, class _Compare, class _Allocator>
2079multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002080 : __tree_(_VSTD::move(__m.__tree_), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
2082 if (__a != __m.get_allocator())
2083 {
2084 const_iterator __e = cend();
2085 while (!__m.empty())
2086 __tree_.__insert_multi(__e.__i_,
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002087 _VSTD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088 }
2089}
Dan Albert1d4a1ed2016-05-25 22:36:09 -07002090
2091template <class _Key, class _Tp, class _Compare, class _Allocator>
2092typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
2093multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node()
2094{
2095 __node_allocator& __na = __tree_.__node_alloc();
2096 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2097 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.first));
2098 __h.get_deleter().__first_constructed = true;
2099 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.__cc.second));
2100 __h.get_deleter().__second_constructed = true;
2101 return __h;
2102}
2103
2104template <class _Key, class _Tp, class _Compare, class _Allocator>
2105template <class _A0>
2106typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
2107multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
2108{
2109 __node_allocator& __na = __tree_.__node_alloc();
2110 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2111 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_A0>(__a0));
2112 __h.get_deleter().__first_constructed = true;
2113 __h.get_deleter().__second_constructed = true;
2114 return __h;
2115}
2116
2117#ifndef _LIBCPP_HAS_NO_VARIADICS
2118
2119template <class _Key, class _Tp, class _Compare, class _Allocator>
2120template <class _A0, class _A1, class ..._Args>
2121typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
2122multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _A1&& __a1, _Args&& ...__args)
2123{
2124 __node_allocator& __na = __tree_.__node_alloc();
2125 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2126 __node_traits::construct(__na, _VSTD::addressof(__h->__value_),
2127 _VSTD::forward<_A0>(__a0), _VSTD::forward<_A1>(__a1),
2128 _VSTD::forward<_Args>(__args)...);
2129 __h.get_deleter().__first_constructed = true;
2130 __h.get_deleter().__second_constructed = true;
2131 return __h;
2132}
2133
2134#endif // _LIBCPP_HAS_NO_VARIADICS
2135#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
2136
2137#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
2138
2139template <class _Key, class _Tp, class _Compare, class _Allocator>
2140template <class ..._Args>
2141typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
2142multimap<_Key, _Tp, _Compare, _Allocator>::emplace(_Args&& ...__args)
2143{
2144 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2145 iterator __r = __tree_.__node_insert_multi(__h.get());
2146 __h.release();
2147 return __r;
2148}
2149
2150template <class _Key, class _Tp, class _Compare, class _Allocator>
2151template <class ..._Args>
2152typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
2153multimap<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
2154 _Args&& ...__args)
2155{
2156 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2157 iterator __r = __tree_.__node_insert_multi(__p.__i_, __h.get());
2158 __h.release();
2159 return __r;
2160}
2161
2162#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163
2164template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002165inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002166bool
2167operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2168 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2169{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002170 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171}
2172
2173template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175bool
2176operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2177 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2178{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002179 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180}
2181
2182template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002183inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184bool
2185operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2186 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2187{
2188 return !(__x == __y);
2189}
2190
2191template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002192inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193bool
2194operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2195 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2196{
2197 return __y < __x;
2198}
2199
2200template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002201inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202bool
2203operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2204 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2205{
2206 return !(__x < __y);
2207}
2208
2209template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002210inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211bool
2212operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2213 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
2214{
2215 return !(__y < __x);
2216}
2217
2218template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00002219inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220void
2221swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
2222 multimap<_Key, _Tp, _Compare, _Allocator>& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00002223 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224{
2225 __x.swap(__y);
2226}
2227
2228_LIBCPP_END_NAMESPACE_STD
2229
2230#endif // _LIBCPP_MAP