|
libstdc++
|
00001 // Map implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /* 00026 * 00027 * Copyright (c) 1994 00028 * Hewlett-Packard Company 00029 * 00030 * Permission to use, copy, modify, distribute and sell this software 00031 * and its documentation for any purpose is hereby granted without fee, 00032 * provided that the above copyright notice appear in all copies and 00033 * that both that copyright notice and this permission notice appear 00034 * in supporting documentation. Hewlett-Packard Company makes no 00035 * representations about the suitability of this software for any 00036 * purpose. It is provided "as is" without express or implied warranty. 00037 * 00038 * 00039 * Copyright (c) 1996,1997 00040 * Silicon Graphics Computer Systems, Inc. 00041 * 00042 * Permission to use, copy, modify, distribute and sell this software 00043 * and its documentation for any purpose is hereby granted without fee, 00044 * provided that the above copyright notice appear in all copies and 00045 * that both that copyright notice and this permission notice appear 00046 * in supporting documentation. Silicon Graphics makes no 00047 * representations about the suitability of this software for any 00048 * purpose. It is provided "as is" without express or implied warranty. 00049 */ 00050 00051 /** @file bits/stl_map.h 00052 * This is an internal header file, included by other library headers. 00053 * Do not attempt to use it directly. @headername{map} 00054 */ 00055 00056 #ifndef _STL_MAP_H 00057 #define _STL_MAP_H 1 00058 00059 #include <bits/functexcept.h> 00060 #include <bits/concept_check.h> 00061 #if __cplusplus >= 201103L 00062 #include <initializer_list> 00063 #include <tuple> 00064 #endif 00065 00066 namespace std _GLIBCXX_VISIBILITY(default) 00067 { 00068 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00069 00070 /** 00071 * @brief A standard container made up of (key,value) pairs, which can be 00072 * retrieved based on a key, in logarithmic time. 00073 * 00074 * @ingroup associative_containers 00075 * 00076 * @tparam _Key Type of key objects. 00077 * @tparam _Tp Type of mapped objects. 00078 * @tparam _Compare Comparison function object type, defaults to less<_Key>. 00079 * @tparam _Alloc Allocator type, defaults to 00080 * allocator<pair<const _Key, _Tp>. 00081 * 00082 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00083 * <a href="tables.html#66">reversible container</a>, and an 00084 * <a href="tables.html#69">associative container</a> (using unique keys). 00085 * For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the 00086 * value_type is std::pair<const Key,T>. 00087 * 00088 * Maps support bidirectional iterators. 00089 * 00090 * The private tree data is declared exactly the same way for map and 00091 * multimap; the distinction is made entirely in how the tree functions are 00092 * called (*_unique versus *_equal, same as the standard). 00093 */ 00094 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>, 00095 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > > 00096 class map 00097 { 00098 public: 00099 typedef _Key key_type; 00100 typedef _Tp mapped_type; 00101 typedef std::pair<const _Key, _Tp> value_type; 00102 typedef _Compare key_compare; 00103 typedef _Alloc allocator_type; 00104 00105 private: 00106 // concept requirements 00107 typedef typename _Alloc::value_type _Alloc_value_type; 00108 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00109 __glibcxx_class_requires4(_Compare, bool, _Key, _Key, 00110 _BinaryFunctionConcept) 00111 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept) 00112 00113 public: 00114 class value_compare 00115 : public std::binary_function<value_type, value_type, bool> 00116 { 00117 friend class map<_Key, _Tp, _Compare, _Alloc>; 00118 protected: 00119 _Compare comp; 00120 00121 value_compare(_Compare __c) 00122 : comp(__c) { } 00123 00124 public: 00125 bool operator()(const value_type& __x, const value_type& __y) const 00126 { return comp(__x.first, __y.first); } 00127 }; 00128 00129 private: 00130 /// This turns a red-black tree into a [multi]map. 00131 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template 00132 rebind<value_type>::other _Pair_alloc_type; 00133 00134 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>, 00135 key_compare, _Pair_alloc_type> _Rep_type; 00136 00137 /// The actual tree structure. 00138 _Rep_type _M_t; 00139 00140 typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits; 00141 00142 public: 00143 // many of these are specified differently in ISO, but the following are 00144 // "functionally equivalent" 00145 typedef typename _Alloc_traits::pointer pointer; 00146 typedef typename _Alloc_traits::const_pointer const_pointer; 00147 typedef typename _Alloc_traits::reference reference; 00148 typedef typename _Alloc_traits::const_reference const_reference; 00149 typedef typename _Rep_type::iterator iterator; 00150 typedef typename _Rep_type::const_iterator const_iterator; 00151 typedef typename _Rep_type::size_type size_type; 00152 typedef typename _Rep_type::difference_type difference_type; 00153 typedef typename _Rep_type::reverse_iterator reverse_iterator; 00154 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00155 00156 // [23.3.1.1] construct/copy/destroy 00157 // (get_allocator() is also listed in this section) 00158 00159 /** 00160 * @brief Default constructor creates no elements. 00161 */ 00162 map() 00163 #if __cplusplus >= 201103L 00164 noexcept(is_nothrow_default_constructible<allocator_type>::value 00165 && is_nothrow_default_constructible<key_compare>::value) 00166 #endif 00167 : _M_t() { } 00168 00169 /** 00170 * @brief Creates a %map with no elements. 00171 * @param __comp A comparison object. 00172 * @param __a An allocator object. 00173 */ 00174 explicit 00175 map(const _Compare& __comp, 00176 const allocator_type& __a = allocator_type()) 00177 : _M_t(__comp, _Pair_alloc_type(__a)) { } 00178 00179 /** 00180 * @brief %Map copy constructor. 00181 * @param __x A %map of identical element and allocator types. 00182 * 00183 * The newly-created %map uses a copy of the allocation object 00184 * used by @a __x. 00185 */ 00186 map(const map& __x) 00187 : _M_t(__x._M_t) { } 00188 00189 #if __cplusplus >= 201103L 00190 /** 00191 * @brief %Map move constructor. 00192 * @param __x A %map of identical element and allocator types. 00193 * 00194 * The newly-created %map contains the exact contents of @a __x. 00195 * The contents of @a __x are a valid, but unspecified %map. 00196 */ 00197 map(map&& __x) 00198 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00199 : _M_t(std::move(__x._M_t)) { } 00200 00201 /** 00202 * @brief Builds a %map from an initializer_list. 00203 * @param __l An initializer_list. 00204 * @param __comp A comparison object. 00205 * @param __a An allocator object. 00206 * 00207 * Create a %map consisting of copies of the elements in the 00208 * initializer_list @a __l. 00209 * This is linear in N if the range is already sorted, and NlogN 00210 * otherwise (where N is @a __l.size()). 00211 */ 00212 map(initializer_list<value_type> __l, 00213 const _Compare& __comp = _Compare(), 00214 const allocator_type& __a = allocator_type()) 00215 : _M_t(__comp, _Pair_alloc_type(__a)) 00216 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00217 00218 /// Allocator-extended default constructor. 00219 explicit 00220 map(const allocator_type& __a) 00221 : _M_t(_Compare(), _Pair_alloc_type(__a)) { } 00222 00223 /// Allocator-extended copy constructor. 00224 map(const map& __m, const allocator_type& __a) 00225 : _M_t(__m._M_t, _Pair_alloc_type(__a)) { } 00226 00227 /// Allocator-extended move constructor. 00228 map(map&& __m, const allocator_type& __a) 00229 noexcept(is_nothrow_copy_constructible<_Compare>::value 00230 && _Alloc_traits::_S_always_equal()) 00231 : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { } 00232 00233 /// Allocator-extended initialier-list constructor. 00234 map(initializer_list<value_type> __l, const allocator_type& __a) 00235 : _M_t(_Compare(), _Pair_alloc_type(__a)) 00236 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00237 00238 /// Allocator-extended range constructor. 00239 template<typename _InputIterator> 00240 map(_InputIterator __first, _InputIterator __last, 00241 const allocator_type& __a) 00242 : _M_t(_Compare(), _Pair_alloc_type(__a)) 00243 { _M_t._M_insert_unique(__first, __last); } 00244 #endif 00245 00246 /** 00247 * @brief Builds a %map from a range. 00248 * @param __first An input iterator. 00249 * @param __last An input iterator. 00250 * 00251 * Create a %map consisting of copies of the elements from 00252 * [__first,__last). This is linear in N if the range is 00253 * already sorted, and NlogN otherwise (where N is 00254 * distance(__first,__last)). 00255 */ 00256 template<typename _InputIterator> 00257 map(_InputIterator __first, _InputIterator __last) 00258 : _M_t() 00259 { _M_t._M_insert_unique(__first, __last); } 00260 00261 /** 00262 * @brief Builds a %map from a range. 00263 * @param __first An input iterator. 00264 * @param __last An input iterator. 00265 * @param __comp A comparison functor. 00266 * @param __a An allocator object. 00267 * 00268 * Create a %map consisting of copies of the elements from 00269 * [__first,__last). This is linear in N if the range is 00270 * already sorted, and NlogN otherwise (where N is 00271 * distance(__first,__last)). 00272 */ 00273 template<typename _InputIterator> 00274 map(_InputIterator __first, _InputIterator __last, 00275 const _Compare& __comp, 00276 const allocator_type& __a = allocator_type()) 00277 : _M_t(__comp, _Pair_alloc_type(__a)) 00278 { _M_t._M_insert_unique(__first, __last); } 00279 00280 // FIXME There is no dtor declared, but we should have something 00281 // generated by Doxygen. I don't know what tags to add to this 00282 // paragraph to make that happen: 00283 /** 00284 * The dtor only erases the elements, and note that if the elements 00285 * themselves are pointers, the pointed-to memory is not touched in any 00286 * way. Managing the pointer is the user's responsibility. 00287 */ 00288 00289 /** 00290 * @brief %Map assignment operator. 00291 * @param __x A %map of identical element and allocator types. 00292 * 00293 * All the elements of @a __x are copied, but unlike the copy 00294 * constructor, the allocator object is not copied. 00295 */ 00296 map& 00297 operator=(const map& __x) 00298 { 00299 _M_t = __x._M_t; 00300 return *this; 00301 } 00302 00303 #if __cplusplus >= 201103L 00304 /// Move assignment operator. 00305 map& 00306 operator=(map&&) = default; 00307 00308 /** 00309 * @brief %Map list assignment operator. 00310 * @param __l An initializer_list. 00311 * 00312 * This function fills a %map with copies of the elements in the 00313 * initializer list @a __l. 00314 * 00315 * Note that the assignment completely changes the %map and 00316 * that the resulting %map's size is the same as the number 00317 * of elements assigned. Old data may be lost. 00318 */ 00319 map& 00320 operator=(initializer_list<value_type> __l) 00321 { 00322 _M_t._M_assign_unique(__l.begin(), __l.end()); 00323 return *this; 00324 } 00325 #endif 00326 00327 /// Get a copy of the memory allocation object. 00328 allocator_type 00329 get_allocator() const _GLIBCXX_NOEXCEPT 00330 { return allocator_type(_M_t.get_allocator()); } 00331 00332 // iterators 00333 /** 00334 * Returns a read/write iterator that points to the first pair in the 00335 * %map. 00336 * Iteration is done in ascending order according to the keys. 00337 */ 00338 iterator 00339 begin() _GLIBCXX_NOEXCEPT 00340 { return _M_t.begin(); } 00341 00342 /** 00343 * Returns a read-only (constant) iterator that points to the first pair 00344 * in the %map. Iteration is done in ascending order according to the 00345 * keys. 00346 */ 00347 const_iterator 00348 begin() const _GLIBCXX_NOEXCEPT 00349 { return _M_t.begin(); } 00350 00351 /** 00352 * Returns a read/write iterator that points one past the last 00353 * pair in the %map. Iteration is done in ascending order 00354 * according to the keys. 00355 */ 00356 iterator 00357 end() _GLIBCXX_NOEXCEPT 00358 { return _M_t.end(); } 00359 00360 /** 00361 * Returns a read-only (constant) iterator that points one past the last 00362 * pair in the %map. Iteration is done in ascending order according to 00363 * the keys. 00364 */ 00365 const_iterator 00366 end() const _GLIBCXX_NOEXCEPT 00367 { return _M_t.end(); } 00368 00369 /** 00370 * Returns a read/write reverse iterator that points to the last pair in 00371 * the %map. Iteration is done in descending order according to the 00372 * keys. 00373 */ 00374 reverse_iterator 00375 rbegin() _GLIBCXX_NOEXCEPT 00376 { return _M_t.rbegin(); } 00377 00378 /** 00379 * Returns a read-only (constant) reverse iterator that points to the 00380 * last pair in the %map. Iteration is done in descending order 00381 * according to the keys. 00382 */ 00383 const_reverse_iterator 00384 rbegin() const _GLIBCXX_NOEXCEPT 00385 { return _M_t.rbegin(); } 00386 00387 /** 00388 * Returns a read/write reverse iterator that points to one before the 00389 * first pair in the %map. Iteration is done in descending order 00390 * according to the keys. 00391 */ 00392 reverse_iterator 00393 rend() _GLIBCXX_NOEXCEPT 00394 { return _M_t.rend(); } 00395 00396 /** 00397 * Returns a read-only (constant) reverse iterator that points to one 00398 * before the first pair in the %map. Iteration is done in descending 00399 * order according to the keys. 00400 */ 00401 const_reverse_iterator 00402 rend() const _GLIBCXX_NOEXCEPT 00403 { return _M_t.rend(); } 00404 00405 #if __cplusplus >= 201103L 00406 /** 00407 * Returns a read-only (constant) iterator that points to the first pair 00408 * in the %map. Iteration is done in ascending order according to the 00409 * keys. 00410 */ 00411 const_iterator 00412 cbegin() const noexcept 00413 { return _M_t.begin(); } 00414 00415 /** 00416 * Returns a read-only (constant) iterator that points one past the last 00417 * pair in the %map. Iteration is done in ascending order according to 00418 * the keys. 00419 */ 00420 const_iterator 00421 cend() const noexcept 00422 { return _M_t.end(); } 00423 00424 /** 00425 * Returns a read-only (constant) reverse iterator that points to the 00426 * last pair in the %map. Iteration is done in descending order 00427 * according to the keys. 00428 */ 00429 const_reverse_iterator 00430 crbegin() const noexcept 00431 { return _M_t.rbegin(); } 00432 00433 /** 00434 * Returns a read-only (constant) reverse iterator that points to one 00435 * before the first pair in the %map. Iteration is done in descending 00436 * order according to the keys. 00437 */ 00438 const_reverse_iterator 00439 crend() const noexcept 00440 { return _M_t.rend(); } 00441 #endif 00442 00443 // capacity 00444 /** Returns true if the %map is empty. (Thus begin() would equal 00445 * end().) 00446 */ 00447 bool 00448 empty() const _GLIBCXX_NOEXCEPT 00449 { return _M_t.empty(); } 00450 00451 /** Returns the size of the %map. */ 00452 size_type 00453 size() const _GLIBCXX_NOEXCEPT 00454 { return _M_t.size(); } 00455 00456 /** Returns the maximum size of the %map. */ 00457 size_type 00458 max_size() const _GLIBCXX_NOEXCEPT 00459 { return _M_t.max_size(); } 00460 00461 // [23.3.1.2] element access 00462 /** 00463 * @brief Subscript ( @c [] ) access to %map data. 00464 * @param __k The key for which data should be retrieved. 00465 * @return A reference to the data of the (key,data) %pair. 00466 * 00467 * Allows for easy lookup with the subscript ( @c [] ) 00468 * operator. Returns data associated with the key specified in 00469 * subscript. If the key does not exist, a pair with that key 00470 * is created using default values, which is then returned. 00471 * 00472 * Lookup requires logarithmic time. 00473 */ 00474 mapped_type& 00475 operator[](const key_type& __k) 00476 { 00477 // concept requirements 00478 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00479 00480 iterator __i = lower_bound(__k); 00481 // __i->first is greater than or equivalent to __k. 00482 if (__i == end() || key_comp()(__k, (*__i).first)) 00483 #if __cplusplus >= 201103L 00484 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, 00485 std::tuple<const key_type&>(__k), 00486 std::tuple<>()); 00487 #else 00488 __i = insert(__i, value_type(__k, mapped_type())); 00489 #endif 00490 return (*__i).second; 00491 } 00492 00493 #if __cplusplus >= 201103L 00494 mapped_type& 00495 operator[](key_type&& __k) 00496 { 00497 // concept requirements 00498 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00499 00500 iterator __i = lower_bound(__k); 00501 // __i->first is greater than or equivalent to __k. 00502 if (__i == end() || key_comp()(__k, (*__i).first)) 00503 __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct, 00504 std::forward_as_tuple(std::move(__k)), 00505 std::tuple<>()); 00506 return (*__i).second; 00507 } 00508 #endif 00509 00510 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00511 // DR 464. Suggestion for new member functions in standard containers. 00512 /** 00513 * @brief Access to %map data. 00514 * @param __k The key for which data should be retrieved. 00515 * @return A reference to the data whose key is equivalent to @a __k, if 00516 * such a data is present in the %map. 00517 * @throw std::out_of_range If no such data is present. 00518 */ 00519 mapped_type& 00520 at(const key_type& __k) 00521 { 00522 iterator __i = lower_bound(__k); 00523 if (__i == end() || key_comp()(__k, (*__i).first)) 00524 __throw_out_of_range(__N("map::at")); 00525 return (*__i).second; 00526 } 00527 00528 const mapped_type& 00529 at(const key_type& __k) const 00530 { 00531 const_iterator __i = lower_bound(__k); 00532 if (__i == end() || key_comp()(__k, (*__i).first)) 00533 __throw_out_of_range(__N("map::at")); 00534 return (*__i).second; 00535 } 00536 00537 // modifiers 00538 #if __cplusplus >= 201103L 00539 /** 00540 * @brief Attempts to build and insert a std::pair into the %map. 00541 * 00542 * @param __args Arguments used to generate a new pair instance (see 00543 * std::piecewise_contruct for passing arguments to each 00544 * part of the pair constructor). 00545 * 00546 * @return A pair, of which the first element is an iterator that points 00547 * to the possibly inserted pair, and the second is a bool that 00548 * is true if the pair was actually inserted. 00549 * 00550 * This function attempts to build and insert a (key, value) %pair into 00551 * the %map. 00552 * A %map relies on unique keys and thus a %pair is only inserted if its 00553 * first element (the key) is not already present in the %map. 00554 * 00555 * Insertion requires logarithmic time. 00556 */ 00557 template<typename... _Args> 00558 std::pair<iterator, bool> 00559 emplace(_Args&&... __args) 00560 { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); } 00561 00562 /** 00563 * @brief Attempts to build and insert a std::pair into the %map. 00564 * 00565 * @param __pos An iterator that serves as a hint as to where the pair 00566 * should be inserted. 00567 * @param __args Arguments used to generate a new pair instance (see 00568 * std::piecewise_contruct for passing arguments to each 00569 * part of the pair constructor). 00570 * @return An iterator that points to the element with key of the 00571 * std::pair built from @a __args (may or may not be that 00572 * std::pair). 00573 * 00574 * This function is not concerned about whether the insertion took place, 00575 * and thus does not return a boolean like the single-argument emplace() 00576 * does. 00577 * Note that the first parameter is only a hint and can potentially 00578 * improve the performance of the insertion process. A bad hint would 00579 * cause no gains in efficiency. 00580 * 00581 * See 00582 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00583 * for more on @a hinting. 00584 * 00585 * Insertion requires logarithmic time (if the hint is not taken). 00586 */ 00587 template<typename... _Args> 00588 iterator 00589 emplace_hint(const_iterator __pos, _Args&&... __args) 00590 { 00591 return _M_t._M_emplace_hint_unique(__pos, 00592 std::forward<_Args>(__args)...); 00593 } 00594 #endif 00595 00596 /** 00597 * @brief Attempts to insert a std::pair into the %map. 00598 00599 * @param __x Pair to be inserted (see std::make_pair for easy 00600 * creation of pairs). 00601 * 00602 * @return A pair, of which the first element is an iterator that 00603 * points to the possibly inserted pair, and the second is 00604 * a bool that is true if the pair was actually inserted. 00605 * 00606 * This function attempts to insert a (key, value) %pair into the %map. 00607 * A %map relies on unique keys and thus a %pair is only inserted if its 00608 * first element (the key) is not already present in the %map. 00609 * 00610 * Insertion requires logarithmic time. 00611 */ 00612 std::pair<iterator, bool> 00613 insert(const value_type& __x) 00614 { return _M_t._M_insert_unique(__x); } 00615 00616 #if __cplusplus >= 201103L 00617 template<typename _Pair, typename = typename 00618 std::enable_if<std::is_constructible<value_type, 00619 _Pair&&>::value>::type> 00620 std::pair<iterator, bool> 00621 insert(_Pair&& __x) 00622 { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } 00623 #endif 00624 00625 #if __cplusplus >= 201103L 00626 /** 00627 * @brief Attempts to insert a list of std::pairs into the %map. 00628 * @param __list A std::initializer_list<value_type> of pairs to be 00629 * inserted. 00630 * 00631 * Complexity similar to that of the range constructor. 00632 */ 00633 void 00634 insert(std::initializer_list<value_type> __list) 00635 { insert(__list.begin(), __list.end()); } 00636 #endif 00637 00638 /** 00639 * @brief Attempts to insert a std::pair into the %map. 00640 * @param __position An iterator that serves as a hint as to where the 00641 * pair should be inserted. 00642 * @param __x Pair to be inserted (see std::make_pair for easy creation 00643 * of pairs). 00644 * @return An iterator that points to the element with key of 00645 * @a __x (may or may not be the %pair passed in). 00646 * 00647 00648 * This function is not concerned about whether the insertion 00649 * took place, and thus does not return a boolean like the 00650 * single-argument insert() does. Note that the first 00651 * parameter is only a hint and can potentially improve the 00652 * performance of the insertion process. A bad hint would 00653 * cause no gains in efficiency. 00654 * 00655 * See 00656 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/associative.html#containers.associative.insert_hints 00657 * for more on @a hinting. 00658 * 00659 * Insertion requires logarithmic time (if the hint is not taken). 00660 */ 00661 iterator 00662 #if __cplusplus >= 201103L 00663 insert(const_iterator __position, const value_type& __x) 00664 #else 00665 insert(iterator __position, const value_type& __x) 00666 #endif 00667 { return _M_t._M_insert_unique_(__position, __x); } 00668 00669 #if __cplusplus >= 201103L 00670 template<typename _Pair, typename = typename 00671 std::enable_if<std::is_constructible<value_type, 00672 _Pair&&>::value>::type> 00673 iterator 00674 insert(const_iterator __position, _Pair&& __x) 00675 { return _M_t._M_insert_unique_(__position, 00676 std::forward<_Pair>(__x)); } 00677 #endif 00678 00679 /** 00680 * @brief Template function that attempts to insert a range of elements. 00681 * @param __first Iterator pointing to the start of the range to be 00682 * inserted. 00683 * @param __last Iterator pointing to the end of the range. 00684 * 00685 * Complexity similar to that of the range constructor. 00686 */ 00687 template<typename _InputIterator> 00688 void 00689 insert(_InputIterator __first, _InputIterator __last) 00690 { _M_t._M_insert_unique(__first, __last); } 00691 00692 #if __cplusplus >= 201103L 00693 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00694 // DR 130. Associative erase should return an iterator. 00695 /** 00696 * @brief Erases an element from a %map. 00697 * @param __position An iterator pointing to the element to be erased. 00698 * @return An iterator pointing to the element immediately following 00699 * @a position prior to the element being erased. If no such 00700 * element exists, end() is returned. 00701 * 00702 * This function erases an element, pointed to by the given 00703 * iterator, from a %map. Note that this function only erases 00704 * the element, and that if the element is itself a pointer, 00705 * the pointed-to memory is not touched in any way. Managing 00706 * the pointer is the user's responsibility. 00707 */ 00708 iterator 00709 erase(const_iterator __position) 00710 { return _M_t.erase(__position); } 00711 00712 // LWG 2059 00713 _GLIBCXX_ABI_TAG_CXX11 00714 iterator 00715 erase(iterator __position) 00716 { return _M_t.erase(__position); } 00717 #else 00718 /** 00719 * @brief Erases an element from a %map. 00720 * @param __position An iterator pointing to the element to be erased. 00721 * 00722 * This function erases an element, pointed to by the given 00723 * iterator, from a %map. Note that this function only erases 00724 * the element, and that if the element is itself a pointer, 00725 * the pointed-to memory is not touched in any way. Managing 00726 * the pointer is the user's responsibility. 00727 */ 00728 void 00729 erase(iterator __position) 00730 { _M_t.erase(__position); } 00731 #endif 00732 00733 /** 00734 * @brief Erases elements according to the provided key. 00735 * @param __x Key of element to be erased. 00736 * @return The number of elements erased. 00737 * 00738 * This function erases all the elements located by the given key from 00739 * a %map. 00740 * Note that this function only erases the element, and that if 00741 * the element is itself a pointer, the pointed-to memory is not touched 00742 * in any way. Managing the pointer is the user's responsibility. 00743 */ 00744 size_type 00745 erase(const key_type& __x) 00746 { return _M_t.erase(__x); } 00747 00748 #if __cplusplus >= 201103L 00749 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00750 // DR 130. Associative erase should return an iterator. 00751 /** 00752 * @brief Erases a [first,last) range of elements from a %map. 00753 * @param __first Iterator pointing to the start of the range to be 00754 * erased. 00755 * @param __last Iterator pointing to the end of the range to 00756 * be erased. 00757 * @return The iterator @a __last. 00758 * 00759 * This function erases a sequence of elements from a %map. 00760 * Note that this function only erases the element, and that if 00761 * the element is itself a pointer, the pointed-to memory is not touched 00762 * in any way. Managing the pointer is the user's responsibility. 00763 */ 00764 iterator 00765 erase(const_iterator __first, const_iterator __last) 00766 { return _M_t.erase(__first, __last); } 00767 #else 00768 /** 00769 * @brief Erases a [__first,__last) range of elements from a %map. 00770 * @param __first Iterator pointing to the start of the range to be 00771 * erased. 00772 * @param __last Iterator pointing to the end of the range to 00773 * be erased. 00774 * 00775 * This function erases a sequence of elements from a %map. 00776 * Note that this function only erases the element, and that if 00777 * the element is itself a pointer, the pointed-to memory is not touched 00778 * in any way. Managing the pointer is the user's responsibility. 00779 */ 00780 void 00781 erase(iterator __first, iterator __last) 00782 { _M_t.erase(__first, __last); } 00783 #endif 00784 00785 /** 00786 * @brief Swaps data with another %map. 00787 * @param __x A %map of the same element and allocator types. 00788 * 00789 * This exchanges the elements between two maps in constant 00790 * time. (It is only swapping a pointer, an integer, and an 00791 * instance of the @c Compare type (which itself is often 00792 * stateless and empty), so it should be quite fast.) Note 00793 * that the global std::swap() function is specialized such 00794 * that std::swap(m1,m2) will feed to this function. 00795 */ 00796 void 00797 swap(map& __x) 00798 #if __cplusplus >= 201103L 00799 noexcept(_Alloc_traits::_S_nothrow_swap()) 00800 #endif 00801 { _M_t.swap(__x._M_t); } 00802 00803 /** 00804 * Erases all elements in a %map. Note that this function only 00805 * erases the elements, and that if the elements themselves are 00806 * pointers, the pointed-to memory is not touched in any way. 00807 * Managing the pointer is the user's responsibility. 00808 */ 00809 void 00810 clear() _GLIBCXX_NOEXCEPT 00811 { _M_t.clear(); } 00812 00813 // observers 00814 /** 00815 * Returns the key comparison object out of which the %map was 00816 * constructed. 00817 */ 00818 key_compare 00819 key_comp() const 00820 { return _M_t.key_comp(); } 00821 00822 /** 00823 * Returns a value comparison object, built from the key comparison 00824 * object out of which the %map was constructed. 00825 */ 00826 value_compare 00827 value_comp() const 00828 { return value_compare(_M_t.key_comp()); } 00829 00830 // [23.3.1.3] map operations 00831 00832 //@{ 00833 /** 00834 * @brief Tries to locate an element in a %map. 00835 * @param __x Key of (key, value) %pair to be located. 00836 * @return Iterator pointing to sought-after element, or end() if not 00837 * found. 00838 * 00839 * This function takes a key and tries to locate the element with which 00840 * the key matches. If successful the function returns an iterator 00841 * pointing to the sought after %pair. If unsuccessful it returns the 00842 * past-the-end ( @c end() ) iterator. 00843 */ 00844 00845 iterator 00846 find(const key_type& __x) 00847 { return _M_t.find(__x); } 00848 00849 #if __cplusplus > 201103L 00850 template<typename _Kt> 00851 auto 00852 find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x)) 00853 { return _M_t._M_find_tr(__x); } 00854 #endif 00855 //@} 00856 00857 //@{ 00858 /** 00859 * @brief Tries to locate an element in a %map. 00860 * @param __x Key of (key, value) %pair to be located. 00861 * @return Read-only (constant) iterator pointing to sought-after 00862 * element, or end() if not found. 00863 * 00864 * This function takes a key and tries to locate the element with which 00865 * the key matches. If successful the function returns a constant 00866 * iterator pointing to the sought after %pair. If unsuccessful it 00867 * returns the past-the-end ( @c end() ) iterator. 00868 */ 00869 00870 const_iterator 00871 find(const key_type& __x) const 00872 { return _M_t.find(__x); } 00873 00874 #if __cplusplus > 201103L 00875 template<typename _Kt> 00876 auto 00877 find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x)) 00878 { return _M_t._M_find_tr(__x); } 00879 #endif 00880 //@} 00881 00882 //@{ 00883 /** 00884 * @brief Finds the number of elements with given key. 00885 * @param __x Key of (key, value) pairs to be located. 00886 * @return Number of elements with specified key. 00887 * 00888 * This function only makes sense for multimaps; for map the result will 00889 * either be 0 (not present) or 1 (present). 00890 */ 00891 size_type 00892 count(const key_type& __x) const 00893 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00894 00895 #if __cplusplus > 201103L 00896 template<typename _Kt> 00897 auto 00898 count(const _Kt& __x) const -> decltype(_M_t._M_count_tr(__x)) 00899 { return _M_t._M_count_tr(__x); } 00900 #endif 00901 //@} 00902 00903 //@{ 00904 /** 00905 * @brief Finds the beginning of a subsequence matching given key. 00906 * @param __x Key of (key, value) pair to be located. 00907 * @return Iterator pointing to first element equal to or greater 00908 * than key, or end(). 00909 * 00910 * This function returns the first element of a subsequence of elements 00911 * that matches the given key. If unsuccessful it returns an iterator 00912 * pointing to the first element that has a greater value than given key 00913 * or end() if no such element exists. 00914 */ 00915 iterator 00916 lower_bound(const key_type& __x) 00917 { return _M_t.lower_bound(__x); } 00918 00919 #if __cplusplus > 201103L 00920 template<typename _Kt> 00921 auto 00922 lower_bound(const _Kt& __x) 00923 -> decltype(iterator(_M_t._M_lower_bound_tr(__x))) 00924 { return iterator(_M_t._M_lower_bound_tr(__x)); } 00925 #endif 00926 //@} 00927 00928 //@{ 00929 /** 00930 * @brief Finds the beginning of a subsequence matching given key. 00931 * @param __x Key of (key, value) pair to be located. 00932 * @return Read-only (constant) iterator pointing to first element 00933 * equal to or greater than key, or end(). 00934 * 00935 * This function returns the first element of a subsequence of elements 00936 * that matches the given key. If unsuccessful it returns an iterator 00937 * pointing to the first element that has a greater value than given key 00938 * or end() if no such element exists. 00939 */ 00940 const_iterator 00941 lower_bound(const key_type& __x) const 00942 { return _M_t.lower_bound(__x); } 00943 00944 #if __cplusplus > 201103L 00945 template<typename _Kt> 00946 auto 00947 lower_bound(const _Kt& __x) const 00948 -> decltype(const_iterator(_M_t._M_lower_bound_tr(__x))) 00949 { return const_iterator(_M_t._M_lower_bound_tr(__x)); } 00950 #endif 00951 //@} 00952 00953 //@{ 00954 /** 00955 * @brief Finds the end of a subsequence matching given key. 00956 * @param __x Key of (key, value) pair to be located. 00957 * @return Iterator pointing to the first element 00958 * greater than key, or end(). 00959 */ 00960 iterator 00961 upper_bound(const key_type& __x) 00962 { return _M_t.upper_bound(__x); } 00963 00964 #if __cplusplus > 201103L 00965 template<typename _Kt> 00966 auto 00967 upper_bound(const _Kt& __x) 00968 -> decltype(iterator(_M_t._M_upper_bound_tr(__x))) 00969 { return iterator(_M_t._M_upper_bound_tr(__x)); } 00970 #endif 00971 //@} 00972 00973 //@{ 00974 /** 00975 * @brief Finds the end of a subsequence matching given key. 00976 * @param __x Key of (key, value) pair to be located. 00977 * @return Read-only (constant) iterator pointing to first iterator 00978 * greater than key, or end(). 00979 */ 00980 const_iterator 00981 upper_bound(const key_type& __x) const 00982 { return _M_t.upper_bound(__x); } 00983 00984 #if __cplusplus > 201103L 00985 template<typename _Kt> 00986 auto 00987 upper_bound(const _Kt& __x) const 00988 -> decltype(const_iterator(_M_t._M_upper_bound_tr(__x))) 00989 { return const_iterator(_M_t._M_upper_bound_tr(__x)); } 00990 #endif 00991 //@} 00992 00993 //@{ 00994 /** 00995 * @brief Finds a subsequence matching given key. 00996 * @param __x Key of (key, value) pairs to be located. 00997 * @return Pair of iterators that possibly points to the subsequence 00998 * matching given key. 00999 * 01000 * This function is equivalent to 01001 * @code 01002 * std::make_pair(c.lower_bound(val), 01003 * c.upper_bound(val)) 01004 * @endcode 01005 * (but is faster than making the calls separately). 01006 * 01007 * This function probably only makes sense for multimaps. 01008 */ 01009 std::pair<iterator, iterator> 01010 equal_range(const key_type& __x) 01011 { return _M_t.equal_range(__x); } 01012 01013 #if __cplusplus > 201103L 01014 template<typename _Kt> 01015 auto 01016 equal_range(const _Kt& __x) 01017 -> decltype(pair<iterator, iterator>(_M_t._M_equal_range_tr(__x))) 01018 { return pair<iterator, iterator>(_M_t._M_equal_range_tr(__x)); } 01019 #endif 01020 //@} 01021 01022 //@{ 01023 /** 01024 * @brief Finds a subsequence matching given key. 01025 * @param __x Key of (key, value) pairs to be located. 01026 * @return Pair of read-only (constant) iterators that possibly points 01027 * to the subsequence matching given key. 01028 * 01029 * This function is equivalent to 01030 * @code 01031 * std::make_pair(c.lower_bound(val), 01032 * c.upper_bound(val)) 01033 * @endcode 01034 * (but is faster than making the calls separately). 01035 * 01036 * This function probably only makes sense for multimaps. 01037 */ 01038 std::pair<const_iterator, const_iterator> 01039 equal_range(const key_type& __x) const 01040 { return _M_t.equal_range(__x); } 01041 01042 #if __cplusplus > 201103L 01043 template<typename _Kt> 01044 auto 01045 equal_range(const _Kt& __x) const 01046 -> decltype(pair<const_iterator, const_iterator>( 01047 _M_t._M_equal_range_tr(__x))) 01048 { 01049 return pair<const_iterator, const_iterator>( 01050 _M_t._M_equal_range_tr(__x)); 01051 } 01052 #endif 01053 //@} 01054 01055 template<typename _K1, typename _T1, typename _C1, typename _A1> 01056 friend bool 01057 operator==(const map<_K1, _T1, _C1, _A1>&, 01058 const map<_K1, _T1, _C1, _A1>&); 01059 01060 template<typename _K1, typename _T1, typename _C1, typename _A1> 01061 friend bool 01062 operator<(const map<_K1, _T1, _C1, _A1>&, 01063 const map<_K1, _T1, _C1, _A1>&); 01064 }; 01065 01066 /** 01067 * @brief Map equality comparison. 01068 * @param __x A %map. 01069 * @param __y A %map of the same type as @a x. 01070 * @return True iff the size and elements of the maps are equal. 01071 * 01072 * This is an equivalence relation. It is linear in the size of the 01073 * maps. Maps are considered equivalent if their sizes are equal, 01074 * and if corresponding elements compare equal. 01075 */ 01076 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01077 inline bool 01078 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01079 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01080 { return __x._M_t == __y._M_t; } 01081 01082 /** 01083 * @brief Map ordering relation. 01084 * @param __x A %map. 01085 * @param __y A %map of the same type as @a x. 01086 * @return True iff @a x is lexicographically less than @a y. 01087 * 01088 * This is a total ordering relation. It is linear in the size of the 01089 * maps. The elements must be comparable with @c <. 01090 * 01091 * See std::lexicographical_compare() for how the determination is made. 01092 */ 01093 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01094 inline bool 01095 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01096 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01097 { return __x._M_t < __y._M_t; } 01098 01099 /// Based on operator== 01100 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01101 inline bool 01102 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01103 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01104 { return !(__x == __y); } 01105 01106 /// Based on operator< 01107 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01108 inline bool 01109 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01110 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01111 { return __y < __x; } 01112 01113 /// Based on operator< 01114 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01115 inline bool 01116 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01117 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01118 { return !(__y < __x); } 01119 01120 /// Based on operator< 01121 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01122 inline bool 01123 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 01124 const map<_Key, _Tp, _Compare, _Alloc>& __y) 01125 { return !(__x < __y); } 01126 01127 /// See std::map::swap(). 01128 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 01129 inline void 01130 swap(map<_Key, _Tp, _Compare, _Alloc>& __x, 01131 map<_Key, _Tp, _Compare, _Alloc>& __y) 01132 { __x.swap(__y); } 01133 01134 _GLIBCXX_END_NAMESPACE_CONTAINER 01135 } // namespace std 01136 01137 #endif /* _STL_MAP_H */