stl_map.h

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 __SGI_STL_INTERNAL_MAP_H
00032 #define __SGI_STL_INTERNAL_MAP_H
00033 
00034 #include <concept_checks.h>
00035 
00036 __STL_BEGIN_NAMESPACE
00037 
00038 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00039 #pragma set woff 1174
00040 #pragma set woff 1375
00041 #endif
00042 
00043 // Forward declarations of operators == and <, needed for friend declarations.
00044 template <class _Key, class _Tp, 
00045           class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
00046           class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
00047 class map;
00048 
00049 template <class _Key, class _Tp, class _Compare, class _Alloc>
00050 inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00051                        const map<_Key,_Tp,_Compare,_Alloc>& __y);
00052 
00053 template <class _Key, class _Tp, class _Compare, class _Alloc>
00054 inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00055                       const map<_Key,_Tp,_Compare,_Alloc>& __y);
00056 
00057 template <class _Key, class _Tp, class _Compare, class _Alloc>
00058 class map {
00059 public:
00060 
00061 // requirements:
00062 
00063   __STL_CLASS_REQUIRES(_Tp, _Assignable);
00064   __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
00065 
00066 // typedefs:
00067 
00068   typedef _Key                  key_type;
00069   typedef _Tp                   data_type;
00070   typedef _Tp                   mapped_type;
00071   typedef pair<const _Key, _Tp> value_type;
00072   typedef _Compare              key_compare;
00073     
00074   class value_compare
00075     : public binary_function<value_type, value_type, bool> {
00076   friend class map<_Key,_Tp,_Compare,_Alloc>;
00077   protected :
00078     _Compare comp;
00079     value_compare(_Compare __c) : comp(__c) {}
00080   public:
00081     bool operator()(const value_type& __x, const value_type& __y) const {
00082       return comp(__x.first, __y.first);
00083     }
00084   };
00085 
00086 private:
00087   typedef _Rb_tree<key_type, value_type, 
00088                    _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
00089   _Rep_type _M_t;  // red-black tree representing map
00090 public:
00091   typedef typename _Rep_type::pointer pointer;
00092   typedef typename _Rep_type::const_pointer const_pointer;
00093   typedef typename _Rep_type::reference reference;
00094   typedef typename _Rep_type::const_reference const_reference;
00095   typedef typename _Rep_type::iterator iterator;
00096   typedef typename _Rep_type::const_iterator const_iterator;
00097   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00098   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00099   typedef typename _Rep_type::size_type size_type;
00100   typedef typename _Rep_type::difference_type difference_type;
00101   typedef typename _Rep_type::allocator_type allocator_type;
00102 
00103   // allocation/deallocation
00104 
00105   map() : _M_t(_Compare(), allocator_type()) {}
00106   explicit map(const _Compare& __comp,
00107                const allocator_type& __a = allocator_type())
00108     : _M_t(__comp, __a) {}
00109 
00110 #ifdef __STL_MEMBER_TEMPLATES
00111   template <class _InputIterator>
00112   map(_InputIterator __first, _InputIterator __last)
00113     : _M_t(_Compare(), allocator_type())
00114     { _M_t.insert_unique(__first, __last); }
00115 
00116   template <class _InputIterator>
00117   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
00118       const allocator_type& __a = allocator_type())
00119     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00120 #else
00121   map(const value_type* __first, const value_type* __last)
00122     : _M_t(_Compare(), allocator_type())
00123     { _M_t.insert_unique(__first, __last); }
00124 
00125   map(const value_type* __first,
00126       const value_type* __last, const _Compare& __comp,
00127       const allocator_type& __a = allocator_type())
00128     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00129 
00130   map(const_iterator __first, const_iterator __last)
00131     : _M_t(_Compare(), allocator_type()) 
00132     { _M_t.insert_unique(__first, __last); }
00133 
00134   map(const_iterator __first, const_iterator __last, const _Compare& __comp,
00135       const allocator_type& __a = allocator_type())
00136     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00137 
00138 #endif /* __STL_MEMBER_TEMPLATES */
00139 
00140   map(const map<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
00141   map<_Key,_Tp,_Compare,_Alloc>&
00142   operator=(const map<_Key, _Tp, _Compare, _Alloc>& __x)
00143   {
00144     _M_t = __x._M_t;
00145     return *this; 
00146   }
00147 
00148   // accessors:
00149 
00150   key_compare key_comp() const { return _M_t.key_comp(); }
00151   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00152   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00153 
00154   iterator begin() { return _M_t.begin(); }
00155   const_iterator begin() const { return _M_t.begin(); }
00156   iterator end() { return _M_t.end(); }
00157   const_iterator end() const { return _M_t.end(); }
00158   reverse_iterator rbegin() { return _M_t.rbegin(); }
00159   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00160   reverse_iterator rend() { return _M_t.rend(); }
00161   const_reverse_iterator rend() const { return _M_t.rend(); }
00162   bool empty() const { return _M_t.empty(); }
00163   size_type size() const { return _M_t.size(); }
00164   size_type max_size() const { return _M_t.max_size(); }
00165   _Tp& operator[](const key_type& __k) {
00166     iterator __i = lower_bound(__k);
00167     // __i->first is greater than or equivalent to __k.
00168     if (__i == end() || key_comp()(__k, (*__i).first))
00169       __i = insert(__i, value_type(__k, _Tp()));
00170     return (*__i).second;
00171   }
00172   void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
00173 
00174   // insert/erase
00175 
00176   pair<iterator,bool> insert(const value_type& __x) 
00177     { return _M_t.insert_unique(__x); }
00178   iterator insert(iterator position, const value_type& __x)
00179     { return _M_t.insert_unique(position, __x); }
00180 #ifdef __STL_MEMBER_TEMPLATES
00181   template <class _InputIterator>
00182   void insert(_InputIterator __first, _InputIterator __last) {
00183     _M_t.insert_unique(__first, __last);
00184   }
00185 #else
00186   void insert(const value_type* __first, const value_type* __last) {
00187     _M_t.insert_unique(__first, __last);
00188   }
00189   void insert(const_iterator __first, const_iterator __last) {
00190     _M_t.insert_unique(__first, __last);
00191   }
00192 #endif /* __STL_MEMBER_TEMPLATES */
00193 
00194   void erase(iterator __position) { _M_t.erase(__position); }
00195   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
00196   void erase(iterator __first, iterator __last)
00197     { _M_t.erase(__first, __last); }
00198   void clear() { _M_t.clear(); }
00199 
00200   // map operations:
00201 
00202   iterator find(const key_type& __x) { return _M_t.find(__x); }
00203   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
00204   size_type count(const key_type& __x) const {
00205     return _M_t.find(__x) == _M_t.end() ? 0 : 1; 
00206   }
00207   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
00208   const_iterator lower_bound(const key_type& __x) const {
00209     return _M_t.lower_bound(__x); 
00210   }
00211   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
00212   const_iterator upper_bound(const key_type& __x) const {
00213     return _M_t.upper_bound(__x); 
00214   }
00215   
00216   pair<iterator,iterator> equal_range(const key_type& __x) {
00217     return _M_t.equal_range(__x);
00218   }
00219   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
00220     return _M_t.equal_range(__x);
00221   }
00222 
00223 #ifdef __STL_TEMPLATE_FRIENDS 
00224   template <class _K1, class _T1, class _C1, class _A1>
00225   friend bool operator== (const map<_K1, _T1, _C1, _A1>&,
00226                           const map<_K1, _T1, _C1, _A1>&);
00227   template <class _K1, class _T1, class _C1, class _A1>
00228   friend bool operator< (const map<_K1, _T1, _C1, _A1>&,
00229                          const map<_K1, _T1, _C1, _A1>&);
00230 #else /* __STL_TEMPLATE_FRIENDS */
00231   friend bool __STD_QUALIFIER
00232   operator== __STL_NULL_TMPL_ARGS (const map&, const map&);
00233   friend bool __STD_QUALIFIER
00234   operator< __STL_NULL_TMPL_ARGS (const map&, const map&);
00235 #endif /* __STL_TEMPLATE_FRIENDS */
00236 };
00237 
00238 template <class _Key, class _Tp, class _Compare, class _Alloc>
00239 inline bool operator==(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00240                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00241   return __x._M_t == __y._M_t;
00242 }
00243 
00244 template <class _Key, class _Tp, class _Compare, class _Alloc>
00245 inline bool operator<(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00246                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00247   return __x._M_t < __y._M_t;
00248 }
00249 
00250 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00251 
00252 template <class _Key, class _Tp, class _Compare, class _Alloc>
00253 inline bool operator!=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00254                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00255   return !(__x == __y);
00256 }
00257 
00258 template <class _Key, class _Tp, class _Compare, class _Alloc>
00259 inline bool operator>(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00260                       const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00261   return __y < __x;
00262 }
00263 
00264 template <class _Key, class _Tp, class _Compare, class _Alloc>
00265 inline bool operator<=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00266                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00267   return !(__y < __x);
00268 }
00269 
00270 template <class _Key, class _Tp, class _Compare, class _Alloc>
00271 inline bool operator>=(const map<_Key,_Tp,_Compare,_Alloc>& __x, 
00272                        const map<_Key,_Tp,_Compare,_Alloc>& __y) {
00273   return !(__x < __y);
00274 }
00275 
00276 template <class _Key, class _Tp, class _Compare, class _Alloc>
00277 inline void swap(map<_Key,_Tp,_Compare,_Alloc>& __x, 
00278                  map<_Key,_Tp,_Compare,_Alloc>& __y) {
00279   __x.swap(__y);
00280 }
00281 
00282 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00283 
00284 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00285 #pragma reset woff 1174
00286 #pragma reset woff 1375
00287 #endif
00288 
00289 __STL_END_NAMESPACE
00290 
00291 #endif /* __SGI_STL_INTERNAL_MAP_H */
00292 
00293 // Local Variables:
00294 // mode:C++
00295 // End:

Generated on Mon Jun 5 10:20:44 2006 for Intelligence.kdevelop by  doxygen 1.4.6