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 #else 00616 /** 00617 * @brief Erases an element from a %map. 00618 * @param position An iterator pointing to the element to be erased. 00619 * 00620 * This function erases an element, pointed to by the given 00621 * iterator, from a %map. Note that this function only erases 00622 * the element, and that if the element is itself a pointer, 00623 * the pointed-to memory is not touched in any way. Managing 00624 * the pointer is the user's responsibility. 00625 */ 00626 void 00627 erase(iterator __position) 00628 { _M_t.erase(__position); } 00629 #endif 00630 00631 /** 00632 * @brief Erases elements according to the provided key. 00633 * @param x Key of element to be erased. 00634 * @return The number of elements erased. 00635 * 00636 * This function erases all the elements located by the given key from 00637 * a %map. 00638 * Note that this function only erases the element, and that if 00639 * the element is itself a pointer, the pointed-to memory is not touched 00640 * in any way. Managing the pointer is the user's responsibility. 00641 */ 00642 size_type 00643 erase(const key_type& __x) 00644 { return _M_t.erase(__x); } 00645 00646 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00647 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00648 // DR 130. Associative erase should return an iterator. 00649 /** 00650 * @brief Erases a [first,last) range of elements from a %map. 00651 * @param first Iterator pointing to the start of the range to be 00652 * erased. 00653 * @param last Iterator pointing to the end of the range to be erased. 00654 * @return The iterator @a last. 00655 * 00656 * This function erases a sequence of elements from a %map. 00657 * Note that this function only erases the element, and that if 00658 * the element is itself a pointer, the pointed-to memory is not touched 00659 * in any way. Managing the pointer is the user's responsibility. 00660 */ 00661 iterator 00662 erase(const_iterator __first, const_iterator __last) 00663 { return _M_t.erase(__first, __last); } 00664 #else 00665 /** 00666 * @brief Erases a [first,last) range of elements from a %map. 00667 * @param first Iterator pointing to the start of the range to be 00668 * erased. 00669 * @param last Iterator pointing to the end of the range to be erased. 00670 * 00671 * This function erases a sequence of elements from a %map. 00672 * Note that this function only erases the element, and that if 00673 * the element is itself a pointer, the pointed-to memory is not touched 00674 * in any way. Managing the pointer is the user's responsibility. 00675 */ 00676 void 00677 erase(iterator __first, iterator __last) 00678 { _M_t.erase(__first, __last); } 00679 #endif 00680 00681 /** 00682 * @brief Swaps data with another %map. 00683 * @param x A %map of the same element and allocator types. 00684 * 00685 * This exchanges the elements between two maps in constant 00686 * time. (It is only swapping a pointer, an integer, and an 00687 * instance of the @c Compare type (which itself is often 00688 * stateless and empty), so it should be quite fast.) Note 00689 * that the global std::swap() function is specialized such 00690 * that std::swap(m1,m2) will feed to this function. 00691 */ 00692 void 00693 swap(map& __x) 00694 { _M_t.swap(__x._M_t); } 00695 00696 /** 00697 * Erases all elements in a %map. Note that this function only 00698 * erases the elements, and that if the elements themselves are 00699 * pointers, the pointed-to memory is not touched in any way. 00700 * Managing the pointer is the user's responsibility. 00701 */ 00702 void 00703 clear() 00704 { _M_t.clear(); } 00705 00706 // observers 00707 /** 00708 * Returns the key comparison object out of which the %map was 00709 * constructed. 00710 */ 00711 key_compare 00712 key_comp() const 00713 { return _M_t.key_comp(); } 00714 00715 /** 00716 * Returns a value comparison object, built from the key comparison 00717 * object out of which the %map was constructed. 00718 */ 00719 value_compare 00720 value_comp() const 00721 { return value_compare(_M_t.key_comp()); } 00722 00723 // [23.3.1.3] map operations 00724 /** 00725 * @brief Tries to locate an element in a %map. 00726 * @param x Key of (key, value) %pair to be located. 00727 * @return Iterator pointing to sought-after element, or end() if not 00728 * found. 00729 * 00730 * This function takes a key and tries to locate the element with which 00731 * the key matches. If successful the function returns an iterator 00732 * pointing to the sought after %pair. If unsuccessful it returns the 00733 * past-the-end ( @c end() ) iterator. 00734 */ 00735 iterator 00736 find(const key_type& __x) 00737 { return _M_t.find(__x); } 00738 00739 /** 00740 * @brief Tries to locate an element in a %map. 00741 * @param x Key of (key, value) %pair to be located. 00742 * @return Read-only (constant) iterator pointing to sought-after 00743 * element, or end() if not found. 00744 * 00745 * This function takes a key and tries to locate the element with which 00746 * the key matches. If successful the function returns a constant 00747 * iterator pointing to the sought after %pair. If unsuccessful it 00748 * returns the past-the-end ( @c end() ) iterator. 00749 */ 00750 const_iterator 00751 find(const key_type& __x) const 00752 { return _M_t.find(__x); } 00753 00754 /** 00755 * @brief Finds the number of elements with given key. 00756 * @param x Key of (key, value) pairs to be located. 00757 * @return Number of elements with specified key. 00758 * 00759 * This function only makes sense for multimaps; for map the result will 00760 * either be 0 (not present) or 1 (present). 00761 */ 00762 size_type 00763 count(const key_type& __x) const 00764 { return _M_t.find(__x) == _M_t.end() ? 0 : 1; } 00765 00766 /** 00767 * @brief Finds the beginning of a subsequence matching given key. 00768 * @param x Key of (key, value) pair to be located. 00769 * @return Iterator pointing to first element equal to or greater 00770 * than key, or end(). 00771 * 00772 * This function returns the first element of a subsequence of elements 00773 * that matches the given key. If unsuccessful it returns an iterator 00774 * pointing to the first element that has a greater value than given key 00775 * or end() if no such element exists. 00776 */ 00777 iterator 00778 lower_bound(const key_type& __x) 00779 { return _M_t.lower_bound(__x); } 00780 00781 /** 00782 * @brief Finds the beginning of a subsequence matching given key. 00783 * @param x Key of (key, value) pair to be located. 00784 * @return Read-only (constant) iterator pointing to first element 00785 * equal to or greater than key, or end(). 00786 * 00787 * This function returns the first element of a subsequence of elements 00788 * that matches the given key. If unsuccessful it returns an iterator 00789 * pointing to the first element that has a greater value than given key 00790 * or end() if no such element exists. 00791 */ 00792 const_iterator 00793 lower_bound(const key_type& __x) const 00794 { return _M_t.lower_bound(__x); } 00795 00796 /** 00797 * @brief Finds the end of a subsequence matching given key. 00798 * @param x Key of (key, value) pair to be located. 00799 * @return Iterator pointing to the first element 00800 * greater than key, or end(). 00801 */ 00802 iterator 00803 upper_bound(const key_type& __x) 00804 { return _M_t.upper_bound(__x); } 00805 00806 /** 00807 * @brief Finds the end of a subsequence matching given key. 00808 * @param x Key of (key, value) pair to be located. 00809 * @return Read-only (constant) iterator pointing to first iterator 00810 * greater than key, or end(). 00811 */ 00812 const_iterator 00813 upper_bound(const key_type& __x) const 00814 { return _M_t.upper_bound(__x); } 00815 00816 /** 00817 * @brief Finds a subsequence matching given key. 00818 * @param x Key of (key, value) pairs to be located. 00819 * @return Pair of iterators that possibly points to the subsequence 00820 * matching given key. 00821 * 00822 * This function is equivalent to 00823 * @code 00824 * std::make_pair(c.lower_bound(val), 00825 * c.upper_bound(val)) 00826 * @endcode 00827 * (but is faster than making the calls separately). 00828 * 00829 * This function probably only makes sense for multimaps. 00830 */ 00831 std::pair<iterator, iterator> 00832 equal_range(const key_type& __x) 00833 { return _M_t.equal_range(__x); } 00834 00835 /** 00836 * @brief Finds a subsequence matching given key. 00837 * @param x Key of (key, value) pairs to be located. 00838 * @return Pair of read-only (constant) iterators that possibly points 00839 * to the subsequence matching given key. 00840 * 00841 * This function is equivalent to 00842 * @code 00843 * std::make_pair(c.lower_bound(val), 00844 * c.upper_bound(val)) 00845 * @endcode 00846 * (but is faster than making the calls separately). 00847 * 00848 * This function probably only makes sense for multimaps. 00849 */ 00850 std::pair<const_iterator, const_iterator> 00851 equal_range(const key_type& __x) const 00852 { return _M_t.equal_range(__x); } 00853 00854 template<typename _K1, typename _T1, typename _C1, typename _A1> 00855 friend bool 00856 operator==(const map<_K1, _T1, _C1, _A1>&, 00857 const map<_K1, _T1, _C1, _A1>&); 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 00865 /** 00866 * @brief Map equality comparison. 00867 * @param x A %map. 00868 * @param y A %map of the same type as @a x. 00869 * @return True iff the size and elements of the maps are equal. 00870 * 00871 * This is an equivalence relation. It is linear in the size of the 00872 * maps. Maps are considered equivalent if their sizes are equal, 00873 * and if corresponding elements compare equal. 00874 */ 00875 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00876 inline bool 00877 operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00878 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00879 { return __x._M_t == __y._M_t; } 00880 00881 /** 00882 * @brief Map ordering relation. 00883 * @param x A %map. 00884 * @param y A %map of the same type as @a x. 00885 * @return True iff @a x is lexicographically less than @a y. 00886 * 00887 * This is a total ordering relation. It is linear in the size of the 00888 * maps. The elements must be comparable with @c <. 00889 * 00890 * See std::lexicographical_compare() for how the determination is made. 00891 */ 00892 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00893 inline bool 00894 operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00895 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00896 { return __x._M_t < __y._M_t; } 00897 00898 /// Based on operator== 00899 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00900 inline bool 00901 operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00902 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00903 { return !(__x == __y); } 00904 00905 /// Based on operator< 00906 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00907 inline bool 00908 operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00909 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00910 { return __y < __x; } 00911 00912 /// Based on operator< 00913 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00914 inline bool 00915 operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00916 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00917 { return !(__y < __x); } 00918 00919 /// Based on operator< 00920 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00921 inline bool 00922 operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x, 00923 const map<_Key, _Tp, _Compare, _Alloc>& __y) 00924 { return !(__x < __y); } 00925 00926 /// See std::map::swap(). 00927 template<typename _Key, typename _Tp, typename _Compare, typename _Alloc> 00928 inline void 00929 swap(map<_Key, _Tp, _Compare, _Alloc>& __x, 00930 map<_Key, _Tp, _Compare, _Alloc>& __y) 00931 { __x.swap(__y); } 00932 00933 _GLIBCXX_END_NAMESPACE_CONTAINER 00934 } // namespace std 00935 00936 #endif /* _STL_MAP_H */