stl_map.h

Go to the documentation of this file.
00001 // Map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 1204.
00269     // NB: DR 675.
00270     this->clear();
00271     this->swap(__x);
00272     return *this;
00273       }
00274 
00275       /**
00276        *  @brief  %Map list assignment operator.
00277        *  @param  l  An initializer_list.
00278        *
00279        *  This function fills a %map with copies of the elements in the
00280        *  initializer list @a l.
00281        *
00282        *  Note that the assignment completely changes the %map and
00283        *  that the resulting %map's size is the same as the number
00284        *  of elements assigned.  Old data may be lost.
00285        */
00286       map&
00287       operator=(initializer_list<value_type> __l)
00288       {
00289     this->clear();
00290     this->insert(__l.begin(), __l.end());
00291     return *this;
00292       }
00293 #endif
00294 
00295       /// Get a copy of the memory allocation object.
00296       allocator_type
00297       get_allocator() const
00298       { return _M_t.get_allocator(); }
00299 
00300       // iterators
00301       /**
00302        *  Returns a read/write iterator that points to the first pair in the
00303        *  %map.
00304        *  Iteration is done in ascending order according to the keys.
00305        */
00306       iterator
00307       begin()
00308       { return _M_t.begin(); }
00309 
00310       /**
00311        *  Returns a read-only (constant) iterator that points to the first pair
00312        *  in the %map.  Iteration is done in ascending order according to the
00313        *  keys.
00314        */
00315       const_iterator
00316       begin() const
00317       { return _M_t.begin(); }
00318 
00319       /**
00320        *  Returns a read/write iterator that points one past the last
00321        *  pair in the %map.  Iteration is done in ascending order
00322        *  according to the keys.
00323        */
00324       iterator
00325       end()
00326       { return _M_t.end(); }
00327 
00328       /**
00329        *  Returns a read-only (constant) iterator that points one past the last
00330        *  pair in the %map.  Iteration is done in ascending order according to
00331        *  the keys.
00332        */
00333       const_iterator
00334       end() const
00335       { return _M_t.end(); }
00336 
00337       /**
00338        *  Returns a read/write reverse iterator that points to the last pair in
00339        *  the %map.  Iteration is done in descending order according to the
00340        *  keys.
00341        */
00342       reverse_iterator
00343       rbegin()
00344       { return _M_t.rbegin(); }
00345 
00346       /**
00347        *  Returns a read-only (constant) reverse iterator that points to the
00348        *  last pair in the %map.  Iteration is done in descending order
00349        *  according to the keys.
00350        */
00351       const_reverse_iterator
00352       rbegin() const
00353       { return _M_t.rbegin(); }
00354 
00355       /**
00356        *  Returns a read/write reverse iterator that points to one before the
00357        *  first pair in the %map.  Iteration is done in descending order
00358        *  according to the keys.
00359        */
00360       reverse_iterator
00361       rend()
00362       { return _M_t.rend(); }
00363 
00364       /**
00365        *  Returns a read-only (constant) reverse iterator that points to one
00366        *  before the first pair in the %map.  Iteration is done in descending
00367        *  order according to the keys.
00368        */
00369       const_reverse_iterator
00370       rend() const
00371       { return _M_t.rend(); }
00372 
00373 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00374       /**
00375        *  Returns a read-only (constant) iterator that points to the first pair
00376        *  in the %map.  Iteration is done in ascending order according to the
00377        *  keys.
00378        */
00379       const_iterator
00380       cbegin() const
00381       { return _M_t.begin(); }
00382 
00383       /**
00384        *  Returns a read-only (constant) iterator that points one past the last
00385        *  pair in the %map.  Iteration is done in ascending order according to
00386        *  the keys.
00387        */
00388       const_iterator
00389       cend() const
00390       { return _M_t.end(); }
00391 
00392       /**
00393        *  Returns a read-only (constant) reverse iterator that points to the
00394        *  last pair in the %map.  Iteration is done in descending order
00395        *  according to the keys.
00396        */
00397       const_reverse_iterator
00398       crbegin() const
00399       { return _M_t.rbegin(); }
00400 
00401       /**
00402        *  Returns a read-only (constant) reverse iterator that points to one
00403        *  before the first pair in the %map.  Iteration is done in descending
00404        *  order according to the keys.
00405        */
00406       const_reverse_iterator
00407       crend() const
00408       { return _M_t.rend(); }
00409 #endif
00410 
00411       // capacity
00412       /** Returns true if the %map is empty.  (Thus begin() would equal
00413        *  end().)
00414       */
00415       bool
00416       empty() const
00417       { return _M_t.empty(); }
00418 
00419       /** Returns the size of the %map.  */
00420       size_type
00421       size() const
00422       { return _M_t.size(); }
00423 
00424       /** Returns the maximum size of the %map.  */
00425       size_type
00426       max_size() const
00427       { return _M_t.max_size(); }
00428 
00429       // [23.3.1.2] element access
00430       /**
00431        *  @brief  Subscript ( @c [] ) access to %map data.
00432        *  @param  k  The key for which data should be retrieved.
00433        *  @return  A reference to the data of the (key,data) %pair.
00434        *
00435        *  Allows for easy lookup with the subscript ( @c [] )
00436        *  operator.  Returns data associated with the key specified in
00437        *  subscript.  If the key does not exist, a pair with that key
00438        *  is created using default values, which is then returned.
00439        *
00440        *  Lookup requires logarithmic time.
00441        */
00442       mapped_type&
00443       operator[](const key_type& __k)
00444       {
00445     // concept requirements
00446     __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
00447 
00448     iterator __i = lower_bound(__k);
00449     // __i->first is greater than or equivalent to __k.
00450     if (__i == end() || key_comp()(__k, (*__i).first))
00451           __i = insert(__i, value_type(__k, mapped_type()));
00452     return (*__i).second;
00453       }
00454 
00455       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00456       // DR 464. Suggestion for new member functions in standard containers.
00457       /**
00458        *  @brief  Access to %map data.
00459        *  @param  k  The key for which data should be retrieved.
00460        *  @return  A reference to the data whose key is equivalent to @a k, if
00461        *           such a data is present in the %map.
00462        *  @throw  std::out_of_range  If no such data is present.
00463        */
00464       mapped_type&
00465       at(const key_type& __k)
00466       {
00467     iterator __i = lower_bound(__k);
00468     if (__i == end() || key_comp()(__k, (*__i).first))
00469       __throw_out_of_range(__N("map::at"));
00470     return (*__i).second;
00471       }
00472 
00473       const mapped_type&
00474       at(const key_type& __k) const
00475       {
00476     const_iterator __i = lower_bound(__k);
00477     if (__i == end() || key_comp()(__k, (*__i).first))
00478       __throw_out_of_range(__N("map::at"));
00479     return (*__i).second;
00480       }
00481 
00482       // modifiers
00483       /**
00484        *  @brief Attempts to insert a std::pair into the %map.
00485 
00486        *  @param  x  Pair to be inserted (see std::make_pair for easy creation 
00487        *         of pairs).
00488 
00489        *  @return  A pair, of which the first element is an iterator that 
00490        *           points to the possibly inserted pair, and the second is 
00491        *           a bool that is true if the pair was actually inserted.
00492        *
00493        *  This function attempts to insert a (key, value) %pair into the %map.
00494        *  A %map relies on unique keys and thus a %pair is only inserted if its
00495        *  first element (the key) is not already present in the %map.
00496        *
00497        *  Insertion requires logarithmic time.
00498        */
00499       std::pair<iterator, bool>
00500       insert(const value_type& __x)
00501       { return _M_t._M_insert_unique(__x); }
00502 
00503 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00504       /**
00505        *  @brief Attempts to insert a list of std::pairs into the %map.
00506        *  @param  list  A std::initializer_list<value_type> of pairs to be
00507        *                inserted.
00508        *
00509        *  Complexity similar to that of the range constructor.
00510        */
00511       void
00512       insert(std::initializer_list<value_type> __list)
00513       { insert (__list.begin(), __list.end()); }
00514 #endif
00515 
00516       /**
00517        *  @brief Attempts to insert a std::pair into the %map.
00518        *  @param  position  An iterator that serves as a hint as to where the
00519        *                    pair should be inserted.
00520        *  @param  x  Pair to be inserted (see std::make_pair for easy creation
00521        *             of pairs).
00522        *  @return  An iterator that points to the element with key of @a x (may
00523        *           or may not be the %pair passed in).
00524        *
00525 
00526        *  This function is not concerned about whether the insertion
00527        *  took place, and thus does not return a boolean like the
00528        *  single-argument insert() does.  Note that the first
00529        *  parameter is only a hint and can potentially improve the
00530        *  performance of the insertion process.  A bad hint would
00531        *  cause no gains in efficiency.
00532        *
00533        *  See
00534        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
00535        *  for more on @a hinting.
00536        *
00537        *  Insertion requires logarithmic time (if the hint is not taken).
00538        */
00539       iterator
00540       insert(iterator __position, const value_type& __x)
00541       { return _M_t._M_insert_unique_(__position, __x); }
00542 
00543       /**
00544        *  @brief Template function that attempts to insert a range of elements.
00545        *  @param  first  Iterator pointing to the start of the range to be
00546        *                 inserted.
00547        *  @param  last  Iterator pointing to the end of the range.
00548        *
00549        *  Complexity similar to that of the range constructor.
00550        */
00551       template<typename _InputIterator>
00552         void
00553         insert(_InputIterator __first, _InputIterator __last)
00554         { _M_t._M_insert_unique(__first, __last); }
00555 
00556 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00557       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00558       // DR 130. Associative erase should return an iterator.
00559       /**
00560        *  @brief Erases an element from a %map.
00561        *  @param  position  An iterator pointing to the element to be erased.
00562        *  @return An iterator pointing to the element immediately following
00563        *          @a position prior to the element being erased. If no such 
00564        *          element exists, end() is returned.
00565        *
00566        *  This function erases an element, pointed to by the given
00567        *  iterator, from a %map.  Note that this function only erases
00568        *  the element, and that if the element is itself a pointer,
00569        *  the pointed-to memory is not touched in any way.  Managing
00570        *  the pointer is the user's responsibility.
00571        */
00572       iterator
00573       erase(iterator __position)
00574       { return _M_t.erase(__position); }
00575 #else
00576       /**
00577        *  @brief Erases an element from a %map.
00578        *  @param  position  An iterator pointing to the element to be erased.
00579        *
00580        *  This function erases an element, pointed to by the given
00581        *  iterator, from a %map.  Note that this function only erases
00582        *  the element, and that if the element is itself a pointer,
00583        *  the pointed-to memory is not touched in any way.  Managing
00584        *  the pointer is the user's responsibility.
00585        */
00586       void
00587       erase(iterator __position)
00588       { _M_t.erase(__position); }
00589 #endif
00590 
00591       /**
00592        *  @brief Erases elements according to the provided key.
00593        *  @param  x  Key of element to be erased.
00594        *  @return  The number of elements erased.
00595        *
00596        *  This function erases all the elements located by the given key from
00597        *  a %map.
00598        *  Note that this function only erases the element, and that if
00599        *  the element is itself a pointer, the pointed-to memory is not touched
00600        *  in any way.  Managing the pointer is the user's responsibility.
00601        */
00602       size_type
00603       erase(const key_type& __x)
00604       { return _M_t.erase(__x); }
00605 
00606 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00607       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00608       // DR 130. Associative erase should return an iterator.
00609       /**
00610        *  @brief Erases a [first,last) range of elements from a %map.
00611        *  @param  first  Iterator pointing to the start of the range to be
00612        *                 erased.
00613        *  @param  last  Iterator pointing to the end of the range to be erased.
00614        *  @return The iterator @a last.
00615        *
00616        *  This function erases a sequence of elements from a %map.
00617        *  Note that this function only erases the element, and that if
00618        *  the element is itself a pointer, the pointed-to memory is not touched
00619        *  in any way.  Managing the pointer is the user's responsibility.
00620        */
00621       iterator
00622       erase(iterator __first, iterator __last)
00623       { return _M_t.erase(__first, __last); }
00624 #else
00625       /**
00626        *  @brief Erases a [first,last) range of elements from a %map.
00627        *  @param  first  Iterator pointing to the start of the range to be
00628        *                 erased.
00629        *  @param  last  Iterator pointing to the end of the range to be erased.
00630        *
00631        *  This function erases a sequence of elements from a %map.
00632        *  Note that this function only erases the element, and that if
00633        *  the element is itself a pointer, the pointed-to memory is not touched
00634        *  in any way.  Managing the pointer is the user's responsibility.
00635        */
00636       void
00637       erase(iterator __first, iterator __last)
00638       { _M_t.erase(__first, __last); }
00639 #endif
00640 
00641       /**
00642        *  @brief  Swaps data with another %map.
00643        *  @param  x  A %map of the same element and allocator types.
00644        *
00645        *  This exchanges the elements between two maps in constant
00646        *  time.  (It is only swapping a pointer, an integer, and an
00647        *  instance of the @c Compare type (which itself is often
00648        *  stateless and empty), so it should be quite fast.)  Note
00649        *  that the global std::swap() function is specialized such
00650        *  that std::swap(m1,m2) will feed to this function.
00651        */
00652       void
00653       swap(map& __x)
00654       { _M_t.swap(__x._M_t); }
00655 
00656       /**
00657        *  Erases all elements in a %map.  Note that this function only
00658        *  erases the elements, and that if the elements themselves are
00659        *  pointers, the pointed-to memory is not touched in any way.
00660        *  Managing the pointer is the user's responsibility.
00661        */
00662       void
00663       clear()
00664       { _M_t.clear(); }
00665 
00666       // observers
00667       /**
00668        *  Returns the key comparison object out of which the %map was
00669        *  constructed.
00670        */
00671       key_compare
00672       key_comp() const
00673       { return _M_t.key_comp(); }
00674 
00675       /**
00676        *  Returns a value comparison object, built from the key comparison
00677        *  object out of which the %map was constructed.
00678        */
00679       value_compare
00680       value_comp() const
00681       { return value_compare(_M_t.key_comp()); }
00682 
00683       // [23.3.1.3] map operations
00684       /**
00685        *  @brief Tries to locate an element in a %map.
00686        *  @param  x  Key of (key, value) %pair to be located.
00687        *  @return  Iterator pointing to sought-after element, or end() if not
00688        *           found.
00689        *
00690        *  This function takes a key and tries to locate the element with which
00691        *  the key matches.  If successful the function returns an iterator
00692        *  pointing to the sought after %pair.  If unsuccessful it returns the
00693        *  past-the-end ( @c end() ) iterator.
00694        */
00695       iterator
00696       find(const key_type& __x)
00697       { return _M_t.find(__x); }
00698 
00699       /**
00700        *  @brief Tries to locate an element in a %map.
00701        *  @param  x  Key of (key, value) %pair to be located.
00702        *  @return  Read-only (constant) iterator pointing to sought-after
00703        *           element, or end() if not found.
00704        *
00705        *  This function takes a key and tries to locate the element with which
00706        *  the key matches.  If successful the function returns a constant
00707        *  iterator pointing to the sought after %pair. If unsuccessful it
00708        *  returns the past-the-end ( @c end() ) iterator.
00709        */
00710       const_iterator
00711       find(const key_type& __x) const
00712       { return _M_t.find(__x); }
00713 
00714       /**
00715        *  @brief  Finds the number of elements with given key.
00716        *  @param  x  Key of (key, value) pairs to be located.
00717        *  @return  Number of elements with specified key.
00718        *
00719        *  This function only makes sense for multimaps; for map the result will
00720        *  either be 0 (not present) or 1 (present).
00721        */
00722       size_type
00723       count(const key_type& __x) const
00724       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00725 
00726       /**
00727        *  @brief Finds the beginning of a subsequence matching given key.
00728        *  @param  x  Key of (key, value) pair to be located.
00729        *  @return  Iterator pointing to first element equal to or greater
00730        *           than key, or end().
00731        *
00732        *  This function returns the first element of a subsequence of elements
00733        *  that matches the given key.  If unsuccessful it returns an iterator
00734        *  pointing to the first element that has a greater value than given key
00735        *  or end() if no such element exists.
00736        */
00737       iterator
00738       lower_bound(const key_type& __x)
00739       { return _M_t.lower_bound(__x); }
00740 
00741       /**
00742        *  @brief Finds the beginning of a subsequence matching given key.
00743        *  @param  x  Key of (key, value) pair to be located.
00744        *  @return  Read-only (constant) iterator pointing to first element
00745        *           equal to or greater than key, or end().
00746        *
00747        *  This function returns the first element of a subsequence of elements
00748        *  that matches the given key.  If unsuccessful it returns an iterator
00749        *  pointing to the first element that has a greater value than given key
00750        *  or end() if no such element exists.
00751        */
00752       const_iterator
00753       lower_bound(const key_type& __x) const
00754       { return _M_t.lower_bound(__x); }
00755 
00756       /**
00757        *  @brief Finds the end of a subsequence matching given key.
00758        *  @param  x  Key of (key, value) pair to be located.
00759        *  @return Iterator pointing to the first element
00760        *          greater than key, or end().
00761        */
00762       iterator
00763       upper_bound(const key_type& __x)
00764       { return _M_t.upper_bound(__x); }
00765 
00766       /**
00767        *  @brief Finds the end of a subsequence matching given key.
00768        *  @param  x  Key of (key, value) pair to be located.
00769        *  @return  Read-only (constant) iterator pointing to first iterator
00770        *           greater than key, or end().
00771        */
00772       const_iterator
00773       upper_bound(const key_type& __x) const
00774       { return _M_t.upper_bound(__x); }
00775 
00776       /**
00777        *  @brief Finds a subsequence matching given key.
00778        *  @param  x  Key of (key, value) pairs to be located.
00779        *  @return  Pair of iterators that possibly points to the subsequence
00780        *           matching given key.
00781        *
00782        *  This function is equivalent to
00783        *  @code
00784        *    std::make_pair(c.lower_bound(val),
00785        *                   c.upper_bound(val))
00786        *  @endcode
00787        *  (but is faster than making the calls separately).
00788        *
00789        *  This function probably only makes sense for multimaps.
00790        */
00791       std::pair<iterator, iterator>
00792       equal_range(const key_type& __x)
00793       { return _M_t.equal_range(__x); }
00794 
00795       /**
00796        *  @brief Finds a subsequence matching given key.
00797        *  @param  x  Key of (key, value) pairs to be located.
00798        *  @return  Pair of read-only (constant) iterators that possibly points
00799        *           to the subsequence matching given key.
00800        *
00801        *  This function is equivalent to
00802        *  @code
00803        *    std::make_pair(c.lower_bound(val),
00804        *                   c.upper_bound(val))
00805        *  @endcode
00806        *  (but is faster than making the calls separately).
00807        *
00808        *  This function probably only makes sense for multimaps.
00809        */
00810       std::pair<const_iterator, const_iterator>
00811       equal_range(const key_type& __x) const
00812       { return _M_t.equal_range(__x); }
00813 
00814       template<typename _K1, typename _T1, typename _C1, typename _A1>
00815         friend bool
00816         operator==(const map<_K1, _T1, _C1, _A1>&,
00817            const map<_K1, _T1, _C1, _A1>&);
00818 
00819       template<typename _K1, typename _T1, typename _C1, typename _A1>
00820         friend bool
00821         operator<(const map<_K1, _T1, _C1, _A1>&,
00822           const map<_K1, _T1, _C1, _A1>&);
00823     };
00824 
00825   /**
00826    *  @brief  Map equality comparison.
00827    *  @param  x  A %map.
00828    *  @param  y  A %map of the same type as @a x.
00829    *  @return  True iff the size and elements of the maps are equal.
00830    *
00831    *  This is an equivalence relation.  It is linear in the size of the
00832    *  maps.  Maps are considered equivalent if their sizes are equal,
00833    *  and if corresponding elements compare equal.
00834   */
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 __x._M_t == __y._M_t; }
00840 
00841   /**
00842    *  @brief  Map ordering relation.
00843    *  @param  x  A %map.
00844    *  @param  y  A %map of the same type as @a x.
00845    *  @return  True iff @a x is lexicographically less than @a y.
00846    *
00847    *  This is a total ordering relation.  It is linear in the size of the
00848    *  maps.  The elements must be comparable with @c <.
00849    *
00850    *  See std::lexicographical_compare() for how the determination is made.
00851   */
00852   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00853     inline bool
00854     operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00855               const map<_Key, _Tp, _Compare, _Alloc>& __y)
00856     { return __x._M_t < __y._M_t; }
00857 
00858   /// Based on operator==
00859   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00860     inline bool
00861     operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00862                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00863     { return !(__x == __y); }
00864 
00865   /// Based on operator<
00866   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00867     inline bool
00868     operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00869               const map<_Key, _Tp, _Compare, _Alloc>& __y)
00870     { return __y < __x; }
00871 
00872   /// Based on operator<
00873   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00874     inline bool
00875     operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00876                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00877     { return !(__y < __x); }
00878 
00879   /// Based on operator<
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 < __y); }
00885 
00886   /// See std::map::swap().
00887   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00888     inline void
00889     swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
00890      map<_Key, _Tp, _Compare, _Alloc>& __y)
00891     { __x.swap(__y); }
00892 
00893 _GLIBCXX_END_NESTED_NAMESPACE
00894 
00895 #endif /* _STL_MAP_H */