blob: 6fe4bcd3114d607e27fae2d7052527fc11921747 [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
458#ifdef _LIBCPP_MOVE
459 __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 Hinnant324bb032010-08-22 00:02:43 +0000466#endif // _LIBCPP_MOVE
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
675#ifdef _LIBCPP_MOVE
676
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 Hinnant324bb032010-08-22 00:02:43 +0000708#endif // _LIBCPP_MOVE
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);
741#ifdef _LIBCPP_MOVE
742 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
752#ifdef _LIBCPP_MOVE
753
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
763 template <class _A0, class ..._Args,
764 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
765 pair<iterator, bool>
766 emplace(_A0&& __a0, _Args&& ...__args);
767
768 iterator
769 emplace_hint(const_iterator __p)
770 {return __tree_.__emplace_hint_unique(__p.__i_);}
771
772 template <class _A0,
773 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
774 iterator
775 emplace_hint(const_iterator __p, _A0&& __a0)
776 {return __tree_.__emplace_hint_unique(__p.__i_, _STD::forward<_A0>(__a0));}
777
778 template <class _A0, class ..._Args,
779 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
780 iterator
781 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
782
783 template <class _P,
784 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
785 pair<iterator, bool> insert(_P&& __p)
786 {return __tree_.__insert_unique(_STD::forward<_P>(__p));}
787
788 template <class _P,
789 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
790 iterator insert(const_iterator __pos, _P&& __p)
791 {return __tree_.__insert_unique(__pos.__i_, _STD::forward<_P>(__p));}
792
Howard Hinnant324bb032010-08-22 00:02:43 +0000793#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000794
795 pair<iterator, bool>
796 insert(const value_type& __v) {return __tree_.__insert_unique(__v);}
797
798 iterator
799 insert(const_iterator __p, const value_type& __v)
800 {return __tree_.__insert_unique(__p.__i_, __v);}
801
802 template <class _InputIterator>
803 void insert(_InputIterator __f, _InputIterator __l)
804 {
805 for (const_iterator __e = cend(); __f != __l; ++__f)
806 insert(__e.__i_, *__f);
807 }
808
809 void insert(initializer_list<value_type> __il)
810 {insert(__il.begin(), __il.end());}
811
812 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
813 size_type erase(const key_type& __k)
814 {return __tree_.__erase_unique(__k);}
815 iterator erase(const_iterator __f, const_iterator __l)
816 {return __tree_.erase(__f.__i_, __l.__i_);}
817 void clear() {__tree_.clear();}
818
819 void swap(map& __m) {__tree_.swap(__m.__tree_);}
820
821 iterator find(const key_type& __k) {return __tree_.find(__k);}
822 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
823 size_type count(const key_type& __k) const
824 {return __tree_.__count_unique(__k);}
825 iterator lower_bound(const key_type& __k)
826 {return __tree_.lower_bound(__k);}
827 const_iterator lower_bound(const key_type& __k) const
828 {return __tree_.lower_bound(__k);}
829 iterator upper_bound(const key_type& __k)
830 {return __tree_.upper_bound(__k);}
831 const_iterator upper_bound(const key_type& __k) const
832 {return __tree_.upper_bound(__k);}
833 pair<iterator,iterator> equal_range(const key_type& __k)
834 {return __tree_.__equal_range_unique(__k);}
835 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
836 {return __tree_.__equal_range_unique(__k);}
837
838private:
839 typedef typename __base::__node __node;
840 typedef typename __base::__node_allocator __node_allocator;
841 typedef typename __base::__node_pointer __node_pointer;
842 typedef typename __base::__node_const_pointer __node_const_pointer;
843 typedef typename __base::__node_base_pointer __node_base_pointer;
844 typedef typename __base::__node_base_const_pointer __node_base_const_pointer;
845 typedef __map_node_destructor<__node_allocator> _D;
846 typedef unique_ptr<__node, _D> __node_holder;
847
848#ifdef _LIBCPP_MOVE
849 __node_holder __construct_node();
850 template <class _A0,
851 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
852 __node_holder __construct_node(_A0&& __a0);
853 template <class _A0, class ..._Args,
854 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
855 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000856#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 __node_holder __construct_node(const key_type& __k);
858#endif
859
860 __node_base_pointer&
861 __find_equal_key(__node_base_pointer& __parent, const key_type& __k);
862 __node_base_pointer&
863 __find_equal_key(const_iterator __hint,
864 __node_base_pointer& __parent, const key_type& __k);
865 __node_base_const_pointer
866 __find_equal_key(__node_base_const_pointer& __parent, const key_type& __k) const;
867};
868
869// Find place to insert if __k doesn't exist
870// Set __parent to parent of null leaf
871// Return reference to null leaf
872// If __k exists, set parent to node of __k and return reference to node of __k
873template <class _Key, class _Tp, class _Compare, class _Allocator>
874typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
875map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_pointer& __parent,
876 const key_type& __k)
877{
878 __node_pointer __nd = __tree_.__root();
879 if (__nd != nullptr)
880 {
881 while (true)
882 {
883 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
884 {
885 if (__nd->__left_ != nullptr)
886 __nd = static_cast<__node_pointer>(__nd->__left_);
887 else
888 {
889 __parent = __nd;
890 return __parent->__left_;
891 }
892 }
893 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
894 {
895 if (__nd->__right_ != nullptr)
896 __nd = static_cast<__node_pointer>(__nd->__right_);
897 else
898 {
899 __parent = __nd;
900 return __parent->__right_;
901 }
902 }
903 else
904 {
905 __parent = __nd;
906 return __parent;
907 }
908 }
909 }
910 __parent = __tree_.__end_node();
911 return __parent->__left_;
912}
913
914// Find place to insert if __k doesn't exist
915// First check prior to __hint.
916// Next check after __hint.
917// Next do O(log N) search.
918// Set __parent to parent of null leaf
919// Return reference to null leaf
920// If __k exists, set parent to node of __k and return reference to node of __k
921template <class _Key, class _Tp, class _Compare, class _Allocator>
922typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer&
923map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(const_iterator __hint,
924 __node_base_pointer& __parent,
925 const key_type& __k)
926{
927 if (__hint == end() || __tree_.value_comp().key_comp()(__k, __hint->first)) // check before
928 {
929 // __k < *__hint
930 const_iterator __prior = __hint;
931 if (__prior == begin() || __tree_.value_comp().key_comp()((--__prior)->first, __k))
932 {
933 // *prev(__hint) < __k < *__hint
934 if (__hint.__ptr_->__left_ == nullptr)
935 {
936 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
937 return __parent->__left_;
938 }
939 else
940 {
941 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
942 return __parent->__right_;
943 }
944 }
945 // __k <= *prev(__hint)
946 return __find_equal_key(__parent, __k);
947 }
948 else if (__tree_.value_comp().key_comp()(__hint->first, __k)) // check after
949 {
950 // *__hint < __k
951 const_iterator __next = next(__hint);
952 if (__next == end() || __tree_.value_comp().key_comp()(__k, __next->first))
953 {
954 // *__hint < __k < *next(__hint)
955 if (__hint.__ptr_->__right_ == nullptr)
956 {
957 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
958 return __parent->__right_;
959 }
960 else
961 {
962 __parent = const_cast<__node_pointer&>(__next.__ptr_);
963 return __parent->__left_;
964 }
965 }
966 // *next(__hint) <= __k
967 return __find_equal_key(__parent, __k);
968 }
969 // else __k == *__hint
970 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
971 return __parent;
972}
973
974// Find __k
975// Set __parent to parent of null leaf and
976// return reference to null leaf iv __k does not exist.
977// If __k exists, set parent to node of __k and return reference to node of __k
978template <class _Key, class _Tp, class _Compare, class _Allocator>
979typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_const_pointer
980map<_Key, _Tp, _Compare, _Allocator>::__find_equal_key(__node_base_const_pointer& __parent,
981 const key_type& __k) const
982{
983 __node_const_pointer __nd = __tree_.__root();
984 if (__nd != nullptr)
985 {
986 while (true)
987 {
988 if (__tree_.value_comp().key_comp()(__k, __nd->__value_.first))
989 {
990 if (__nd->__left_ != nullptr)
991 __nd = static_cast<__node_pointer>(__nd->__left_);
992 else
993 {
994 __parent = __nd;
995 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
996 }
997 }
998 else if (__tree_.value_comp().key_comp()(__nd->__value_.first, __k))
999 {
1000 if (__nd->__right_ != nullptr)
1001 __nd = static_cast<__node_pointer>(__nd->__right_);
1002 else
1003 {
1004 __parent = __nd;
1005 return const_cast<const __node_base_const_pointer&>(__parent->__right_);
1006 }
1007 }
1008 else
1009 {
1010 __parent = __nd;
1011 return __parent;
1012 }
1013 }
1014 }
1015 __parent = __tree_.__end_node();
1016 return const_cast<const __node_base_const_pointer&>(__parent->__left_);
1017}
1018
1019#ifdef _LIBCPP_MOVE
1020
1021template <class _Key, class _Tp, class _Compare, class _Allocator>
1022map<_Key, _Tp, _Compare, _Allocator>::map(map&& __m, const allocator_type& __a)
1023 : __tree_(_STD::move(__m.__tree_), __a)
1024{
1025 if (__a != __m.get_allocator())
1026 {
1027 const_iterator __e = cend();
1028 while (!__m.empty())
1029 __tree_.__insert_unique(__e.__i_,
1030 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1031 }
1032}
1033
1034template <class _Key, class _Tp, class _Compare, class _Allocator>
1035typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1036map<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1037{
1038 __node_allocator& __na = __tree_.__node_alloc();
1039 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1040 __node_traits::construct(__na, addressof(__h->__value_.first));
1041 __h.get_deleter().__first_constructed = true;
1042 __node_traits::construct(__na, addressof(__h->__value_.second));
1043 __h.get_deleter().__second_constructed = true;
1044 return __h;
1045}
1046
1047template <class _Key, class _Tp, class _Compare, class _Allocator>
1048template <class _A0,
1049 class>
1050typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1051map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1052{
1053 __node_allocator& __na = __tree_.__node_alloc();
1054 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1055 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_A0>(__a0));
1056 __h.get_deleter().__first_constructed = true;
1057 __h.get_deleter().__second_constructed = true;
1058 return __h;
1059}
1060
1061template <class _Key, class _Tp, class _Compare, class _Allocator>
1062template <class _A0, class ..._Args,
1063 class>
1064typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1065map<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1066{
1067 __node_allocator& __na = __tree_.__node_alloc();
1068 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1069 __node_traits::construct(__na, addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
1070 __h.get_deleter().__first_constructed = true;
1071 __node_traits::construct(__na, addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
1072 __h.get_deleter().__second_constructed = true;
1073 return __h;
1074}
1075
Howard Hinnant324bb032010-08-22 00:02:43 +00001076#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077
1078template <class _Key, class _Tp, class _Compare, class _Allocator>
1079typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder
1080map<_Key, _Tp, _Compare, _Allocator>::__construct_node(const key_type& __k)
1081{
1082 __node_allocator& __na = __tree_.__node_alloc();
1083 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1084 __node_traits::construct(__na, addressof(__h->__value_.first), __k);
1085 __h.get_deleter().__first_constructed = true;
1086 __node_traits::construct(__na, addressof(__h->__value_.second));
1087 __h.get_deleter().__second_constructed = true;
1088 return _STD::move(__h);
1089}
1090
Howard Hinnant324bb032010-08-22 00:02:43 +00001091#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092
1093template <class _Key, class _Tp, class _Compare, class _Allocator>
1094_Tp&
1095map<_Key, _Tp, _Compare, _Allocator>::operator[](const key_type& __k)
1096{
1097 __node_base_pointer __parent;
1098 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1099 __node_pointer __r = static_cast<__node_pointer>(__child);
1100 if (__child == nullptr)
1101 {
1102 __node_holder __h = __construct_node(__k);
1103 __tree_.__insert_node_at(__parent, __child, __h.get());
1104 __r = __h.release();
1105 }
1106 return __r->__value_.second;
1107}
1108
1109#ifdef _LIBCPP_MOVE
1110
1111template <class _Key, class _Tp, class _Compare, class _Allocator>
1112_Tp&
1113map<_Key, _Tp, _Compare, _Allocator>::operator[](key_type&& __k)
1114{
1115 __node_base_pointer __parent;
1116 __node_base_pointer& __child = __find_equal_key(__parent, __k);
1117 __node_pointer __r = static_cast<__node_pointer>(__child);
1118 if (__child == nullptr)
1119 {
1120 __node_holder __h = __construct_node(_STD::move(__k));
1121 __tree_.__insert_node_at(__parent, __child, __h.get());
1122 __r = __h.release();
1123 }
1124 return __r->__value_.second;
1125}
1126
Howard Hinnant324bb032010-08-22 00:02:43 +00001127#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128
1129template <class _Key, class _Tp, class _Compare, class _Allocator>
1130_Tp&
1131map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k)
1132{
1133 __node_base_pointer __parent;
1134 __node_base_pointer& __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001135#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136 if (__child == nullptr)
1137 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001138#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139 return static_cast<__node_pointer>(__child)->__value_.second;
1140}
1141
1142template <class _Key, class _Tp, class _Compare, class _Allocator>
1143const _Tp&
1144map<_Key, _Tp, _Compare, _Allocator>::at(const key_type& __k) const
1145{
1146 __node_base_const_pointer __parent;
1147 __node_base_const_pointer __child = __find_equal_key(__parent, __k);
Howard Hinnantd4444702010-08-11 17:04:31 +00001148#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001149 if (__child == nullptr)
1150 throw out_of_range("map::at: key not found");
Howard Hinnant324bb032010-08-22 00:02:43 +00001151#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 return static_cast<__node_const_pointer>(__child)->__value_.second;
1153}
1154
1155#ifdef _LIBCPP_MOVE
1156
1157template <class _Key, class _Tp, class _Compare, class _Allocator>
1158template <class _A0, class ..._Args,
1159 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1160 >
1161pair<typename map<_Key, _Tp, _Compare, _Allocator>::iterator, bool>
1162map<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1163{
1164 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1165 _STD::forward<_Args>(__args)...);
1166 pair<iterator, bool> __r = __tree_.__node_insert_unique(__h.get());
1167 if (__r.second)
1168 __h.release();
1169 return __r;
1170}
1171
1172template <class _Key, class _Tp, class _Compare, class _Allocator>
1173template <class _A0, class ..._Args,
1174 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1175 >
1176typename map<_Key, _Tp, _Compare, _Allocator>::iterator
1177map<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1178 _A0&& __a0, _Args&& ...__args)
1179{
1180 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1181 _STD::forward<_Args>(__args)...);
1182 iterator __r = __tree_.__node_insert_unique(__p.__i_, __h.get());
1183 if (__r.__i_.__ptr_ == __h.get())
1184 __h.release();
1185 return __r;
1186}
1187
Howard Hinnant324bb032010-08-22 00:02:43 +00001188#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189
1190template <class _Key, class _Tp, class _Compare, class _Allocator>
1191inline
1192bool
1193operator==(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1194 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1195{
1196 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1197}
1198
1199template <class _Key, class _Tp, class _Compare, class _Allocator>
1200inline
1201bool
1202operator< (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1203 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1204{
1205 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1206}
1207
1208template <class _Key, class _Tp, class _Compare, class _Allocator>
1209inline
1210bool
1211operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1212 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1213{
1214 return !(__x == __y);
1215}
1216
1217template <class _Key, class _Tp, class _Compare, class _Allocator>
1218inline
1219bool
1220operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x,
1221 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1222{
1223 return __y < __x;
1224}
1225
1226template <class _Key, class _Tp, class _Compare, class _Allocator>
1227inline
1228bool
1229operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1230 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1231{
1232 return !(__x < __y);
1233}
1234
1235template <class _Key, class _Tp, class _Compare, class _Allocator>
1236inline
1237bool
1238operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x,
1239 const map<_Key, _Tp, _Compare, _Allocator>& __y)
1240{
1241 return !(__y < __x);
1242}
1243
1244template <class _Key, class _Tp, class _Compare, class _Allocator>
1245inline
1246void
1247swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
1248 map<_Key, _Tp, _Compare, _Allocator>& __y)
1249{
1250 __x.swap(__y);
1251}
1252
1253template <class _Key, class _Tp, class _Compare = less<_Key>,
1254 class _Allocator = allocator<pair<const _Key, _Tp> > >
1255class multimap
1256{
1257public:
1258 // types:
1259 typedef _Key key_type;
1260 typedef _Tp mapped_type;
1261 typedef pair<const key_type, mapped_type> value_type;
1262 typedef _Compare key_compare;
1263 typedef _Allocator allocator_type;
1264 typedef value_type& reference;
1265 typedef const value_type& const_reference;
1266
1267 class value_compare
1268 : public binary_function<value_type, value_type, bool>
1269 {
1270 friend class multimap;
1271 protected:
1272 key_compare comp;
1273
1274 value_compare(key_compare c) : comp(c) {}
1275 public:
1276 bool operator()(const value_type& __x, const value_type& __y) const
1277 {return comp(__x.first, __y.first);}
1278 };
1279
1280private:
1281 typedef pair<key_type, mapped_type> __value_type;
1282 typedef __map_value_compare<key_type, mapped_type, key_compare> __vc;
1283 typedef typename allocator_traits<allocator_type>::template
1284#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
1285 rebind_alloc<__value_type>
1286#else
1287 rebind_alloc<__value_type>::other
1288#endif
1289 __allocator_type;
1290 typedef __tree<__value_type, __vc, __allocator_type> __base;
1291 typedef typename __base::__node_traits __node_traits;
1292 typedef allocator_traits<allocator_type> __alloc_traits;
1293
1294 __base __tree_;
1295
1296public:
1297 typedef typename __alloc_traits::pointer pointer;
1298 typedef typename __alloc_traits::const_pointer const_pointer;
1299 typedef typename __alloc_traits::size_type size_type;
1300 typedef typename __alloc_traits::difference_type difference_type;
1301 typedef __map_iterator<typename __base::iterator> iterator;
1302 typedef __map_const_iterator<typename __base::const_iterator> const_iterator;
1303 typedef _STD::reverse_iterator<iterator> reverse_iterator;
1304 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
1305
1306 explicit multimap(const key_compare& __comp = key_compare())
1307 : __tree_(__vc(__comp)) {}
1308
1309 explicit multimap(const key_compare& __comp, const allocator_type& __a)
1310 : __tree_(__vc(__comp), __a) {}
1311
1312 template <class _InputIterator>
1313 multimap(_InputIterator __f, _InputIterator __l,
1314 const key_compare& __comp = key_compare())
1315 : __tree_(__vc(__comp))
1316 {
1317 insert(__f, __l);
1318 }
1319
1320 template <class _InputIterator>
1321 multimap(_InputIterator __f, _InputIterator __l,
1322 const key_compare& __comp, const allocator_type& __a)
1323 : __tree_(__vc(__comp), __a)
1324 {
1325 insert(__f, __l);
1326 }
1327
1328 multimap(const multimap& __m)
1329 : __tree_(__m.__tree_.value_comp(),
1330 __alloc_traits::select_on_container_copy_construction(__m.__tree_.__alloc()))
1331 {
1332 insert(__m.begin(), __m.end());
1333 }
1334
1335#ifdef _LIBCPP_MOVE
1336
1337 multimap(multimap&& __m)
1338 : __tree_(_STD::move(__m.__tree_))
1339 {
1340 }
1341
1342 multimap(multimap&& __m, const allocator_type& __a);
1343
1344 multimap(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
1345 : __tree_(__vc(__comp))
1346 {
1347 insert(__il.begin(), __il.end());
1348 }
1349
1350 multimap(initializer_list<value_type> __il, const key_compare& __comp, const allocator_type& __a)
1351 : __tree_(__vc(__comp), __a)
1352 {
1353 insert(__il.begin(), __il.end());
1354 }
1355
1356 multimap& operator=(multimap&& __m)
1357 {
1358 __tree_ = _STD::move(__m.__tree_);
1359 return *this;
1360 }
1361
1362 multimap& operator=(initializer_list<value_type> __il)
1363 {
1364 __tree_.__assign_multi(__il.begin(), __il.end());
1365 return *this;
1366 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001367#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368
1369 explicit multimap(const allocator_type& __a)
1370 : __tree_(__a)
1371 {
1372 }
1373
1374 multimap(const multimap& __m, const allocator_type& __a)
1375 : __tree_(__m.__tree_.value_comp(), __a)
1376 {
1377 insert(__m.begin(), __m.end());
1378 }
1379
1380 iterator begin() {return __tree_.begin();}
1381 const_iterator begin() const {return __tree_.begin();}
1382 iterator end() {return __tree_.end();}
1383 const_iterator end() const {return __tree_.end();}
1384
1385 reverse_iterator rbegin() {return reverse_iterator(end());}
1386 const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1387 reverse_iterator rend() {return reverse_iterator(begin());}
1388 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1389
1390 const_iterator cbegin() const {return begin();}
1391 const_iterator cend() const {return end();}
1392 const_reverse_iterator crbegin() const {return rbegin();}
1393 const_reverse_iterator crend() const {return rend();}
1394
1395 bool empty() const {return __tree_.size() == 0;}
1396 size_type size() const {return __tree_.size();}
1397 size_type max_size() const {return __tree_.max_size();}
1398
1399 allocator_type get_allocator() const {return __tree_.__alloc();}
1400 key_compare key_comp() const {return __tree_.value_comp().key_comp();}
1401 value_compare value_comp() const {return value_compare(__tree_.value_comp().key_comp());}
1402
1403#ifdef _LIBCPP_MOVE
1404
1405 iterator emplace() {return __tree_.__emplace_multi();}
1406
1407 template <class _A0,
1408 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1409 iterator
1410 emplace(_A0&& __a0)
1411 {return __tree_.__emplace_multi(_STD::forward<_A0>(__a0));}
1412
1413 template <class _A0, class ..._Args,
1414 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1415 iterator
1416 emplace(_A0&& __a0, _Args&& ...__args);
1417
1418 iterator emplace_hint(const_iterator __p)
1419 {return __tree_.__emplace_hint_multi(__p.__i_);}
1420
1421 template <class _A0,
1422 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1423 iterator
1424 emplace_hint(const_iterator __p, _A0&& __a0)
1425 {return __tree_.__emplace_hint_multi(__p.__i_, _STD::forward<_A0>(__a0));}
1426
1427 template <class _A0, class ..._Args,
1428 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1429 iterator
1430 emplace_hint(const_iterator __p, _A0&& __a0, _Args&& ...__args);
1431
1432 template <class _P,
1433 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
1434 iterator insert(_P&& __p)
1435 {return __tree_.__insert_multi(_STD::forward<_P>(__p));}
1436
1437 template <class _P,
1438 class = typename enable_if<is_convertible<_P, value_type>::value>::type>
1439 iterator insert(const_iterator __pos, _P&& __p)
1440 {return __tree_.__insert_multi(__pos.__i_, _STD::forward<_P>(__p));}
1441
Howard Hinnant324bb032010-08-22 00:02:43 +00001442#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443
1444 iterator insert(const value_type& __v) {return __tree_.__insert_multi(__v);}
1445
1446 iterator insert(const_iterator __p, const value_type& __v)
1447 {return __tree_.__insert_multi(__p.__i_, __v);}
1448
1449 template <class _InputIterator>
1450 void insert(_InputIterator __f, _InputIterator __l)
1451 {
1452 for (const_iterator __e = cend(); __f != __l; ++__f)
1453 __tree_.__insert_multi(__e.__i_, *__f);
1454 }
1455
1456 void insert(initializer_list<value_type> __il)
1457 {insert(__il.begin(), __il.end());}
1458
1459 iterator erase(const_iterator __p) {return __tree_.erase(__p.__i_);}
1460 size_type erase(const key_type& __k) {return __tree_.__erase_multi(__k);}
1461 iterator erase(const_iterator __f, const_iterator __l)
1462 {return __tree_.erase(__f.__i_, __l.__i_);}
1463 void clear() {__tree_.clear();}
1464
1465 void swap(multimap& __m) {__tree_.swap(__m.__tree_);}
1466
1467 iterator find(const key_type& __k) {return __tree_.find(__k);}
1468 const_iterator find(const key_type& __k) const {return __tree_.find(__k);}
1469 size_type count(const key_type& __k) const
1470 {return __tree_.__count_multi(__k);}
1471 iterator lower_bound(const key_type& __k)
1472 {return __tree_.lower_bound(__k);}
1473 const_iterator lower_bound(const key_type& __k) const
1474 {return __tree_.lower_bound(__k);}
1475 iterator upper_bound(const key_type& __k)
1476 {return __tree_.upper_bound(__k);}
1477 const_iterator upper_bound(const key_type& __k) const
1478 {return __tree_.upper_bound(__k);}
1479 pair<iterator,iterator> equal_range(const key_type& __k)
1480 {return __tree_.__equal_range_multi(__k);}
1481 pair<const_iterator,const_iterator> equal_range(const key_type& __k) const
1482 {return __tree_.__equal_range_multi(__k);}
1483
1484private:
1485 typedef typename __base::__node __node;
1486 typedef typename __base::__node_allocator __node_allocator;
1487 typedef typename __base::__node_pointer __node_pointer;
1488 typedef typename __base::__node_const_pointer __node_const_pointer;
1489 typedef __map_node_destructor<__node_allocator> _D;
1490 typedef unique_ptr<__node, _D> __node_holder;
1491
1492#ifdef _LIBCPP_MOVE
1493 __node_holder __construct_node();
1494 template <class _A0,
1495 class = typename enable_if<is_convertible<_A0, value_type>::value>::type>
1496 __node_holder __construct_node(_A0&& __a0);
1497 template <class _A0, class ..._Args,
1498 class = typename enable_if<is_convertible<_A0, key_type>::value>::type>
1499 __node_holder __construct_node(_A0&& __a0, _Args&& ...__args);
Howard Hinnant324bb032010-08-22 00:02:43 +00001500#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501};
1502
1503#ifdef _LIBCPP_MOVE
1504
1505template <class _Key, class _Tp, class _Compare, class _Allocator>
1506multimap<_Key, _Tp, _Compare, _Allocator>::multimap(multimap&& __m, const allocator_type& __a)
1507 : __tree_(_STD::move(__m.__tree_), __a)
1508{
1509 if (__a != __m.get_allocator())
1510 {
1511 const_iterator __e = cend();
1512 while (!__m.empty())
1513 __tree_.__insert_multi(__e.__i_,
1514 _STD::move(__m.__tree_.remove(__m.begin().__i_)->__value_));
1515 }
1516}
1517
1518template <class _Key, class _Tp, class _Compare, class _Allocator>
1519typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1520multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node()
1521{
1522 __node_allocator& __na = __tree_.__node_alloc();
1523 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1524 __node_traits::construct(__na, addressof(__h->__value_.first));
1525 __h.get_deleter().__first_constructed = true;
1526 __node_traits::construct(__na, addressof(__h->__value_.second));
1527 __h.get_deleter().__second_constructed = true;
1528 return __h;
1529}
1530
1531template <class _Key, class _Tp, class _Compare, class _Allocator>
1532template <class _A0,
1533 class // = typename enable_if<is_convertible<_A0, value_type>::value>::type
1534 >
1535typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1536multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0)
1537{
1538 __node_allocator& __na = __tree_.__node_alloc();
1539 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1540 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_A0>(__a0));
1541 __h.get_deleter().__first_constructed = true;
1542 __h.get_deleter().__second_constructed = true;
1543 return __h;
1544}
1545
1546template <class _Key, class _Tp, class _Compare, class _Allocator>
1547template <class _A0, class ..._Args,
1548 class // = typename enable_if<is_convertible<_A0, key_type>::value>::type
1549 >
1550typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder
1551multimap<_Key, _Tp, _Compare, _Allocator>::__construct_node(_A0&& __a0, _Args&& ...__args)
1552{
1553 __node_allocator& __na = __tree_.__node_alloc();
1554 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1555 __node_traits::construct(__na, addressof(__h->__value_.first), _STD::forward<_A0>(__a0));
1556 __h.get_deleter().__first_constructed = true;
1557 __node_traits::construct(__na, addressof(__h->__value_.second), _STD::forward<_Args>(__args)...);
1558 __h.get_deleter().__second_constructed = true;
1559 return __h;
1560}
1561
Howard Hinnant324bb032010-08-22 00:02:43 +00001562#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563
1564#ifdef _LIBCPP_MOVE
1565
1566template <class _Key, class _Tp, class _Compare, class _Allocator>
1567template <class _A0, class ..._Args,
1568 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1569 >
1570typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1571multimap<_Key, _Tp, _Compare, _Allocator>::emplace(_A0&& __a0, _Args&& ...__args)
1572{
1573 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1574 _STD::forward<_Args>(__args)...);
1575 iterator __r = __tree_.__node_insert_multi(__h.get());
1576 __h.release();
1577 return __r;
1578}
1579
1580template <class _Key, class _Tp, class _Compare, class _Allocator>
1581template <class _A0, class ..._Args,
1582 class //= typename enable_if<is_convertible<_A0, _Key>::value>::type
1583 >
1584typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator
1585multimap<_Key, _Tp, _Compare, _Allocator>::emplace_hint(const_iterator __p,
1586 _A0&& __a0,
1587 _Args&& ...__args)
1588{
1589 __node_holder __h = __construct_node(_STD::forward<_A0>(__a0),
1590 _STD::forward<_Args>(__args)...);
1591 iterator __r = __tree_.__node_insert_multi(__p.__i_, __h.get());
1592 __h.release();
1593 return __r;
1594}
1595
Howard Hinnant324bb032010-08-22 00:02:43 +00001596#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597
1598template <class _Key, class _Tp, class _Compare, class _Allocator>
1599inline
1600bool
1601operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1602 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1603{
1604 return __x.size() == __y.size() && _STD::equal(__x.begin(), __x.end(), __y.begin());
1605}
1606
1607template <class _Key, class _Tp, class _Compare, class _Allocator>
1608inline
1609bool
1610operator< (const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1611 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1612{
1613 return _STD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
1614}
1615
1616template <class _Key, class _Tp, class _Compare, class _Allocator>
1617inline
1618bool
1619operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1620 const multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1621{
1622 return !(__x == __y);
1623}
1624
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 __y < __x;
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 !(__x < __y);
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 !(__y < __x);
1650}
1651
1652template <class _Key, class _Tp, class _Compare, class _Allocator>
1653inline
1654void
1655swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,
1656 multimap<_Key, _Tp, _Compare, _Allocator>& __y)
1657{
1658 __x.swap(__y);
1659}
1660
1661_LIBCPP_END_NAMESPACE_STD
1662
1663#endif // _LIBCPP_MAP