Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

stl_map.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996,1997
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 #ifndef _CPP_BITS_STL_MAP_H
00032 #define _CPP_BITS_STL_MAP_H 1
00033 
00034 #include <bits/concept_check.h>
00035 
00036 namespace std
00037 {
00038 
00039 template <class _Key, class _Tp, class _Compare = less<_Key>,
00040           class _Alloc = allocator<pair<const _Key, _Tp> > >
00041 class map
00042 {
00043   // concept requirements
00044   __glibcpp_class_requires(_Tp, _SGIAssignableConcept);
00045   __glibcpp_class_requires4(_Compare, bool, _Key, _Key, _BinaryFunctionConcept);
00046 
00047 public:
00048   // typedefs:
00049   typedef _Key                 key_type;
00050   typedef _Tp                   data_type;
00051   typedef _Tp                   mapped_type;
00052   typedef pair<const _Key, _Tp> value_type;
00053   typedef _Compare             key_compare;
00054     
00055   class value_compare
00056     : public binary_function<value_type, value_type, bool> {
00057   friend class map<_Key,_Tp,_Compare,_Alloc>;
00058   protected :
00059     _Compare comp;
00060     value_compare(_Compare __c) : comp(__c) {}
00061   public:
00062     bool operator()(const value_type& __x, const value_type& __y) const {
00063       return comp(__x.first, __y.first);
00064     }
00065   };
00066 
00067 private:
00068   typedef _Rb_tree<key_type, value_type, 
00069                    _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
00070   _Rep_type _M_t;  // red-black tree representing map
00071 public:
00072   typedef typename _Rep_type::pointer pointer;
00073   typedef typename _Rep_type::const_pointer const_pointer;
00074   typedef typename _Rep_type::reference reference;
00075   typedef typename _Rep_type::const_reference const_reference;
00076   typedef typename _Rep_type::iterator iterator;
00077   typedef typename _Rep_type::const_iterator const_iterator;
00078   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00079   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00080   typedef typename _Rep_type::size_type size_type;
00081   typedef typename _Rep_type::difference_type difference_type;
00082   typedef typename _Rep_type::allocator_type allocator_type;
00083 
00084   // allocation/deallocation
00085 
00086   map() : _M_t(_Compare(), allocator_type()) {}
00087   explicit map(const _Compare& __comp,
00088                const allocator_type& __a = allocator_type())
00089     : _M_t(__comp, __a) {}
00090 
00091   template <class _InputIterator>
00092   map(_InputIterator __first, _InputIterator __last)
00093     : _M_t(_Compare(), allocator_type())
00094     { _M_t.insert_unique(__first, __last); }
00095 
00096   template <class _InputIterator>
00097   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
00098       const allocator_type& __a = allocator_type())
00099     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00100   map(const map<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
00101 
00102   map<_Key,_Tp,_Compare,_Alloc>&
00103   operator=(const map<_Key, _Tp, _Compare, _Alloc>& __x)
00104   {
00105     _M_t = __x._M_t;
00106     return *this; 
00107   }
00108 
00109   // accessors:
00110 
00111   key_compare key_comp() const { return _M_t.key_comp(); }
00112   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00113   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00114 
00115   iterator begin() { return _M_t.begin(); }
00116   const_iterator begin() const { return _M_t.begin(); }
00117   iterator end() { return _M_t.end(); }
00118   const_iterator end() const { return _M_t.end(); }
00119   reverse_iterator rbegin() { return _M_t.rbegin(); }
00120   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00121   reverse_iterator rend() { return _M_t.rend(); }
00122   const_reverse_iterator rend() const { return _M_t.rend(); }
00123   bool empty() const { return _M_t.empty(); }
00124   size_type size() const { return _M_t.size(); }
00125   size_type max_size() const { return _M_t.max_size(); }
00126   _Tp& operator[](const key_type& __k) {
00127     iterator __i = lower_bound(__k);
00128     // __i->first is greater than or equivalent to __k.
00129     if (__i == end() || key_comp()(__k, (*__i).first))
00130       __i = insert(__i, value_type(__k, _Tp()));
00131     return (*__i).second;
00132   }
00133   void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
00134 
00135   // insert/erase
00136 
00137   pair<iterator,bool> insert(const value_type& __x) 
00138     { return _M_t.insert_unique(__x); }
00139   iterator insert(iterator position, const value_type& __x)
00140     { return _M_t.insert_unique(position, __x); }
00141   template <class _InputIterator>
00142   void insert(_InputIterator __first, _InputIterator __last) {
00143     _M_t.insert_unique(__first, __last);
00144   }
00145 
00146   void erase(iterator __position) { _M_t.erase(__position); }
00147   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
00148   void erase(iterator __first, iterator __last)
00149     { _M_t.erase(__first, __last); }
00150   void clear() { _M_t.clear(); }
00151 
00152   // map operations:
00153 
00154   iterator find(const key_type& __x) { return _M_t.find(__x); }
00155   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
00156   size_type count(const key_type& __x) const {
00157     return _M_t.find(__x) == _M_t.end() ? 0 : 1; 
00158   }
00159   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
00160   const_iterator lower_bound(const key_type& __x) const {
00161     return _M_t.lower_bound(__x); 
00162   }
00163   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
00164   const_iterator upper_bound(const key_type& __x) const {
00165     return _M_t.upper_bound(__x); 
00166   }
00167   
00168   pair<iterator,iterator> equal_range(const key_type& __x) {
00169     return _M_t.equal_range(__x);
00170   }
00171   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
00172     return _M_t.equal_range(__x);
00173   }
00174 
00175   template <class _K1, class _T1, class _C1, class _A1>
00176   friend bool operator== (const map<_K1, _T1, _C1, _A1>&,
00177                           const map<_K1, _T1, _C1, _A1>&);
00178   template <class _K1, class _T1, class _C1, class _A1>
00179   friend bool operator< (const map<_K1, _T1, _C1, _A1>&,
00180                          const map<_K1, _T1, _C1, _A1>&);
00181 };
00182 
00183 template <class _Key, class _Tp, class _Compare, class _Alloc>
00184 inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00185                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00186   return __x._M_t == __y._M_t;
00187 }
00188 
00189 template <class _Key, class _Tp, class _Compare, class _Alloc>
00190 inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00191                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00192   return __x._M_t < __y._M_t;
00193 }
00194 
00195 template <class _Key, class _Tp, class _Compare, class _Alloc>
00196 inline bool operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00197                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00198   return !(__x == __y);
00199 }
00200 
00201 template <class _Key, class _Tp, class _Compare, class _Alloc>
00202 inline bool operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00203                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00204   return __y < __x;
00205 }
00206 
00207 template <class _Key, class _Tp, class _Compare, class _Alloc>
00208 inline bool operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00209                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00210   return !(__y < __x);
00211 }
00212 
00213 template <class _Key, class _Tp, class _Compare, class _Alloc>
00214 inline bool operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00215                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00216   return !(__x < __y);
00217 }
00218 
00219 template <class _Key, class _Tp, class _Compare, class _Alloc>
00220 inline void swap(map<_Key,_Tp,_Compare,_Alloc>& __x, 
00221                  map<_Key,_Tp,_Compare,_Alloc>& __y) {
00222   __x.swap(__y);
00223 }
00224 
00225 } // namespace std
00226 
00227 #endif /* _CPP_BITS_STL_MAP_H */
00228 
00229 // Local Variables:
00230 // mode:C++
00231 // End:

Generated at Tue May 1 16:28:39 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001