stl_map.h

Go to the documentation of this file.
00001 // Map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 __GLIBCPP_INTERNAL_MAP_H
00062 #define __GLIBCPP_INTERNAL_MAP_H
00063 
00064 #include <bits/concept_check.h>
00065 
00066 namespace std
00067 {
00089   template <typename _Key, typename _Tp, typename _Compare = less<_Key>,
00090             typename _Alloc = allocator<pair<const _Key, _Tp> > >
00091     class map
00092   {
00093     // concept requirements
00094     __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
00095     __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept)
00096   
00097   public:
00098     typedef _Key                                          key_type;
00099     typedef _Tp                                           mapped_type;
00100     typedef pair<const _Key, _Tp>                         value_type;
00101     typedef _Compare                                      key_compare;
00102   
00103     class value_compare
00104       : public binary_function<value_type, value_type, bool>
00105       {
00106         friend class map<_Key,_Tp,_Compare,_Alloc>;
00107       protected:
00108         _Compare comp;
00109         value_compare(_Compare __c) : comp(__c) {}
00110       public:
00111         bool operator()(const value_type& __x, const value_type& __y) const
00112         { return comp(__x.first, __y.first); }
00113       };
00114   
00115   private:
00117     typedef _Rb_tree<key_type, value_type,
00118                      _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
00120     _Rep_type _M_t;
00121   
00122   public:
00123     // many of these are specified differently in ISO, but the following are
00124     // "functionally equivalent"
00125     typedef typename _Rep_type::allocator_type            allocator_type;
00126     typedef typename _Rep_type::reference                 reference;
00127     typedef typename _Rep_type::const_reference           const_reference;
00128     typedef typename _Rep_type::iterator                  iterator;
00129     typedef typename _Rep_type::const_iterator            const_iterator;
00130     typedef typename _Rep_type::size_type                 size_type;
00131     typedef typename _Rep_type::difference_type           difference_type;
00132     typedef typename _Rep_type::pointer                   pointer;
00133     typedef typename _Rep_type::const_pointer             const_pointer;
00134     typedef typename _Rep_type::reverse_iterator          reverse_iterator;
00135     typedef typename _Rep_type::const_reverse_iterator    const_reverse_iterator;
00136   
00137   
00138     // [23.3.1.1] construct/copy/destroy
00139     // (get_allocator() is normally listed in this section, but seems to have
00140     // been accidentally omitted in the printed standard)
00144     map() : _M_t(_Compare(), allocator_type()) { }
00145   
00146     // for some reason this was made a separate function
00150     explicit
00151     map(const _Compare& __comp, const allocator_type& __a = allocator_type())
00152       : _M_t(__comp, __a) { }
00153   
00161     map(const map& __x)
00162       : _M_t(__x._M_t) { }
00163   
00173     template <typename _InputIterator>
00174       map(_InputIterator __first, _InputIterator __last)
00175       : _M_t(_Compare(), allocator_type())
00176       { _M_t.insert_unique(__first, __last); }
00177   
00189     template <typename _InputIterator>
00190       map(_InputIterator __first, _InputIterator __last,
00191           const _Compare& __comp, const allocator_type& __a = allocator_type())
00192       : _M_t(__comp, __a)
00193       { _M_t.insert_unique(__first, __last); }
00194   
00195     // FIXME There is no dtor declared, but we should have something generated
00196     // by Doxygen.  I don't know what tags to add to this paragraph to make
00197     // that happen:
00211     map&
00212     operator=(const map& __x)
00213     {
00214       _M_t = __x._M_t;
00215       return *this;
00216     }
00217   
00219     allocator_type
00220     get_allocator() const { return _M_t.get_allocator(); }
00221   
00222     // iterators
00227     iterator
00228     begin() { return _M_t.begin(); }
00229   
00235     const_iterator
00236     begin() const { return _M_t.begin(); }
00237   
00242     iterator
00243     end() { return _M_t.end(); }
00244   
00250     const_iterator
00251     end() const { return _M_t.end(); }
00252   
00257     reverse_iterator
00258     rbegin() { return _M_t.rbegin(); }
00259   
00265     const_reverse_iterator
00266     rbegin() const { return _M_t.rbegin(); }
00267   
00273     reverse_iterator
00274     rend() { return _M_t.rend(); }
00275   
00281     const_reverse_iterator
00282     rend() const { return _M_t.rend(); }
00283   
00284     // capacity
00286     bool
00287     empty() const { return _M_t.empty(); }
00288   
00290     size_type
00291     size() const { return _M_t.size(); }
00292   
00294     size_type
00295     max_size() const { return _M_t.max_size(); }
00296   
00297     // [23.3.1.2] element access
00310     mapped_type&
00311     operator[](const key_type& __k)
00312     {
00313       // concept requirements
00314       __glibcpp_function_requires(_DefaultConstructibleConcept<mapped_type>)
00315   
00316       iterator __i = lower_bound(__k);
00317       // __i->first is greater than or equivalent to __k.
00318       if (__i == end() || key_comp()(__k, (*__i).first))
00319         __i = insert(__i, value_type(__k, mapped_type()));
00320       return (*__i).second;
00321     }
00322   
00323     // modifiers
00338     pair<iterator,bool>
00339     insert(const value_type& __x)
00340     { return _M_t.insert_unique(__x); }
00341   
00362     iterator
00363     insert(iterator position, const value_type& __x)
00364     { return _M_t.insert_unique(position, __x); }
00365   
00374     template <typename _InputIterator>
00375       void
00376       insert(_InputIterator __first, _InputIterator __last)
00377       { _M_t.insert_unique(__first, __last); }
00378   
00388     void
00389     erase(iterator __position) { _M_t.erase(__position); }
00390   
00402     size_type
00403     erase(const key_type& __x) { return _M_t.erase(__x); }
00404   
00415     void
00416     erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
00417   
00429     void
00430     swap(map& __x) { _M_t.swap(__x._M_t); }
00431   
00438     void
00439     clear() { _M_t.clear(); }
00440   
00441     // observers
00445     key_compare
00446     key_comp() const { return _M_t.key_comp(); }
00447   
00452     value_compare
00453     value_comp() const { return value_compare(_M_t.key_comp()); }
00454   
00455     // [23.3.1.3] map operations
00467     iterator
00468     find(const key_type& __x) { return _M_t.find(__x); }
00469   
00481     const_iterator
00482     find(const key_type& __x) const { return _M_t.find(__x); }
00483   
00492     size_type
00493     count(const key_type& __x) const
00494     { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00495   
00507     iterator
00508     lower_bound(const key_type& __x) { return _M_t.lower_bound(__x); }
00509   
00521     const_iterator
00522     lower_bound(const key_type& __x) const { return _M_t.lower_bound(__x); }
00523   
00531     iterator
00532     upper_bound(const key_type& __x) { return _M_t.upper_bound(__x); }
00533   
00542     const_iterator
00543     upper_bound(const key_type& __x) const
00544     { return _M_t.upper_bound(__x); }
00545   
00561     pair<iterator,iterator>
00562     equal_range(const key_type& __x)
00563     { return _M_t.equal_range(__x); }
00564   
00580     pair<const_iterator,const_iterator>
00581     equal_range(const key_type& __x) const
00582     { return _M_t.equal_range(__x); }
00583   
00584     template <typename _K1, typename _T1, typename _C1, typename _A1>
00585     friend bool operator== (const map<_K1,_T1,_C1,_A1>&,
00586                             const map<_K1,_T1,_C1,_A1>&);
00587     template <typename _K1, typename _T1, typename _C1, typename _A1>
00588     friend bool operator< (const map<_K1,_T1,_C1,_A1>&,
00589                            const map<_K1,_T1,_C1,_A1>&);
00590   };
00591   
00592   
00603   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00604     inline bool
00605     operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00606                const map<_Key,_Tp,_Compare,_Alloc>& __y)
00607     { return __x._M_t == __y._M_t; }
00608   
00620   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00621     inline bool
00622     operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00623               const map<_Key,_Tp,_Compare,_Alloc>& __y)
00624     { return __x._M_t < __y._M_t; }
00625   
00627   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00628     inline bool
00629     operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00630                const map<_Key,_Tp,_Compare,_Alloc>& __y)
00631     { return !(__x == __y); }
00632   
00634   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00635     inline bool
00636     operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00637               const map<_Key,_Tp,_Compare,_Alloc>& __y)
00638     { return __y < __x; }
00639   
00641   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00642     inline bool
00643     operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00644                const map<_Key,_Tp,_Compare,_Alloc>& __y)
00645     { return !(__y < __x); }
00646   
00648   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00649     inline bool
00650     operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x,
00651                const map<_Key,_Tp,_Compare,_Alloc>& __y)
00652     { return !(__x < __y); }
00653   
00655   template <typename _Key, typename _Tp, typename _Compare, typename _Alloc>
00656     inline void
00657     swap(map<_Key,_Tp,_Compare,_Alloc>& __x, map<_Key,_Tp,_Compare,_Alloc>& __y)
00658     { __x.swap(__y); }
00659 } // namespace std
00660 
00661 #endif /* __GLIBCPP_INTERNAL_MAP_H */

Generated on Thu Nov 21 03:12:50 2002 for libstdc++-v3 Source by doxygen1.2.18-20021030