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