blob: c0bc78413a82851b008b776641ccd86c4c89a67c [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);
80 ~map();
81
82 map& operator=(const map& m);
Howard Hinnant7686add2011-06-04 14:31:57 +000083 map& operator=(map&& m)
84 noexcept(
85 allocator_type::propagate_on_container_move_assignment::value &&
86 is_nothrow_move_assignable<allocator_type>::value &&
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +000087 is_nothrow_move_assignable<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 map& operator=(initializer_list<value_type> il);
89
90 // iterators:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +000091 iterator begin() noexcept;
92 const_iterator begin() const noexcept;
93 iterator end() noexcept;
94 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +000096 reverse_iterator rbegin() noexcept;
97 const_reverse_iterator rbegin() const noexcept;
98 reverse_iterator rend() noexcept;
99 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000101 const_iterator cbegin() const noexcept;
102 const_iterator cend() const noexcept;
103 const_reverse_iterator crbegin() const noexcept;
104 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105
106 // capacity:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000107 bool empty() const noexcept;
108 size_type size() const noexcept;
109 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110
111 // element access:
112 mapped_type& operator[](const key_type& k);
113 mapped_type& operator[](key_type&& k);
114
115 mapped_type& at(const key_type& k);
116 const mapped_type& at(const key_type& k) const;
117
118 // modifiers:
119 template <class... Args>
120 pair<iterator, bool> emplace(Args&&... args);
121 template <class... Args>
122 iterator emplace_hint(const_iterator position, Args&&... args);
123 pair<iterator, bool> insert(const value_type& v);
124 template <class P>
125 pair<iterator, bool> insert(P&& p);
126 iterator insert(const_iterator position, const value_type& v);
127 template <class P>
128 iterator insert(const_iterator position, P&& p);
129 template <class InputIterator>
130 void insert(InputIterator first, InputIterator last);
131 void insert(initializer_list<value_type> il);
132
133 iterator erase(const_iterator position);
134 size_type erase(const key_type& k);
135 iterator erase(const_iterator first, const_iterator last);
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000136 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137
Howard Hinnant7686add2011-06-04 14:31:57 +0000138 void swap(map& m)
139 noexcept(
140 __is_nothrow_swappable<key_compare>::value &&
141 (!allocator_type::propagate_on_container_swap::value ||
142 __is_nothrow_swappable<allocator_type>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143
144 // observers:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000145 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146 key_compare key_comp() const;
147 value_compare value_comp() const;
148
149 // map operations:
150 iterator find(const key_type& k);
151 const_iterator find(const key_type& k) const;
152 size_type count(const key_type& k) const;
153 iterator lower_bound(const key_type& k);
154 const_iterator lower_bound(const key_type& k) const;
155 iterator upper_bound(const key_type& k);
156 const_iterator upper_bound(const key_type& k) const;
157 pair<iterator,iterator> equal_range(const key_type& k);
158 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
159};
160
161template <class Key, class T, class Compare, class Allocator>
162bool
163operator==(const map<Key, T, Compare, Allocator>& x,
164 const map<Key, T, Compare, Allocator>& y);
165
166template <class Key, class T, class Compare, class Allocator>
167bool
168operator< (const map<Key, T, Compare, Allocator>& x,
169 const map<Key, T, Compare, Allocator>& y);
170
171template <class Key, class T, class Compare, class Allocator>
172bool
173operator!=(const map<Key, T, Compare, Allocator>& x,
174 const map<Key, T, Compare, Allocator>& y);
175
176template <class Key, class T, class Compare, class Allocator>
177bool
178operator> (const map<Key, T, Compare, Allocator>& x,
179 const map<Key, T, Compare, Allocator>& y);
180
181template <class Key, class T, class Compare, class Allocator>
182bool
183operator>=(const map<Key, T, Compare, Allocator>& x,
184 const map<Key, T, Compare, Allocator>& y);
185
186template <class Key, class T, class Compare, class Allocator>
187bool
188operator<=(const map<Key, T, Compare, Allocator>& x,
189 const map<Key, T, Compare, Allocator>& y);
190
191// specialized algorithms:
192template <class Key, class T, class Compare, class Allocator>
193void
Howard Hinnant7686add2011-06-04 14:31:57 +0000194swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y)
195 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196
197template <class Key, class T, class Compare = less<Key>,
198 class Allocator = allocator<pair<const Key, T>>>
199class multimap
200{
201public:
202 // types:
203 typedef Key key_type;
204 typedef T mapped_type;
205 typedef pair<const key_type,mapped_type> value_type;
206 typedef Compare key_compare;
207 typedef Allocator allocator_type;
208 typedef typename allocator_type::reference reference;
209 typedef typename allocator_type::const_reference const_reference;
210 typedef typename allocator_type::size_type size_type;
211 typedef typename allocator_type::difference_type difference_type;
212 typedef typename allocator_type::pointer pointer;
213 typedef typename allocator_type::const_pointer const_pointer;
214
215 typedef implementation-defined iterator;
216 typedef implementation-defined const_iterator;
217 typedef std::reverse_iterator<iterator> reverse_iterator;
218 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
219
220 class value_compare
221 : public binary_function<value_type,value_type,bool>
222 {
223 friend class multimap;
224 protected:
225 key_compare comp;
226 value_compare(key_compare c);
227 public:
228 bool operator()(const value_type& x, const value_type& y) const;
229 };
230
231 // construct/copy/destroy:
Howard Hinnant7686add2011-06-04 14:31:57 +0000232 multimap()
233 noexcept(
234 is_nothrow_default_constructible<allocator_type>::value &&
235 is_nothrow_default_constructible<key_compare>::value &&
236 is_nothrow_copy_constructible<key_compare>::value);
237 explicit multimap(const key_compare& comp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 multimap(const key_compare& comp, const allocator_type& a);
239 template <class InputIterator>
240 multimap(InputIterator first, InputIterator last, const key_compare& comp);
241 template <class InputIterator>
242 multimap(InputIterator first, InputIterator last, const key_compare& comp,
243 const allocator_type& a);
244 multimap(const multimap& m);
Howard Hinnant7686add2011-06-04 14:31:57 +0000245 multimap(multimap&& m)
246 noexcept(
247 is_nothrow_move_constructible<allocator_type>::value &&
248 is_nothrow_move_constructible<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 explicit multimap(const allocator_type& a);
250 multimap(const multimap& m, const allocator_type& a);
251 multimap(multimap&& m, const allocator_type& a);
252 multimap(initializer_list<value_type> il, const key_compare& comp = key_compare());
253 multimap(initializer_list<value_type> il, const key_compare& comp,
254 const allocator_type& a);
255 ~multimap();
256
257 multimap& operator=(const multimap& m);
Howard Hinnant7686add2011-06-04 14:31:57 +0000258 multimap& operator=(multimap&& m)
259 noexcept(
260 allocator_type::propagate_on_container_move_assignment::value &&
261 is_nothrow_move_assignable<allocator_type>::value &&
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000262 is_nothrow_move_assignable<key_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263 multimap& operator=(initializer_list<value_type> il);
264
265 // iterators:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000266 iterator begin() noexcept;
267 const_iterator begin() const noexcept;
268 iterator end() noexcept;
269 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000271 reverse_iterator rbegin() noexcept;
272 const_reverse_iterator rbegin() const noexcept;
273 reverse_iterator rend() noexcept;
274 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000275
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000276 const_iterator cbegin() const noexcept;
277 const_iterator cend() const noexcept;
278 const_reverse_iterator crbegin() const noexcept;
279 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280
281 // capacity:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000282 bool empty() const noexcept;
283 size_type size() const noexcept;
284 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285
286 // modifiers:
287 template <class... Args>
288 iterator emplace(Args&&... args);
289 template <class... Args>
290 iterator emplace_hint(const_iterator position, Args&&... args);
291 iterator insert(const value_type& v);
292 template <class P>
293 iterator insert(P&& p);
294 iterator insert(const_iterator position, const value_type& v);
295 template <class P>
296 iterator insert(const_iterator position, P&& p);
297 template <class InputIterator>
298 void insert(InputIterator first, InputIterator last);
299 void insert(initializer_list<value_type> il);
300
301 iterator erase(const_iterator position);
302 size_type erase(const key_type& k);
303 iterator erase(const_iterator first, const_iterator last);
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000304 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305
Howard Hinnant7686add2011-06-04 14:31:57 +0000306 void swap(multimap& m)
307 noexcept(
308 __is_nothrow_swappable<key_compare>::value &&
309 (!allocator_type::propagate_on_container_swap::value ||
310 __is_nothrow_swappable<allocator_type>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312 // observers:
Howard Hinnantb2e2a8f2011-06-04 15:22:34 +0000313 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314 key_compare key_comp() const;
315 value_compare value_comp() const;
316
317 // map operations:
318 iterator find(const key_type& k);
319 const_iterator find(const key_type& k) const;
320 size_type count(const key_type& k) const;
321 iterator lower_bound(const key_type& k);
322 const_iterator lower_bound(const key_type& k) const;
323 iterator upper_bound(const key_type& k);
324 const_iterator upper_bound(const key_type& k) const;
325 pair<iterator,iterator> equal_range(const key_type& k);
326 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
327};
328
329template <class Key, class T, class Compare, class Allocator>
330bool
331operator==(const multimap<Key, T, Compare, Allocator>& x,
332 const multimap<Key, T, Compare, Allocator>& y);
333
334template <class Key, class T, class Compare, class Allocator>
335bool
336operator< (const multimap<Key, T, Compare, Allocator>& x,
337 const multimap<Key, T, Compare, Allocator>& y);
338
339template <class Key, class T, class Compare, class Allocator>
340bool
341operator!=(const multimap<Key, T, Compare, Allocator>& x,
342 const multimap<Key, T, Compare, Allocator>& y);
343
344template <class Key, class T, class Compare, class Allocator>
345bool
346operator> (const multimap<Key, T, Compare, Allocator>& x,
347 const multimap<Key, T, Compare, Allocator>& y);
348
349template <class Key, class T, class Compare, class Allocator>
350bool
351operator>=(const multimap<Key, T, Compare, Allocator>& x,
352 const multimap<Key, T, Compare, Allocator>& y);
353
354template <class Key, class T, class Compare, class Allocator>
355bool
356operator<=(const multimap<Key, T, Compare, Allocator>& x,
357 const multimap<Key, T, Compare, Allocator>& y);
358
359// specialized algorithms:
360template <class Key, class T, class Compare, class Allocator>
361void
362swap(multimap<Key, T, Compare, Allocator>& x,
Howard Hinnant7686add2011-06-04 14:31:57 +0000363 multimap<Key, T, Compare, Allocator>& y)
364 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000365
366} // std
367
368*/
369
370#include <__config>
371#include <__tree>
372#include <iterator>
373#include <memory>
374#include <utility>
375#include <functional>
376#include <initializer_list>
377
378#pragma GCC system_header
379
380_LIBCPP_BEGIN_NAMESPACE_STD
381
382template <class _Key, class _Tp, class _Compare, bool = is_empty<_Compare>::value>
383class __map_value_compare
384 : private _Compare
385{
Howard Hinnant93c382b2011-01-04 19:21:05 +0000386 typedef pair<typename std::remove_const<_Key>::type, _Tp> _P;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 typedef pair<const _Key, _Tp> _CP;
388public:
Howard Hinnant82894812010-09-22 16:48:34 +0000389 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000390 __map_value_compare()
391 _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
392 : _Compare() {}
Howard Hinnant82894812010-09-22 16:48:34 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000394 __map_value_compare(_Compare c)
395 _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
396 : _Compare(c) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000397 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000398 const _Compare& key_comp() const _NOEXCEPT {return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000399 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400 bool operator()(const _CP& __x, const _CP& __y) const
401 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 bool operator()(const _CP& __x, const _P& __y) const
404 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406 bool operator()(const _CP& __x, const _Key& __y) const
407 {return static_cast<const _Compare&>(*this)(__x.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 bool operator()(const _P& __x, const _CP& __y) const
410 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 bool operator()(const _P& __x, const _P& __y) const
413 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000414 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000415 bool operator()(const _P& __x, const _Key& __y) const
416 {return static_cast<const _Compare&>(*this)(__x.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 bool operator()(const _Key& __x, const _CP& __y) const
419 {return static_cast<const _Compare&>(*this)(__x, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 bool operator()(const _Key& __x, const _P& __y) const
422 {return static_cast<const _Compare&>(*this)(__x, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 bool operator()(const _Key& __x, const _Key& __y) const
425 {return static_cast<const _Compare&>(*this)(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426};
427
428template <class _Key, class _Tp, class _Compare>
429class __map_value_compare<_Key, _Tp, _Compare, false>
430{
431 _Compare comp;
432
Howard Hinnant93c382b2011-01-04 19:21:05 +0000433 typedef pair<typename std::remove_const<_Key>::type, _Tp> _P;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 typedef pair<const _Key, _Tp> _CP;
435
436public:
Howard Hinnant82894812010-09-22 16:48:34 +0000437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000438 __map_value_compare()
439 _NOEXCEPT_(is_nothrow_default_constructible<_Compare>::value)
440 : comp() {}
Howard Hinnant82894812010-09-22 16:48:34 +0000441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000442 __map_value_compare(_Compare c)
443 _NOEXCEPT_(is_nothrow_copy_constructible<_Compare>::value)
444 : comp(c) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000446 const _Compare& key_comp() const _NOEXCEPT {return comp;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447
Howard Hinnant82894812010-09-22 16:48:34 +0000448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 bool operator()(const _CP& __x, const _CP& __y) const
450 {return comp(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452 bool operator()(const _CP& __x, const _P& __y) const
453 {return comp(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000454 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000455 bool operator()(const _CP& __x, const _Key& __y) const
456 {return comp(__x.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000458 bool operator()(const _P& __x, const _CP& __y) const
459 {return comp(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461 bool operator()(const _P& __x, const _P& __y) const
462 {return comp(__x.first, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000463 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000464 bool operator()(const _P& __x, const _Key& __y) const
465 {return comp(__x.first, __y);}
Howard Hinnant82894812010-09-22 16:48:34 +0000466 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 bool operator()(const _Key& __x, const _CP& __y) const
468 {return comp(__x, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 bool operator()(const _Key& __x, const _P& __y) const
471 {return comp(__x, __y.first);}
Howard Hinnant82894812010-09-22 16:48:34 +0000472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473 bool operator()(const _Key& __x, const _Key& __y) const
474 {return comp(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000475};
476
477template <class _Allocator>
478class __map_node_destructor
479{
480 typedef _Allocator allocator_type;
481 typedef allocator_traits<allocator_type> __alloc_traits;
482 typedef typename __alloc_traits::value_type::value_type value_type;
483public:
484 typedef typename __alloc_traits::pointer pointer;
485private:
486 typedef typename value_type::first_type first_type;
487 typedef typename value_type::second_type second_type;
488
489 allocator_type& __na_;
490
491 __map_node_destructor& operator=(const __map_node_destructor&);
492
493public:
494 bool __first_constructed;
495 bool __second_constructed;
496
Howard Hinnant82894812010-09-22 16:48:34 +0000497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000498 explicit __map_node_destructor(allocator_type& __na) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 : __na_(__na),
500 __first_constructed(false),
501 __second_constructed(false)
502 {}
503
Howard Hinnant73d21a42010-09-04 23:28:19 +0000504#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000506 __map_node_destructor(__tree_node_destructor<allocator_type>&& __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000507 : __na_(__x.__na_),
508 __first_constructed(__x.__value_constructed),
509 __second_constructed(__x.__value_constructed)
510 {
511 __x.__value_constructed = false;
512 }
Howard Hinnantbfd55302010-09-04 23:46:48 +0000513#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514
Howard Hinnant82894812010-09-22 16:48:34 +0000515 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000516 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 {
518 if (__second_constructed)
Howard Hinnant2529d022011-02-02 17:36:20 +0000519 __alloc_traits::destroy(__na_, _STD::addressof(__p->__value_.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 if (__first_constructed)
Howard Hinnant2529d022011-02-02 17:36:20 +0000521 __alloc_traits::destroy(__na_, _STD::addressof(__p->__value_.first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 if (__p)
523 __alloc_traits::deallocate(__na_, __p, 1);
524 }
525};
526
527template <class, class, class, class> class map;
528template <class, class, class, class> class multimap;
529template <class> class __map_const_iterator;
530
531template <class _TreeIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000532class _LIBCPP_VISIBLE __map_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533{
534 _TreeIterator __i_;
535
536 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
Howard Hinnantdf85e572011-02-27 18:02:02 +0000537 typedef const typename _TreeIterator::value_type::first_type __key_type;
538 typedef typename _TreeIterator::value_type::second_type __mapped_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000539public:
540 typedef bidirectional_iterator_tag iterator_category;
Howard Hinnantdf85e572011-02-27 18:02:02 +0000541 typedef pair<__key_type, __mapped_type> value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542 typedef typename _TreeIterator::difference_type difference_type;
543 typedef value_type& reference;
544 typedef typename __pointer_traits::template
545#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
546 rebind<value_type>
547#else
548 rebind<value_type>::other
549#endif
550 pointer;
551
Howard Hinnant82894812010-09-22 16:48:34 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000553 __map_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
Howard Hinnant82894812010-09-22 16:48:34 +0000555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000556 __map_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557
Howard Hinnant82894812010-09-22 16:48:34 +0000558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000559 reference operator*() const {return *operator->();}
Howard Hinnant82894812010-09-22 16:48:34 +0000560 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561 pointer operator->() const {return (pointer)__i_.operator->();}
562
Howard Hinnant82894812010-09-22 16:48:34 +0000563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564 __map_iterator& operator++() {++__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566 __map_iterator operator++(int)
567 {
568 __map_iterator __t(*this);
569 ++(*this);
570 return __t;
571 }
572
Howard Hinnant82894812010-09-22 16:48:34 +0000573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574 __map_iterator& operator--() {--__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576 __map_iterator operator--(int)
577 {
578 __map_iterator __t(*this);
579 --(*this);
580 return __t;
581 }
582
Howard Hinnant82894812010-09-22 16:48:34 +0000583 friend _LIBCPP_INLINE_VISIBILITY
584 bool operator==(const __map_iterator& __x, const __map_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 {return __x.__i_ == __y.__i_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000586 friend
587 _LIBCPP_INLINE_VISIBILITY
588 bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000589 {return __x.__i_ != __y.__i_;}
590
Howard Hinnant82894812010-09-22 16:48:34 +0000591 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
592 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
593 template <class> friend class _LIBCPP_VISIBLE __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594};
595
596template <class _TreeIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000597class _LIBCPP_VISIBLE __map_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598{
599 _TreeIterator __i_;
600
601 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
Howard Hinnantdf85e572011-02-27 18:02:02 +0000602 typedef const typename _TreeIterator::value_type::first_type __key_type;
603 typedef typename _TreeIterator::value_type::second_type __mapped_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604public:
605 typedef bidirectional_iterator_tag iterator_category;
Howard Hinnantdf85e572011-02-27 18:02:02 +0000606 typedef pair<__key_type, __mapped_type> value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 typedef typename _TreeIterator::difference_type difference_type;
608 typedef const value_type& reference;
609 typedef typename __pointer_traits::template
610#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant9dbeff92011-04-11 02:18:41 +0000611 rebind<const value_type>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000612#else
Howard Hinnant9dbeff92011-04-11 02:18:41 +0000613 rebind<const value_type>::other
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614#endif
615 pointer;
616
Howard Hinnant82894812010-09-22 16:48:34 +0000617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000618 __map_const_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000619
Howard Hinnant82894812010-09-22 16:48:34 +0000620 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000621 __map_const_iterator(_TreeIterator __i) _NOEXCEPT : __i_(__i) {}
Howard Hinnant82894812010-09-22 16:48:34 +0000622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 __map_const_iterator(
624 __map_iterator<typename _TreeIterator::__non_const_iterator> __i)
Howard Hinnant7686add2011-06-04 14:31:57 +0000625 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000626 : __i_(__i.__i_) {}
627
Howard Hinnant82894812010-09-22 16:48:34 +0000628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629 reference operator*() const {return *operator->();}
Howard Hinnant82894812010-09-22 16:48:34 +0000630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 pointer operator->() const {return (pointer)__i_.operator->();}
632
Howard Hinnant82894812010-09-22 16:48:34 +0000633 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 __map_const_iterator& operator++() {++__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 __map_const_iterator operator++(int)
637 {
638 __map_const_iterator __t(*this);
639 ++(*this);
640 return __t;
641 }
642
Howard Hinnant82894812010-09-22 16:48:34 +0000643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 __map_const_iterator& operator--() {--__i_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646 __map_const_iterator operator--(int)
647 {
648 __map_const_iterator __t(*this);
649 --(*this);
650 return __t;
651 }
652
Howard Hinnant82894812010-09-22 16:48:34 +0000653 friend _LIBCPP_INLINE_VISIBILITY
654 bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 {return __x.__i_ == __y.__i_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000656 friend _LIBCPP_INLINE_VISIBILITY
657 bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 {return __x.__i_ != __y.__i_;}
659
Howard Hinnant82894812010-09-22 16:48:34 +0000660 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
661 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
662 template <class, class, class> friend class _LIBCPP_VISIBLE __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663};
664
665template <class _Key, class _Tp, class _Compare = less<_Key>,
666 class _Allocator = allocator<pair<const _Key, _Tp> > >
Howard Hinnant82894812010-09-22 16:48:34 +0000667class _LIBCPP_VISIBLE map
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668{
669public:
670 // types:
671 typedef _Key key_type;
672 typedef _Tp mapped_type;
673 typedef pair<const key_type, mapped_type> value_type;
674 typedef _Compare key_compare;
675 typedef _Allocator allocator_type;
676 typedef value_type& reference;
677 typedef const value_type& const_reference;
678
Howard Hinnant82894812010-09-22 16:48:34 +0000679 class _LIBCPP_VISIBLE value_compare
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 : public binary_function<value_type, value_type, bool>
681 {
682 friend class map;
683 protected:
684 key_compare comp;
685
Howard Hinnant82894812010-09-22 16:48:34 +0000686 _LIBCPP_INLINE_VISIBILITY value_compare(key_compare c) : comp(c) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 public:
Howard Hinnant82894812010-09-22 16:48:34 +0000688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000689 bool operator()(const value_type& __x, const value_type& __y) const
690 {return comp(__x.first, __y.first);}
691 };
692
693private:
694 typedef pair<key_type, mapped_type> __value_type;
695 typedef __map_value_compare<key_type, mapped_type, key_compare> __vc;
696 typedef typename allocator_traits<allocator_type>::template
697#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
698 rebind_alloc<__value_type>
699#else
700 rebind_alloc<__value_type>::other
701#endif
702 __allocator_type;
703 typedef __tree<__value_type, __vc, __allocator_type> __base;
704 typedef typename __base::__node_traits __node_traits;
705 typedef allocator_traits<allocator_type> __alloc_traits;
706
707 __base __tree_;
708
709public:
710 typedef typename __alloc_traits::pointer pointer;
711 typedef typename __alloc_traits::const_pointer const_pointer;
712 typedef typename __alloc_traits::size_type size_type;
713 typedef typename __alloc_traits::difference_type difference_type;
714 typedef __map_iterator<typename __base::iterator> iterator;
715 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
716 typedef _STD::reverse_iterator<iterator> reverse_iterator;
717 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
718
Howard Hinnant82894812010-09-22 16:48:34 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 explicit map(const key_compare& __comp = key_compare())
Howard Hinnant7686add2011-06-04 14:31:57 +0000721 _NOEXCEPT_(
722 is_nothrow_default_constructible<allocator_type>::value &&
723 is_nothrow_default_constructible<key_compare>::value &&
724 is_nothrow_copy_constructible<key_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725 : __tree_(__vc(__comp)) {}
726
Howard Hinnant82894812010-09-22 16:48:34 +0000727 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 explicit map(const key_compare& __comp, const allocator_type& __a)
729 : __tree_(__vc(__comp), __a) {}
730
731 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733 map(_InputIterator __f, _InputIterator __l,
734 const key_compare& __comp = key_compare())
735 : __tree_(__vc(__comp))
736 {
737 insert(__f, __l);
738 }
739
740 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 map(_InputIterator __f, _InputIterator __l,
743 const key_compare& __comp, const allocator_type& __a)
744 : __tree_(__vc(__comp), __a)
745 {
746 insert(__f, __l);
747 }
748
Howard Hinnant82894812010-09-22 16:48:34 +0000749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 map(const map& __m)
751 : __tree_(__m.__tree_)
752 {
753 insert(__m.begin(), __m.end());
754 }
755
Howard Hinnant73d21a42010-09-04 23:28:19 +0000756#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757
Howard Hinnant82894812010-09-22 16:48:34 +0000758 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 map(map&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +0000760 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 : __tree_(_STD::move(__m.__tree_))
762 {
763 }
764
765 map(map&& __m, const allocator_type& __a);
766
Howard Hinnant82894812010-09-22 16:48:34 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768 map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
769 : __tree_(__vc(__comp))
770 {
771 insert(__il.begin(), __il.end());
772 }
773
Howard Hinnant82894812010-09-22 16:48:34 +0000774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775 map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
776 : __tree_(__vc(__comp), __a)
777 {
778 insert(__il.begin(), __il.end());
779 }
780
Howard Hinnant82894812010-09-22 16:48:34 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000782 map& operator=(map&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +0000783 _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784 {
785 __tree_ = _STD::move(__m.__tree_);
786 return *this;
787 }
788
Howard Hinnant82894812010-09-22 16:48:34 +0000789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000790 map& operator=(initializer_list<value_type> __il)
791 {
792 __tree_.__assign_unique(__il.begin(), __il.end());
793 return *this;
794 }
795
Howard Hinnantbfd55302010-09-04 23:46:48 +0000796#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797
Howard Hinnant82894812010-09-22 16:48:34 +0000798 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000799 explicit map(const allocator_type& __a)
800 : __tree_(__a)
801 {
802 }
803
Howard Hinnant82894812010-09-22 16:48:34 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805 map(const map& __m, const allocator_type& __a)
806 : __tree_(__m.__tree_.value_comp(), __a)
807 {
808 insert(__m.begin(), __m.end());
809 }
810
Howard Hinnant82894812010-09-22 16:48:34 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000812 iterator begin() _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000814 const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000816 iterator end() _NOEXCEPT {return __tree_.end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000818 const_iterator end() const _NOEXCEPT {return __tree_.end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
Howard Hinnant82894812010-09-22 16:48:34 +0000820 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000821 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000823 const_reverse_iterator rbegin() const _NOEXCEPT
824 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000826 reverse_iterator rend() _NOEXCEPT
827 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000829 const_reverse_iterator rend() const _NOEXCEPT
830 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831
Howard Hinnant82894812010-09-22 16:48:34 +0000832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000833 const_iterator cbegin() const _NOEXCEPT {return begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000835 const_iterator cend() const _NOEXCEPT {return end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000837 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000839 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840
Howard Hinnant82894812010-09-22 16:48:34 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000842 bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000844 size_type size() const _NOEXCEPT {return __tree_.size();}
Howard Hinnant82894812010-09-22 16:48:34 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000846 size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847
848 mapped_type& operator[](const key_type& __k);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000849#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850 mapped_type& operator[](key_type&& __k);
851#endif
852
853 mapped_type& at(const key_type& __k);
854 const mapped_type& at(const key_type& __k) const;
855
Howard Hinnant82894812010-09-22 16:48:34 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000857 allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
Howard Hinnant82894812010-09-22 16:48:34 +0000858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
Howard Hinnant82894812010-09-22 16:48:34 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861 value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
862
Howard Hinnant73d21a42010-09-04 23:28:19 +0000863#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864
Howard Hinnant82894812010-09-22 16:48:34 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866 pair<iterator, bool>
867 emplace() {return __tree_.__emplace_unique();}
868
869 template <class _A0,
870 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872 pair<iterator, bool>
873 emplace(_A0&& __a0)
874 {return __tree_.__emplace_unique(_STD::forward<_A0>(__a0));}
875
Howard Hinnant73d21a42010-09-04 23:28:19 +0000876#ifndef _LIBCPP_HAS_NO_VARIADICS
877
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 template <class _A0, class ..._Args,
879 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
880 pair<iterator, bool>
881 emplace(_A0&& __a0, _Args&& ...__args);
882
Howard Hinnant73d21a42010-09-04 23:28:19 +0000883#endif // _LIBCPP_HAS_NO_VARIADICS
884
Howard Hinnant82894812010-09-22 16:48:34 +0000885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 iterator
887 emplace_hint(const_iterator __p)
888 {return __tree_.__emplace_hint_unique(__p.__i_);}
889
890 template <class _A0,
891 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000893 iterator
894 emplace_hint(const_iterator __p, _A0&& __a0)
895 {return __tree_.__emplace_hint_unique(__p.__i_, _STD::forward<_A0>(__a0));}
896
Howard Hinnant73d21a42010-09-04 23:28:19 +0000897#ifndef _LIBCPP_HAS_NO_VARIADICS
898
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899 template <class _A0, class ..._Args,
900 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
901 iterator
902 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
903
Howard Hinnant73d21a42010-09-04 23:28:19 +0000904#endif // _LIBCPP_HAS_NO_VARIADICS
905
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906 template <class _P,
907 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 pair<iterator, bool> insert(_P&& __p)
910 {return __tree_.__insert_unique(_STD::forward<_P>(__p));}
911
912 template <class _P,
913 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +0000914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 iterator insert(const_iterator __pos, _P&& __p)
916 {return __tree_.__insert_unique(__pos.__i_, _STD::forward<_P>(__p));}
917
Howard Hinnant73d21a42010-09-04 23:28:19 +0000918#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919
Howard Hinnant82894812010-09-22 16:48:34 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 pair<iterator, bool>
922 insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
923
Howard Hinnant82894812010-09-22 16:48:34 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 iterator
926 insert(const_iterator __p, const value_type& __v)
927 {return __tree_.__insert_unique(__p.__i_, __v);}
928
929 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +0000930 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000931 void insert(_InputIterator __f, _InputIterator __l)
932 {
933 for (const_iterator __e = cend(); __f != __l; ++__f)
934 insert(__e.__i_, *__f);
935 }
936
Howard Hinnant82894812010-09-22 16:48:34 +0000937 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000938 void insert(initializer_list<value_type> __il)
939 {insert(__il.begin(), __il.end());}
940
Howard Hinnant82894812010-09-22 16:48:34 +0000941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 size_type erase(const key_type& __k)
945 {return __tree_.__erase_unique(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000946 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 iterator erase(const_iterator __f, const_iterator __l)
948 {return __tree_.erase(__f.__i_, __l.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000950 void clear() _NOEXCEPT {__tree_.clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951
Howard Hinnant82894812010-09-22 16:48:34 +0000952 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000953 void swap(map& __m)
954 _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
955 {__tree_.swap(__m.__tree_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956
Howard Hinnant82894812010-09-22 16:48:34 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 iterator find(const key_type& __k) {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000959 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000961 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 size_type count(const key_type& __k) const
963 {return __tree_.__count_unique(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 iterator lower_bound(const key_type& __k)
966 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 const_iterator lower_bound(const key_type& __k) const
969 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 iterator upper_bound(const key_type& __k)
972 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 const_iterator upper_bound(const key_type& __k) const
975 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000976 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977 pair<iterator,iterator> equal_range(const key_type& __k)
978 {return __tree_.__equal_range_unique(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000980 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
981 {return __tree_.__equal_range_unique(__k);}
982
983private:
984 typedef typename __base::__node __node;
985 typedef typename __base::__node_allocator __node_allocator;
986 typedef typename __base::__node_pointer __node_pointer;
987 typedef typename __base::__node_const_pointer __node_const_pointer;
988 typedef typename __base::__node_base_pointer __node_base_pointer;
989 typedef typename __base::__node_base_const_pointer __node_base_const_pointer;
990 typedef __map_node_destructor<__node_allocator> _D;
991 typedef unique_ptr<__node, _D> __node_holder;
992
Howard Hinnant73d21a42010-09-04 23:28:19 +0000993#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 __node_holder __construct_node();
995 template <class _A0,
996 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
997 __node_holder __construct_node(_A0&& __a0);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000998#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000999 template <class _A0, class ..._Args,
1000 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1001 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001002#endif // _LIBCPP_HAS_NO_VARIADICS
1003#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 __node_holder __construct_node(const key_type& __k);
1005#endif
1006
1007 __node_base_pointer&
1008 __find_equal_key(__node_base_pointer& __parent, const key_type& __k);
1009 __node_base_pointer&
1010 __find_equal_key(const_iterator __hint,
1011 __node_base_pointer& __parent, const key_type& __k);
1012 __node_base_const_pointer
1013 __find_equal_key(__node_base_const_pointer& __parent, const key_type& __k) const;
1014};
1015
1016// Find place to insert if __k doesn't exist
1017// Set __parent to parent of null leaf
1018// Return reference to null leaf
1019// If __k exists, set parent to node of __k and return reference to node of __k
1020template <class _Key, class _Tp, class _Compare, class _Allocator>
1021typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
1022map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_pointer& __parent,
1023 const key_type& __k)
1024{
1025 __node_pointer __nd = __tree_.__root();
1026 if (__nd != nullptr)
1027 {
1028 while (true)
1029 {
1030 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
1031 {
1032 if (__nd->__left_ != nullptr)
1033 __nd = static_cast<__node_pointer>(__nd->__left_);
1034 else
1035 {
1036 __parent = __nd;
1037 return __parent->__left_;
1038 }
1039 }
1040 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
1041 {
1042 if (__nd->__right_ != nullptr)
1043 __nd = static_cast<__node_pointer>(__nd->__right_);
1044 else
1045 {
1046 __parent = __nd;
1047 return __parent->__right_;
1048 }
1049 }
1050 else
1051 {
1052 __parent = __nd;
1053 return __parent;
1054 }
1055 }
1056 }
1057 __parent = __tree_.__end_node();
1058 return __parent->__left_;
1059}
1060
1061// Find place to insert if __k doesn't exist
1062// First check prior to __hint.
1063// Next check after __hint.
1064// Next do O(log N) search.
1065// Set __parent to parent of null leaf
1066// Return reference to null leaf
1067// If __k exists, set parent to node of __k and return reference to node of __k
1068template <class _Key, class _Tp, class _Compare, class _Allocator>
1069typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
1070map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(const_iterator __hint,
1071 __node_base_pointer& __parent,
1072 const key_type& __k)
1073{
1074 if (__hint == end() || __tree_.value_comp().key_comp()(__k, __hint->first)) // check before
1075 {
1076 // __k < *__hint
1077 const_iterator __prior = __hint;
1078 if (__prior == begin() || __tree_.value_comp().key_comp()((--__prior)->first, __k))
1079 {
1080 // *prev(__hint) < __k < *__hint
1081 if (__hint.__ptr_->__left_ == nullptr)
1082 {
1083 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1084 return __parent->__left_;
1085 }
1086 else
1087 {
1088 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1089 return __parent->__right_;
1090 }
1091 }
1092 // __k <= *prev(__hint)
1093 return __find_equal_key(__parent, __k);
1094 }
1095 else if (__tree_.value_comp().key_comp()(__hint->first, __k)) // check after
1096 {
1097 // *__hint < __k
Howard Hinnant6cf5d8c2011-02-14 19:12:38 +00001098 const_iterator __next = _STD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 if (__next == end() || __tree_.value_comp().key_comp()(__k, __next->first))
1100 {
1101 // *__hint < __k < *next(__hint)
1102 if (__hint.__ptr_->__right_ == nullptr)
1103 {
1104 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1105 return __parent->__right_;
1106 }
1107 else
1108 {
1109 __parent = const_cast<__node_pointer&>(__next.__ptr_);
1110 return __parent->__left_;
1111 }
1112 }
1113 // *next(__hint) <= __k
1114 return __find_equal_key(__parent, __k);
1115 }
1116 // else __k == *__hint
1117 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1118 return __parent;
1119}
1120
1121// Find __k
1122// Set __parent to parent of null leaf and
1123// return reference to null leaf iv __k does not exist.
1124// If __k exists, set parent to node of __k and return reference to node of __k
1125template <class _Key, class _Tp, class _Compare, class _Allocator>
1126typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_const_pointer
1127map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_const_pointer& __parent,
1128 const key_type& __k) const
1129{
1130 __node_const_pointer __nd = __tree_.__root();
1131 if (__nd != nullptr)
1132 {
1133 while (true)
1134 {
1135 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
1136 {
1137 if (__nd->__left_ != nullptr)
1138 __nd = static_cast<__node_pointer>(__nd->__left_);
1139 else
1140 {
1141 __parent = __nd;
1142 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1143 }
1144 }
1145 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
1146 {
1147 if (__nd->__right_ != nullptr)
1148 __nd = static_cast<__node_pointer>(__nd->__right_);
1149 else
1150 {
1151 __parent = __nd;
1152 return const_cast<const __node_base_const_pointer&>(__parent->__right_);
1153 }
1154 }
1155 else
1156 {
1157 __parent = __nd;
1158 return __parent;
1159 }
1160 }
1161 }
1162 __parent = __tree_.__end_node();
1163 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1164}
1165
Howard Hinnant73d21a42010-09-04 23:28:19 +00001166#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167
1168template <class _Key, class _Tp, class _Compare, class _Allocator>
1169map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
1170 : __tree_(_STD::move(__m.__tree_), __a)
1171{
1172 if (__a != __m.get_allocator())
1173 {
1174 const_iterator __e = cend();
1175 while (!__m.empty())
1176 __tree_.__insert_unique(__e.__i_,
1177 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1178 }
1179}
1180
1181template <class _Key, class _Tp, class _Compare, class _Allocator>
1182typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1183map<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1184{
1185 __node_allocator& __na = __tree_.__node_alloc();
1186 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001187 __node_traits::construct(__na, _STD::addressof(__h->__value_.first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 __h.get_deleter().__first_constructed = true;
Howard Hinnant2529d022011-02-02 17:36:20 +00001189 __node_traits::construct(__na, _STD::addressof(__h->__value_.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001190 __h.get_deleter().__second_constructed = true;
1191 return __h;
1192}
1193
1194template <class _Key, class _Tp, class _Compare, class _Allocator>
1195template <class _A0,
1196 class>
1197typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1198map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1199{
1200 __node_allocator& __na = __tree_.__node_alloc();
1201 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001202 __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::forward<_A0>(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203 __h.get_deleter().__first_constructed = true;
1204 __h.get_deleter().__second_constructed = true;
1205 return __h;
1206}
1207
Howard Hinnant73d21a42010-09-04 23:28:19 +00001208#ifndef _LIBCPP_HAS_NO_VARIADICS
1209
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210template <class _Key, class _Tp, class _Compare, class _Allocator>
1211template <class _A0, class ..._Args,
1212 class>
1213typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1214map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1215{
1216 __node_allocator& __na = __tree_.__node_alloc();
1217 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001218 __node_traits::construct(__na, _STD::addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219 __h.get_deleter().__first_constructed = true;
Howard Hinnant2529d022011-02-02 17:36:20 +00001220 __node_traits::construct(__na, _STD::addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 __h.get_deleter().__second_constructed = true;
1222 return __h;
1223}
1224
Howard Hinnant73d21a42010-09-04 23:28:19 +00001225#endif // _LIBCPP_HAS_NO_VARIADICS
1226
1227#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228
1229template <class _Key, class _Tp, class _Compare, class _Allocator>
1230typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1231map<_Key, _Tp, _Compare, _Allocator>::__construct_node(const key_type& __k)
1232{
1233 __node_allocator& __na = __tree_.__node_alloc();
1234 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001235 __node_traits::construct(__na, _STD::addressof(__h->__value_.first), __k);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236 __h.get_deleter().__first_constructed = true;
Howard Hinnant2529d022011-02-02 17:36:20 +00001237 __node_traits::construct(__na, _STD::addressof(__h->__value_.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238 __h.get_deleter().__second_constructed = true;
1239 return _STD::move(__h);
1240}
1241
Howard Hinnant73d21a42010-09-04 23:28:19 +00001242#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243
1244template <class _Key, class _Tp, class _Compare, class _Allocator>
1245_Tp&
1246map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1247{
1248 __node_base_pointer __parent;
1249 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1250 __node_pointer __r = static_cast<__node_pointer>(__child);
1251 if (__child == nullptr)
1252 {
1253 __node_holder __h = __construct_node(__k);
1254 __tree_.__insert_node_at(__parent, __child, __h.get());
1255 __r = __h.release();
1256 }
1257 return __r->__value_.second;
1258}
1259
Howard Hinnant73d21a42010-09-04 23:28:19 +00001260#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261
1262template <class _Key, class _Tp, class _Compare, class _Allocator>
1263_Tp&
1264map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1265{
1266 __node_base_pointer __parent;
1267 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1268 __node_pointer __r = static_cast<__node_pointer>(__child);
1269 if (__child == nullptr)
1270 {
1271 __node_holder __h = __construct_node(_STD::move(__k));
1272 __tree_.__insert_node_at(__parent, __child, __h.get());
1273 __r = __h.release();
1274 }
1275 return __r->__value_.second;
1276}
1277
Howard Hinnant73d21a42010-09-04 23:28:19 +00001278#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279
1280template <class _Key, class _Tp, class _Compare, class _Allocator>
1281_Tp&
1282map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1283{
1284 __node_base_pointer __parent;
1285 __node_base_pointer& __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001286#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 if (__child == nullptr)
1288 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001289#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 return static_cast<__node_pointer>(__child)->__value_.second;
1291}
1292
1293template <class _Key, class _Tp, class _Compare, class _Allocator>
1294const _Tp&
1295map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1296{
1297 __node_base_const_pointer __parent;
1298 __node_base_const_pointer __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001299#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 if (__child == nullptr)
1301 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001302#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303 return static_cast<__node_const_pointer>(__child)->__value_.second;
1304}
1305
Howard Hinnant73d21a42010-09-04 23:28:19 +00001306#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307
1308template <class _Key, class _Tp, class _Compare, class _Allocator>
1309template <class _A0, class ..._Args,
1310 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1311 >
1312pair<typename map<_Key, _Tp, _Compare, _Allocator>::iterator, bool>
1313map<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1314{
1315 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1316 _STD::forward<_Args>(__args)...);
1317 pair<iterator, bool> __r = __tree_.__node_insert_unique(__h.get());
1318 if (__r.second)
1319 __h.release();
1320 return __r;
1321}
1322
1323template <class _Key, class _Tp, class _Compare, class _Allocator>
1324template <class _A0, class ..._Args,
1325 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1326 >
1327typename map<_Key, _Tp, _Compare, _Allocator>::iterator
1328map<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1329 _A0&& __a0, _Args&& ...__args)
1330{
1331 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1332 _STD::forward<_Args>(__args)...);
1333 iterator __r = __tree_.__node_insert_unique(__p.__i_, __h.get());
1334 if (__r.__i_.__ptr_ == __h.get())
1335 __h.release();
1336 return __r;
1337}
1338
Howard Hinnant73d21a42010-09-04 23:28:19 +00001339#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340
1341template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001342inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343bool
1344operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1345 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1346{
1347 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1348}
1349
1350template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001351inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352bool
1353operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1354 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1355{
1356 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1357}
1358
1359template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001360inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361bool
1362operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1363 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1364{
1365 return !(__x == __y);
1366}
1367
1368template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001369inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001370bool
1371operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1372 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1373{
1374 return __y < __x;
1375}
1376
1377template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379bool
1380operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1381 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1382{
1383 return !(__x < __y);
1384}
1385
1386template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001387inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388bool
1389operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1390 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1391{
1392 return !(__y < __x);
1393}
1394
1395template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001396inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397void
1398swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1399 map<_Key, _Tp, _Compare, _Allocator>& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001400 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401{
1402 __x.swap(__y);
1403}
1404
1405template <class _Key, class _Tp, class _Compare = less<_Key>,
1406 class _Allocator = allocator<pair<const _Key, _Tp> > >
Howard Hinnant82894812010-09-22 16:48:34 +00001407class _LIBCPP_VISIBLE multimap
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408{
1409public:
1410 // types:
1411 typedef _Key key_type;
1412 typedef _Tp mapped_type;
1413 typedef pair<const key_type, mapped_type> value_type;
1414 typedef _Compare key_compare;
1415 typedef _Allocator allocator_type;
1416 typedef value_type& reference;
1417 typedef const value_type& const_reference;
1418
Howard Hinnant82894812010-09-22 16:48:34 +00001419 class _LIBCPP_VISIBLE value_compare
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 : public binary_function<value_type, value_type, bool>
1421 {
1422 friend class multimap;
1423 protected:
1424 key_compare comp;
1425
Howard Hinnant82894812010-09-22 16:48:34 +00001426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427 value_compare(key_compare c) : comp(c) {}
1428 public:
Howard Hinnant82894812010-09-22 16:48:34 +00001429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001430 bool operator()(const value_type& __x, const value_type& __y) const
1431 {return comp(__x.first, __y.first);}
1432 };
1433
1434private:
1435 typedef pair<key_type, mapped_type> __value_type;
1436 typedef __map_value_compare<key_type, mapped_type, key_compare> __vc;
1437 typedef typename allocator_traits<allocator_type>::template
1438#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1439 rebind_alloc<__value_type>
1440#else
1441 rebind_alloc<__value_type>::other
1442#endif
1443 __allocator_type;
1444 typedef __tree<__value_type, __vc, __allocator_type> __base;
1445 typedef typename __base::__node_traits __node_traits;
1446 typedef allocator_traits<allocator_type> __alloc_traits;
1447
1448 __base __tree_;
1449
1450public:
1451 typedef typename __alloc_traits::pointer pointer;
1452 typedef typename __alloc_traits::const_pointer const_pointer;
1453 typedef typename __alloc_traits::size_type size_type;
1454 typedef typename __alloc_traits::difference_type difference_type;
1455 typedef __map_iterator<typename __base::iterator> iterator;
1456 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1457 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1458 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1459
Howard Hinnant82894812010-09-22 16:48:34 +00001460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 explicit multimap(const key_compare& __comp = key_compare())
Howard Hinnant7686add2011-06-04 14:31:57 +00001462 _NOEXCEPT_(
1463 is_nothrow_default_constructible<allocator_type>::value &&
1464 is_nothrow_default_constructible<key_compare>::value &&
1465 is_nothrow_copy_constructible<key_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 : __tree_(__vc(__comp)) {}
1467
Howard Hinnant82894812010-09-22 16:48:34 +00001468 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 explicit multimap(const key_compare& __comp, const allocator_type& __a)
1470 : __tree_(__vc(__comp), __a) {}
1471
1472 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 multimap(_InputIterator __f, _InputIterator __l,
1475 const key_compare& __comp = key_compare())
1476 : __tree_(__vc(__comp))
1477 {
1478 insert(__f, __l);
1479 }
1480
1481 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 multimap(_InputIterator __f, _InputIterator __l,
1484 const key_compare& __comp, const allocator_type& __a)
1485 : __tree_(__vc(__comp), __a)
1486 {
1487 insert(__f, __l);
1488 }
1489
Howard Hinnant82894812010-09-22 16:48:34 +00001490 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 multimap(const multimap& __m)
1492 : __tree_(__m.__tree_.value_comp(),
1493 __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1494 {
1495 insert(__m.begin(), __m.end());
1496 }
1497
Howard Hinnant73d21a42010-09-04 23:28:19 +00001498#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499
Howard Hinnant82894812010-09-22 16:48:34 +00001500 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501 multimap(multimap&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +00001502 _NOEXCEPT_(is_nothrow_move_constructible<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 : __tree_(_STD::move(__m.__tree_))
1504 {
1505 }
1506
1507 multimap(multimap&& __m, const allocator_type& __a);
1508
Howard Hinnant82894812010-09-22 16:48:34 +00001509 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510 multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1511 : __tree_(__vc(__comp))
1512 {
1513 insert(__il.begin(), __il.end());
1514 }
1515
Howard Hinnant82894812010-09-22 16:48:34 +00001516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1518 : __tree_(__vc(__comp), __a)
1519 {
1520 insert(__il.begin(), __il.end());
1521 }
1522
Howard Hinnant82894812010-09-22 16:48:34 +00001523 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524 multimap& operator=(multimap&& __m)
Howard Hinnant7686add2011-06-04 14:31:57 +00001525 _NOEXCEPT_(is_nothrow_move_assignable<__base>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 {
1527 __tree_ = _STD::move(__m.__tree_);
1528 return *this;
1529 }
1530
Howard Hinnant82894812010-09-22 16:48:34 +00001531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 multimap& operator=(initializer_list<value_type> __il)
1533 {
1534 __tree_.__assign_multi(__il.begin(), __il.end());
1535 return *this;
1536 }
Howard Hinnantbfd55302010-09-04 23:46:48 +00001537#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538
Howard Hinnant82894812010-09-22 16:48:34 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001540 explicit multimap(const allocator_type& __a)
1541 : __tree_(__a)
1542 {
1543 }
1544
Howard Hinnant82894812010-09-22 16:48:34 +00001545 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 multimap(const multimap& __m, const allocator_type& __a)
1547 : __tree_(__m.__tree_.value_comp(), __a)
1548 {
1549 insert(__m.begin(), __m.end());
1550 }
1551
Howard Hinnant82894812010-09-22 16:48:34 +00001552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001553 iterator begin() _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001555 const_iterator begin() const _NOEXCEPT {return __tree_.begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001557 iterator end() _NOEXCEPT {return __tree_.end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001559 const_iterator end() const _NOEXCEPT {return __tree_.end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560
Howard Hinnant82894812010-09-22 16:48:34 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001562 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001564 const_reverse_iterator rbegin() const _NOEXCEPT
1565 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +00001566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001567 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +00001568 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001569 const_reverse_iterator rend() const _NOEXCEPT
1570 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571
Howard Hinnant82894812010-09-22 16:48:34 +00001572 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001573 const_iterator cbegin() const _NOEXCEPT {return begin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001575 const_iterator cend() const _NOEXCEPT {return end();}
Howard Hinnant82894812010-09-22 16:48:34 +00001576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001577 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
Howard Hinnant82894812010-09-22 16:48:34 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001579 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580
Howard Hinnant82894812010-09-22 16:48:34 +00001581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001582 bool empty() const _NOEXCEPT {return __tree_.size() == 0;}
Howard Hinnant82894812010-09-22 16:48:34 +00001583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001584 size_type size() const _NOEXCEPT {return __tree_.size();}
Howard Hinnant82894812010-09-22 16:48:34 +00001585 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001586 size_type max_size() const _NOEXCEPT {return __tree_.max_size();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587
Howard Hinnant82894812010-09-22 16:48:34 +00001588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001589 allocator_type get_allocator() const _NOEXCEPT {return __tree_.__alloc();}
Howard Hinnant82894812010-09-22 16:48:34 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001591 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
Howard Hinnant82894812010-09-22 16:48:34 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001593 value_compare value_comp() const
1594 {return value_compare(__tree_.value_comp().key_comp());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595
Howard Hinnant73d21a42010-09-04 23:28:19 +00001596#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597
Howard Hinnant82894812010-09-22 16:48:34 +00001598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001599 iterator emplace() {return __tree_.__emplace_multi();}
1600
1601 template <class _A0,
1602 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604 iterator
1605 emplace(_A0&& __a0)
1606 {return __tree_.__emplace_multi(_STD::forward<_A0>(__a0));}
1607
Howard Hinnant73d21a42010-09-04 23:28:19 +00001608#ifndef _LIBCPP_HAS_NO_VARIADICS
1609
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 template <class _A0, class ..._Args,
1611 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1612 iterator
1613 emplace(_A0&& __a0, _Args&& ...__args);
1614
Howard Hinnant73d21a42010-09-04 23:28:19 +00001615#endif // _LIBCPP_HAS_NO_VARIADICS
1616
Howard Hinnant82894812010-09-22 16:48:34 +00001617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618 iterator emplace_hint(const_iterator __p)
1619 {return __tree_.__emplace_hint_multi(__p.__i_);}
1620
1621 template <class _A0,
1622 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 iterator
1625 emplace_hint(const_iterator __p, _A0&& __a0)
1626 {return __tree_.__emplace_hint_multi(__p.__i_, _STD::forward<_A0>(__a0));}
1627
Howard Hinnant73d21a42010-09-04 23:28:19 +00001628#ifndef _LIBCPP_HAS_NO_VARIADICS
1629
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630 template <class _A0, class ..._Args,
1631 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1632 iterator
1633 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
1634
Howard Hinnant73d21a42010-09-04 23:28:19 +00001635#endif // _LIBCPP_HAS_NO_VARIADICS
1636
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637 template <class _P,
1638 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001639 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 iterator insert(_P&& __p)
1641 {return __tree_.__insert_multi(_STD::forward<_P>(__p));}
1642
1643 template <class _P,
1644 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
Howard Hinnant82894812010-09-22 16:48:34 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 iterator insert(const_iterator __pos, _P&& __p)
1647 {return __tree_.__insert_multi(__pos.__i_, _STD::forward<_P>(__p));}
1648
Howard Hinnant73d21a42010-09-04 23:28:19 +00001649#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650
Howard Hinnant82894812010-09-22 16:48:34 +00001651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001652 iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
1653
Howard Hinnant82894812010-09-22 16:48:34 +00001654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 iterator insert(const_iterator __p, const value_type& __v)
1656 {return __tree_.__insert_multi(__p.__i_, __v);}
1657
1658 template <class _InputIterator>
Howard Hinnant82894812010-09-22 16:48:34 +00001659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 void insert(_InputIterator __f, _InputIterator __l)
1661 {
1662 for (const_iterator __e = cend(); __f != __l; ++__f)
1663 __tree_.__insert_multi(__e.__i_, *__f);
1664 }
1665
Howard Hinnant82894812010-09-22 16:48:34 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667 void insert(initializer_list<value_type> __il)
1668 {insert(__il.begin(), __il.end());}
1669
Howard Hinnant82894812010-09-22 16:48:34 +00001670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 iterator erase(const_iterator __f, const_iterator __l)
1676 {return __tree_.erase(__f.__i_, __l.__i_);}
Howard Hinnant82894812010-09-22 16:48:34 +00001677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 void clear() {__tree_.clear();}
1679
Howard Hinnant82894812010-09-22 16:48:34 +00001680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001681 void swap(multimap& __m)
1682 _NOEXCEPT_(__is_nothrow_swappable<__base>::value)
1683 {__tree_.swap(__m.__tree_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001684
Howard Hinnant82894812010-09-22 16:48:34 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 iterator find(const key_type& __k) {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 size_type count(const key_type& __k) const
1691 {return __tree_.__count_multi(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693 iterator lower_bound(const key_type& __k)
1694 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001695 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 const_iterator lower_bound(const key_type& __k) const
1697 {return __tree_.lower_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001698 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699 iterator upper_bound(const key_type& __k)
1700 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001701 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702 const_iterator upper_bound(const key_type& __k) const
1703 {return __tree_.upper_bound(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001704 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 pair<iterator,iterator> equal_range(const key_type& __k)
1706 {return __tree_.__equal_range_multi(__k);}
Howard Hinnant82894812010-09-22 16:48:34 +00001707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001708 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1709 {return __tree_.__equal_range_multi(__k);}
1710
1711private:
1712 typedef typename __base::__node __node;
1713 typedef typename __base::__node_allocator __node_allocator;
1714 typedef typename __base::__node_pointer __node_pointer;
1715 typedef typename __base::__node_const_pointer __node_const_pointer;
1716 typedef __map_node_destructor<__node_allocator> _D;
1717 typedef unique_ptr<__node, _D> __node_holder;
1718
Howard Hinnant73d21a42010-09-04 23:28:19 +00001719#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 __node_holder __construct_node();
1721 template <class _A0,
1722 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1723 __node_holder __construct_node(_A0&& __a0);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001724#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725 template <class _A0, class ..._Args,
1726 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1727 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001728#endif // _LIBCPP_HAS_NO_VARIADICS
1729#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730};
1731
Howard Hinnant73d21a42010-09-04 23:28:19 +00001732#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733
1734template <class _Key, class _Tp, class _Compare, class _Allocator>
1735multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
1736 : __tree_(_STD::move(__m.__tree_), __a)
1737{
1738 if (__a != __m.get_allocator())
1739 {
1740 const_iterator __e = cend();
1741 while (!__m.empty())
1742 __tree_.__insert_multi(__e.__i_,
1743 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1744 }
1745}
1746
1747template <class _Key, class _Tp, class _Compare, class _Allocator>
1748typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1749multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1750{
1751 __node_allocator& __na = __tree_.__node_alloc();
1752 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001753 __node_traits::construct(__na, _STD::addressof(__h->__value_.first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754 __h.get_deleter().__first_constructed = true;
Howard Hinnant2529d022011-02-02 17:36:20 +00001755 __node_traits::construct(__na, _STD::addressof(__h->__value_.second));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001756 __h.get_deleter().__second_constructed = true;
1757 return __h;
1758}
1759
1760template <class _Key, class _Tp, class _Compare, class _Allocator>
1761template <class _A0,
1762 class // = typename enable_if<is_convertible<_A0, value_type>::value>::type
1763 >
1764typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1765multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1766{
1767 __node_allocator& __na = __tree_.__node_alloc();
1768 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001769 __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::forward<_A0>(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 __h.get_deleter().__first_constructed = true;
1771 __h.get_deleter().__second_constructed = true;
1772 return __h;
1773}
1774
Howard Hinnant73d21a42010-09-04 23:28:19 +00001775#ifndef _LIBCPP_HAS_NO_VARIADICS
1776
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777template <class _Key, class _Tp, class _Compare, class _Allocator>
1778template <class _A0, class ..._Args,
1779 class // = typename enable_if<is_convertible<_A0, key_type>::value>::type
1780 >
1781typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1782multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1783{
1784 __node_allocator& __na = __tree_.__node_alloc();
1785 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001786 __node_traits::construct(__na, _STD::addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001787 __h.get_deleter().__first_constructed = true;
Howard Hinnant2529d022011-02-02 17:36:20 +00001788 __node_traits::construct(__na, _STD::addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 __h.get_deleter().__second_constructed = true;
1790 return __h;
1791}
1792
Howard Hinnant73d21a42010-09-04 23:28:19 +00001793#endif // _LIBCPP_HAS_NO_VARIADICS
1794#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795
Howard Hinnant73d21a42010-09-04 23:28:19 +00001796#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797
1798template <class _Key, class _Tp, class _Compare, class _Allocator>
1799template <class _A0, class ..._Args,
1800 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1801 >
1802typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1803multimap<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1804{
1805 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1806 _STD::forward<_Args>(__args)...);
1807 iterator __r = __tree_.__node_insert_multi(__h.get());
1808 __h.release();
1809 return __r;
1810}
1811
1812template <class _Key, class _Tp, class _Compare, class _Allocator>
1813template <class _A0, class ..._Args,
1814 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1815 >
1816typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1817multimap<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1818 _A0&& __a0,
1819 _Args&& ...__args)
1820{
1821 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1822 _STD::forward<_Args>(__args)...);
1823 iterator __r = __tree_.__node_insert_multi(__p.__i_, __h.get());
1824 __h.release();
1825 return __r;
1826}
1827
Howard Hinnant73d21a42010-09-04 23:28:19 +00001828#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829
1830template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001832bool
1833operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1834 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1835{
1836 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1837}
1838
1839template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841bool
1842operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1843 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1844{
1845 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1846}
1847
1848template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001849inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850bool
1851operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1852 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1853{
1854 return !(__x == __y);
1855}
1856
1857template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001858inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001859bool
1860operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1861 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1862{
1863 return __y < __x;
1864}
1865
1866template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001867inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001868bool
1869operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1870 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1871{
1872 return !(__x < __y);
1873}
1874
1875template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001876inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877bool
1878operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1879 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1880{
1881 return !(__y < __x);
1882}
1883
1884template <class _Key, class _Tp, class _Compare, class _Allocator>
Howard Hinnant82894812010-09-22 16:48:34 +00001885inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001886void
1887swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1888 multimap<_Key, _Tp, _Compare, _Allocator>& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001889 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890{
1891 __x.swap(__y);
1892}
1893
1894_LIBCPP_END_NAMESPACE_STD
1895
1896#endif // _LIBCPP_MAP