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