/home/ntakagi/work/STLport-5.1.5/stlport/stl/_map.h

Go to the documentation of this file.
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 _STLP_BEGIN_NAMESPACE
00038 
00039 //Specific iterator traits creation
00040 _STLP_CREATE_ITERATOR_TRAITS(MapTraitsT, traits)
00041 
00042 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key> ),
00043           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00044 class map
00045 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00046           : public __stlport_class<map<_Key, _Tp, _Compare, _Alloc> >
00047 #endif
00048 {
00049   typedef map<_Key, _Tp, _Compare, _Alloc> _Self;
00050 public:
00051 
00052 // typedefs:
00053 
00054   typedef _Key                  key_type;
00055   typedef _Tp                   data_type;
00056   typedef _Tp                   mapped_type;
00057   typedef pair<const _Key, _Tp> value_type;
00058   typedef _Compare              key_compare;
00059 
00060   class value_compare
00061     : public binary_function<value_type, value_type, bool> {
00062   friend class map<_Key,_Tp,_Compare,_Alloc>;
00063   protected :
00064     //c is a Standard name (23.3.1), do no make it STLport naming convention compliant.
00065     _Compare comp;
00066     value_compare(_Compare __c) : comp(__c) {}
00067   public:
00068     bool operator()(const value_type& __x, const value_type& __y) const
00069     { return comp(__x.first, __y.first); }
00070   };
00071 
00072 protected:
00073   typedef _STLP_PRIV _MapTraitsT<value_type> _MapTraits;
00074 
00075 public:
00076   //Following typedef have to be public for __move_traits specialization.
00077   typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
00078                               value_type, _STLP_SELECT1ST(value_type, _Key),
00079                               _MapTraits, _Alloc> _Rep_type;
00080 
00081   typedef typename _Rep_type::pointer pointer;
00082   typedef typename _Rep_type::const_pointer const_pointer;
00083   typedef typename _Rep_type::reference reference;
00084   typedef typename _Rep_type::const_reference const_reference;
00085   typedef typename _Rep_type::iterator iterator;
00086   typedef typename _Rep_type::const_iterator const_iterator;
00087   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00088   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00089   typedef typename _Rep_type::size_type size_type;
00090   typedef typename _Rep_type::difference_type difference_type;
00091   typedef typename _Rep_type::allocator_type allocator_type;
00092 
00093 private:
00094   _Rep_type _M_t;  // red-black tree representing map
00095   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00096 
00097 public:
00098   // allocation/deallocation
00099   map() : _M_t(_Compare(), allocator_type()) {}
00100 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
00101   explicit map(const _Compare& __comp,
00102                const allocator_type& __a = allocator_type())
00103 #else
00104   explicit map(const _Compare& __comp)
00105     : _M_t(__comp, allocator_type()) {}
00106   explicit map(const _Compare& __comp, const allocator_type& __a)
00107 #endif
00108     : _M_t(__comp, __a) {}
00109 
00110 #if defined (_STLP_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 _STLP_ALLOCATOR_TYPE_DFL)
00119     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00120 
00121 #  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
00122   template <class _InputIterator>
00123   map(_InputIterator __first, _InputIterator __last, const _Compare& __comp)
00124     : _M_t(__comp, allocator_type()) { _M_t.insert_unique(__first, __last); }
00125 #  endif
00126 
00127 #else
00128   map(const value_type* __first, const value_type* __last)
00129     : _M_t(_Compare(), allocator_type())
00130     { _M_t.insert_unique(__first, __last); }
00131 
00132   map(const value_type* __first,
00133       const value_type* __last, const _Compare& __comp,
00134       const allocator_type& __a = allocator_type())
00135     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00136 
00137   map(const_iterator __first, const_iterator __last)
00138     : _M_t(_Compare(), allocator_type())
00139     { _M_t.insert_unique(__first, __last); }
00140 
00141   map(const_iterator __first, const_iterator __last, const _Compare& __comp,
00142       const allocator_type& __a = allocator_type())
00143     : _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
00144 #endif /* _STLP_MEMBER_TEMPLATES */
00145 
00146   map(const _Self& __x) : _M_t(__x._M_t) {}
00147 
00148   map(__move_source<_Self> src)
00149     : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
00150 
00151   _Self& operator=(const _Self& __x) {
00152     _M_t = __x._M_t;
00153     return *this;
00154   }
00155 
00156   // accessors:
00157   key_compare key_comp() const { return _M_t.key_comp(); }
00158   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00159   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00160 
00161   iterator begin() { return _M_t.begin(); }
00162   const_iterator begin() const { return _M_t.begin(); }
00163   iterator end() { return _M_t.end(); }
00164   const_iterator end() const { return _M_t.end(); }
00165   reverse_iterator rbegin() { return _M_t.rbegin(); }
00166   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00167   reverse_iterator rend() { return _M_t.rend(); }
00168   const_reverse_iterator rend() const { return _M_t.rend(); }
00169   bool empty() const { return _M_t.empty(); }
00170   size_type size() const { return _M_t.size(); }
00171   size_type max_size() const { return _M_t.max_size(); }
00172   _STLP_TEMPLATE_FOR_CONT_EXT
00173   _Tp& operator[](const _KT& __k) {
00174     iterator __i = lower_bound(__k);
00175     // __i->first is greater than or equivalent to __k.
00176     if (__i == end() || key_comp()(__k, (*__i).first))
00177       __i = insert(__i, value_type(__k, _STLP_DEFAULT_CONSTRUCTED(_Tp)));
00178     return (*__i).second;
00179   }
00180   void swap(_Self& __x) { _M_t.swap(__x._M_t); }
00181 
00182   // insert/erase
00183   pair<iterator,bool> insert(const value_type& __x)
00184   { return _M_t.insert_unique(__x); }
00185   iterator insert(iterator __pos, const value_type& __x)
00186   { return _M_t.insert_unique(__pos, __x); }
00187 #ifdef _STLP_MEMBER_TEMPLATES
00188   template <class _InputIterator>
00189   void insert(_InputIterator __first, _InputIterator __last)
00190   { _M_t.insert_unique(__first, __last); }
00191 #else
00192   void insert(const value_type* __first, const value_type* __last)
00193   { _M_t.insert_unique(__first, __last); }
00194   void insert(const_iterator __first, const_iterator __last)
00195   { _M_t.insert_unique(__first, __last); }
00196 #endif /* _STLP_MEMBER_TEMPLATES */
00197 
00198   void erase(iterator __pos) { _M_t.erase(__pos); }
00199   size_type erase(const key_type& __x) { return _M_t.erase_unique(__x); }
00200   void erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
00201   void clear() { _M_t.clear(); }
00202 
00203   // map operations:
00204   _STLP_TEMPLATE_FOR_CONT_EXT
00205   iterator find(const _KT& __x) { return _M_t.find(__x); }
00206   _STLP_TEMPLATE_FOR_CONT_EXT
00207   const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
00208   _STLP_TEMPLATE_FOR_CONT_EXT
00209   size_type count(const _KT& __x) const { return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
00210   _STLP_TEMPLATE_FOR_CONT_EXT
00211   iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
00212   _STLP_TEMPLATE_FOR_CONT_EXT
00213   const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
00214   _STLP_TEMPLATE_FOR_CONT_EXT
00215   iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
00216   _STLP_TEMPLATE_FOR_CONT_EXT
00217   const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
00218 
00219   _STLP_TEMPLATE_FOR_CONT_EXT
00220   pair<iterator,iterator> equal_range(const _KT& __x)
00221   { return _M_t.equal_range_unique(__x); }
00222   _STLP_TEMPLATE_FOR_CONT_EXT
00223   pair<const_iterator,const_iterator> equal_range(const _KT& __x) const
00224   { return _M_t.equal_range_unique(__x); }
00225 };
00226 
00227 //Specific iterator traits creation
00228 _STLP_CREATE_ITERATOR_TRAITS(MultimapTraitsT, traits)
00229 
00230 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_Compare, less<_Key> ),
00231           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00232 class multimap
00233 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00234                : public __stlport_class<multimap<_Key, _Tp, _Compare, _Alloc> >
00235 #endif
00236 {
00237   typedef multimap<_Key, _Tp, _Compare, _Alloc> _Self;
00238 public:
00239 
00240 // typedefs:
00241 
00242   typedef _Key                  key_type;
00243   typedef _Tp                   data_type;
00244   typedef _Tp                   mapped_type;
00245   typedef pair<const _Key, _Tp> value_type;
00246   typedef _Compare              key_compare;
00247 
00248   class value_compare : public binary_function<value_type, value_type, bool> {
00249     friend class multimap<_Key,_Tp,_Compare,_Alloc>;
00250   protected:
00251     //comp is a Standard name (23.3.2), do no make it STLport naming convention compliant.
00252     _Compare comp;
00253     value_compare(_Compare __c) : comp(__c) {}
00254   public:
00255     bool operator()(const value_type& __x, const value_type& __y) const
00256     { return comp(__x.first, __y.first); }
00257   };
00258 
00259 protected:
00260   //Specific iterator traits creation
00261   typedef _STLP_PRIV _MultimapTraitsT<value_type> _MultimapTraits;
00262 
00263 public:
00264   //Following typedef have to be public for __move_traits specialization.
00265   typedef _STLP_PRIV _Rb_tree<key_type, key_compare,
00266                               value_type, _STLP_SELECT1ST(value_type, _Key),
00267                               _MultimapTraits, _Alloc> _Rep_type;
00268 
00269   typedef typename _Rep_type::pointer pointer;
00270   typedef typename _Rep_type::const_pointer const_pointer;
00271   typedef typename _Rep_type::reference reference;
00272   typedef typename _Rep_type::const_reference const_reference;
00273   typedef typename _Rep_type::iterator iterator;
00274   typedef typename _Rep_type::const_iterator const_iterator;
00275   typedef typename _Rep_type::reverse_iterator reverse_iterator;
00276   typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
00277   typedef typename _Rep_type::size_type size_type;
00278   typedef typename _Rep_type::difference_type difference_type;
00279   typedef typename _Rep_type::allocator_type allocator_type;
00280 
00281 private:
00282   _Rep_type _M_t;  // red-black tree representing multimap
00283   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00284 
00285 public:
00286   // allocation/deallocation
00287   multimap() : _M_t(_Compare(), allocator_type()) { }
00288   explicit multimap(const _Compare& __comp,
00289                     const allocator_type& __a = allocator_type())
00290     : _M_t(__comp, __a) { }
00291 
00292 #ifdef _STLP_MEMBER_TEMPLATES
00293   template <class _InputIterator>
00294   multimap(_InputIterator __first, _InputIterator __last)
00295     : _M_t(_Compare(), allocator_type())
00296     { _M_t.insert_equal(__first, __last); }
00297 # ifdef _STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS
00298   template <class _InputIterator>
00299   multimap(_InputIterator __first, _InputIterator __last,
00300            const _Compare& __comp)
00301     : _M_t(__comp, allocator_type()) { _M_t.insert_equal(__first, __last); }
00302 #  endif
00303   template <class _InputIterator>
00304   multimap(_InputIterator __first, _InputIterator __last,
00305            const _Compare& __comp,
00306            const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
00307     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00308 #else
00309   multimap(const value_type* __first, const value_type* __last)
00310     : _M_t(_Compare(), allocator_type())
00311     { _M_t.insert_equal(__first, __last); }
00312   multimap(const value_type* __first, const value_type* __last,
00313            const _Compare& __comp,
00314            const allocator_type& __a = allocator_type())
00315     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00316 
00317   multimap(const_iterator __first, const_iterator __last)
00318     : _M_t(_Compare(), allocator_type())
00319     { _M_t.insert_equal(__first, __last); }
00320   multimap(const_iterator __first, const_iterator __last,
00321            const _Compare& __comp,
00322            const allocator_type& __a = allocator_type())
00323     : _M_t(__comp, __a) { _M_t.insert_equal(__first, __last); }
00324 #endif /* _STLP_MEMBER_TEMPLATES */
00325 
00326   multimap(const _Self& __x) : _M_t(__x._M_t) {}
00327 
00328   multimap(__move_source<_Self> src)
00329     : _M_t(__move_source<_Rep_type>(src.get()._M_t)) {}
00330 
00331   _Self& operator=(const _Self& __x) {
00332     _M_t = __x._M_t;
00333     return *this;
00334   }
00335 
00336   // accessors:
00337 
00338   key_compare key_comp() const { return _M_t.key_comp(); }
00339   value_compare value_comp() const { return value_compare(_M_t.key_comp()); }
00340   allocator_type get_allocator() const { return _M_t.get_allocator(); }
00341 
00342   iterator begin() { return _M_t.begin(); }
00343   const_iterator begin() const { return _M_t.begin(); }
00344   iterator end() { return _M_t.end(); }
00345   const_iterator end() const { return _M_t.end(); }
00346   reverse_iterator rbegin() { return _M_t.rbegin(); }
00347   const_reverse_iterator rbegin() const { return _M_t.rbegin(); }
00348   reverse_iterator rend() { return _M_t.rend(); }
00349   const_reverse_iterator rend() const { return _M_t.rend(); }
00350   bool empty() const { return _M_t.empty(); }
00351   size_type size() const { return _M_t.size(); }
00352   size_type max_size() const { return _M_t.max_size(); }
00353   void swap(_Self& __x) { _M_t.swap(__x._M_t); }
00354 
00355   // insert/erase
00356   iterator insert(const value_type& __x) { return _M_t.insert_equal(__x); }
00357   iterator insert(iterator __pos, const value_type& __x) { return _M_t.insert_equal(__pos, __x); }
00358 #if defined (_STLP_MEMBER_TEMPLATES)
00359   template <class _InputIterator>
00360   void insert(_InputIterator __first, _InputIterator __last)
00361   { _M_t.insert_equal(__first, __last); }
00362 #else
00363   void insert(const value_type* __first, const value_type* __last)
00364   { _M_t.insert_equal(__first, __last); }
00365   void insert(const_iterator __first, const_iterator __last)
00366   { _M_t.insert_equal(__first, __last); }
00367 #endif /* _STLP_MEMBER_TEMPLATES */
00368   void erase(iterator __pos) { _M_t.erase(__pos); }
00369   size_type erase(const key_type& __x) { return _M_t.erase(__x); }
00370   void erase(iterator __first, iterator __last) { _M_t.erase(__first, __last); }
00371   void clear() { _M_t.clear(); }
00372 
00373   // multimap operations:
00374 
00375   _STLP_TEMPLATE_FOR_CONT_EXT
00376   iterator find(const _KT& __x) { return _M_t.find(__x); }
00377   _STLP_TEMPLATE_FOR_CONT_EXT
00378   const_iterator find(const _KT& __x) const { return _M_t.find(__x); }
00379   _STLP_TEMPLATE_FOR_CONT_EXT
00380   size_type count(const _KT& __x) const { return _M_t.count(__x); }
00381   _STLP_TEMPLATE_FOR_CONT_EXT
00382   iterator lower_bound(const _KT& __x) { return _M_t.lower_bound(__x); }
00383   _STLP_TEMPLATE_FOR_CONT_EXT
00384   const_iterator lower_bound(const _KT& __x) const { return _M_t.lower_bound(__x); }
00385   _STLP_TEMPLATE_FOR_CONT_EXT
00386   iterator upper_bound(const _KT& __x) { return _M_t.upper_bound(__x); }
00387   _STLP_TEMPLATE_FOR_CONT_EXT
00388   const_iterator upper_bound(const _KT& __x) const { return _M_t.upper_bound(__x); }
00389   _STLP_TEMPLATE_FOR_CONT_EXT
00390   pair<iterator,iterator> equal_range(const _KT& __x)
00391   { return _M_t.equal_range(__x); }
00392   _STLP_TEMPLATE_FOR_CONT_EXT
00393   pair<const_iterator,const_iterator> equal_range(const _KT& __x) const
00394   { return _M_t.equal_range(__x); }
00395 };
00396 
00397 #define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _Compare, class _Alloc>
00398 #define _STLP_TEMPLATE_CONTAINER map<_Key,_Tp,_Compare,_Alloc>
00399 #include <stl/_relops_cont.h>
00400 #undef  _STLP_TEMPLATE_CONTAINER
00401 #define _STLP_TEMPLATE_CONTAINER multimap<_Key,_Tp,_Compare,_Alloc>
00402 #include <stl/_relops_cont.h>
00403 #undef  _STLP_TEMPLATE_CONTAINER
00404 #undef  _STLP_TEMPLATE_HEADER
00405 
00406 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00407 template <class _Key, class _Tp, class _Compare, class _Alloc>
00408 struct __move_traits<map<_Key,_Tp,_Compare,_Alloc> > :
00409   _STLP_PRIV __move_traits_aux<typename map<_Key,_Tp,_Compare,_Alloc>::_Rep_type>
00410 {};
00411 
00412 template <class _Key, class _Tp, class _Compare, class _Alloc>
00413 struct __move_traits<multimap<_Key,_Tp,_Compare,_Alloc> > :
00414   _STLP_PRIV __move_traits_aux<typename multimap<_Key,_Tp,_Compare,_Alloc>::_Rep_type>
00415 {};
00416 #endif
00417 
00418 _STLP_END_NAMESPACE
00419 
00420 #endif /* _STLP_INTERNAL_MAP_H */
00421 
00422 // Local Variables:
00423 // mode:C++
00424 // End:
00425 



Generated on Mon Mar 10 15:32:31 2008 by  doxygen 1.5.1