|
libstdc++
|
00001 // Map implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 00004 // 2011 Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 3, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // Under Section 7 of GPL version 3, you are granted additional 00018 // permissions described in the GCC Runtime Library Exception, version 00019 // 3.1, as published by the Free Software Foundation. 00020 00021 // You should have received a copy of the GNU General Public License and 00022 // a copy of the GCC Runtime Library Exception along with this program; 00023 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00024 // <http://www.gnu.org/licenses/>. 00025 00026 /* 00027 * 00028 * Copyright (c) 1994 00029 * Hewlett-Packard Company 00030 * 00031 * Permission to use, copy, modify, distribute and sell this software 00032 * and its documentation for any purpose is hereby granted without fee, 00033 * provided that the above copyright notice appear in all copies and 00034 * that both that copyright notice and this permission notice appear 00035 * in supporting documentation. Hewlett-Packard Company makes no 00036 * representations about the suitability of this software for any 00037 * purpose. It is provided "as is" without express or implied warranty. 00038 * 00039 * 00040 * Copyright (c) 1996,1997 00041 * Silicon Graphics Computer Systems, Inc. 00042 * 00043 * Permission to use, copy, modify, distribute and sell this software 00044 * and its documentation for any purpose is hereby granted without fee, 00045 * provided that the above copyright notice appear in all copies and 00046 * that both that copyright notice and this permission notice appear 00047 * in supporting documentation. Silicon Graphics makes no 00048 * representations about the suitability of this software for any 00049 * purpose. It is provided "as is" without express or implied warranty. 00050 */ 00051 00052 /** @file bits/stl_map.h 00053 * This is an internal header file, included by other library headers. 00054 * Do not attempt to use it directly. @headername{map} 00055 */ 00056 00057 #ifndef _STL_MAP_H 00058 #define _STL_MAP_H 1 00059 00060 #include <bits/functexcept.h> 00061 #include <bits/concept_check.h> 00062 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00063 #include <initializer_list> 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 _Alloc::template rebind<value_type>::other 00132 _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 public: 00141 // many of these are specified differently in ISO, but the following are 00142 // "functionally equivalent" 00143 typedef typename _Pair_alloc_type::pointer pointer; 00144 typedef typename _Pair_alloc_type::const_pointer const_pointer; 00145 typedef typename _Pair_alloc_type::reference reference; 00146 typedef typename _Pair_alloc_type::const_reference const_reference; 00147 typedef typename _Rep_type::iterator iterator; 00148 typedef typename _Rep_type::const_iterator const_iterator; 00149 typedef typename _Rep_type::size_type size_type; 00150 typedef typename _Rep_type::difference_type difference_type; 00151 typedef typename _Rep_type::reverse_iterator reverse_iterator; 00152 typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator; 00153 00154 // [23.3.1.1] construct/copy/destroy 00155 // (get_allocator() is normally listed in this section, but seems to have 00156 // been accidentally omitted in the printed standard) 00157 /** 00158 * @brief Default constructor creates no elements. 00159 */ 00160 map() 00161 : _M_t() { } 00162 00163 /** 00164 * @brief Creates a %map with no elements. 00165 * @param __comp A comparison object. 00166 * @param __a An allocator object. 00167 */ 00168 explicit 00169 map(const _Compare& __comp, 00170 const allocator_type& __a = allocator_type()) 00171 : _M_t(__comp, _Pair_alloc_type(__a)) { } 00172 00173 /** 00174 * @brief %Map copy constructor. 00175 * @param __x A %map of identical element and allocator types. 00176 * 00177 * The newly-created %map uses a copy of the allocation object 00178 * used by @a __x. 00179 */ 00180 map(const map& __x) 00181 : _M_t(__x._M_t) { } 00182 00183 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00184 /** 00185 * @brief %Map move constructor. 00186 * @param __x A %map of identical element and allocator types. 00187 * 00188 * The newly-created %map contains the exact contents of @a __x. 00189 * The contents of @a __x are a valid, but unspecified %map. 00190 */ 00191 map(map&& __x) 00192 noexcept(is_nothrow_copy_constructible<_Compare>::value) 00193 : _M_t(std::move(__x._M_t)) { } 00194 00195 /** 00196 * @brief Builds a %map from an initializer_list. 00197 * @param __l An initializer_list. 00198 * @param __comp A comparison object. 00199 * @param __a An allocator object. 00200 * 00201 * Create a %map consisting of copies of the elements in the 00202 * initializer_list @a __l. 00203 * This is linear in N if the range is already sorted, and NlogN 00204 * otherwise (where N is @a __l.size()). 00205 */ 00206 map(initializer_list<value_type> __l, 00207 const _Compare& __comp = _Compare(), 00208 const allocator_type& __a = allocator_type()) 00209 : _M_t(__comp, _Pair_alloc_type(__a)) 00210 { _M_t._M_insert_unique(__l.begin(), __l.end()); } 00211 #endif 00212 00213 /** 00214 * @brief Builds a %map from a range. 00215 * @param __first An input iterator. 00216 * @param __last An input iterator. 00217 * 00218 * Create a %map consisting of copies of the elements from 00219 * [__first,__last). This is linear in N if the range is 00220 * already sorted, and NlogN otherwise (where N is 00221 * distance(__first,__last)). 00222 */ 00223 template<typename _InputIterator> 00224 map(_InputIterator __first, _InputIterator __last) 00225 : _M_t() 00226 { _M_t._M_insert_unique(__first, __last); } 00227 00228 /** 00229 * @brief Builds a %map from a range. 00230 * @param __first An input iterator. 00231 * @param __last An input iterator. 00232 * @param __comp A comparison functor. 00233 * @param __a An allocator object. 00234 * 00235 * Create a %map consisting of copies of the elements from 00236 * [__first,__last). This is linear in N if the range is 00237 * already sorted, and NlogN otherwise (where N is 00238 * distance(__first,__last)). 00239 */ 00240 template<typename _InputIterator> 00241 map(_InputIterator __first, _InputIterator __last, 00242 const _Compare& __comp, 00243 const allocator_type& __a = allocator_type()) 00244 : _M_t(__comp, _Pair_alloc_type(__a)) 00245 { _M_t._M_insert_unique(__first, __last); } 00246 00247 // FIXME There is no dtor declared, but we should have something 00248 // generated by Doxygen. I don't know what tags to add to this 00249 // paragraph to make that happen: 00250 /** 00251 * The dtor only erases the elements, and note that if the elements 00252 * themselves are pointers, the pointed-to memory is not touched in any 00253 * way. Managing the pointer is the user's responsibility. 00254 */ 00255 00256 /** 00257 * @brief %Map assignment operator. 00258 * @param __x A %map of identical element and allocator types. 00259 * 00260 * All the elements of @a __x are copied, but unlike the copy 00261 * constructor, the allocator object is not copied. 00262 */ 00263 map& 00264 operator=(const map& __x) 00265 { 00266 _M_t = __x._M_t; 00267 return *this; 00268 } 00269 00270 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00271 /** 00272 * @brief %Map move assignment operator. 00273 * @param __x A %map of identical element and allocator types. 00274 * 00275 * The contents of @a __x are moved into this map (without copying). 00276 * @a __x is a valid, but unspecified %map. 00277 */ 00278 map& 00279 operator=(map&& __x) 00280 { 00281 // NB: DR 1204. 00282 // NB: DR 675. 00283 this->clear(); 00284 this->swap(__x); 00285 return *this; 00286 } 00287 00288 /** 00289 * @brief %Map list assignment operator. 00290 * @param __l An initializer_list. 00291 * 00292 * This function fills a %map with copies of the elements in the 00293 * initializer list @a __l. 00294 * 00295 * Note that the assignment completely changes the %map and 00296 * that the resulting %map's size is the same as the number 00297 * of elements assigned. Old data may be lost. 00298 */ 00299 map& 00300 operator=(initializer_list<value_type> __l) 00301 { 00302 this->clear(); 00303 this->insert(__l.begin(), __l.end()); 00304 return *this; 00305 } 00306 #endif 00307 00308 /// Get a copy of the memory allocation object. 00309 allocator_type 00310 get_allocator() const _GLIBCXX_NOEXCEPT 00311 { return allocator_type(_M_t.get_allocator()); } 00312 00313 // iterators 00314 /** 00315 * Returns a read/write iterator that points to the first pair in the 00316 * %map. 00317 * Iteration is done in ascending order according to the keys. 00318 */ 00319 iterator 00320 begin() _GLIBCXX_NOEXCEPT 00321 { return _M_t.begin(); } 00322 00323 /** 00324 * Returns a read-only (constant) iterator that points to the first pair 00325 * in the %map. Iteration is done in ascending order according to the 00326 * keys. 00327 */ 00328 const_iterator 00329 begin() const _GLIBCXX_NOEXCEPT 00330 { return _M_t.begin(); } 00331 00332 /** 00333 * Returns a read/write iterator that points one past the last 00334 * pair in the %map. Iteration is done in ascending order 00335 * according to the keys. 00336 */ 00337 iterator 00338 end() _GLIBCXX_NOEXCEPT 00339 { return _M_t.end(); } 00340 00341 /** 00342 * Returns a read-only (constant) iterator that points one past the last 00343 * pair in the %map. Iteration is done in ascending order according to 00344 * the keys. 00345 */ 00346 const_iterator 00347 end() const _GLIBCXX_NOEXCEPT 00348 { return _M_t.end(); } 00349 00350 /** 00351 * Returns a read/write reverse iterator that points to the last pair in 00352 * the %map. Iteration is done in descending order according to the 00353 * keys. 00354 */ 00355 reverse_iterator 00356 rbegin() _GLIBCXX_NOEXCEPT 00357 { return _M_t.rbegin(); } 00358 00359 /** 00360 * Returns a read-only (constant) reverse iterator that points to the 00361 * last pair in the %map. Iteration is done in descending order 00362 * according to the keys. 00363 */ 00364 const_reverse_iterator 00365 rbegin() const _GLIBCXX_NOEXCEPT 00366 { return _M_t.rbegin(); } 00367 00368 /** 00369 * Returns a read/write reverse iterator that points to one before the 00370 * first pair in the %map. Iteration is done in descending order 00371 * according to the keys. 00372 */ 00373 reverse_iterator 00374 rend() _GLIBCXX_NOEXCEPT 00375 { return _M_t.rend(); } 00376 00377 /** 00378 * Returns a read-only (constant) reverse iterator that points to one 00379 * before the first pair in the %map. Iteration is done in descending 00380 * order according to the keys. 00381 */ 00382 const_reverse_iterator 00383 rend() const _GLIBCXX_NOEXCEPT 00384 { return _M_t.rend(); } 00385 00386 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00387 /** 00388 * Returns a read-only (constant) iterator that points to the first pair 00389 * in the %map. Iteration is done in ascending order according to the 00390 * keys. 00391 */ 00392 const_iterator 00393 cbegin() const noexcept 00394 { return _M_t.begin(); } 00395 00396 /** 00397 * Returns a read-only (constant) iterator that points one past the last 00398 * pair in the %map. Iteration is done in ascending order according to 00399 * the keys. 00400 */ 00401 const_iterator 00402 cend() const noexcept 00403 { return _M_t.end(); } 00404 00405 /** 00406 * Returns a read-only (constant) reverse iterator that points to the 00407 * last pair in the %map. Iteration is done in descending order 00408 * according to the keys. 00409 */ 00410 const_reverse_iterator 00411 crbegin() const noexcept 00412 { return _M_t.rbegin(); } 00413 00414 /** 00415 * Returns a read-only (constant) reverse iterator that points to one 00416 * before the first pair in the %map. Iteration is done in descending 00417 * order according to the keys. 00418 */ 00419 const_reverse_iterator 00420 crend() const noexcept 00421 { return _M_t.rend(); } 00422 #endif 00423 00424 // capacity 00425 /** Returns true if the %map is empty. (Thus begin() would equal 00426 * end().) 00427 */ 00428 bool 00429 empty() const _GLIBCXX_NOEXCEPT 00430 { return _M_t.empty(); } 00431 00432 /** Returns the size of the %map. */ 00433 size_type 00434 size() const _GLIBCXX_NOEXCEPT 00435 { return _M_t.size(); } 00436 00437 /** Returns the maximum size of the %map. */ 00438 size_type 00439 max_size() const _GLIBCXX_NOEXCEPT 00440 { return _M_t.max_size(); } 00441 00442 // [23.3.1.2] element access 00443 /** 00444 * @brief Subscript ( @c [] ) access to %map data. 00445 * @param __k The key for which data should be retrieved. 00446 * @return A reference to the data of the (key,data) %pair. 00447 * 00448 * Allows for easy lookup with the subscript ( @c [] ) 00449 * operator. Returns data associated with the key specified in 00450 * subscript. If the key does not exist, a pair with that key 00451 * is created using default values, which is then returned. 00452 * 00453 * Lookup requires logarithmic time. 00454 */ 00455 mapped_type& 00456 operator[](const key_type& __k) 00457 { 00458 // concept requirements 00459 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00460 00461 iterator __i = lower_bound(__k); 00462 // __i->first is greater than or equivalent to __k. 00463 if (__i == end() || key_comp()(__k, (*__i).first)) 00464 __i = insert(__i, value_type(__k, mapped_type())); 00465 return (*__i).second; 00466 } 00467 00468 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00469 mapped_type& 00470 operator[](key_type&& __k) 00471 { 00472 // concept requirements 00473 __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>) 00474 00475 iterator __i = lower_bound(__k); 00476 // __i->first is greater than or equivalent to __k. 00477 if (__i == end() || key_comp()(__k, (*__i).first)) 00478 __i = insert(__i, std::make_pair(std::move(__k), mapped_type())); 00479 return (*__i).second; 00480 } 00481 #endif 00482 00483 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00484 // DR 464. Suggestion for new member functions in standard containers. 00485 /** 00486 * @brief Access to %map data. 00487 * @param __k The key for which data should be retrieved. 00488 * @return A reference to the data whose key is equivalent to @a __k, if 00489 * such a data is present in the %map. 00490 * @throw std::out_of_range If no such data is present. 00491 */ 00492 mapped_type& 00493 at(const key_type& __k) 00494 { 00495 iterator __i = lower_bound(__k); 00496 if (__i == end() || key_comp()(__k, (*__i).first)) 00497 __throw_out_of_range(__N("map::at")); 00498 return (*__i).second; 00499 } 00500 00501 const mapped_type& 00502 at(const key_type& __k) const 00503 { 00504 const_iterator __i = lower_bound(__k); 00505 if (__i == end() || key_comp()(__k, (*__i).first)) 00506 __throw_out_of_range(__N("map::at")); 00507 return (*__i).second; 00508 } 00509 00510 // modifiers 00511 /** 00512 * @brief Attempts to insert a std::pair into the %map. 00513 00514 * @param __x Pair to be inserted (see std::make_pair for easy 00515 * creation of pairs). 00516 * 00517 * @return A pair, of which the first element is an iterator that 00518 * points to the possibly inserted pair, and the second is 00519 * a bool that is true if the pair was actually inserted. 00520 * 00521 * This function attempts to insert a (key, value) %pair into the %map. 00522 * A %map relies on unique keys and thus a %pair is only inserted if its 00523 * first element (the key) is not already present in the %map. 00524 * 00525 * Insertion requires logarithmic time. 00526 */ 00527 std::pair<iterator, bool> 00528 insert(const value_type& __x) 00529 { return _M_t._M_insert_unique(__x); } 00530 00531 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00532 template<typename _Pair, typename = typename 00533 std::enable_if<std::is_convertible<_Pair, 00534 value_type>::value>::type> 00535 std::pair<iterator, bool> 00536 insert(_Pair&& __x) 00537 { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); } 00538 #endif 00539 00540 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00541 /** 00542 * @brief Attempts to insert a list of std::pairs into the %map. 00543 * @param __list A std::initializer_list<value_type> of pairs to be 00544 * inserted. 00545 * 00546 * Complexity similar to that of the range constructor. 00547 */ 00548 void 00549 insert(std::initializer_list<value_type> __list) 00550 { insert(__list.begin(), __list.end()); } 00551 #endif 00552 00553 /** 00554 * @brief Attempts to insert a std::pair into the %map. 00555 * @param __position An iterator that serves as a hint as to where the 00556 * pair should be inserted. 00557 * @param __x Pair to be inserted (see std::make_pair for easy creation 00558 * of pairs). 00559 * @return An iterator that points to the element with key of 00560 * @a __x (may or may not be the %pair passed in). 00561 * 00562 00563 * This function is not concerned about whether the insertion 00564 * took place, and thus does not return a boolean like the 00565 * single-argument insert() does. Note that the first 00566 * parameter is only a hint and can potentially improve the 00567 * performance of the insertion process. A bad hint would 00568 * cause no gains in efficiency. 00569 * 00570 * See 00571 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html 00572 * for more on @a hinting. 00573 * 00574 * Insertion requires logarithmic time (if the hint is not taken). 00575 */ 00576 iterator 00577 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00578 insert(const_iterator __position, const value_type& __x) 00579 #else 00580 insert(iterator __position, const value_type& __x) 00581 #endif 00582 { return _M_t._M_insert_unique_(__position, __x); } 00583 00584 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00585 template<typename _Pair, typename = typename 00586 std::enable_if<std::is_convertible<_Pair, 00587 value_type>::value>::type> 00588 iterator 00589 insert(const_iterator __position, _Pair&& __x) 00590 { return _M_t._M_insert_unique_(__position, 00591 std::forward<_Pair>(__x)); } 00592 #endif 00593 00594 /** 00595 * @brief Template function that attempts to insert a range of elements. 00596 * @param __first Iterator pointing to the start of the range to be 00597 * inserted. 00598 * @param __last Iterator pointing to the end of the range. 00599 * 00600 * Complexity similar to that of the range constructor. 00601 */ 00602 template<typename _InputIterator> 00603 void 00604 insert(_InputIterator __first, _InputIterator __last) 00605 { _M_t._M_insert_unique(__first, __last); } 00606 00607 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00608 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00609 // DR 130. Associative erase should return an iterator. 00610 /** 00611 * @brief Erases an element from a %map. 00612 * @param __position An iterator pointing to the element to be erased. 00613 * @return An iterator pointing to the element immediately following 00614 * @a position prior to the element being erased. If no such 00615 * element exists, end() is returned. 00616 * 00617 * This function erases an element, pointed to by the given 00618 * iterator, from a %map. Note that this function only erases 00619 * the element, and that if the element is itself a pointer, 00620 * the pointed-to memory is not touched in any way. Managing 00621 * the pointer is the user's responsibility. 00622 */ 00623 iterator 00624 erase(const_iterator __position) 00625 { return _M_t.erase(__position); } 00626 00627 // LWG 2059. 00628 iterator 00629 erase(iterator __position) 00630 { return _M_t.erase(__position); } 00631 #else 00632 /** 00633 * @brief Erases an element from a %map. 00634 * @param __position An iterator pointing to the element to be erased. 00635 * 00636 * This function erases an element, pointed to by the given 00637 * iterator, from a %map. Note that this function only erases 00638 * the element, and that if the element is itself a pointer, 00639 * the pointed-to memory is not touched in any way. Managing 00640 * the pointer is the user's responsibility. 00641 */ 00642 void 00643 erase(iterator __position) 00644 { _M_t.erase(__position); } 00645 #endif 00646 00647 /** 00648 * @brief Erases elements according to the provided key. 00649 * @param __x Key of element to be erased. 00650 * @return The number of elements erased. 00651 * 00652 * This function erases all the elements located by the given key from 00653 * a %map. 00654 * Note that this function only erases the element, and that if 00655 * the element is itself a pointer, the pointed-to memory is not touched 00656 * in any way. Managing the pointer is the user's responsibility. 00657 */ 00658 size_type 00659 erase(const key_type& __x) 00660 { return _M_t.erase(__x); } 00661 00662 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00663 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00664 // DR 130. Associative erase should return an iterator. 00665 /** 00666 * @brief Erases a [first,last) range of elements from a %map. 00667 * @param __first Iterator pointing to the start of the range to be 00668 * erased. 00669 * @param __last Iterator pointing to the end of the range to 00670 * be erased. 00671 * @return The iterator @a __last. 00672 * 00673 * This function erases a sequence of elements from a %map. 00674 * Note that this function only erases the element, and that if 00675 * the element is itself a pointer, the pointed-to memory is not touched 00676 * in any way. Managing the pointer is the user's responsibility. 00677 */ 00678 iterator 00679 erase(const_iterator __first, const_iterator __last) 00680 { return _M_t.erase(__first, __last); } 00681 #else 00682 /** 00683 * @brief Erases a [__first,__last) range of elements from a %map. 00684 * @param __first Iterator pointing to the start of the range to be 00685 * erased. 00686 * @param __last Iterator pointing to the end of the range to 00687 * be erased. 00688 * 00689 * This function erases a sequence of elements from a %map. 00690 * Note that this function only erases the element, and that if 00691 * the element is itself a pointer, the pointed-to memory is not touched 00692 * in any way. Managing the pointer is the user's responsibility. 00693 */ 00694 void 00695 erase(iterator __first, iterator __last) 00696 { _M_t.erase(__first, __last); } 00697 #endif 00698 00699 /** 00700 * @brief Swaps data with another %map. 00701 * @param __x A %map of the same element and allocator types. 00702 * 00703 * This exchanges the elements between two maps in constant 00704 * time. (It is only swapping a pointer, an integer, and an 00705 * instance of the @c Compare type (which itself is often 00706 * stateless and empty), so it should be quite fast.) Note 00707 * that the global std::swap() function is specialized such 00708 * that std::swap(m1,m2) will feed to this function. 00709 */ 00710 void 00711 swap(map& __x) 00712 { _M_t.swap(__x._M_t); } 00713 00714 /** 00715 * Erases all elements in a %map. Note that this function only 00716 * erases the elements, and that if the elements themselves are 00717 * pointers, the pointed-to memory is not touched in any way. 00718 * Managing the pointer is the user's responsibility. 00719 */ 00720 void 00721 clear() _GLIBCXX_NOEXCEPT 00722 { _M_t.clear(); } 00723 00724 // observers 00725 /** 00726 * Returns the key comparison object out of which the %map was 00727 * constructed. 00728 */ 00729 key_compare 00730 key_comp() const 00731 { return _M_t.key_comp(); } 00732 00733 /** 00734 * Returns a value comparison object, built from the key comparison 00735 * object out of which the %map was constructed. 00736 */ 00737 value_compare 00738 value_comp() const 00739 { return value_compare(_M_t.key_comp()); } 00740 00741 // [23.3.1.3] map operations 00742 /** 00743 * @brief Tries to locate an element in a %map. 00744 * @param __x Key of (key, value) %pair to be located. 00745 * @return Iterator pointing to sought-after element, or end() if not 00746 * found. 00747 * 00748 * This function takes a key and tries to locate the element with which 00749 * the key matches. If successful the function returns an iterator 00750 * pointing to the sought after %pair. If unsuccessful it returns the 00751 * past-the-end ( @c end() ) iterator. 00752 */ 00753 iterator 00754 find(const key_type& __x) 00755 { return _M_t.find(__x); } 00756 00757 /** 00758 * @brief Tries to locate an element in a %map. 00759 * @param __x Key of (key, value) %pair to be located. 00760 * @return Read-only (constant) iterator pointing to sought-after 00761 * element, or end() if not found. 00762 * 00763 * This function takes a key and tries to locate the element with which 00764 * the key matches. If successful the function returns a constant 00765 * iterator pointing to the sought after %pair. If unsuccessful it 00766 * returns the past-the-end ( @c end() ) iterator. 00767 */ 00768 const_iterator 00769 find(const key_type& __x) const 00770 { return _M_t.find(__x); } 00771 00772 /** 00773 * @brief Finds the number of elements with given key. 00774 * @param __x Key of (key, value) pairs to be located. 00775 * @return Number of elements with specified key. 00776 * 00777 * This function only makes sense for multimaps; for map the result will 00778 * either be 0 (not present) or 1 (present). 00779 */ 00780 size_type 00781 count(const key_type& __x) const 00782 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00783 00784 /** 00785 * @brief Finds the beginning of a subsequence matching given key. 00786 * @param __x Key of (key, value) pair to be located. 00787 * @return Iterator pointing to first element equal to or greater 00788 * than key, or end(). 00789 * 00790 * This function returns the first element of a subsequence of elements 00791 * that matches the given key. If unsuccessful it returns an iterator 00792 * pointing to the first element that has a greater value than given key 00793 * or end() if no such element exists. 00794 */ 00795 iterator 00796 lower_bound(const key_type& __x) 00797 { return _M_t.lower_bound(__x); } 00798 00799 /** 00800 * @brief Finds the beginning of a subsequence matching given key. 00801 * @param __x Key of (key, value) pair to be located. 00802 * @return Read-only (constant) iterator pointing to first element 00803 * equal to or greater than key, or end(). 00804 * 00805 * This function returns the first element of a subsequence of elements 00806 * that matches the given key. If unsuccessful it returns an iterator 00807 * pointing to the first element that has a greater value than given key 00808 * or end() if no such element exists. 00809 */ 00810 const_iterator 00811 lower_bound(const key_type& __x) const 00812 { return _M_t.lower_bound(__x); } 00813 00814 /** 00815 * @brief Finds the end of a subsequence matching given key. 00816 * @param __x Key of (key, value) pair to be located. 00817 * @return Iterator pointing to the first element 00818 * greater than key, or end(). 00819 */ 00820 iterator 00821 upper_bound(const key_type& __x) 00822 { return _M_t.upper_bound(__x); } 00823 00824 /** 00825 * @brief Finds the end of a subsequence matching given key. 00826 * @param __x Key of (key, value) pair to be located. 00827 * @return Read-only (constant) iterator pointing to first iterator 00828 * greater than key, or end(). 00829 */ 00830 const_iterator 00831 upper_bound(const key_type& __x) const 00832 { return _M_t.upper_bound(__x); } 00833 00834 /** 00835 * @brief Finds a subsequence matching given key. 00836 * @param __x Key of (key, value) pairs to be located. 00837 * @return Pair of iterators that possibly points to the subsequence 00838 * matching given key. 00839 * 00840 * This function is equivalent to 00841 * @code 00842 * std::make_pair(c.lower_bound(val), 00843 * c.upper_bound(val)) 00844 * @endcode 00845 * (but is faster than making the calls separately). 00846 * 00847 * This function probably only makes sense for multimaps. 00848 */ 00849 std::pair<iterator, iterator> 00850 equal_range(const key_type& __x) 00851 { return _M_t.equal_range(__x); } 00852 00853 /** 00854 * @brief Finds a subsequence matching given key. 00855 * @param __x Key of (key, value) pairs to be located. 00856 * @return Pair of read-only (constant) iterators that possibly points 00857 * to the subsequence matching given key. 00858 * 00859 * This function is equivalent to 00860 * @code 00861 * std::make_pair(c.lower_bound(val), 00862 * c.upper_bound(val)) 00863 * @endcode 00864 * (but is faster than making the calls separately). 00865 * 00866 * This function probably only makes sense for multimaps. 00867 */ 00868 std::pair<const_iterator, const_iterator> 00869 equal_range(const key_type& __x) const 00870 { return _M_t.equal_range(__x); } 00871 00872 template<typename _K1, typename _T1, typename _C1, typename _A1> 00873 friend bool 00874 operator==(const map<_K1, _T1, _C1, _A1>&, 00875 const map<_K1, _T1, _C1, _A1>&); 00876 00877 template<typename _K1, typename _T1, typename _C1, typename _A1> 00878 friend bool 00879 operator<(const map<_K1, _T1, _C1, _A1>&, 00880 const map<_K1, _T1, _C1, _A1>&); 00881 }; 00882 00883 /** 00884 * @brief Map equality comparison. 00885 * @param __x A %map. 00886 * @param __y A %map of the same type as @a x. 00887 * @return True iff the size and elements of the maps are equal. 00888 * 00889 * This is an equivalence relation. It is linear in the size of the 00890 * maps. Maps are considered equivalent if their sizes are equal, 00891 * and if corresponding elements compare equal. 00892 */ 00893 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00894 inline bool 00895 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00896 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00897 { return __x._M_t == __y._M_t; } 00898 00899 /** 00900 * @brief Map ordering relation. 00901 * @param __x A %map. 00902 * @param __y A %map of the same type as @a x. 00903 * @return True iff @a x is lexicographically less than @a y. 00904 * 00905 * This is a total ordering relation. It is linear in the size of the 00906 * maps. The elements must be comparable with @c <. 00907 * 00908 * See std::lexicographical_compare() for how the determination is made. 00909 */ 00910 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00911 inline bool 00912 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00913 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00914 { return __x._M_t < __y._M_t; } 00915 00916 /// Based on operator== 00917 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00918 inline bool 00919 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00920 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00921 { return !(__x == __y); } 00922 00923 /// Based on operator< 00924 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00925 inline bool 00926 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00927 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00928 { return __y < __x; } 00929 00930 /// Based on operator< 00931 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00932 inline bool 00933 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00934 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00935 { return !(__y < __x); } 00936 00937 /// Based on operator< 00938 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00939 inline bool 00940 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00941 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00942 { return !(__x < __y); } 00943 00944 /// See std::map::swap(). 00945 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00946 inline void 00947 swap(map<_Key, _Tp, _Compare, _Alloc>& __x, 00948 map<_Key, _Tp, _Compare, _Alloc>& __y) 00949 { __x.swap(__y); } 00950 00951 _GLIBCXX_END_NAMESPACE_CONTAINER 00952 } // namespace std 00953 00954 #endif /* _STL_MAP_H */