libstdc++
stl_map.h
Go to the documentation of this file.
00001 // Map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2014 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996,1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_map.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{map}
00054  */
00055 
00056 #ifndef _STL_MAP_H
00057 #define _STL_MAP_H 1
00058 
00059 #include <bits/functexcept.h>
00060 #include <bits/concept_check.h>
00061 #if __cplusplus >= 201103L
00062 #include <initializer_list>
00063 #include <tuple>
00064 #endif
00065 
00066 namespace std _GLIBCXX_VISIBILITY(default)
00067 {
00068 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
00069 
00070   /**
00071    *  @brief A standard container made up of (key,value) pairs, which can be
00072    *  retrieved based on a key, in logarithmic time.
00073    *
00074    *  @ingroup associative_containers
00075    *
00076    *  @tparam _Key  Type of key objects.
00077    *  @tparam  _Tp  Type of mapped objects.
00078    *  @tparam _Compare  Comparison function object type, defaults to less<_Key>.
00079    *  @tparam _Alloc  Allocator type, defaults to 
00080    *                  allocator<pair<const _Key, _Tp>.
00081    *
00082    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
00083    *  <a href="tables.html#66">reversible container</a>, and an
00084    *  <a href="tables.html#69">associative container</a> (using unique keys).
00085    *  For a @c map<Key,T> the key_type is Key, the mapped_type is T, and the
00086    *  value_type is std::pair<const Key,T>.
00087    *
00088    *  Maps support bidirectional iterators.
00089    *
00090    *  The private tree data is declared exactly the same way for map and
00091    *  multimap; the distinction is made entirely in how the tree functions are
00092    *  called (*_unique versus *_equal, same as the standard).
00093   */
00094   template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00095             typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
00096     class map
00097     {
00098     public:
00099       typedef _Key                                          key_type;
00100       typedef _Tp                                           mapped_type;
00101       typedef std::pair<const _Key, _Tp>                    value_type;
00102       typedef _Compare                                      key_compare;
00103       typedef _Alloc                                        allocator_type;
00104 
00105     private:
00106       // concept requirements
00107       typedef typename _Alloc::value_type                   _Alloc_value_type;
00108       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00109       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00110                 _BinaryFunctionConcept)
00111       __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
00112 
00113     public:
00114       class value_compare
00115       : public std::binary_function<value_type, value_type, bool>
00116       {
00117     friend class map<_Key, _Tp, _Compare, _Alloc>;
00118       protected:
00119     _Compare comp;
00120 
00121     value_compare(_Compare __c)
00122     : comp(__c) { }
00123 
00124       public:
00125     bool operator()(const value_type& __x, const value_type& __y) const
00126     { return comp(__x.first, __y.first); }
00127       };
00128 
00129     private:
00130       /// This turns a red-black tree into a [multi]map. 
00131       typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
00132     rebind<value_type>::other _Pair_alloc_type;
00133 
00134       typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
00135                key_compare, _Pair_alloc_type> _Rep_type;
00136 
00137       /// The actual tree structure.
00138       _Rep_type _M_t;
00139 
00140       typedef __gnu_cxx::__alloc_traits<_Pair_alloc_type> _Alloc_traits;
00141 
00142     public:
00143       // many of these are specified differently in ISO, but the following are
00144       // "functionally equivalent"
00145       typedef typename _Alloc_traits::pointer            pointer;
00146       typedef typename _Alloc_traits::const_pointer      const_pointer;
00147       typedef typename _Alloc_traits::reference          reference;
00148       typedef typename _Alloc_traits::const_reference    const_reference;
00149       typedef typename _Rep_type::iterator               iterator;
00150       typedef typename _Rep_type::const_iterator         const_iterator;
00151       typedef typename _Rep_type::size_type              size_type;
00152       typedef typename _Rep_type::difference_type        difference_type;
00153       typedef typename _Rep_type::reverse_iterator       reverse_iterator;
00154       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00155 
00156       // [23.3.1.1] construct/copy/destroy
00157       // (get_allocator() is also listed in this section)
00158 
00159       /**
00160        *  @brief  Default constructor creates no elements.
00161        */
00162       map()
00163       : _M_t() { }
00164 
00165       /**
00166        *  @brief  Creates a %map with no elements.
00167        *  @param  __comp  A comparison object.
00168        *  @param  __a  An allocator object.
00169        */
00170       explicit
00171       map(const _Compare& __comp,
00172       const allocator_type& __a = allocator_type())
00173       : _M_t(__comp, _Pair_alloc_type(__a)) { }
00174 
00175       /**
00176        *  @brief  %Map copy constructor.
00177        *  @param  __x  A %map of identical element and allocator types.
00178        *
00179        *  The newly-created %map uses a copy of the allocation object
00180        *  used by @a __x.
00181        */
00182       map(const map& __x)
00183       : _M_t(__x._M_t) { }
00184 
00185 #if __cplusplus >= 201103L
00186       /**
00187        *  @brief  %Map move constructor.
00188        *  @param  __x  A %map of identical element and allocator types.
00189        *
00190        *  The newly-created %map contains the exact contents of @a __x.
00191        *  The contents of @a __x are a valid, but unspecified %map.
00192        */
00193       map(map&& __x)
00194       noexcept(is_nothrow_copy_constructible<_Compare>::value)
00195       : _M_t(std::move(__x._M_t)) { }
00196 
00197       /**
00198        *  @brief  Builds a %map from an initializer_list.
00199        *  @param  __l  An initializer_list.
00200        *  @param  __comp  A comparison object.
00201        *  @param  __a  An allocator object.
00202        *
00203        *  Create a %map consisting of copies of the elements in the
00204        *  initializer_list @a __l.
00205        *  This is linear in N if the range is already sorted, and NlogN
00206        *  otherwise (where N is @a __l.size()).
00207        */
00208       map(initializer_list<value_type> __l,
00209       const _Compare& __comp = _Compare(),
00210       const allocator_type& __a = allocator_type())
00211       : _M_t(__comp, _Pair_alloc_type(__a))
00212       { _M_t._M_insert_unique(__l.begin(), __l.end()); }
00213 
00214       /// Allocator-extended default constructor.
00215       explicit
00216       map(const allocator_type& __a)
00217       : _M_t(_Compare(), _Pair_alloc_type(__a)) { }
00218 
00219       /// Allocator-extended copy constructor.
00220       map(const map& __m, const allocator_type& __a)
00221       : _M_t(__m._M_t, _Pair_alloc_type(__a)) { }
00222 
00223       /// Allocator-extended move constructor.
00224       map(map&& __m, const allocator_type& __a)
00225       noexcept(is_nothrow_copy_constructible<_Compare>::value
00226            && _Alloc_traits::_S_always_equal())
00227       : _M_t(std::move(__m._M_t), _Pair_alloc_type(__a)) { }
00228 
00229       /// Allocator-extended initialier-list constructor.
00230       map(initializer_list<value_type> __l, const allocator_type& __a)
00231       : _M_t(_Compare(), _Pair_alloc_type(__a))
00232       { _M_t._M_insert_unique(__l.begin(), __l.end()); }
00233 
00234       /// Allocator-extended range constructor.
00235       template<typename _InputIterator>
00236         map(_InputIterator __first, _InputIterator __last,
00237         const allocator_type& __a)
00238     : _M_t(_Compare(), _Pair_alloc_type(__a))
00239         { _M_t._M_insert_unique(__first, __last); }
00240 #endif
00241 
00242       /**
00243        *  @brief  Builds a %map from a range.
00244        *  @param  __first  An input iterator.
00245        *  @param  __last  An input iterator.
00246        *
00247        *  Create a %map consisting of copies of the elements from
00248        *  [__first,__last).  This is linear in N if the range is
00249        *  already sorted, and NlogN otherwise (where N is
00250        *  distance(__first,__last)).
00251        */
00252       template<typename _InputIterator>
00253         map(_InputIterator __first, _InputIterator __last)
00254     : _M_t()
00255         { _M_t._M_insert_unique(__first, __last); }
00256 
00257       /**
00258        *  @brief  Builds a %map from a range.
00259        *  @param  __first  An input iterator.
00260        *  @param  __last  An input iterator.
00261        *  @param  __comp  A comparison functor.
00262        *  @param  __a  An allocator object.
00263        *
00264        *  Create a %map consisting of copies of the elements from
00265        *  [__first,__last).  This is linear in N if the range is
00266        *  already sorted, and NlogN otherwise (where N is
00267        *  distance(__first,__last)).
00268        */
00269       template<typename _InputIterator>
00270         map(_InputIterator __first, _InputIterator __last,
00271         const _Compare& __comp,
00272         const allocator_type& __a = allocator_type())
00273     : _M_t(__comp, _Pair_alloc_type(__a))
00274         { _M_t._M_insert_unique(__first, __last); }
00275 
00276       // FIXME There is no dtor declared, but we should have something
00277       // generated by Doxygen.  I don't know what tags to add to this
00278       // paragraph to make that happen:
00279       /**
00280        *  The dtor only erases the elements, and note that if the elements
00281        *  themselves are pointers, the pointed-to memory is not touched in any
00282        *  way.  Managing the pointer is the user's responsibility.
00283        */
00284 
00285       /**
00286        *  @brief  %Map assignment operator.
00287        *  @param  __x  A %map of identical element and allocator types.
00288        *
00289        *  All the elements of @a __x are copied, but unlike the copy
00290        *  constructor, the allocator object is not copied.
00291        */
00292       map&
00293       operator=(const map& __x)
00294       {
00295     _M_t = __x._M_t;
00296     return *this;
00297       }
00298 
00299 #if __cplusplus >= 201103L
00300       /**
00301        *  @brief  %Map move assignment operator.
00302        *  @param  __x  A %map of identical element and allocator types.
00303        *
00304        *  The contents of @a __x are moved into this map (without copying
00305        *  if the allocators compare equal or get moved on assignment).
00306        *  Afterwards @a __x is in a valid, but unspecified state.
00307        */
00308       map&
00309       operator=(map&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
00310       {
00311     if (!_M_t._M_move_assign(__x._M_t))
00312       {
00313         // The rvalue's allocator cannot be moved and is not equal,
00314         // so we need to individually move each element.
00315         clear();
00316         insert(std::__make_move_if_noexcept_iterator(__x.begin()),
00317            std::__make_move_if_noexcept_iterator(__x.end()));
00318         __x.clear();
00319       }
00320     return *this;
00321       }
00322 
00323       /**
00324        *  @brief  %Map list assignment operator.
00325        *  @param  __l  An initializer_list.
00326        *
00327        *  This function fills a %map with copies of the elements in the
00328        *  initializer list @a __l.
00329        *
00330        *  Note that the assignment completely changes the %map and
00331        *  that the resulting %map's size is the same as the number
00332        *  of elements assigned.  Old data may be lost.
00333        */
00334       map&
00335       operator=(initializer_list<value_type> __l)
00336       {
00337     this->clear();
00338     this->insert(__l.begin(), __l.end());
00339     return *this;
00340       }
00341 #endif
00342 
00343       /// Get a copy of the memory allocation object.
00344       allocator_type
00345       get_allocator() const _GLIBCXX_NOEXCEPT
00346       { return allocator_type(_M_t.get_allocator()); }
00347 
00348       // iterators
00349       /**
00350        *  Returns a read/write iterator that points to the first pair in the
00351        *  %map.
00352        *  Iteration is done in ascending order according to the keys.
00353        */
00354       iterator
00355       begin() _GLIBCXX_NOEXCEPT
00356       { return _M_t.begin(); }
00357 
00358       /**
00359        *  Returns a read-only (constant) iterator that points to the first pair
00360        *  in the %map.  Iteration is done in ascending order according to the
00361        *  keys.
00362        */
00363       const_iterator
00364       begin() const _GLIBCXX_NOEXCEPT
00365       { return _M_t.begin(); }
00366 
00367       /**
00368        *  Returns a read/write iterator that points one past the last
00369        *  pair in the %map.  Iteration is done in ascending order
00370        *  according to the keys.
00371        */
00372       iterator
00373       end() _GLIBCXX_NOEXCEPT
00374       { return _M_t.end(); }
00375 
00376       /**
00377        *  Returns a read-only (constant) iterator that points one past the last
00378        *  pair in the %map.  Iteration is done in ascending order according to
00379        *  the keys.
00380        */
00381       const_iterator
00382       end() const _GLIBCXX_NOEXCEPT
00383       { return _M_t.end(); }
00384 
00385       /**
00386        *  Returns a read/write reverse iterator that points to the last pair in
00387        *  the %map.  Iteration is done in descending order according to the
00388        *  keys.
00389        */
00390       reverse_iterator
00391       rbegin() _GLIBCXX_NOEXCEPT
00392       { return _M_t.rbegin(); }
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       rbegin() const _GLIBCXX_NOEXCEPT
00401       { return _M_t.rbegin(); }
00402 
00403       /**
00404        *  Returns a read/write reverse iterator that points to one before the
00405        *  first pair in the %map.  Iteration is done in descending order
00406        *  according to the keys.
00407        */
00408       reverse_iterator
00409       rend() _GLIBCXX_NOEXCEPT
00410       { return _M_t.rend(); }
00411 
00412       /**
00413        *  Returns a read-only (constant) reverse iterator that points to one
00414        *  before the first pair in the %map.  Iteration is done in descending
00415        *  order according to the keys.
00416        */
00417       const_reverse_iterator
00418       rend() const _GLIBCXX_NOEXCEPT
00419       { return _M_t.rend(); }
00420 
00421 #if __cplusplus >= 201103L
00422       /**
00423        *  Returns a read-only (constant) iterator that points to the first pair
00424        *  in the %map.  Iteration is done in ascending order according to the
00425        *  keys.
00426        */
00427       const_iterator
00428       cbegin() const noexcept
00429       { return _M_t.begin(); }
00430 
00431       /**
00432        *  Returns a read-only (constant) iterator that points one past the last
00433        *  pair in the %map.  Iteration is done in ascending order according to
00434        *  the keys.
00435        */
00436       const_iterator
00437       cend() const noexcept
00438       { return _M_t.end(); }
00439 
00440       /**
00441        *  Returns a read-only (constant) reverse iterator that points to the
00442        *  last pair in the %map.  Iteration is done in descending order
00443        *  according to the keys.
00444        */
00445       const_reverse_iterator
00446       crbegin() const noexcept
00447       { return _M_t.rbegin(); }
00448 
00449       /**
00450        *  Returns a read-only (constant) reverse iterator that points to one
00451        *  before the first pair in the %map.  Iteration is done in descending
00452        *  order according to the keys.
00453        */
00454       const_reverse_iterator
00455       crend() const noexcept
00456       { return _M_t.rend(); }
00457 #endif
00458 
00459       // capacity
00460       /** Returns true if the %map is empty.  (Thus begin() would equal
00461        *  end().)
00462       */
00463       bool
00464       empty() const _GLIBCXX_NOEXCEPT
00465       { return _M_t.empty(); }
00466 
00467       /** Returns the size of the %map.  */
00468       size_type
00469       size() const _GLIBCXX_NOEXCEPT
00470       { return _M_t.size(); }
00471 
00472       /** Returns the maximum size of the %map.  */
00473       size_type
00474       max_size() const _GLIBCXX_NOEXCEPT
00475       { return _M_t.max_size(); }
00476 
00477       // [23.3.1.2] element access
00478       /**
00479        *  @brief  Subscript ( @c [] ) access to %map data.
00480        *  @param  __k  The key for which data should be retrieved.
00481        *  @return  A reference to the data of the (key,data) %pair.
00482        *
00483        *  Allows for easy lookup with the subscript ( @c [] )
00484        *  operator.  Returns data associated with the key specified in
00485        *  subscript.  If the key does not exist, a pair with that key
00486        *  is created using default values, which is then returned.
00487        *
00488        *  Lookup requires logarithmic time.
00489        */
00490       mapped_type&
00491       operator[](const key_type& __k)
00492       {
00493     // concept requirements
00494     __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
00495 
00496     iterator __i = lower_bound(__k);
00497     // __i->first is greater than or equivalent to __k.
00498     if (__i == end() || key_comp()(__k, (*__i).first))
00499 #if __cplusplus >= 201103L
00500       __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
00501                         std::tuple<const key_type&>(__k),
00502                         std::tuple<>());
00503 #else
00504           __i = insert(__i, value_type(__k, mapped_type()));
00505 #endif
00506     return (*__i).second;
00507       }
00508 
00509 #if __cplusplus >= 201103L
00510       mapped_type&
00511       operator[](key_type&& __k)
00512       {
00513     // concept requirements
00514     __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
00515 
00516     iterator __i = lower_bound(__k);
00517     // __i->first is greater than or equivalent to __k.
00518     if (__i == end() || key_comp()(__k, (*__i).first))
00519       __i = _M_t._M_emplace_hint_unique(__i, std::piecewise_construct,
00520                     std::forward_as_tuple(std::move(__k)),
00521                     std::tuple<>());
00522     return (*__i).second;
00523       }
00524 #endif
00525 
00526       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00527       // DR 464. Suggestion for new member functions in standard containers.
00528       /**
00529        *  @brief  Access to %map data.
00530        *  @param  __k  The key for which data should be retrieved.
00531        *  @return  A reference to the data whose key is equivalent to @a __k, if
00532        *           such a data is present in the %map.
00533        *  @throw  std::out_of_range  If no such data is present.
00534        */
00535       mapped_type&
00536       at(const key_type& __k)
00537       {
00538     iterator __i = lower_bound(__k);
00539     if (__i == end() || key_comp()(__k, (*__i).first))
00540       __throw_out_of_range(__N("map::at"));
00541     return (*__i).second;
00542       }
00543 
00544       const mapped_type&
00545       at(const key_type& __k) const
00546       {
00547     const_iterator __i = lower_bound(__k);
00548     if (__i == end() || key_comp()(__k, (*__i).first))
00549       __throw_out_of_range(__N("map::at"));
00550     return (*__i).second;
00551       }
00552 
00553       // modifiers
00554 #if __cplusplus >= 201103L
00555       /**
00556        *  @brief Attempts to build and insert a std::pair into the %map.
00557        *
00558        *  @param __args  Arguments used to generate a new pair instance (see
00559        *            std::piecewise_contruct for passing arguments to each
00560        *            part of the pair constructor).
00561        *
00562        *  @return  A pair, of which the first element is an iterator that points
00563        *           to the possibly inserted pair, and the second is a bool that
00564        *           is true if the pair was actually inserted.
00565        *
00566        *  This function attempts to build and insert a (key, value) %pair into
00567        *  the %map.
00568        *  A %map relies on unique keys and thus a %pair is only inserted if its
00569        *  first element (the key) is not already present in the %map.
00570        *
00571        *  Insertion requires logarithmic time.
00572        */
00573       template<typename... _Args>
00574     std::pair<iterator, bool>
00575     emplace(_Args&&... __args)
00576     { return _M_t._M_emplace_unique(std::forward<_Args>(__args)...); }
00577 
00578       /**
00579        *  @brief Attempts to build and insert a std::pair into the %map.
00580        *
00581        *  @param  __pos  An iterator that serves as a hint as to where the pair
00582        *                should be inserted.
00583        *  @param  __args  Arguments used to generate a new pair instance (see
00584        *             std::piecewise_contruct for passing arguments to each
00585        *             part of the pair constructor).
00586        *  @return An iterator that points to the element with key of the
00587        *          std::pair built from @a __args (may or may not be that
00588        *          std::pair).
00589        *
00590        *  This function is not concerned about whether the insertion took place,
00591        *  and thus does not return a boolean like the single-argument emplace()
00592        *  does.
00593        *  Note that the first parameter is only a hint and can potentially
00594        *  improve the performance of the insertion process. A bad hint would
00595        *  cause no gains in efficiency.
00596        *
00597        *  See
00598        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
00599        *  for more on @a hinting.
00600        *
00601        *  Insertion requires logarithmic time (if the hint is not taken).
00602        */
00603       template<typename... _Args>
00604     iterator
00605     emplace_hint(const_iterator __pos, _Args&&... __args)
00606     {
00607       return _M_t._M_emplace_hint_unique(__pos,
00608                          std::forward<_Args>(__args)...);
00609     }
00610 #endif
00611 
00612       /**
00613        *  @brief Attempts to insert a std::pair into the %map.
00614 
00615        *  @param __x Pair to be inserted (see std::make_pair for easy
00616        *         creation of pairs).
00617        *
00618        *  @return  A pair, of which the first element is an iterator that 
00619        *           points to the possibly inserted pair, and the second is 
00620        *           a bool that is true if the pair was actually inserted.
00621        *
00622        *  This function attempts to insert a (key, value) %pair into the %map.
00623        *  A %map relies on unique keys and thus a %pair is only inserted if its
00624        *  first element (the key) is not already present in the %map.
00625        *
00626        *  Insertion requires logarithmic time.
00627        */
00628       std::pair<iterator, bool>
00629       insert(const value_type& __x)
00630       { return _M_t._M_insert_unique(__x); }
00631 
00632 #if __cplusplus >= 201103L
00633       template<typename _Pair, typename = typename
00634            std::enable_if<std::is_constructible<value_type,
00635                             _Pair&&>::value>::type>
00636         std::pair<iterator, bool>
00637         insert(_Pair&& __x)
00638         { return _M_t._M_insert_unique(std::forward<_Pair>(__x)); }
00639 #endif
00640 
00641 #if __cplusplus >= 201103L
00642       /**
00643        *  @brief Attempts to insert a list of std::pairs into the %map.
00644        *  @param  __list  A std::initializer_list<value_type> of pairs to be
00645        *                  inserted.
00646        *
00647        *  Complexity similar to that of the range constructor.
00648        */
00649       void
00650       insert(std::initializer_list<value_type> __list)
00651       { insert(__list.begin(), __list.end()); }
00652 #endif
00653 
00654       /**
00655        *  @brief Attempts to insert a std::pair into the %map.
00656        *  @param  __position  An iterator that serves as a hint as to where the
00657        *                    pair should be inserted.
00658        *  @param  __x  Pair to be inserted (see std::make_pair for easy creation
00659        *               of pairs).
00660        *  @return An iterator that points to the element with key of
00661        *           @a __x (may or may not be the %pair passed in).
00662        *
00663 
00664        *  This function is not concerned about whether the insertion
00665        *  took place, and thus does not return a boolean like the
00666        *  single-argument insert() does.  Note that the first
00667        *  parameter is only a hint and can potentially improve the
00668        *  performance of the insertion process.  A bad hint would
00669        *  cause no gains in efficiency.
00670        *
00671        *  See
00672        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt07ch17.html
00673        *  for more on @a hinting.
00674        *
00675        *  Insertion requires logarithmic time (if the hint is not taken).
00676        */
00677       iterator
00678 #if __cplusplus >= 201103L
00679       insert(const_iterator __position, const value_type& __x)
00680 #else
00681       insert(iterator __position, const value_type& __x)
00682 #endif
00683       { return _M_t._M_insert_unique_(__position, __x); }
00684 
00685 #if __cplusplus >= 201103L
00686       template<typename _Pair, typename = typename
00687            std::enable_if<std::is_constructible<value_type,
00688                             _Pair&&>::value>::type>
00689         iterator
00690         insert(const_iterator __position, _Pair&& __x)
00691         { return _M_t._M_insert_unique_(__position,
00692                     std::forward<_Pair>(__x)); }
00693 #endif
00694 
00695       /**
00696        *  @brief Template function that attempts to insert a range of elements.
00697        *  @param  __first  Iterator pointing to the start of the range to be
00698        *                   inserted.
00699        *  @param  __last  Iterator pointing to the end of the range.
00700        *
00701        *  Complexity similar to that of the range constructor.
00702        */
00703       template<typename _InputIterator>
00704         void
00705         insert(_InputIterator __first, _InputIterator __last)
00706         { _M_t._M_insert_unique(__first, __last); }
00707 
00708 #if __cplusplus >= 201103L
00709       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00710       // DR 130. Associative erase should return an iterator.
00711       /**
00712        *  @brief Erases an element from a %map.
00713        *  @param  __position  An iterator pointing to the element to be erased.
00714        *  @return An iterator pointing to the element immediately following
00715        *          @a position prior to the element being erased. If no such 
00716        *          element exists, end() is returned.
00717        *
00718        *  This function erases an element, pointed to by the given
00719        *  iterator, from a %map.  Note that this function only erases
00720        *  the element, and that if the element is itself a pointer,
00721        *  the pointed-to memory is not touched in any way.  Managing
00722        *  the pointer is the user's responsibility.
00723        */
00724       iterator
00725       erase(const_iterator __position)
00726       { return _M_t.erase(__position); }
00727 
00728       // LWG 2059
00729       _GLIBCXX_ABI_TAG_CXX11
00730       iterator
00731       erase(iterator __position)
00732       { return _M_t.erase(__position); }
00733 #else
00734       /**
00735        *  @brief Erases an element from a %map.
00736        *  @param  __position  An iterator pointing to the element to be erased.
00737        *
00738        *  This function erases an element, pointed to by the given
00739        *  iterator, from a %map.  Note that this function only erases
00740        *  the element, and that if the element is itself a pointer,
00741        *  the pointed-to memory is not touched in any way.  Managing
00742        *  the pointer is the user's responsibility.
00743        */
00744       void
00745       erase(iterator __position)
00746       { _M_t.erase(__position); }
00747 #endif
00748 
00749       /**
00750        *  @brief Erases elements according to the provided key.
00751        *  @param  __x  Key of element to be erased.
00752        *  @return  The number of elements erased.
00753        *
00754        *  This function erases all the elements located by the given key from
00755        *  a %map.
00756        *  Note that this function only erases the element, and that if
00757        *  the element is itself a pointer, the pointed-to memory is not touched
00758        *  in any way.  Managing the pointer is the user's responsibility.
00759        */
00760       size_type
00761       erase(const key_type& __x)
00762       { return _M_t.erase(__x); }
00763 
00764 #if __cplusplus >= 201103L
00765       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00766       // DR 130. Associative erase should return an iterator.
00767       /**
00768        *  @brief Erases a [first,last) range of elements from a %map.
00769        *  @param  __first  Iterator pointing to the start of the range to be
00770        *                   erased.
00771        *  @param __last Iterator pointing to the end of the range to
00772        *                be erased.
00773        *  @return The iterator @a __last.
00774        *
00775        *  This function erases a sequence of elements from a %map.
00776        *  Note that this function only erases the element, and that if
00777        *  the element is itself a pointer, the pointed-to memory is not touched
00778        *  in any way.  Managing the pointer is the user's responsibility.
00779        */
00780       iterator
00781       erase(const_iterator __first, const_iterator __last)
00782       { return _M_t.erase(__first, __last); }
00783 #else
00784       /**
00785        *  @brief Erases a [__first,__last) range of elements from a %map.
00786        *  @param  __first  Iterator pointing to the start of the range to be
00787        *                   erased.
00788        *  @param __last Iterator pointing to the end of the range to
00789        *                be erased.
00790        *
00791        *  This function erases a sequence of elements from a %map.
00792        *  Note that this function only erases the element, and that if
00793        *  the element is itself a pointer, the pointed-to memory is not touched
00794        *  in any way.  Managing the pointer is the user's responsibility.
00795        */
00796       void
00797       erase(iterator __first, iterator __last)
00798       { _M_t.erase(__first, __last); }
00799 #endif
00800 
00801       /**
00802        *  @brief  Swaps data with another %map.
00803        *  @param  __x  A %map of the same element and allocator types.
00804        *
00805        *  This exchanges the elements between two maps in constant
00806        *  time.  (It is only swapping a pointer, an integer, and an
00807        *  instance of the @c Compare type (which itself is often
00808        *  stateless and empty), so it should be quite fast.)  Note
00809        *  that the global std::swap() function is specialized such
00810        *  that std::swap(m1,m2) will feed to this function.
00811        */
00812       void
00813       swap(map& __x)
00814 #if __cplusplus >= 201103L
00815       noexcept(_Alloc_traits::_S_nothrow_swap())
00816 #endif
00817       { _M_t.swap(__x._M_t); }
00818 
00819       /**
00820        *  Erases all elements in a %map.  Note that this function only
00821        *  erases the elements, and that if the elements themselves are
00822        *  pointers, the pointed-to memory is not touched in any way.
00823        *  Managing the pointer is the user's responsibility.
00824        */
00825       void
00826       clear() _GLIBCXX_NOEXCEPT
00827       { _M_t.clear(); }
00828 
00829       // observers
00830       /**
00831        *  Returns the key comparison object out of which the %map was
00832        *  constructed.
00833        */
00834       key_compare
00835       key_comp() const
00836       { return _M_t.key_comp(); }
00837 
00838       /**
00839        *  Returns a value comparison object, built from the key comparison
00840        *  object out of which the %map was constructed.
00841        */
00842       value_compare
00843       value_comp() const
00844       { return value_compare(_M_t.key_comp()); }
00845 
00846       // [23.3.1.3] map operations
00847       /**
00848        *  @brief Tries to locate an element in a %map.
00849        *  @param  __x  Key of (key, value) %pair to be located.
00850        *  @return  Iterator pointing to sought-after element, or end() if not
00851        *           found.
00852        *
00853        *  This function takes a key and tries to locate the element with which
00854        *  the key matches.  If successful the function returns an iterator
00855        *  pointing to the sought after %pair.  If unsuccessful it returns the
00856        *  past-the-end ( @c end() ) iterator.
00857        */
00858       iterator
00859       find(const key_type& __x)
00860       { return _M_t.find(__x); }
00861 
00862       /**
00863        *  @brief Tries to locate an element in a %map.
00864        *  @param  __x  Key of (key, value) %pair to be located.
00865        *  @return  Read-only (constant) iterator pointing to sought-after
00866        *           element, or end() if not found.
00867        *
00868        *  This function takes a key and tries to locate the element with which
00869        *  the key matches.  If successful the function returns a constant
00870        *  iterator pointing to the sought after %pair. If unsuccessful it
00871        *  returns the past-the-end ( @c end() ) iterator.
00872        */
00873       const_iterator
00874       find(const key_type& __x) const
00875       { return _M_t.find(__x); }
00876 
00877       /**
00878        *  @brief  Finds the number of elements with given key.
00879        *  @param  __x  Key of (key, value) pairs to be located.
00880        *  @return  Number of elements with specified key.
00881        *
00882        *  This function only makes sense for multimaps; for map the result will
00883        *  either be 0 (not present) or 1 (present).
00884        */
00885       size_type
00886       count(const key_type& __x) const
00887       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00888 
00889       /**
00890        *  @brief Finds the beginning of a subsequence matching given key.
00891        *  @param  __x  Key of (key, value) pair to be located.
00892        *  @return  Iterator pointing to first element equal to or greater
00893        *           than key, or end().
00894        *
00895        *  This function returns the first element of a subsequence of elements
00896        *  that matches the given key.  If unsuccessful it returns an iterator
00897        *  pointing to the first element that has a greater value than given key
00898        *  or end() if no such element exists.
00899        */
00900       iterator
00901       lower_bound(const key_type& __x)
00902       { return _M_t.lower_bound(__x); }
00903 
00904       /**
00905        *  @brief Finds the beginning of a subsequence matching given key.
00906        *  @param  __x  Key of (key, value) pair to be located.
00907        *  @return  Read-only (constant) iterator pointing to first element
00908        *           equal to or greater than key, or end().
00909        *
00910        *  This function returns the first element of a subsequence of elements
00911        *  that matches the given key.  If unsuccessful it returns an iterator
00912        *  pointing to the first element that has a greater value than given key
00913        *  or end() if no such element exists.
00914        */
00915       const_iterator
00916       lower_bound(const key_type& __x) const
00917       { return _M_t.lower_bound(__x); }
00918 
00919       /**
00920        *  @brief Finds the end of a subsequence matching given key.
00921        *  @param  __x  Key of (key, value) pair to be located.
00922        *  @return Iterator pointing to the first element
00923        *          greater than key, or end().
00924        */
00925       iterator
00926       upper_bound(const key_type& __x)
00927       { return _M_t.upper_bound(__x); }
00928 
00929       /**
00930        *  @brief Finds the end of a subsequence matching given key.
00931        *  @param  __x  Key of (key, value) pair to be located.
00932        *  @return  Read-only (constant) iterator pointing to first iterator
00933        *           greater than key, or end().
00934        */
00935       const_iterator
00936       upper_bound(const key_type& __x) const
00937       { return _M_t.upper_bound(__x); }
00938 
00939       /**
00940        *  @brief Finds a subsequence matching given key.
00941        *  @param  __x  Key of (key, value) pairs to be located.
00942        *  @return  Pair of iterators that possibly points to the subsequence
00943        *           matching given key.
00944        *
00945        *  This function is equivalent to
00946        *  @code
00947        *    std::make_pair(c.lower_bound(val),
00948        *                   c.upper_bound(val))
00949        *  @endcode
00950        *  (but is faster than making the calls separately).
00951        *
00952        *  This function probably only makes sense for multimaps.
00953        */
00954       std::pair<iterator, iterator>
00955       equal_range(const key_type& __x)
00956       { return _M_t.equal_range(__x); }
00957 
00958       /**
00959        *  @brief Finds a subsequence matching given key.
00960        *  @param  __x  Key of (key, value) pairs to be located.
00961        *  @return  Pair of read-only (constant) iterators that possibly points
00962        *           to the subsequence matching given key.
00963        *
00964        *  This function is equivalent to
00965        *  @code
00966        *    std::make_pair(c.lower_bound(val),
00967        *                   c.upper_bound(val))
00968        *  @endcode
00969        *  (but is faster than making the calls separately).
00970        *
00971        *  This function probably only makes sense for multimaps.
00972        */
00973       std::pair<const_iterator, const_iterator>
00974       equal_range(const key_type& __x) const
00975       { return _M_t.equal_range(__x); }
00976 
00977       template<typename _K1, typename _T1, typename _C1, typename _A1>
00978         friend bool
00979         operator==(const map<_K1, _T1, _C1, _A1>&,
00980            const map<_K1, _T1, _C1, _A1>&);
00981 
00982       template<typename _K1, typename _T1, typename _C1, typename _A1>
00983         friend bool
00984         operator<(const map<_K1, _T1, _C1, _A1>&,
00985           const map<_K1, _T1, _C1, _A1>&);
00986     };
00987 
00988   /**
00989    *  @brief  Map equality comparison.
00990    *  @param  __x  A %map.
00991    *  @param  __y  A %map of the same type as @a x.
00992    *  @return  True iff the size and elements of the maps are equal.
00993    *
00994    *  This is an equivalence relation.  It is linear in the size of the
00995    *  maps.  Maps are considered equivalent if their sizes are equal,
00996    *  and if corresponding elements compare equal.
00997   */
00998   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00999     inline bool
01000     operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01001                const map<_Key, _Tp, _Compare, _Alloc>& __y)
01002     { return __x._M_t == __y._M_t; }
01003 
01004   /**
01005    *  @brief  Map ordering relation.
01006    *  @param  __x  A %map.
01007    *  @param  __y  A %map of the same type as @a x.
01008    *  @return  True iff @a x is lexicographically less than @a y.
01009    *
01010    *  This is a total ordering relation.  It is linear in the size of the
01011    *  maps.  The elements must be comparable with @c <.
01012    *
01013    *  See std::lexicographical_compare() for how the determination is made.
01014   */
01015   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01016     inline bool
01017     operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01018               const map<_Key, _Tp, _Compare, _Alloc>& __y)
01019     { return __x._M_t < __y._M_t; }
01020 
01021   /// Based on operator==
01022   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01023     inline bool
01024     operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01025                const map<_Key, _Tp, _Compare, _Alloc>& __y)
01026     { return !(__x == __y); }
01027 
01028   /// Based on operator<
01029   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01030     inline bool
01031     operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01032               const map<_Key, _Tp, _Compare, _Alloc>& __y)
01033     { return __y < __x; }
01034 
01035   /// Based on operator<
01036   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01037     inline bool
01038     operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01039                const map<_Key, _Tp, _Compare, _Alloc>& __y)
01040     { return !(__y < __x); }
01041 
01042   /// Based on operator<
01043   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01044     inline bool
01045     operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
01046                const map<_Key, _Tp, _Compare, _Alloc>& __y)
01047     { return !(__x < __y); }
01048 
01049   /// See std::map::swap().
01050   template<typename _Key, typename _Tp, typename _Compare, typename _Alloc>
01051     inline void
01052     swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
01053      map<_Key, _Tp, _Compare, _Alloc>& __y)
01054     { __x.swap(__y); }
01055 
01056 _GLIBCXX_END_NAMESPACE_CONTAINER
01057 } // namespace std
01058 
01059 #endif /* _STL_MAP_H */