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