Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------- map ------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 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 | |
| 18 | namespace std |
| 19 | { |
| 20 | |
| 21 | template <class Key, class T, class Compare = less<Key>, |
| 22 | class Allocator = allocator<pair<const Key, T>>> |
| 23 | class map |
| 24 | { |
| 25 | public: |
| 26 | // types: |
| 27 | typedef Key key_type; |
| 28 | typedef T mapped_type; |
| 29 | typedef pair<const key_type, mapped_type> value_type; |
| 30 | typedef Compare key_compare; |
| 31 | typedef Allocator allocator_type; |
| 32 | typedef typename allocator_type::reference reference; |
| 33 | typedef typename allocator_type::const_reference const_reference; |
| 34 | typedef typename allocator_type::pointer pointer; |
| 35 | typedef typename allocator_type::const_pointer const_pointer; |
| 36 | typedef typename allocator_type::size_type size_type; |
| 37 | typedef typename allocator_type::difference_type difference_type; |
| 38 | |
| 39 | typedef implementation-defined iterator; |
| 40 | typedef implementation-defined const_iterator; |
| 41 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 42 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 43 | |
| 44 | class value_compare |
| 45 | : public binary_function<value_type, value_type, bool> |
| 46 | { |
| 47 | friend class map; |
| 48 | protected: |
| 49 | key_compare comp; |
| 50 | |
| 51 | value_compare(key_compare c); |
| 52 | public: |
| 53 | bool operator()(const value_type& x, const value_type& y) const; |
| 54 | }; |
| 55 | |
| 56 | // construct/copy/destroy: |
| 57 | map(); |
| 58 | 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 | |
| 146 | template <class Key, class T, class Compare, class Allocator> |
| 147 | bool |
| 148 | operator==(const map<Key, T, Compare, Allocator>& x, |
| 149 | const map<Key, T, Compare, Allocator>& y); |
| 150 | |
| 151 | template <class Key, class T, class Compare, class Allocator> |
| 152 | bool |
| 153 | operator< (const map<Key, T, Compare, Allocator>& x, |
| 154 | const map<Key, T, Compare, Allocator>& y); |
| 155 | |
| 156 | template <class Key, class T, class Compare, class Allocator> |
| 157 | bool |
| 158 | operator!=(const map<Key, T, Compare, Allocator>& x, |
| 159 | const map<Key, T, Compare, Allocator>& y); |
| 160 | |
| 161 | template <class Key, class T, class Compare, class Allocator> |
| 162 | bool |
| 163 | operator> (const map<Key, T, Compare, Allocator>& x, |
| 164 | const map<Key, T, Compare, Allocator>& y); |
| 165 | |
| 166 | template <class Key, class T, class Compare, class Allocator> |
| 167 | bool |
| 168 | operator>=(const map<Key, T, Compare, Allocator>& x, |
| 169 | const map<Key, T, Compare, Allocator>& y); |
| 170 | |
| 171 | template <class Key, class T, class Compare, class Allocator> |
| 172 | bool |
| 173 | operator<=(const map<Key, T, Compare, Allocator>& x, |
| 174 | const map<Key, T, Compare, Allocator>& y); |
| 175 | |
| 176 | // specialized algorithms: |
| 177 | template <class Key, class T, class Compare, class Allocator> |
| 178 | void |
| 179 | swap(map<Key, T, Compare, Allocator>& x, map<Key, T, Compare, Allocator>& y); |
| 180 | |
| 181 | template <class Key, class T, class Compare = less<Key>, |
| 182 | class Allocator = allocator<pair<const Key, T>>> |
| 183 | class multimap |
| 184 | { |
| 185 | public: |
| 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 | |
| 297 | template <class Key, class T, class Compare, class Allocator> |
| 298 | bool |
| 299 | operator==(const multimap<Key, T, Compare, Allocator>& x, |
| 300 | const multimap<Key, T, Compare, Allocator>& y); |
| 301 | |
| 302 | template <class Key, class T, class Compare, class Allocator> |
| 303 | bool |
| 304 | operator< (const multimap<Key, T, Compare, Allocator>& x, |
| 305 | const multimap<Key, T, Compare, Allocator>& y); |
| 306 | |
| 307 | template <class Key, class T, class Compare, class Allocator> |
| 308 | bool |
| 309 | operator!=(const multimap<Key, T, Compare, Allocator>& x, |
| 310 | const multimap<Key, T, Compare, Allocator>& y); |
| 311 | |
| 312 | template <class Key, class T, class Compare, class Allocator> |
| 313 | bool |
| 314 | operator> (const multimap<Key, T, Compare, Allocator>& x, |
| 315 | const multimap<Key, T, Compare, Allocator>& y); |
| 316 | |
| 317 | template <class Key, class T, class Compare, class Allocator> |
| 318 | bool |
| 319 | operator>=(const multimap<Key, T, Compare, Allocator>& x, |
| 320 | const multimap<Key, T, Compare, Allocator>& y); |
| 321 | |
| 322 | template <class Key, class T, class Compare, class Allocator> |
| 323 | bool |
| 324 | operator<=(const multimap<Key, T, Compare, Allocator>& x, |
| 325 | const multimap<Key, T, Compare, Allocator>& y); |
| 326 | |
| 327 | // specialized algorithms: |
| 328 | template <class Key, class T, class Compare, class Allocator> |
| 329 | void |
| 330 | swap(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 | |
| 349 | template <class _Key, class _Tp, class _Compare, bool = is_empty<_Compare>::value> |
| 350 | class __map_value_compare |
| 351 | : private _Compare |
| 352 | { |
| 353 | typedef pair<_Key, _Tp> _P; |
| 354 | typedef pair<const _Key, _Tp> _CP; |
| 355 | public: |
| 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 378 | // 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 | |
| 389 | template <class _Key, class _Tp, class _Compare> |
| 390 | class __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 | |
| 397 | public: |
| 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 Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 421 | // 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 | |
| 432 | template <class _Allocator> |
| 433 | class __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; |
| 438 | public: |
| 439 | typedef typename __alloc_traits::pointer pointer; |
| 440 | private: |
| 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 | |
| 448 | public: |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 458 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 466 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 467 | |
| 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 | |
| 479 | template <class, class, class, class> class map; |
| 480 | template <class, class, class, class> class multimap; |
| 481 | template <class> class __map_const_iterator; |
| 482 | |
| 483 | template <class _TreeIterator> |
| 484 | class __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; |
| 491 | public: |
| 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 | |
| 537 | template <class _TreeIterator> |
| 538 | class __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; |
| 545 | public: |
| 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 | |
| 594 | template <class _Key, class _Tp, class _Compare = less<_Key>, |
| 595 | class _Allocator = allocator<pair<const _Key, _Tp> > > |
| 596 | class map |
| 597 | { |
| 598 | public: |
| 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 | |
| 621 | private: |
| 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 | |
| 637 | public: |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 675 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 708 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 709 | |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 741 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 752 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 763 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 764 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 765 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 770 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 771 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 772 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 782 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 783 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 784 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 789 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 790 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 791 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 801 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 802 | |
| 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 | |
| 846 | private: |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 856 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 857 | __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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 861 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 862 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 865 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 866 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 867 | __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 |
| 883 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 884 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer& |
| 885 | map<_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 |
| 931 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 932 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_pointer& |
| 933 | map<_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 |
| 988 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 989 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_base_const_pointer |
| 990 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1029 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1030 | |
| 1031 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1032 | map<_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 | |
| 1044 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1045 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1046 | map<_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 | |
| 1057 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1058 | template <class _A0, |
| 1059 | class> |
| 1060 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1061 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1071 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1072 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1073 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1074 | template <class _A0, class ..._Args, |
| 1075 | class> |
| 1076 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1077 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1088 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1089 | |
| 1090 | #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1091 | |
| 1092 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1093 | typename map<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1094 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1105 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1106 | |
| 1107 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1108 | _Tp& |
| 1109 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1123 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1124 | |
| 1125 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1126 | _Tp& |
| 1127 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1141 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1142 | |
| 1143 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1144 | _Tp& |
| 1145 | map<_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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1149 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1150 | if (__child == nullptr) |
| 1151 | throw out_of_range("map::at: key not found"); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1152 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1153 | return static_cast<__node_pointer>(__child)->__value_.second; |
| 1154 | } |
| 1155 | |
| 1156 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1157 | const _Tp& |
| 1158 | map<_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 Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 1162 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1163 | if (__child == nullptr) |
| 1164 | throw out_of_range("map::at: key not found"); |
Howard Hinnant | 324bb03 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1165 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1166 | return static_cast<__node_const_pointer>(__child)->__value_.second; |
| 1167 | } |
| 1168 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1169 | #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1170 | |
| 1171 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1172 | template <class _A0, class ..._Args, |
| 1173 | class //= typename enable_if<is_convertible<_A0, _Key>::value>::type |
| 1174 | > |
| 1175 | pair<typename map<_Key, _Tp, _Compare, _Allocator>::iterator, bool> |
| 1176 | map<_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 | |
| 1186 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1187 | template <class _A0, class ..._Args, |
| 1188 | class //= typename enable_if<is_convertible<_A0, _Key>::value>::type |
| 1189 | > |
| 1190 | typename map<_Key, _Tp, _Compare, _Allocator>::iterator |
| 1191 | map<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1202 | #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1203 | |
| 1204 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1205 | inline |
| 1206 | bool |
| 1207 | operator==(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 | |
| 1213 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1214 | inline |
| 1215 | bool |
| 1216 | operator< (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 | |
| 1222 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1223 | inline |
| 1224 | bool |
| 1225 | operator!=(const map<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1226 | const map<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1227 | { |
| 1228 | return !(__x == __y); |
| 1229 | } |
| 1230 | |
| 1231 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1232 | inline |
| 1233 | bool |
| 1234 | operator> (const map<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1235 | const map<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1236 | { |
| 1237 | return __y < __x; |
| 1238 | } |
| 1239 | |
| 1240 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1241 | inline |
| 1242 | bool |
| 1243 | operator>=(const map<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1244 | const map<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1245 | { |
| 1246 | return !(__x < __y); |
| 1247 | } |
| 1248 | |
| 1249 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1250 | inline |
| 1251 | bool |
| 1252 | operator<=(const map<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1253 | const map<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1254 | { |
| 1255 | return !(__y < __x); |
| 1256 | } |
| 1257 | |
| 1258 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1259 | inline |
| 1260 | void |
| 1261 | swap(map<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1262 | map<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1263 | { |
| 1264 | __x.swap(__y); |
| 1265 | } |
| 1266 | |
| 1267 | template <class _Key, class _Tp, class _Compare = less<_Key>, |
| 1268 | class _Allocator = allocator<pair<const _Key, _Tp> > > |
| 1269 | class multimap |
| 1270 | { |
| 1271 | public: |
| 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 | |
| 1294 | private: |
| 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 | |
| 1310 | public: |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1349 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1350 | |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1381 | #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1382 | |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1417 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1418 | |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1427 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1428 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1429 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1434 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1435 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1436 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1445 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1446 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1447 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1452 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1453 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1454 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1464 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1465 | |
| 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 | |
| 1506 | private: |
| 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1514 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1515 | __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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1519 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1520 | 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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1523 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1524 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1525 | }; |
| 1526 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1527 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1528 | |
| 1529 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1530 | multimap<_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 | |
| 1542 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1543 | typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1544 | multimap<_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 | |
| 1555 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1556 | template <class _A0, |
| 1557 | class // = typename enable_if<is_convertible<_A0, value_type>::value>::type |
| 1558 | > |
| 1559 | typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1560 | multimap<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1570 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 1571 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1572 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1573 | template <class _A0, class ..._Args, |
| 1574 | class // = typename enable_if<is_convertible<_A0, key_type>::value>::type |
| 1575 | > |
| 1576 | typename multimap<_Key, _Tp, _Compare, _Allocator>::__node_holder |
| 1577 | multimap<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1588 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 1589 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1590 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1591 | #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1592 | |
| 1593 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1594 | template <class _A0, class ..._Args, |
| 1595 | class //= typename enable_if<is_convertible<_A0, _Key>::value>::type |
| 1596 | > |
| 1597 | typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator |
| 1598 | multimap<_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 | |
| 1607 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1608 | template <class _A0, class ..._Args, |
| 1609 | class //= typename enable_if<is_convertible<_A0, _Key>::value>::type |
| 1610 | > |
| 1611 | typename multimap<_Key, _Tp, _Compare, _Allocator>::iterator |
| 1612 | multimap<_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 Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame^] | 1623 | #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1624 | |
| 1625 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1626 | inline |
| 1627 | bool |
| 1628 | operator==(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 | |
| 1634 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1635 | inline |
| 1636 | bool |
| 1637 | operator< (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 | |
| 1643 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1644 | inline |
| 1645 | bool |
| 1646 | operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1647 | const multimap<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1648 | { |
| 1649 | return !(__x == __y); |
| 1650 | } |
| 1651 | |
| 1652 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1653 | inline |
| 1654 | bool |
| 1655 | operator> (const multimap<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1656 | const multimap<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1657 | { |
| 1658 | return __y < __x; |
| 1659 | } |
| 1660 | |
| 1661 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1662 | inline |
| 1663 | bool |
| 1664 | operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1665 | const multimap<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1666 | { |
| 1667 | return !(__x < __y); |
| 1668 | } |
| 1669 | |
| 1670 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1671 | inline |
| 1672 | bool |
| 1673 | operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __x, |
| 1674 | const multimap<_Key, _Tp, _Compare, _Allocator>& __y) |
| 1675 | { |
| 1676 | return !(__y < __x); |
| 1677 | } |
| 1678 | |
| 1679 | template <class _Key, class _Tp, class _Compare, class _Allocator> |
| 1680 | inline |
| 1681 | void |
| 1682 | swap(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 |