stl_map.h

Go to the documentation of this file.
00001 // Map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004, 2005 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /*
00031  *
00032  * Copyright (c) 1994
00033  * Hewlett-Packard Company
00034  *
00035  * Permission to use, copy, modify, distribute and sell this software
00036  * and its documentation for any purpose is hereby granted without fee,
00037  * provided that the above copyright notice appear in all copies and
00038  * that both that copyright notice and this permission notice appear
00039  * in supporting documentation.  Hewlett-Packard Company makes no
00040  * representations about the suitability of this software for any
00041  * purpose.  It is provided "as is" without express or implied warranty.
00042  *
00043  *
00044  * Copyright (c) 1996,1997
00045  * Silicon Graphics Computer Systems, Inc.
00046  *
00047  * Permission to use, copy, modify, distribute and sell this software
00048  * and its documentation for any purpose is hereby granted without fee,
00049  * provided that the above copyright notice appear in all copies and
00050  * that both that copyright notice and this permission notice appear
00051  * in supporting documentation.  Silicon Graphics makes no
00052  * representations about the suitability of this software for any
00053  * purpose.  It is provided "as is" without express or implied warranty.
00054  */
00055 
00061 #ifndef _MAP_H
00062 #define _MAP_H 1
00063 
00064 #include <bits/functexcept.h>
00065 #include <bits/concept_check.h>
00066 
00067 namespace _GLIBCXX_STD
00068 {
00090   template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
00091             typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
00092     class map
00093     {
00094     public:
00095       typedef _Key                                          key_type;
00096       typedef _Tp                                           mapped_type;
00097       typedef std::pair<const _Key, _Tp>                    value_type;
00098       typedef _Compare                                      key_compare;
00099       typedef _Alloc                                        allocator_type;
00100 
00101     private:
00102       // concept requirements
00103       typedef typename _Alloc::value_type                   _Alloc_value_type;
00104       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00105       __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
00106                 _BinaryFunctionConcept)
00107       __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
00108 
00109     public:
00110       class value_compare
00111       : public std::binary_function<value_type, value_type, bool>
00112       {
00113     friend class map<_Key, _Tp, _Compare, _Alloc>;
00114       protected:
00115     _Compare comp;
00116 
00117     value_compare(_Compare __c)
00118     : comp(__c) { }
00119 
00120       public:
00121     bool operator()(const value_type& __x, const value_type& __y) const
00122     { return comp(__x.first, __y.first); }
00123       };
00124 
00125     private:
00127       typedef typename _Alloc::template rebind<value_type>::other 
00128         _Pair_alloc_type;
00129 
00130       typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
00131                key_compare, _Pair_alloc_type> _Rep_type;
00132 
00134       _Rep_type _M_t;
00135 
00136     public:
00137       // many of these are specified differently in ISO, but the following are
00138       // "functionally equivalent"
00139       typedef typename _Pair_alloc_type::pointer         pointer;
00140       typedef typename _Pair_alloc_type::const_pointer   const_pointer;
00141       typedef typename _Pair_alloc_type::reference       reference;
00142       typedef typename _Pair_alloc_type::const_reference const_reference;
00143       typedef typename _Rep_type::iterator               iterator;
00144       typedef typename _Rep_type::const_iterator         const_iterator;
00145       typedef typename _Rep_type::size_type              size_type;
00146       typedef typename _Rep_type::difference_type        difference_type;
00147       typedef typename _Rep_type::reverse_iterator       reverse_iterator;
00148       typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00149 
00150       // [23.3.1.1] construct/copy/destroy
00151       // (get_allocator() is normally listed in this section, but seems to have
00152       // been accidentally omitted in the printed standard)
00156       map()
00157       : _M_t(_Compare(), allocator_type()) { }
00158 
00159       // for some reason this was made a separate function
00163       explicit
00164       map(const _Compare& __comp, const allocator_type& __a = allocator_type())
00165       : _M_t(__comp, __a) { }
00166 
00174       map(const map& __x)
00175       : _M_t(__x._M_t) { }
00176 
00186       template <typename _InputIterator>
00187         map(_InputIterator __first, _InputIterator __last)
00188     : _M_t(_Compare(), allocator_type())
00189         { _M_t.insert_unique(__first, __last); }
00190 
00202       template <typename _InputIterator>
00203         map(_InputIterator __first, _InputIterator __last,
00204         const _Compare& __comp, const allocator_type& __a = allocator_type())
00205     : _M_t(__comp, __a)
00206         { _M_t.insert_unique(__first, __last); }
00207 
00208       // FIXME There is no dtor declared, but we should have something generated
00209       // by Doxygen.  I don't know what tags to add to this paragraph to make
00210       // that happen:
00224       map&
00225       operator=(const map& __x)
00226       {
00227     _M_t = __x._M_t;
00228     return *this;
00229       }
00230 
00232       allocator_type
00233       get_allocator() const
00234       { return _M_t.get_allocator(); }
00235 
00236       // iterators
00242       iterator
00243       begin()
00244       { return _M_t.begin(); }
00245 
00251       const_iterator
00252       begin() const
00253       { return _M_t.begin(); }
00254 
00259       iterator
00260       end()
00261       { return _M_t.end(); }
00262 
00268       const_iterator
00269       end() const
00270       { return _M_t.end(); }
00271 
00277       reverse_iterator
00278       rbegin()
00279       { return _M_t.rbegin(); }
00280 
00286       const_reverse_iterator
00287       rbegin() const
00288       { return _M_t.rbegin(); }
00289 
00295       reverse_iterator
00296       rend()
00297       { return _M_t.rend(); }
00298 
00304       const_reverse_iterator
00305       rend() const
00306       { return _M_t.rend(); }
00307 
00308       // capacity
00312       bool
00313       empty() const
00314       { return _M_t.empty(); }
00315 
00317       size_type
00318       size() const
00319       { return _M_t.size(); }
00320 
00322       size_type
00323       max_size() const
00324       { return _M_t.max_size(); }
00325 
00326       // [23.3.1.2] element access
00339       mapped_type&
00340       operator[](const key_type& __k)
00341       {
00342     // concept requirements
00343     __glibcxx_function_requires(_DefaultConstructibleConcept<mapped_type>)
00344 
00345     iterator __i = lower_bound(__k);
00346     // __i->first is greater than or equivalent to __k.
00347     if (__i == end() || key_comp()(__k, (*__i).first))
00348           __i = insert(__i, value_type(__k, mapped_type()));
00349     return (*__i).second;
00350       }
00351 
00352       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00353       // DR 464. Suggestion for new member functions in standard containers.
00361       mapped_type&
00362       at(const key_type& __k)
00363       {
00364     iterator __i = lower_bound(__k);
00365     if (__i == end() || key_comp()(__k, (*__i).first))
00366       __throw_out_of_range(__N("map::at"));
00367     return (*__i).second;
00368       }
00369 
00370       const mapped_type&
00371       at(const key_type& __k) const
00372       {
00373     const_iterator __i = lower_bound(__k);
00374     if (__i == end() || key_comp()(__k, (*__i).first))
00375       __throw_out_of_range(__N("map::at"));
00376     return (*__i).second;
00377       }
00378 
00379       // modifiers
00394       std::pair<iterator,bool>
00395       insert(const value_type& __x)
00396       { return _M_t.insert_unique(__x); }
00397 
00418       iterator
00419       insert(iterator position, const value_type& __x)
00420       { return _M_t.insert_unique(position, __x); }
00421 
00430       template <typename _InputIterator>
00431         void
00432         insert(_InputIterator __first, _InputIterator __last)
00433         { _M_t.insert_unique(__first, __last); }
00434 
00444       void
00445       erase(iterator __position)
00446       { _M_t.erase(__position); }
00447 
00459       size_type
00460       erase(const key_type& __x)
00461       { return _M_t.erase(__x); }
00462 
00474       void
00475       erase(iterator __first, iterator __last)
00476       { _M_t.erase(__first, __last); }
00477 
00489       void
00490       swap(map& __x)
00491       { _M_t.swap(__x._M_t); }
00492 
00499       void
00500       clear()
00501       { _M_t.clear(); }
00502 
00503       // observers
00508       key_compare
00509       key_comp() const
00510       { return _M_t.key_comp(); }
00511 
00516       value_compare
00517       value_comp() const
00518       { return value_compare(_M_t.key_comp()); }
00519 
00520       // [23.3.1.3] map operations
00532       iterator
00533       find(const key_type& __x)
00534       { return _M_t.find(__x); }
00535 
00547       const_iterator
00548       find(const key_type& __x) const
00549       { return _M_t.find(__x); }
00550 
00559       size_type
00560       count(const key_type& __x) const
00561       { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00562 
00574       iterator
00575       lower_bound(const key_type& __x)
00576       { return _M_t.lower_bound(__x); }
00577 
00589       const_iterator
00590       lower_bound(const key_type& __x) const
00591       { return _M_t.lower_bound(__x); }
00592 
00599       iterator
00600       upper_bound(const key_type& __x)
00601       { return _M_t.upper_bound(__x); }
00602 
00609       const_iterator
00610       upper_bound(const key_type& __x) const
00611       { return _M_t.upper_bound(__x); }
00612 
00628       std::pair<iterator, iterator>
00629       equal_range(const key_type& __x)
00630       { return _M_t.equal_range(__x); }
00631 
00647       std::pair<const_iterator, const_iterator>
00648       equal_range(const key_type& __x) const
00649       { return _M_t.equal_range(__x); }
00650 
00651       template <typename _K1, typename _T1, typename _C1, typename _A1>
00652         friend bool
00653         operator== (const map<_K1, _T1, _C1, _A1>&,
00654             const map<_K1, _T1, _C1, _A1>&);
00655 
00656       template <typename _K1, typename _T1, typename _C1, typename _A1>
00657         friend bool
00658         operator< (const map<_K1, _T1, _C1, _A1>&,
00659            const map<_K1, _T1, _C1, _A1>&);
00660     };
00661 
00672   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00673     inline bool
00674     operator==(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00675                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00676     { return __x._M_t == __y._M_t; }
00677 
00689   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00690     inline bool
00691     operator<(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00692               const map<_Key, _Tp, _Compare, _Alloc>& __y)
00693     { return __x._M_t < __y._M_t; }
00694 
00696   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00697     inline bool
00698     operator!=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00699                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00700     { return !(__x == __y); }
00701 
00703   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00704     inline bool
00705     operator>(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00706               const map<_Key, _Tp, _Compare, _Alloc>& __y)
00707     { return __y < __x; }
00708 
00710   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00711     inline bool
00712     operator<=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00713                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00714     { return !(__y < __x); }
00715 
00717   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00718     inline bool
00719     operator>=(const map<_Key, _Tp, _Compare, _Alloc>& __x,
00720                const map<_Key, _Tp, _Compare, _Alloc>& __y)
00721     { return !(__x < __y); }
00722 
00724   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00725     inline void
00726     swap(map<_Key, _Tp, _Compare, _Alloc>& __x,
00727      map<_Key, _Tp, _Compare, _Alloc>& __y)
00728     { __x.swap(__y); }
00729 } // namespace std
00730 
00731 #endif /* _MAP_H */

Generated on Tue Feb 2 16:56:39 2010 for GNU C++ STL by  doxygen 1.4.7