blob: 36c2acb27f8ed2b3cc87fa4e971aa36a69a94e2d [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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:
57 map();
58 explicit map(const key_compare& comp);
59 map(const key_compare& comp, const allocator_type& a);
60 template <class InputIterator>
61 map(InputIterator first, InputIterator last,
62 const key_compare& comp = key_compare());
63 template <class InputIterator>
64 map(InputIterator first, InputIterator last,
65 const key_compare& comp, const allocator_type& a);
66 map(const map& m);
67 map(map&& m);
68 explicit map(const allocator_type& a);
69 map(const map& m, const allocator_type& a);
70 map(map&& m, const allocator_type& a);
71 map(initializer_list<value_type> il, const key_compare& comp = key_compare());
72 map(initializer_list<value_type> il, const key_compare& comp, const allocator_type& a);
73 ~map();
74
75 map& operator=(const map& m);
76 map& operator=(map&& m);
77 map& operator=(initializer_list<value_type> il);
78
79 // iterators:
80 iterator begin();
81 const_iterator begin() const;
82 iterator end();
83 const_iterator end() const;
84
85 reverse_iterator rbegin();
86 const_reverse_iterator rbegin() const;
87 reverse_iterator rend();
88 const_reverse_iterator rend() const;
89
90 const_iterator cbegin() const;
91 const_iterator cend() const;
92 const_reverse_iterator crbegin() const;
93 const_reverse_iterator crend() const;
94
95 // capacity:
96 bool empty() const;
97 size_type size() const;
98 size_type max_size() const;
99
100 // element access:
101 mapped_type& operator[](const key_type& k);
102 mapped_type& operator[](key_type&& k);
103
104 mapped_type& at(const key_type& k);
105 const mapped_type& at(const key_type& k) const;
106
107 // modifiers:
108 template <class... Args>
109 pair<iterator, bool> emplace(Args&&... args);
110 template <class... Args>
111 iterator emplace_hint(const_iterator position, Args&&... args);
112 pair<iterator, bool> insert(const value_type& v);
113 template <class P>
114 pair<iterator, bool> insert(P&& p);
115 iterator insert(const_iterator position, const value_type& v);
116 template <class P>
117 iterator insert(const_iterator position, P&& p);
118 template <class InputIterator>
119 void insert(InputIterator first, InputIterator last);
120 void insert(initializer_list<value_type> il);
121
122 iterator erase(const_iterator position);
123 size_type erase(const key_type& k);
124 iterator erase(const_iterator first, const_iterator last);
125 void clear();
126
127 void swap(map& m);
128
129 // observers:
130 allocator_type get_allocator() const;
131 key_compare key_comp() const;
132 value_compare value_comp() const;
133
134 // map operations:
135 iterator find(const key_type& k);
136 const_iterator find(const key_type& k) const;
137 size_type count(const key_type& k) const;
138 iterator lower_bound(const key_type& k);
139 const_iterator lower_bound(const key_type& k) const;
140 iterator upper_bound(const key_type& k);
141 const_iterator upper_bound(const key_type& k) const;
142 pair<iterator,iterator> equal_range(const key_type& k);
143 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
144};
145
146template <class Key, class T, class Compare, class Allocator>
147bool
148operator==(const map<Key, T, Compare, Allocator>& x,
149 const map<Key, T, Compare, Allocator>& y);
150
151template <class Key, class T, class Compare, class Allocator>
152bool
153operator< (const map<Key, T, Compare, Allocator>& x,
154 const map<Key, T, Compare, Allocator>& y);
155
156template <class Key, class T, class Compare, class Allocator>
157bool
158operator!=(const map<Key, T, Compare, Allocator>& x,
159 const map<Key, T, Compare, Allocator>& y);
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
176// specialized algorithms:
177template <class Key, class T, class Compare, class Allocator>
178void
179swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y);
180
181template <class Key, class T, class Compare = less<Key>,
182 class Allocator = allocator<pair<const Key, T>>>
183class multimap
184{
185public:
186 // types:
187 typedef Key key_type;
188 typedef T mapped_type;
189 typedef pair<const key_type,mapped_type> value_type;
190 typedef Compare key_compare;
191 typedef Allocator allocator_type;
192 typedef typename allocator_type::reference reference;
193 typedef typename allocator_type::const_reference const_reference;
194 typedef typename allocator_type::size_type size_type;
195 typedef typename allocator_type::difference_type difference_type;
196 typedef typename allocator_type::pointer pointer;
197 typedef typename allocator_type::const_pointer const_pointer;
198
199 typedef implementation-defined iterator;
200 typedef implementation-defined const_iterator;
201 typedef std::reverse_iterator<iterator> reverse_iterator;
202 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
203
204 class value_compare
205 : public binary_function<value_type,value_type,bool>
206 {
207 friend class multimap;
208 protected:
209 key_compare comp;
210 value_compare(key_compare c);
211 public:
212 bool operator()(const value_type& x, const value_type& y) const;
213 };
214
215 // construct/copy/destroy:
216 explicit multimap(const key_compare& comp = key_compare());
217 multimap(const key_compare& comp, const allocator_type& a);
218 template <class InputIterator>
219 multimap(InputIterator first, InputIterator last, const key_compare& comp);
220 template <class InputIterator>
221 multimap(InputIterator first, InputIterator last, const key_compare& comp,
222 const allocator_type& a);
223 multimap(const multimap& m);
224 multimap(multimap&& m);
225 explicit multimap(const allocator_type& a);
226 multimap(const multimap& m, const allocator_type& a);
227 multimap(multimap&& m, const allocator_type& a);
228 multimap(initializer_list<value_type> il, const key_compare& comp = key_compare());
229 multimap(initializer_list<value_type> il, const key_compare& comp,
230 const allocator_type& a);
231 ~multimap();
232
233 multimap& operator=(const multimap& m);
234 multimap& operator=(multimap&& m);
235 multimap& operator=(initializer_list<value_type> il);
236
237 // iterators:
238 iterator begin();
239 const_iterator begin() const;
240 iterator end();
241 const_iterator end() const;
242
243 reverse_iterator rbegin();
244 const_reverse_iterator rbegin() const;
245 reverse_iterator rend();
246 const_reverse_iterator rend() const;
247
248 const_iterator cbegin() const;
249 const_iterator cend() const;
250 const_reverse_iterator crbegin() const;
251 const_reverse_iterator crend() const;
252
253 // capacity:
254 bool empty() const;
255 size_type size() const;
256 size_type max_size() const;
257
258 // modifiers:
259 template <class... Args>
260 iterator emplace(Args&&... args);
261 template <class... Args>
262 iterator emplace_hint(const_iterator position, Args&&... args);
263 iterator insert(const value_type& v);
264 template <class P>
265 iterator insert(P&& p);
266 iterator insert(const_iterator position, const value_type& v);
267 template <class P>
268 iterator insert(const_iterator position, P&& p);
269 template <class InputIterator>
270 void insert(InputIterator first, InputIterator last);
271 void insert(initializer_list<value_type> il);
272
273 iterator erase(const_iterator position);
274 size_type erase(const key_type& k);
275 iterator erase(const_iterator first, const_iterator last);
276 void clear();
277
278 void swap(multimap& m);
279
280 // observers:
281 allocator_type get_allocator() const;
282 key_compare key_comp() const;
283 value_compare value_comp() const;
284
285 // map operations:
286 iterator find(const key_type& k);
287 const_iterator find(const key_type& k) const;
288 size_type count(const key_type& k) const;
289 iterator lower_bound(const key_type& k);
290 const_iterator lower_bound(const key_type& k) const;
291 iterator upper_bound(const key_type& k);
292 const_iterator upper_bound(const key_type& k) const;
293 pair<iterator,iterator> equal_range(const key_type& k);
294 pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
295};
296
297template <class Key, class T, class Compare, class Allocator>
298bool
299operator==(const multimap<Key, T, Compare, Allocator>& x,
300 const multimap<Key, T, Compare, Allocator>& y);
301
302template <class Key, class T, class Compare, class Allocator>
303bool
304operator< (const multimap<Key, T, Compare, Allocator>& x,
305 const multimap<Key, T, Compare, Allocator>& y);
306
307template <class Key, class T, class Compare, class Allocator>
308bool
309operator!=(const multimap<Key, T, Compare, Allocator>& x,
310 const multimap<Key, T, Compare, Allocator>& y);
311
312template <class Key, class T, class Compare, class Allocator>
313bool
314operator> (const multimap<Key, T, Compare, Allocator>& x,
315 const multimap<Key, T, Compare, Allocator>& y);
316
317template <class Key, class T, class Compare, class Allocator>
318bool
319operator>=(const multimap<Key, T, Compare, Allocator>& x,
320 const multimap<Key, T, Compare, Allocator>& y);
321
322template <class Key, class T, class Compare, class Allocator>
323bool
324operator<=(const multimap<Key, T, Compare, Allocator>& x,
325 const multimap<Key, T, Compare, Allocator>& y);
326
327// specialized algorithms:
328template <class Key, class T, class Compare, class Allocator>
329void
330swap(multimap<Key, T, Compare, Allocator>& x,
331 multimap<Key, T, Compare, Allocator>& y);
332
333} // std
334
335*/
336
337#include <__config>
338#include <__tree>
339#include <iterator>
340#include <memory>
341#include <utility>
342#include <functional>
343#include <initializer_list>
344
345#pragma GCC system_header
346
347_LIBCPP_BEGIN_NAMESPACE_STD
348
349template <class _Key, class _Tp, class _Compare, bool = is_empty<_Compare>::value>
350class __map_value_compare
351 : private _Compare
352{
353 typedef pair<_Key, _Tp> _P;
354 typedef pair<const _Key, _Tp> _CP;
355public:
356 __map_value_compare() : _Compare() {}
357 __map_value_compare(_Compare c) : _Compare(c) {}
358 const _Compare& key_comp() const {return *this;}
359 bool operator()(const _CP& __x, const _CP& __y) const
360 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
361 bool operator()(const _CP& __x, const _P& __y) const
362 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
363 bool operator()(const _CP& __x, const _Key& __y) const
364 {return static_cast<const _Compare&>(*this)(__x.first, __y);}
365 bool operator()(const _P& __x, const _CP& __y) const
366 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
367 bool operator()(const _P& __x, const _P& __y) const
368 {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
369 bool operator()(const _P& __x, const _Key& __y) const
370 {return static_cast<const _Compare&>(*this)(__x.first, __y);}
371 bool operator()(const _Key& __x, const _CP& __y) const
372 {return static_cast<const _Compare&>(*this)(__x, __y.first);}
373 bool operator()(const _Key& __x, const _P& __y) const
374 {return static_cast<const _Compare&>(*this)(__x, __y.first);}
375 bool operator()(const _Key& __x, const _Key& __y) const
376 {return static_cast<const _Compare&>(*this)(__x, __y);}
377
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378// bool operator()(const _Tp& __x, const _Tp& __y) const
379// {return static_cast<const _Compare&>(*this)(__x.first, __y.first);}
380// bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
381// {return static_cast<const _Compare&>(*this)(__x, __y.first);}
382// bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
383// {return static_cast<const _Compare&>(*this)(__x.first, __y);}
384// bool operator()(const typename _Tp::first_type& __x,
385// const typename _Tp::first_type& __y) const
386// {return static_cast<const _Compare&>(*this)(__x, __y);}
387};
388
389template <class _Key, class _Tp, class _Compare>
390class __map_value_compare<_Key, _Tp, _Compare, false>
391{
392 _Compare comp;
393
394 typedef pair<_Key, _Tp> _P;
395 typedef pair<const _Key, _Tp> _CP;
396
397public:
398 __map_value_compare() : comp() {}
399 __map_value_compare(_Compare c) : comp(c) {}
400 const _Compare& key_comp() const {return comp;}
401
402 bool operator()(const _CP& __x, const _CP& __y) const
403 {return comp(__x.first, __y.first);}
404 bool operator()(const _CP& __x, const _P& __y) const
405 {return comp(__x.first, __y.first);}
406 bool operator()(const _CP& __x, const _Key& __y) const
407 {return comp(__x.first, __y);}
408 bool operator()(const _P& __x, const _CP& __y) const
409 {return comp(__x.first, __y.first);}
410 bool operator()(const _P& __x, const _P& __y) const
411 {return comp(__x.first, __y.first);}
412 bool operator()(const _P& __x, const _Key& __y) const
413 {return comp(__x.first, __y);}
414 bool operator()(const _Key& __x, const _CP& __y) const
415 {return comp(__x, __y.first);}
416 bool operator()(const _Key& __x, const _P& __y) const
417 {return comp(__x, __y.first);}
418 bool operator()(const _Key& __x, const _Key& __y) const
419 {return comp(__x, __y);}
420
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421// bool operator()(const _Tp& __x, const _Tp& __y) const
422// {return comp(__x.first, __y.first);}
423// bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
424// {return comp(__x, __y.first);}
425// bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
426// {return comp(__x.first, __y);}
427// bool operator()(const typename _Tp::first_type& __x,
428// const typename _Tp::first_type& __y) const
429// {return comp(__x, __y);}
430};
431
432template <class _Allocator>
433class __map_node_destructor
434{
435 typedef _Allocator allocator_type;
436 typedef allocator_traits<allocator_type> __alloc_traits;
437 typedef typename __alloc_traits::value_type::value_type value_type;
438public:
439 typedef typename __alloc_traits::pointer pointer;
440private:
441 typedef typename value_type::first_type first_type;
442 typedef typename value_type::second_type second_type;
443
444 allocator_type& __na_;
445
446 __map_node_destructor& operator=(const __map_node_destructor&);
447
448public:
449 bool __first_constructed;
450 bool __second_constructed;
451
452 explicit __map_node_destructor(allocator_type& __na)
453 : __na_(__na),
454 __first_constructed(false),
455 __second_constructed(false)
456 {}
457
Howard Hinnant73d21a42010-09-04 23:28:19 +0000458#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459 __map_node_destructor(__tree_node_destructor<allocator_type>&& __x)
460 : __na_(__x.__na_),
461 __first_constructed(__x.__value_constructed),
462 __second_constructed(__x.__value_constructed)
463 {
464 __x.__value_constructed = false;
465 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000466#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467
468 void operator()(pointer __p)
469 {
470 if (__second_constructed)
471 __alloc_traits::destroy(__na_, addressof(__p->__value_.second));
472 if (__first_constructed)
473 __alloc_traits::destroy(__na_, addressof(__p->__value_.first));
474 if (__p)
475 __alloc_traits::deallocate(__na_, __p, 1);
476 }
477};
478
479template <class, class, class, class> class map;
480template <class, class, class, class> class multimap;
481template <class> class __map_const_iterator;
482
483template <class _TreeIterator>
484class __map_iterator
485{
486 _TreeIterator __i_;
487
488 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
489 typedef const typename _TreeIterator::value_type::first_type key_type;
490 typedef typename _TreeIterator::value_type::second_type mapped_type;
491public:
492 typedef bidirectional_iterator_tag iterator_category;
493 typedef pair<key_type, mapped_type> value_type;
494 typedef typename _TreeIterator::difference_type difference_type;
495 typedef value_type& reference;
496 typedef typename __pointer_traits::template
497#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
498 rebind<value_type>
499#else
500 rebind<value_type>::other
501#endif
502 pointer;
503
504 __map_iterator() {}
505
506 __map_iterator(_TreeIterator __i) : __i_(__i) {}
507
508 reference operator*() const {return *operator->();}
509 pointer operator->() const {return (pointer)__i_.operator->();}
510
511 __map_iterator& operator++() {++__i_; return *this;}
512 __map_iterator operator++(int)
513 {
514 __map_iterator __t(*this);
515 ++(*this);
516 return __t;
517 }
518
519 __map_iterator& operator--() {--__i_; return *this;}
520 __map_iterator operator--(int)
521 {
522 __map_iterator __t(*this);
523 --(*this);
524 return __t;
525 }
526
527 friend bool operator==(const __map_iterator& __x, const __map_iterator& __y)
528 {return __x.__i_ == __y.__i_;}
529 friend bool operator!=(const __map_iterator& __x, const __map_iterator& __y)
530 {return __x.__i_ != __y.__i_;}
531
532 template <class, class, class, class> friend class map;
533 template <class, class, class, class> friend class multimap;
534 template <class> friend class __map_const_iterator;
535};
536
537template <class _TreeIterator>
538class __map_const_iterator
539{
540 _TreeIterator __i_;
541
542 typedef typename _TreeIterator::__pointer_traits __pointer_traits;
543 typedef const typename _TreeIterator::value_type::first_type key_type;
544 typedef typename _TreeIterator::value_type::second_type mapped_type;
545public:
546 typedef bidirectional_iterator_tag iterator_category;
547 typedef pair<key_type, mapped_type> value_type;
548 typedef typename _TreeIterator::difference_type difference_type;
549 typedef const value_type& reference;
550 typedef typename __pointer_traits::template
551#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
552 rebind<value_type>
553#else
554 rebind<value_type>::other
555#endif
556 pointer;
557
558 __map_const_iterator() {}
559
560 __map_const_iterator(_TreeIterator __i) : __i_(__i) {}
561 __map_const_iterator(
562 __map_iterator<typename _TreeIterator::__non_const_iterator> __i)
563 : __i_(__i.__i_) {}
564
565 reference operator*() const {return *operator->();}
566 pointer operator->() const {return (pointer)__i_.operator->();}
567
568 __map_const_iterator& operator++() {++__i_; return *this;}
569 __map_const_iterator operator++(int)
570 {
571 __map_const_iterator __t(*this);
572 ++(*this);
573 return __t;
574 }
575
576 __map_const_iterator& operator--() {--__i_; return *this;}
577 __map_const_iterator operator--(int)
578 {
579 __map_const_iterator __t(*this);
580 --(*this);
581 return __t;
582 }
583
584 friend bool operator==(const __map_const_iterator& __x, const __map_const_iterator& __y)
585 {return __x.__i_ == __y.__i_;}
586 friend bool operator!=(const __map_const_iterator& __x, const __map_const_iterator& __y)
587 {return __x.__i_ != __y.__i_;}
588
589 template <class, class, class, class> friend class map;
590 template <class, class, class, class> friend class multimap;
591 template <class, class, class> friend class __tree_const_iterator;
592};
593
594template <class _Key, class _Tp, class _Compare = less<_Key>,
595 class _Allocator = allocator<pair<const _Key, _Tp> > >
596class map
597{
598public:
599 // types:
600 typedef _Key key_type;
601 typedef _Tp mapped_type;
602 typedef pair<const key_type, mapped_type> value_type;
603 typedef _Compare key_compare;
604 typedef _Allocator allocator_type;
605 typedef value_type& reference;
606 typedef const value_type& const_reference;
607
608 class value_compare
609 : public binary_function<value_type, value_type, bool>
610 {
611 friend class map;
612 protected:
613 key_compare comp;
614
615 value_compare(key_compare c) : comp(c) {}
616 public:
617 bool operator()(const value_type& __x, const value_type& __y) const
618 {return comp(__x.first, __y.first);}
619 };
620
621private:
622 typedef pair<key_type, mapped_type> __value_type;
623 typedef __map_value_compare<key_type, mapped_type, key_compare> __vc;
624 typedef typename allocator_traits<allocator_type>::template
625#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
626 rebind_alloc<__value_type>
627#else
628 rebind_alloc<__value_type>::other
629#endif
630 __allocator_type;
631 typedef __tree<__value_type, __vc, __allocator_type> __base;
632 typedef typename __base::__node_traits __node_traits;
633 typedef allocator_traits<allocator_type> __alloc_traits;
634
635 __base __tree_;
636
637public:
638 typedef typename __alloc_traits::pointer pointer;
639 typedef typename __alloc_traits::const_pointer const_pointer;
640 typedef typename __alloc_traits::size_type size_type;
641 typedef typename __alloc_traits::difference_type difference_type;
642 typedef __map_iterator<typename __base::iterator> iterator;
643 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
644 typedef _STD::reverse_iterator<iterator> reverse_iterator;
645 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
646
647 explicit map(const key_compare& __comp = key_compare())
648 : __tree_(__vc(__comp)) {}
649
650 explicit map(const key_compare& __comp, const allocator_type& __a)
651 : __tree_(__vc(__comp), __a) {}
652
653 template <class _InputIterator>
654 map(_InputIterator __f, _InputIterator __l,
655 const key_compare& __comp = key_compare())
656 : __tree_(__vc(__comp))
657 {
658 insert(__f, __l);
659 }
660
661 template <class _InputIterator>
662 map(_InputIterator __f, _InputIterator __l,
663 const key_compare& __comp, const allocator_type& __a)
664 : __tree_(__vc(__comp), __a)
665 {
666 insert(__f, __l);
667 }
668
669 map(const map& __m)
670 : __tree_(__m.__tree_)
671 {
672 insert(__m.begin(), __m.end());
673 }
674
Howard Hinnant73d21a42010-09-04 23:28:19 +0000675#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
677 map(map&& __m)
678 : __tree_(_STD::move(__m.__tree_))
679 {
680 }
681
682 map(map&& __m, const allocator_type& __a);
683
684 map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
685 : __tree_(__vc(__comp))
686 {
687 insert(__il.begin(), __il.end());
688 }
689
690 map(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
691 : __tree_(__vc(__comp), __a)
692 {
693 insert(__il.begin(), __il.end());
694 }
695
696 map& operator=(map&& __m)
697 {
698 __tree_ = _STD::move(__m.__tree_);
699 return *this;
700 }
701
702 map& operator=(initializer_list<value_type> __il)
703 {
704 __tree_.__assign_unique(__il.begin(), __il.end());
705 return *this;
706 }
707
Howard Hinnant73d21a42010-09-04 23:28:19 +0000708#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709
710 explicit map(const allocator_type& __a)
711 : __tree_(__a)
712 {
713 }
714
715 map(const map& __m, const allocator_type& __a)
716 : __tree_(__m.__tree_.value_comp(), __a)
717 {
718 insert(__m.begin(), __m.end());
719 }
720
721 iterator begin() {return __tree_.begin();}
722 const_iterator begin() const {return __tree_.begin();}
723 iterator end() {return __tree_.end();}
724 const_iterator end() const {return __tree_.end();}
725
726 reverse_iterator rbegin() {return reverse_iterator(end());}
727 const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
728 reverse_iterator rend() {return reverse_iterator(begin());}
729 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
730
731 const_iterator cbegin() const {return begin();}
732 const_iterator cend() const {return end();}
733 const_reverse_iterator crbegin() const {return rbegin();}
734 const_reverse_iterator crend() const {return rend();}
735
736 bool empty() const {return __tree_.size() == 0;}
737 size_type size() const {return __tree_.size();}
738 size_type max_size() const {return __tree_.max_size();}
739
740 mapped_type& operator[](const key_type& __k);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000741#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 mapped_type& operator[](key_type&& __k);
743#endif
744
745 mapped_type& at(const key_type& __k);
746 const mapped_type& at(const key_type& __k) const;
747
748 allocator_type get_allocator() const {return __tree_.__alloc();}
749 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
750 value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
751
Howard Hinnant73d21a42010-09-04 23:28:19 +0000752#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
754 pair<iterator, bool>
755 emplace() {return __tree_.__emplace_unique();}
756
757 template <class _A0,
758 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
759 pair<iterator, bool>
760 emplace(_A0&& __a0)
761 {return __tree_.__emplace_unique(_STD::forward<_A0>(__a0));}
762
Howard Hinnant73d21a42010-09-04 23:28:19 +0000763#ifndef _LIBCPP_HAS_NO_VARIADICS
764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765 template <class _A0, class ..._Args,
766 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
767 pair<iterator, bool>
768 emplace(_A0&& __a0, _Args&& ...__args);
769
Howard Hinnant73d21a42010-09-04 23:28:19 +0000770#endif // _LIBCPP_HAS_NO_VARIADICS
771
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772 iterator
773 emplace_hint(const_iterator __p)
774 {return __tree_.__emplace_hint_unique(__p.__i_);}
775
776 template <class _A0,
777 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
778 iterator
779 emplace_hint(const_iterator __p, _A0&& __a0)
780 {return __tree_.__emplace_hint_unique(__p.__i_, _STD::forward<_A0>(__a0));}
781
Howard Hinnant73d21a42010-09-04 23:28:19 +0000782#ifndef _LIBCPP_HAS_NO_VARIADICS
783
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784 template <class _A0, class ..._Args,
785 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
786 iterator
787 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
788
Howard Hinnant73d21a42010-09-04 23:28:19 +0000789#endif // _LIBCPP_HAS_NO_VARIADICS
790
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791 template <class _P,
792 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
793 pair<iterator, bool> insert(_P&& __p)
794 {return __tree_.__insert_unique(_STD::forward<_P>(__p));}
795
796 template <class _P,
797 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
798 iterator insert(const_iterator __pos, _P&& __p)
799 {return __tree_.__insert_unique(__pos.__i_, _STD::forward<_P>(__p));}
800
Howard Hinnant73d21a42010-09-04 23:28:19 +0000801#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802
803 pair<iterator, bool>
804 insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
805
806 iterator
807 insert(const_iterator __p, const value_type& __v)
808 {return __tree_.__insert_unique(__p.__i_, __v);}
809
810 template <class _InputIterator>
811 void insert(_InputIterator __f, _InputIterator __l)
812 {
813 for (const_iterator __e = cend(); __f != __l; ++__f)
814 insert(__e.__i_, *__f);
815 }
816
817 void insert(initializer_list<value_type> __il)
818 {insert(__il.begin(), __il.end());}
819
820 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
821 size_type erase(const key_type& __k)
822 {return __tree_.__erase_unique(__k);}
823 iterator erase(const_iterator __f, const_iterator __l)
824 {return __tree_.erase(__f.__i_, __l.__i_);}
825 void clear() {__tree_.clear();}
826
827 void swap(map& __m) {__tree_.swap(__m.__tree_);}
828
829 iterator find(const key_type& __k) {return __tree_.find(__k);}
830 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
831 size_type count(const key_type& __k) const
832 {return __tree_.__count_unique(__k);}
833 iterator lower_bound(const key_type& __k)
834 {return __tree_.lower_bound(__k);}
835 const_iterator lower_bound(const key_type& __k) const
836 {return __tree_.lower_bound(__k);}
837 iterator upper_bound(const key_type& __k)
838 {return __tree_.upper_bound(__k);}
839 const_iterator upper_bound(const key_type& __k) const
840 {return __tree_.upper_bound(__k);}
841 pair<iterator,iterator> equal_range(const key_type& __k)
842 {return __tree_.__equal_range_unique(__k);}
843 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
844 {return __tree_.__equal_range_unique(__k);}
845
846private:
847 typedef typename __base::__node __node;
848 typedef typename __base::__node_allocator __node_allocator;
849 typedef typename __base::__node_pointer __node_pointer;
850 typedef typename __base::__node_const_pointer __node_const_pointer;
851 typedef typename __base::__node_base_pointer __node_base_pointer;
852 typedef typename __base::__node_base_const_pointer __node_base_const_pointer;
853 typedef __map_node_destructor<__node_allocator> _D;
854 typedef unique_ptr<__node, _D> __node_holder;
855
Howard Hinnant73d21a42010-09-04 23:28:19 +0000856#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 __node_holder __construct_node();
858 template <class _A0,
859 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
860 __node_holder __construct_node(_A0&& __a0);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000861#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862 template <class _A0, class ..._Args,
863 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
864 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000865#endif // _LIBCPP_HAS_NO_VARIADICS
866#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867 __node_holder __construct_node(const key_type& __k);
868#endif
869
870 __node_base_pointer&
871 __find_equal_key(__node_base_pointer& __parent, const key_type& __k);
872 __node_base_pointer&
873 __find_equal_key(const_iterator __hint,
874 __node_base_pointer& __parent, const key_type& __k);
875 __node_base_const_pointer
876 __find_equal_key(__node_base_const_pointer& __parent, const key_type& __k) const;
877};
878
879// Find place to insert if __k doesn't exist
880// Set __parent to parent of null leaf
881// Return reference to null leaf
882// If __k exists, set parent to node of __k and return reference to node of __k
883template <class _Key, class _Tp, class _Compare, class _Allocator>
884typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
885map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_pointer& __parent,
886 const key_type& __k)
887{
888 __node_pointer __nd = __tree_.__root();
889 if (__nd != nullptr)
890 {
891 while (true)
892 {
893 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
894 {
895 if (__nd->__left_ != nullptr)
896 __nd = static_cast<__node_pointer>(__nd->__left_);
897 else
898 {
899 __parent = __nd;
900 return __parent->__left_;
901 }
902 }
903 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
904 {
905 if (__nd->__right_ != nullptr)
906 __nd = static_cast<__node_pointer>(__nd->__right_);
907 else
908 {
909 __parent = __nd;
910 return __parent->__right_;
911 }
912 }
913 else
914 {
915 __parent = __nd;
916 return __parent;
917 }
918 }
919 }
920 __parent = __tree_.__end_node();
921 return __parent->__left_;
922}
923
924// Find place to insert if __k doesn't exist
925// First check prior to __hint.
926// Next check after __hint.
927// Next do O(log N) search.
928// Set __parent to parent of null leaf
929// Return reference to null leaf
930// If __k exists, set parent to node of __k and return reference to node of __k
931template <class _Key, class _Tp, class _Compare, class _Allocator>
932typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
933map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(const_iterator __hint,
934 __node_base_pointer& __parent,
935 const key_type& __k)
936{
937 if (__hint == end() || __tree_.value_comp().key_comp()(__k, __hint->first)) // check before
938 {
939 // __k < *__hint
940 const_iterator __prior = __hint;
941 if (__prior == begin() || __tree_.value_comp().key_comp()((--__prior)->first, __k))
942 {
943 // *prev(__hint) < __k < *__hint
944 if (__hint.__ptr_->__left_ == nullptr)
945 {
946 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
947 return __parent->__left_;
948 }
949 else
950 {
951 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
952 return __parent->__right_;
953 }
954 }
955 // __k <= *prev(__hint)
956 return __find_equal_key(__parent, __k);
957 }
958 else if (__tree_.value_comp().key_comp()(__hint->first, __k)) // check after
959 {
960 // *__hint < __k
961 const_iterator __next = next(__hint);
962 if (__next == end() || __tree_.value_comp().key_comp()(__k, __next->first))
963 {
964 // *__hint < __k < *next(__hint)
965 if (__hint.__ptr_->__right_ == nullptr)
966 {
967 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
968 return __parent->__right_;
969 }
970 else
971 {
972 __parent = const_cast<__node_pointer&>(__next.__ptr_);
973 return __parent->__left_;
974 }
975 }
976 // *next(__hint) <= __k
977 return __find_equal_key(__parent, __k);
978 }
979 // else __k == *__hint
980 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
981 return __parent;
982}
983
984// Find __k
985// Set __parent to parent of null leaf and
986// return reference to null leaf iv __k does not exist.
987// If __k exists, set parent to node of __k and return reference to node of __k
988template <class _Key, class _Tp, class _Compare, class _Allocator>
989typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_const_pointer
990map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_const_pointer& __parent,
991 const key_type& __k) const
992{
993 __node_const_pointer __nd = __tree_.__root();
994 if (__nd != nullptr)
995 {
996 while (true)
997 {
998 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
999 {
1000 if (__nd->__left_ != nullptr)
1001 __nd = static_cast<__node_pointer>(__nd->__left_);
1002 else
1003 {
1004 __parent = __nd;
1005 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1006 }
1007 }
1008 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
1009 {
1010 if (__nd->__right_ != nullptr)
1011 __nd = static_cast<__node_pointer>(__nd->__right_);
1012 else
1013 {
1014 __parent = __nd;
1015 return const_cast<const __node_base_const_pointer&>(__parent->__right_);
1016 }
1017 }
1018 else
1019 {
1020 __parent = __nd;
1021 return __parent;
1022 }
1023 }
1024 }
1025 __parent = __tree_.__end_node();
1026 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1027}
1028
Howard Hinnant73d21a42010-09-04 23:28:19 +00001029#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030
1031template <class _Key, class _Tp, class _Compare, class _Allocator>
1032map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
1033 : __tree_(_STD::move(__m.__tree_), __a)
1034{
1035 if (__a != __m.get_allocator())
1036 {
1037 const_iterator __e = cend();
1038 while (!__m.empty())
1039 __tree_.__insert_unique(__e.__i_,
1040 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1041 }
1042}
1043
1044template <class _Key, class _Tp, class _Compare, class _Allocator>
1045typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1046map<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1047{
1048 __node_allocator& __na = __tree_.__node_alloc();
1049 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1050 __node_traits::construct(__na, addressof(__h->__value_.first));
1051 __h.get_deleter().__first_constructed = true;
1052 __node_traits::construct(__na, addressof(__h->__value_.second));
1053 __h.get_deleter().__second_constructed = true;
1054 return __h;
1055}
1056
1057template <class _Key, class _Tp, class _Compare, class _Allocator>
1058template <class _A0,
1059 class>
1060typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1061map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1062{
1063 __node_allocator& __na = __tree_.__node_alloc();
1064 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1065 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_A0>(__a0));
1066 __h.get_deleter().__first_constructed = true;
1067 __h.get_deleter().__second_constructed = true;
1068 return __h;
1069}
1070
Howard Hinnant73d21a42010-09-04 23:28:19 +00001071#ifndef _LIBCPP_HAS_NO_VARIADICS
1072
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073template <class _Key, class _Tp, class _Compare, class _Allocator>
1074template <class _A0, class ..._Args,
1075 class>
1076typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1077map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1078{
1079 __node_allocator& __na = __tree_.__node_alloc();
1080 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1081 __node_traits::construct(__na, addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
1082 __h.get_deleter().__first_constructed = true;
1083 __node_traits::construct(__na, addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
1084 __h.get_deleter().__second_constructed = true;
1085 return __h;
1086}
1087
Howard Hinnant73d21a42010-09-04 23:28:19 +00001088#endif // _LIBCPP_HAS_NO_VARIADICS
1089
1090#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001091
1092template <class _Key, class _Tp, class _Compare, class _Allocator>
1093typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1094map<_Key, _Tp, _Compare, _Allocator>::__construct_node(const key_type& __k)
1095{
1096 __node_allocator& __na = __tree_.__node_alloc();
1097 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1098 __node_traits::construct(__na, addressof(__h->__value_.first), __k);
1099 __h.get_deleter().__first_constructed = true;
1100 __node_traits::construct(__na, addressof(__h->__value_.second));
1101 __h.get_deleter().__second_constructed = true;
1102 return _STD::move(__h);
1103}
1104
Howard Hinnant73d21a42010-09-04 23:28:19 +00001105#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106
1107template <class _Key, class _Tp, class _Compare, class _Allocator>
1108_Tp&
1109map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1110{
1111 __node_base_pointer __parent;
1112 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1113 __node_pointer __r = static_cast<__node_pointer>(__child);
1114 if (__child == nullptr)
1115 {
1116 __node_holder __h = __construct_node(__k);
1117 __tree_.__insert_node_at(__parent, __child, __h.get());
1118 __r = __h.release();
1119 }
1120 return __r->__value_.second;
1121}
1122
Howard Hinnant73d21a42010-09-04 23:28:19 +00001123#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001124
1125template <class _Key, class _Tp, class _Compare, class _Allocator>
1126_Tp&
1127map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1128{
1129 __node_base_pointer __parent;
1130 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1131 __node_pointer __r = static_cast<__node_pointer>(__child);
1132 if (__child == nullptr)
1133 {
1134 __node_holder __h = __construct_node(_STD::move(__k));
1135 __tree_.__insert_node_at(__parent, __child, __h.get());
1136 __r = __h.release();
1137 }
1138 return __r->__value_.second;
1139}
1140
Howard Hinnant73d21a42010-09-04 23:28:19 +00001141#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
1143template <class _Key, class _Tp, class _Compare, class _Allocator>
1144_Tp&
1145map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1146{
1147 __node_base_pointer __parent;
1148 __node_base_pointer& __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001149#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 if (__child == nullptr)
1151 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153 return static_cast<__node_pointer>(__child)->__value_.second;
1154}
1155
1156template <class _Key, class _Tp, class _Compare, class _Allocator>
1157const _Tp&
1158map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1159{
1160 __node_base_const_pointer __parent;
1161 __node_base_const_pointer __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001162#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163 if (__child == nullptr)
1164 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001165#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166 return static_cast<__node_const_pointer>(__child)->__value_.second;
1167}
1168
Howard Hinnant73d21a42010-09-04 23:28:19 +00001169#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170
1171template <class _Key, class _Tp, class _Compare, class _Allocator>
1172template <class _A0, class ..._Args,
1173 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1174 >
1175pair<typename map<_Key, _Tp, _Compare, _Allocator>::iterator, bool>
1176map<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1177{
1178 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1179 _STD::forward<_Args>(__args)...);
1180 pair<iterator, bool> __r = __tree_.__node_insert_unique(__h.get());
1181 if (__r.second)
1182 __h.release();
1183 return __r;
1184}
1185
1186template <class _Key, class _Tp, class _Compare, class _Allocator>
1187template <class _A0, class ..._Args,
1188 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1189 >
1190typename map<_Key, _Tp, _Compare, _Allocator>::iterator
1191map<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1192 _A0&& __a0, _Args&& ...__args)
1193{
1194 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1195 _STD::forward<_Args>(__args)...);
1196 iterator __r = __tree_.__node_insert_unique(__p.__i_, __h.get());
1197 if (__r.__i_.__ptr_ == __h.get())
1198 __h.release();
1199 return __r;
1200}
1201
Howard Hinnant73d21a42010-09-04 23:28:19 +00001202#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203
1204template <class _Key, class _Tp, class _Compare, class _Allocator>
1205inline
1206bool
1207operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1208 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1209{
1210 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1211}
1212
1213template <class _Key, class _Tp, class _Compare, class _Allocator>
1214inline
1215bool
1216operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1217 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1218{
1219 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1220}
1221
1222template <class _Key, class _Tp, class _Compare, class _Allocator>
1223inline
1224bool
1225operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1226 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1227{
1228 return !(__x == __y);
1229}
1230
1231template <class _Key, class _Tp, class _Compare, class _Allocator>
1232inline
1233bool
1234operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1235 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1236{
1237 return __y < __x;
1238}
1239
1240template <class _Key, class _Tp, class _Compare, class _Allocator>
1241inline
1242bool
1243operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1244 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1245{
1246 return !(__x < __y);
1247}
1248
1249template <class _Key, class _Tp, class _Compare, class _Allocator>
1250inline
1251bool
1252operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1253 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1254{
1255 return !(__y < __x);
1256}
1257
1258template <class _Key, class _Tp, class _Compare, class _Allocator>
1259inline
1260void
1261swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1262 map<_Key, _Tp, _Compare, _Allocator>& __y)
1263{
1264 __x.swap(__y);
1265}
1266
1267template <class _Key, class _Tp, class _Compare = less<_Key>,
1268 class _Allocator = allocator<pair<const _Key, _Tp> > >
1269class multimap
1270{
1271public:
1272 // types:
1273 typedef _Key key_type;
1274 typedef _Tp mapped_type;
1275 typedef pair<const key_type, mapped_type> value_type;
1276 typedef _Compare key_compare;
1277 typedef _Allocator allocator_type;
1278 typedef value_type& reference;
1279 typedef const value_type& const_reference;
1280
1281 class value_compare
1282 : public binary_function<value_type, value_type, bool>
1283 {
1284 friend class multimap;
1285 protected:
1286 key_compare comp;
1287
1288 value_compare(key_compare c) : comp(c) {}
1289 public:
1290 bool operator()(const value_type& __x, const value_type& __y) const
1291 {return comp(__x.first, __y.first);}
1292 };
1293
1294private:
1295 typedef pair<key_type, mapped_type> __value_type;
1296 typedef __map_value_compare<key_type, mapped_type, key_compare> __vc;
1297 typedef typename allocator_traits<allocator_type>::template
1298#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1299 rebind_alloc<__value_type>
1300#else
1301 rebind_alloc<__value_type>::other
1302#endif
1303 __allocator_type;
1304 typedef __tree<__value_type, __vc, __allocator_type> __base;
1305 typedef typename __base::__node_traits __node_traits;
1306 typedef allocator_traits<allocator_type> __alloc_traits;
1307
1308 __base __tree_;
1309
1310public:
1311 typedef typename __alloc_traits::pointer pointer;
1312 typedef typename __alloc_traits::const_pointer const_pointer;
1313 typedef typename __alloc_traits::size_type size_type;
1314 typedef typename __alloc_traits::difference_type difference_type;
1315 typedef __map_iterator<typename __base::iterator> iterator;
1316 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1317 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1318 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1319
1320 explicit multimap(const key_compare& __comp = key_compare())
1321 : __tree_(__vc(__comp)) {}
1322
1323 explicit multimap(const key_compare& __comp, const allocator_type& __a)
1324 : __tree_(__vc(__comp), __a) {}
1325
1326 template <class _InputIterator>
1327 multimap(_InputIterator __f, _InputIterator __l,
1328 const key_compare& __comp = key_compare())
1329 : __tree_(__vc(__comp))
1330 {
1331 insert(__f, __l);
1332 }
1333
1334 template <class _InputIterator>
1335 multimap(_InputIterator __f, _InputIterator __l,
1336 const key_compare& __comp, const allocator_type& __a)
1337 : __tree_(__vc(__comp), __a)
1338 {
1339 insert(__f, __l);
1340 }
1341
1342 multimap(const multimap& __m)
1343 : __tree_(__m.__tree_.value_comp(),
1344 __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1345 {
1346 insert(__m.begin(), __m.end());
1347 }
1348
Howard Hinnant73d21a42010-09-04 23:28:19 +00001349#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350
1351 multimap(multimap&& __m)
1352 : __tree_(_STD::move(__m.__tree_))
1353 {
1354 }
1355
1356 multimap(multimap&& __m, const allocator_type& __a);
1357
1358 multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1359 : __tree_(__vc(__comp))
1360 {
1361 insert(__il.begin(), __il.end());
1362 }
1363
1364 multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1365 : __tree_(__vc(__comp), __a)
1366 {
1367 insert(__il.begin(), __il.end());
1368 }
1369
1370 multimap& operator=(multimap&& __m)
1371 {
1372 __tree_ = _STD::move(__m.__tree_);
1373 return *this;
1374 }
1375
1376 multimap& operator=(initializer_list<value_type> __il)
1377 {
1378 __tree_.__assign_multi(__il.begin(), __il.end());
1379 return *this;
1380 }
Howard Hinnant73d21a42010-09-04 23:28:19 +00001381#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382
1383 explicit multimap(const allocator_type& __a)
1384 : __tree_(__a)
1385 {
1386 }
1387
1388 multimap(const multimap& __m, const allocator_type& __a)
1389 : __tree_(__m.__tree_.value_comp(), __a)
1390 {
1391 insert(__m.begin(), __m.end());
1392 }
1393
1394 iterator begin() {return __tree_.begin();}
1395 const_iterator begin() const {return __tree_.begin();}
1396 iterator end() {return __tree_.end();}
1397 const_iterator end() const {return __tree_.end();}
1398
1399 reverse_iterator rbegin() {return reverse_iterator(end());}
1400 const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1401 reverse_iterator rend() {return reverse_iterator(begin());}
1402 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1403
1404 const_iterator cbegin() const {return begin();}
1405 const_iterator cend() const {return end();}
1406 const_reverse_iterator crbegin() const {return rbegin();}
1407 const_reverse_iterator crend() const {return rend();}
1408
1409 bool empty() const {return __tree_.size() == 0;}
1410 size_type size() const {return __tree_.size();}
1411 size_type max_size() const {return __tree_.max_size();}
1412
1413 allocator_type get_allocator() const {return __tree_.__alloc();}
1414 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
1415 value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
1416
Howard Hinnant73d21a42010-09-04 23:28:19 +00001417#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418
1419 iterator emplace() {return __tree_.__emplace_multi();}
1420
1421 template <class _A0,
1422 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1423 iterator
1424 emplace(_A0&& __a0)
1425 {return __tree_.__emplace_multi(_STD::forward<_A0>(__a0));}
1426
Howard Hinnant73d21a42010-09-04 23:28:19 +00001427#ifndef _LIBCPP_HAS_NO_VARIADICS
1428
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 template <class _A0, class ..._Args,
1430 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1431 iterator
1432 emplace(_A0&& __a0, _Args&& ...__args);
1433
Howard Hinnant73d21a42010-09-04 23:28:19 +00001434#endif // _LIBCPP_HAS_NO_VARIADICS
1435
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 iterator emplace_hint(const_iterator __p)
1437 {return __tree_.__emplace_hint_multi(__p.__i_);}
1438
1439 template <class _A0,
1440 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1441 iterator
1442 emplace_hint(const_iterator __p, _A0&& __a0)
1443 {return __tree_.__emplace_hint_multi(__p.__i_, _STD::forward<_A0>(__a0));}
1444
Howard Hinnant73d21a42010-09-04 23:28:19 +00001445#ifndef _LIBCPP_HAS_NO_VARIADICS
1446
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 template <class _A0, class ..._Args,
1448 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1449 iterator
1450 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
1451
Howard Hinnant73d21a42010-09-04 23:28:19 +00001452#endif // _LIBCPP_HAS_NO_VARIADICS
1453
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 template <class _P,
1455 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
1456 iterator insert(_P&& __p)
1457 {return __tree_.__insert_multi(_STD::forward<_P>(__p));}
1458
1459 template <class _P,
1460 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
1461 iterator insert(const_iterator __pos, _P&& __p)
1462 {return __tree_.__insert_multi(__pos.__i_, _STD::forward<_P>(__p));}
1463
Howard Hinnant73d21a42010-09-04 23:28:19 +00001464#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465
1466 iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
1467
1468 iterator insert(const_iterator __p, const value_type& __v)
1469 {return __tree_.__insert_multi(__p.__i_, __v);}
1470
1471 template <class _InputIterator>
1472 void insert(_InputIterator __f, _InputIterator __l)
1473 {
1474 for (const_iterator __e = cend(); __f != __l; ++__f)
1475 __tree_.__insert_multi(__e.__i_, *__f);
1476 }
1477
1478 void insert(initializer_list<value_type> __il)
1479 {insert(__il.begin(), __il.end());}
1480
1481 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
1482 size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
1483 iterator erase(const_iterator __f, const_iterator __l)
1484 {return __tree_.erase(__f.__i_, __l.__i_);}
1485 void clear() {__tree_.clear();}
1486
1487 void swap(multimap& __m) {__tree_.swap(__m.__tree_);}
1488
1489 iterator find(const key_type& __k) {return __tree_.find(__k);}
1490 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
1491 size_type count(const key_type& __k) const
1492 {return __tree_.__count_multi(__k);}
1493 iterator lower_bound(const key_type& __k)
1494 {return __tree_.lower_bound(__k);}
1495 const_iterator lower_bound(const key_type& __k) const
1496 {return __tree_.lower_bound(__k);}
1497 iterator upper_bound(const key_type& __k)
1498 {return __tree_.upper_bound(__k);}
1499 const_iterator upper_bound(const key_type& __k) const
1500 {return __tree_.upper_bound(__k);}
1501 pair<iterator,iterator> equal_range(const key_type& __k)
1502 {return __tree_.__equal_range_multi(__k);}
1503 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1504 {return __tree_.__equal_range_multi(__k);}
1505
1506private:
1507 typedef typename __base::__node __node;
1508 typedef typename __base::__node_allocator __node_allocator;
1509 typedef typename __base::__node_pointer __node_pointer;
1510 typedef typename __base::__node_const_pointer __node_const_pointer;
1511 typedef __map_node_destructor<__node_allocator> _D;
1512 typedef unique_ptr<__node, _D> __node_holder;
1513
Howard Hinnant73d21a42010-09-04 23:28:19 +00001514#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 __node_holder __construct_node();
1516 template <class _A0,
1517 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1518 __node_holder __construct_node(_A0&& __a0);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001519#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 template <class _A0, class ..._Args,
1521 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1522 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001523#endif // _LIBCPP_HAS_NO_VARIADICS
1524#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525};
1526
Howard Hinnant73d21a42010-09-04 23:28:19 +00001527#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001528
1529template <class _Key, class _Tp, class _Compare, class _Allocator>
1530multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
1531 : __tree_(_STD::move(__m.__tree_), __a)
1532{
1533 if (__a != __m.get_allocator())
1534 {
1535 const_iterator __e = cend();
1536 while (!__m.empty())
1537 __tree_.__insert_multi(__e.__i_,
1538 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1539 }
1540}
1541
1542template <class _Key, class _Tp, class _Compare, class _Allocator>
1543typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1544multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1545{
1546 __node_allocator& __na = __tree_.__node_alloc();
1547 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1548 __node_traits::construct(__na, addressof(__h->__value_.first));
1549 __h.get_deleter().__first_constructed = true;
1550 __node_traits::construct(__na, addressof(__h->__value_.second));
1551 __h.get_deleter().__second_constructed = true;
1552 return __h;
1553}
1554
1555template <class _Key, class _Tp, class _Compare, class _Allocator>
1556template <class _A0,
1557 class // = typename enable_if<is_convertible<_A0, value_type>::value>::type
1558 >
1559typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1560multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1561{
1562 __node_allocator& __na = __tree_.__node_alloc();
1563 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1564 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_A0>(__a0));
1565 __h.get_deleter().__first_constructed = true;
1566 __h.get_deleter().__second_constructed = true;
1567 return __h;
1568}
1569
Howard Hinnant73d21a42010-09-04 23:28:19 +00001570#ifndef _LIBCPP_HAS_NO_VARIADICS
1571
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572template <class _Key, class _Tp, class _Compare, class _Allocator>
1573template <class _A0, class ..._Args,
1574 class // = typename enable_if<is_convertible<_A0, key_type>::value>::type
1575 >
1576typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1577multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1578{
1579 __node_allocator& __na = __tree_.__node_alloc();
1580 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1581 __node_traits::construct(__na, addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
1582 __h.get_deleter().__first_constructed = true;
1583 __node_traits::construct(__na, addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
1584 __h.get_deleter().__second_constructed = true;
1585 return __h;
1586}
1587
Howard Hinnant73d21a42010-09-04 23:28:19 +00001588#endif // _LIBCPP_HAS_NO_VARIADICS
1589#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590
Howard Hinnant73d21a42010-09-04 23:28:19 +00001591#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592
1593template <class _Key, class _Tp, class _Compare, class _Allocator>
1594template <class _A0, class ..._Args,
1595 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1596 >
1597typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1598multimap<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1599{
1600 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1601 _STD::forward<_Args>(__args)...);
1602 iterator __r = __tree_.__node_insert_multi(__h.get());
1603 __h.release();
1604 return __r;
1605}
1606
1607template <class _Key, class _Tp, class _Compare, class _Allocator>
1608template <class _A0, class ..._Args,
1609 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1610 >
1611typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1612multimap<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1613 _A0&& __a0,
1614 _Args&& ...__args)
1615{
1616 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1617 _STD::forward<_Args>(__args)...);
1618 iterator __r = __tree_.__node_insert_multi(__p.__i_, __h.get());
1619 __h.release();
1620 return __r;
1621}
1622
Howard Hinnant73d21a42010-09-04 23:28:19 +00001623#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624
1625template <class _Key, class _Tp, class _Compare, class _Allocator>
1626inline
1627bool
1628operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1629 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1630{
1631 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1632}
1633
1634template <class _Key, class _Tp, class _Compare, class _Allocator>
1635inline
1636bool
1637operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1638 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1639{
1640 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1641}
1642
1643template <class _Key, class _Tp, class _Compare, class _Allocator>
1644inline
1645bool
1646operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1647 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1648{
1649 return !(__x == __y);
1650}
1651
1652template <class _Key, class _Tp, class _Compare, class _Allocator>
1653inline
1654bool
1655operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1656 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1657{
1658 return __y < __x;
1659}
1660
1661template <class _Key, class _Tp, class _Compare, class _Allocator>
1662inline
1663bool
1664operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1665 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1666{
1667 return !(__x < __y);
1668}
1669
1670template <class _Key, class _Tp, class _Compare, class _Allocator>
1671inline
1672bool
1673operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1674 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1675{
1676 return !(__y < __x);
1677}
1678
1679template <class _Key, class _Tp, class _Compare, class _Allocator>
1680inline
1681void
1682swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1683 multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1684{
1685 __x.swap(__y);
1686}
1687
1688_LIBCPP_END_NAMESPACE_STD
1689
1690#endif // _LIBCPP_MAP