_map.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996,1997
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999 
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted 
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_MAP_H
00031 #define _STLP_INTERNAL_MAP_H
00032 
00033 #ifndef _STLP_INTERNAL_TREE_H
00034 # include <stl/_tree.h>
00035 #endif
00036 
00037 #define map __WORKAROUND_RENAME(map)
00038 #define multimap __WORKAROUND_RENAME(multimap)
00039 
00040 _STLP_BEGIN_NAMESPACE
00041 
00042 template <class _Key, class _Tp, __DFL_TMPL_PARAM(_Compare, less<_Key> ), 
00043           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00044 class map {
00045 public:
00046 
00047 // typedefs:
00048 
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 _M_comp;
00060     value_compare(_Compare __c) : _M_comp(__c) {}
00061   public:
00062     bool operator()(const value_type& __x, const value_type& __y) const {
00063       return _M_comp(__x.first, __y.first);
00064     }
00065   };
00066 
00067 private:
00068 # ifdef _STLP_MULTI_CONST_TEMPLATE_ARG_BUG
00069   typedef _Rb_tree<key_type, value_type, 
00070                    _Select1st_hint<value_type, _Key>, key_compare, _Alloc> _Rep_type;
00071 # else
00072   typedef _Rb_tree<key_type, value_type, 
00073                    _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
00074 # endif
00075   _Rep_type _M_t;  // red-black tree representing map
00076 public:
00077   typedef typename _Rep_type::pointer pointer;
00078   typedef typename _Rep_type::const_pointer const_pointer;
00079   typedef typename _Rep_type::reference reference;
00080   typedef typename _Rep_type::const_reference const_reference;
00081   typedef typename _Rep_type::iterator iterator;
00082   typedef typename _Rep_type::const_iterator const_iterator;
00083   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00084   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00085   typedef typename _Rep_type::size_type size_type;
00086   typedef typename _Rep_type::difference_type difference_type;
00087   typedef typename _Rep_type::allocator_type allocator_type;
00088 
00089   // allocation/deallocation
00090 
00091   map() : _M_t(_Compare(), allocator_type()) {}
00092   explicit map(const _Compare& __comp,
00093                const allocator_type& __a = allocator_type())
00094     : _M_t(__comp, __a) {}
00095 
00096 #ifdef _STLP_MEMBER_TEMPLATES
00097   template <class _InputIterator>
00098   map(_InputIterator __first, _InputIterator __last)
00099     : _M_t(_Compare(), allocator_type())
00100     { _M_t.insert_unique(__first, __last); }
00101 
00102   template <class _InputIterator>
00103   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
00104       const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
00105     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00106 
00107 # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
00108   template <class _InputIterator>
00109   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp)
00110     : _M_t(__comp, allocator_type()) { _M_t.insert_unique(__first, __last); }
00111 # endif
00112 
00113 #else
00114   map(const value_type* __first, const value_type* __last)
00115     : _M_t(_Compare(), allocator_type())
00116     { _M_t.insert_unique(__first, __last); }
00117 
00118   map(const value_type* __first,
00119       const value_type* __last, const _Compare& __comp,
00120       const allocator_type& __a = allocator_type())
00121     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00122 
00123   map(const_iterator __first, const_iterator __last)
00124     : _M_t(_Compare(), allocator_type()) 
00125     { _M_t.insert_unique(__first, __last); }
00126 
00127   map(const_iterator __first, const_iterator __last, const _Compare& __comp,
00128       const allocator_type& __a = allocator_type())
00129     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00130 
00131 #endif /* _STLP_MEMBER_TEMPLATES */
00132 
00133   map(const map<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
00134   map<_Key,_Tp,_Compare,_Alloc>&
00135   operator=(const map<_Key, _Tp, _Compare, _Alloc>& __x)
00136   {
00137     _M_t = __x._M_t;
00138     return *this; 
00139   }
00140 
00141   // accessors:
00142 
00143   key_compare key_comp() const { return _M_t.key_comp(); }
00144   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00145   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00146 
00147   iterator begin() { return _M_t.begin(); }
00148   const_iterator begin() const { return _M_t.begin(); }
00149   iterator end() { return _M_t.end(); }
00150   const_iterator end() const { return _M_t.end(); }
00151   reverse_iterator rbegin() { return _M_t.rbegin(); }
00152   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00153   reverse_iterator rend() { return _M_t.rend(); }
00154   const_reverse_iterator rend() const { return _M_t.rend(); }
00155   bool empty() const { return _M_t.empty(); }
00156   size_type size() const { return _M_t.size(); }
00157   size_type max_size() const { return _M_t.max_size(); }
00158   _Tp& operator[](const key_type& __k) {
00159     iterator __i = lower_bound(__k);
00160     // __i->first is greater than or equivalent to __k.
00161     if (__i == end() || key_comp()(__k, (*__i).first))
00162       __i = insert(__i, value_type(__k, _Tp()));
00163     return (*__i).second;
00164   }
00165   void swap(map<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
00166 
00167   // insert/erase
00168 
00169   pair<iterator,bool> insert(const value_type& __x) 
00170     { return _M_t.insert_unique(__x); }
00171   iterator insert(iterator position, const value_type& __x)
00172     { return _M_t.insert_unique(position, __x); }
00173 #ifdef _STLP_MEMBER_TEMPLATES
00174   template <class _InputIterator>
00175   void insert(_InputIterator __first, _InputIterator __last) {
00176     _M_t.insert_unique(__first, __last);
00177   }
00178 #else
00179   void insert(const value_type* __first, const value_type* __last) {
00180     _M_t.insert_unique(__first, __last);
00181   }
00182   void insert(const_iterator __first, const_iterator __last) {
00183     _M_t.insert_unique(__first, __last);
00184   }
00185 #endif /* _STLP_MEMBER_TEMPLATES */
00186 
00187   void erase(iterator __position) { _M_t.erase(__position); }
00188   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
00189   void erase(iterator __first, iterator __last)
00190     { _M_t.erase(__first, __last); }
00191   void clear() { _M_t.clear(); }
00192 
00193   // map operations:
00194 
00195   iterator find(const key_type& __x) { return _M_t.find(__x); }
00196   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
00197   size_type count(const key_type& __x) const { 
00198     return _M_t.find(__x) == _M_t.end() ? 0 : 1;
00199   }
00200   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
00201   const_iterator lower_bound(const key_type& __x) const {
00202     return _M_t.lower_bound(__x); 
00203   }
00204   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
00205   const_iterator upper_bound(const key_type& __x) const {
00206     return _M_t.upper_bound(__x); 
00207   }
00208   
00209   pair<iterator,iterator> equal_range(const key_type& __x) {
00210     return _M_t.equal_range(__x);
00211   }
00212   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
00213     return _M_t.equal_range(__x);
00214   }
00215 };
00216 
00217 
00218 template <class _Key, class _Tp, __DFL_TMPL_PARAM(_Compare, less<_Key> ), 
00219           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00220 class multimap {
00221 public:
00222 
00223 // typedefs:
00224 
00225   typedef _Key                  key_type;
00226   typedef _Tp                   data_type;
00227   typedef _Tp                   mapped_type;
00228   typedef pair<const _Key, _Tp> value_type;
00229   typedef _Compare              key_compare;
00230 
00231   class value_compare : public binary_function<value_type, value_type, bool> {
00232   friend class multimap<_Key,_Tp,_Compare,_Alloc>;
00233   protected:
00234     _Compare _M_comp;
00235     value_compare(_Compare __c) : _M_comp(__c) {}
00236   public:
00237     bool operator()(const value_type& __x, const value_type& __y) const {
00238       return _M_comp(__x.first, __y.first);
00239     }
00240   };
00241 
00242 private:
00243 # ifdef _STLP_MULTI_CONST_TEMPLATE_ARG_BUG
00244   typedef _Rb_tree<key_type, value_type, 
00245                   _Select1st_hint<value_type, _Key>, key_compare, _Alloc> _Rep_type;
00246 # else
00247   typedef _Rb_tree<key_type, value_type, 
00248                   _Select1st<value_type>, key_compare, _Alloc> _Rep_type;
00249 # endif
00250   _Rep_type _M_t;  // red-black tree representing multimap
00251 public:
00252   typedef typename _Rep_type::pointer pointer;
00253   typedef typename _Rep_type::const_pointer const_pointer;
00254   typedef typename _Rep_type::reference reference;
00255   typedef typename _Rep_type::const_reference const_reference;
00256   typedef typename _Rep_type::iterator iterator;
00257   typedef typename _Rep_type::const_iterator const_iterator; 
00258   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00259   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00260   typedef typename _Rep_type::size_type size_type;
00261   typedef typename _Rep_type::difference_type difference_type;
00262   typedef typename _Rep_type::allocator_type allocator_type;
00263 
00264 // allocation/deallocation
00265 
00266   multimap() : _M_t(_Compare(), allocator_type()) { }
00267   explicit multimap(const _Compare& __comp,
00268                     const allocator_type& __a = allocator_type())
00269     : _M_t(__comp, __a) { }
00270 
00271 #ifdef _STLP_MEMBER_TEMPLATES  
00272   template <class _InputIterator>
00273   multimap(_InputIterator __first, _InputIterator __last)
00274     : _M_t(_Compare(), allocator_type())
00275     { _M_t.insert_equal(__first, __last); }
00276 # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
00277   template <class _InputIterator>
00278   multimap(_InputIterator __first, _InputIterator __last,
00279            const _Compare& __comp)
00280     : _M_t(__comp, allocator_type()) { _M_t.insert_equal(__first, __last); }
00281 #  endif
00282   template <class _InputIterator>
00283   multimap(_InputIterator __first, _InputIterator __last,
00284            const _Compare& __comp,
00285            const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
00286     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00287 #else
00288   multimap(const value_type* __first, const value_type* __last)
00289     : _M_t(_Compare(), allocator_type())
00290     { _M_t.insert_equal(__first, __last); }
00291   multimap(const value_type* __first, const value_type* __last,
00292            const _Compare& __comp,
00293            const allocator_type& __a = allocator_type())
00294     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00295 
00296   multimap(const_iterator __first, const_iterator __last)
00297     : _M_t(_Compare(), allocator_type())
00298     { _M_t.insert_equal(__first, __last); }
00299   multimap(const_iterator __first, const_iterator __last,
00300            const _Compare& __comp,
00301            const allocator_type& __a = allocator_type())
00302     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00303 #endif /* _STLP_MEMBER_TEMPLATES */
00304 
00305   multimap(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) : _M_t(__x._M_t) { }
00306   multimap<_Key,_Tp,_Compare,_Alloc>&
00307   operator=(const multimap<_Key,_Tp,_Compare,_Alloc>& __x) {
00308     _M_t = __x._M_t;
00309     return *this; 
00310   }
00311 
00312   // accessors:
00313 
00314   key_compare key_comp() const { return _M_t.key_comp(); }
00315   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00316   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00317 
00318   iterator begin() { return _M_t.begin(); }
00319   const_iterator begin() const { return _M_t.begin(); }
00320   iterator end() { return _M_t.end(); }
00321   const_iterator end() const { return _M_t.end(); }
00322   reverse_iterator rbegin() { return _M_t.rbegin(); }
00323   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00324   reverse_iterator rend() { return _M_t.rend(); }
00325   const_reverse_iterator rend() const { return _M_t.rend(); }
00326   bool empty() const { return _M_t.empty(); }
00327   size_type size() const { return _M_t.size(); }
00328   size_type max_size() const { return _M_t.max_size(); }
00329   void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
00330 
00331   // insert/erase
00332 
00333   iterator insert(const value_type& __x) { return _M_t.insert_equal(__x); }
00334   iterator insert(iterator __position, const value_type& __x) {
00335     return _M_t.insert_equal(__position, __x);
00336   }
00337 #ifdef _STLP_MEMBER_TEMPLATES  
00338   template <class _InputIterator>
00339   void insert(_InputIterator __first, _InputIterator __last) {
00340     _M_t.insert_equal(__first, __last);
00341   }
00342 #else
00343   void insert(const value_type* __first, const value_type* __last) {
00344     _M_t.insert_equal(__first, __last);
00345   }
00346   void insert(const_iterator __first, const_iterator __last) {
00347     _M_t.insert_equal(__first, __last);
00348   }
00349 #endif /* _STLP_MEMBER_TEMPLATES */
00350   void erase(iterator __position) { _M_t.erase(__position); }
00351   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
00352   void erase(iterator __first, iterator __last)
00353     { _M_t.erase(__first, __last); }
00354   void clear() { _M_t.clear(); }
00355 
00356   // multimap operations:
00357 
00358   iterator find(const key_type& __x) { return _M_t.find(__x); }
00359   const_iterator find(const key_type& __x) const { return _M_t.find(__x); }
00360   size_type count(const key_type& __x) const { return _M_t.count(__x); }
00361   iterator lower_bound(const key_type& __x) {return _M_t.lower_bound(__x); }
00362   const_iterator lower_bound(const key_type& __x) const {
00363     return _M_t.lower_bound(__x); 
00364   }
00365   iterator upper_bound(const key_type& __x) {return _M_t.upper_bound(__x); }
00366   const_iterator upper_bound(const key_type& __x) const {
00367     return _M_t.upper_bound(__x); 
00368   }
00369    pair<iterator,iterator> equal_range(const key_type& __x) {
00370     return _M_t.equal_range(__x);
00371   }
00372   pair<const_iterator,const_iterator> equal_range(const key_type& __x) const {
00373     return _M_t.equal_range(__x);
00374   }
00375 };
00376 
00377 # define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _Compare, class _Alloc>
00378 
00379 # define _STLP_TEMPLATE_CONTAINER map<_Key,_Tp,_Compare,_Alloc>
00380 
00381 // fbp : if this template header gets protected against your will, report it !
00382 # include <stl/_relops_cont.h>
00383 
00384 # undef  _STLP_TEMPLATE_CONTAINER
00385 # define _STLP_TEMPLATE_CONTAINER multimap<_Key,_Tp,_Compare,_Alloc>
00386 
00387 // fbp : if this template header gets protected against your will, report it !
00388 # include <stl/_relops_cont.h>
00389 
00390 # undef  _STLP_TEMPLATE_CONTAINER
00391 # undef  _STLP_TEMPLATE_HEADER
00392 
00393 _STLP_END_NAMESPACE
00394 
00395 // do a cleanup
00396 #  undef map
00397 #  undef multimap
00398 // provide a way to access full funclionality 
00399 # define __map__  __FULL_NAME(map)
00400 # define __multimap__  __FULL_NAME(multimap)
00401 
00402 # ifdef _STLP_USE_WRAPPER_FOR_ALLOC_PARAM
00403 # include <stl/wrappers/_map.h>
00404 # endif
00405 
00406 #endif /* _STLP_INTERNAL_MAP_H */
00407 
00408 // Local Variables:
00409 // mode:C++
00410 // End:
00411 

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