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