home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / map < prev    next >
Text File  |  1998-06-16  |  11KB  |  342 lines

  1. // map standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _MAP_
  8. #define _MAP_
  9. #include <functional>
  10. #include <xtree>
  11.  
  12. #ifdef  _MSC_VER
  13. #pragma pack(push,8)
  14. #endif  /* _MSC_VER */
  15.  
  16. _STD_BEGIN
  17.         // TEMPLATE CLASS map
  18. template<class _K, class _Ty, class _Pr = less<_K>,
  19.     class _A = allocator<_Ty> >
  20.     class map {
  21. public:
  22.     typedef map<_K, _Ty, _Pr, _A> _Myt;
  23.     typedef pair<const _K, _Ty> value_type;
  24.     struct _Kfn : public unary_function<value_type, _K> {
  25.         const _K& operator()(const value_type& _X) const
  26.         {return (_X.first); }
  27.         };
  28.     class value_compare
  29.         : public binary_function<value_type, value_type, bool> {
  30.         friend class map<_K, _Ty, _Pr, _A>;
  31.     public:
  32.         bool operator()(const value_type& _X,
  33.             const value_type& _Y) const
  34.             {return (comp(_X.first, _Y.first)); }
  35.     _PROTECTED:
  36.         value_compare(_Pr _Pred)
  37.             : comp(_Pred) {}
  38.         _Pr comp;
  39.         };
  40.     typedef _K key_type;
  41.     typedef _Ty referent_type;
  42.     typedef _Pr key_compare;
  43.     typedef _A allocator_type;
  44.     typedef _A::reference _Tref;
  45.     typedef _Tree<_K, value_type, _Kfn, _Pr, _A> _Imp;
  46.     typedef _Imp::size_type size_type;
  47.     typedef _Imp::difference_type difference_type;
  48.     typedef _Imp::reference reference;
  49.     typedef _Imp::const_reference const_reference;
  50.     typedef _Imp::iterator iterator;
  51.     typedef _Imp::const_iterator const_iterator;
  52.     typedef _Imp::reverse_iterator reverse_iterator;
  53.     typedef _Imp::const_reverse_iterator const_reverse_iterator;
  54.     typedef pair<iterator, bool> _Pairib;
  55.     typedef pair<iterator, iterator> _Pairii;
  56.     typedef pair<const_iterator, const_iterator> _Paircc;
  57.     explicit map(const _Pr& _Pred = _Pr(), const _A& _Al = _A())
  58.         : _Tr(_Pred, false, _Al) {}
  59.     typedef const value_type *_It;
  60.     map(_It _F, _It _L, const _Pr& _Pred = _Pr(),
  61.         const _A& _Al = _A())
  62.         : _Tr(_Pred, false, _Al)
  63.         {for (; _F != _L; ++_F)
  64.             _Tr.insert(*_F); }
  65.     _Myt& operator=(const _Myt& _X)
  66.         {_Tr = _X._Tr;
  67.         return (*this); }
  68.     iterator begin()
  69.         {return (_Tr.begin()); }
  70.     const_iterator begin() const
  71.         {return (_Tr.begin()); }
  72.     iterator end()
  73.         {return (_Tr.end()); }
  74.     const_iterator end() const
  75.         {return (_Tr.end()); }
  76.     reverse_iterator rbegin()
  77.         {return (_Tr.rbegin()); }
  78.     const_reverse_iterator rbegin() const
  79.         {return (_Tr.rbegin()); }
  80.     reverse_iterator rend()
  81.         {return (_Tr.rend()); }
  82.     const_reverse_iterator rend() const
  83.         {return (_Tr.rend()); }
  84.     size_type size() const
  85.         {return (_Tr.size()); }
  86.     size_type max_size() const
  87.         {return (_Tr.max_size()); }
  88.     bool empty() const
  89.         {return (_Tr.empty()); }
  90.     _A get_allocator() const
  91.         {return (_Tr.get_allocator()); }
  92.     _Tref operator[](const key_type& _Kv)
  93.         {iterator _P = insert(value_type(_Kv, _Ty())).first;
  94.         return ((*_P).second); }
  95.     _Pairib insert(const value_type& _X)
  96.         {_Imp::_Pairib _Ans = _Tr.insert(_X);
  97.         return (_Pairib(_Ans.first, _Ans.second)); }
  98.     iterator insert(iterator _P, const value_type& _X)
  99.         {return (_Tr.insert((_Imp::iterator&)_P, _X)); }
  100.     void insert(_It _F, _It _L)
  101.         {for (; _F != _L; ++_F)
  102.             _Tr.insert(*_F); }
  103.     iterator erase(iterator _P)
  104.         {return (_Tr.erase((_Imp::iterator&)_P)); }
  105.     iterator erase(iterator _F, iterator _L)
  106.         {return (_Tr.erase((_Imp::iterator&)_F,
  107.             (_Imp::iterator&)_L)); }
  108.     size_type erase(const _K& _Kv)
  109.         {return (_Tr.erase(_Kv)); }
  110.     void clear()
  111.         {_Tr.clear(); }
  112.     void swap(_Myt& _X)
  113.         {std::swap(_Tr, _X._Tr); }
  114.     friend void swap(_Myt& _X, _Myt& _Y)
  115.         {_X.swap(_Y); }
  116.     key_compare key_comp() const
  117.         {return (_Tr.key_comp()); }
  118.     value_compare value_comp() const
  119.         {return (value_compare(_Tr.key_comp())); }
  120.     iterator find(const _K& _Kv)
  121.         {return (_Tr.find(_Kv)); }
  122.     const_iterator find(const _K& _Kv) const
  123.         {return (_Tr.find(_Kv)); }
  124.     size_type count(const _K& _Kv) const
  125.         {return (_Tr.count(_Kv)); }
  126.     iterator lower_bound(const _K& _Kv)
  127.         {return (_Tr.lower_bound(_Kv)); }
  128.     const_iterator lower_bound(const _K& _Kv) const
  129.         {return (_Tr.lower_bound(_Kv)); }
  130.     iterator upper_bound(const _K& _Kv)
  131.         {return (_Tr.upper_bound(_Kv)); }
  132.     const_iterator upper_bound(const _K& _Kv) const
  133.         {return (_Tr.upper_bound(_Kv)); }
  134.     _Pairii equal_range(const _K& _Kv)
  135.         {return (_Tr.equal_range(_Kv)); }
  136.     _Paircc equal_range(const _K& _Kv) const
  137.         {return (_Tr.equal_range(_Kv)); }
  138. protected:
  139.     _Imp _Tr;
  140.     };
  141.         // map TEMPLATE OPERATORS
  142. template<class _K, class _Ty, class _Pr, class _A> inline
  143.     bool operator==(const map<_K, _Ty, _Pr, _A>& _X,
  144.         const map<_K, _Ty, _Pr, _A>& _Y)
  145.     {return (_X.size() == _Y.size()
  146.         && equal(_X.begin(), _X.end(), _Y.begin())); }
  147. template<class _K, class _Ty, class _Pr, class _A> inline
  148.     bool operator!=(const map<_K, _Ty, _Pr, _A>& _X,
  149.         const map<_K, _Ty, _Pr, _A>& _Y)
  150.     {return (!(_X == _Y)); }
  151. template<class _K, class _Ty, class _Pr, class _A> inline
  152.     bool operator<(const map<_K, _Ty, _Pr, _A>& _X,
  153.         const map<_K, _Ty, _Pr, _A>& _Y)
  154.     {return (lexicographical_compare(_X.begin(), _X.end(),
  155.         _Y.begin(), _Y.end())); }
  156. template<class _K, class _Ty, class _Pr, class _A> inline
  157.     bool operator>(const map<_K, _Ty, _Pr, _A>& _X,
  158.         const map<_K, _Ty, _Pr, _A>& _Y)
  159.     {return (_Y < _X); }
  160. template<class _K, class _Ty, class _Pr, class _A> inline
  161.     bool operator<=(const map<_K, _Ty, _Pr, _A>& _X,
  162.         const map<_K, _Ty, _Pr, _A>& _Y)
  163.     {return (!(_Y < _X)); }
  164. template<class _K, class _Ty, class _Pr, class _A> inline
  165.     bool operator>=(const map<_K, _Ty, _Pr, _A>& _X,
  166.         const map<_K, _Ty, _Pr, _A>& _Y)
  167.     {return (!(_X < _Y)); }
  168.         // TEMPLATE CLASS multimap
  169. template<class _K, class _Ty, class _Pr = less<_K>,
  170.     class _A = allocator<_Ty> >
  171.     class multimap {
  172. public:
  173.     typedef multimap<_K, _Ty, _Pr, _A> _Myt;
  174.     typedef pair<const _K, _Ty> value_type;
  175.     struct _Kfn : public unary_function<value_type, _K> {
  176.         const _K& operator()(const value_type& _X) const
  177.         {return (_X.first); }
  178.         };
  179.     class value_compare
  180.         : public binary_function<value_type, value_type, bool> {
  181.         friend class map<_K, _Ty, _Pr, _A>;
  182.     public:
  183.         bool operator()(const value_type& _X,
  184.             const value_type& _Y) const
  185.             {return (comp(_X.first, _Y.first)); }
  186.     _PROTECTED:
  187.         value_compare(_Pr _Pred)
  188.             : comp(_Pred) {}
  189.         _Pr comp;
  190.         };
  191.     typedef _K key_type;
  192.     typedef _Ty referent_type;
  193.     typedef _Pr key_compare;
  194.     typedef _A allocator_type;
  195.     typedef _Tree<_K, value_type, _Kfn, _Pr, _A> _Imp;
  196.     typedef _Imp::size_type size_type;
  197.     typedef _Imp::difference_type difference_type;
  198.     typedef _Imp::reference reference;
  199.     typedef _Imp::const_reference const_reference;
  200.     typedef _Imp::iterator iterator;
  201.     typedef _Imp::const_iterator const_iterator;
  202.     typedef _Imp::reverse_iterator reverse_iterator;
  203.     typedef _Imp::const_reverse_iterator const_reverse_iterator;
  204.     typedef pair<iterator, iterator> _Pairii;
  205.     typedef pair<const_iterator, const_iterator> _Paircc;
  206.     explicit multimap(const _Pr& _Pred = _Pr(),
  207.         const _A& _Al = _A())
  208.         : _Tr(_Pred, true, _Al) {}
  209.     typedef const value_type *_It;
  210.     multimap(_It _F, _It _L, const _Pr& _Pred = _Pr(),
  211.             const _A& _Al = _A())
  212.         : _Tr(_Pred, true, _Al)
  213.         {for (; _F != _L; ++_F)
  214.             _Tr.insert(*_F); }
  215.     _Myt& operator=(const _Myt& _X)
  216.         {_Tr = _X._Tr;
  217.         return (*this); }
  218.     iterator begin()
  219.         {return (_Tr.begin()); }
  220.     const_iterator begin() const
  221.         {return (_Tr.begin()); }
  222.     iterator end()
  223.         {return (_Tr.end()); }
  224.     const_iterator end() const
  225.         {return (_Tr.end()); }
  226.     reverse_iterator rbegin()
  227.         {return (_Tr.rbegin()); }
  228.     const_reverse_iterator rbegin() const
  229.         {return (_Tr.rbegin()); }
  230.     reverse_iterator rend()
  231.         {return (_Tr.rend()); }
  232.     const_reverse_iterator rend() const
  233.         {return (_Tr.rend()); }
  234.     size_type size() const
  235.         {return (_Tr.size()); }
  236.     size_type max_size() const
  237.         {return (_Tr.max_size()); }
  238.     bool empty() const
  239.         {return (_Tr.empty()); }
  240.     _A get_allocator() const
  241.         {return (_Tr.get_allocator()); }
  242.     iterator insert(const value_type& _X)
  243.         {return (_Tr.insert(_X).first); }
  244.     iterator insert(iterator _P, const value_type& _X)
  245.         {return (_Tr.insert((_Imp::iterator&)_P, _X)); }
  246.     void insert(_It _F, _It _L)
  247.         {for (; _F != _L; ++_F)
  248.             _Tr.insert(*_F); }
  249.     iterator erase(iterator _P)
  250.         {return (_Tr.erase((_Imp::iterator&)_P)); }
  251.     iterator erase(iterator _F, iterator _L)
  252.         {return (_Tr.erase((_Imp::iterator&)_F,
  253.             (_Imp::iterator&)_L)); }
  254.     size_type erase(const _K& _Kv = _K())
  255.         {return (_Tr.erase(_Kv)); }
  256.     void clear()
  257.         {_Tr.clear(); }
  258.     void swap(_Myt& _X)
  259.         {std::swap(_Tr, _X._Tr); }
  260.     friend void swap(_Myt& _X, _Myt& _Y)
  261.         {_X.swap(_Y); }
  262.     key_compare key_comp() const
  263.         {return (_Tr.key_comp()); }
  264.     value_compare value_comp() const
  265.         {return (value_compare(_Tr.key_comp())); }
  266.     iterator find(const _K& _Kv)
  267.         {return (_Tr.find(_Kv)); }
  268.     const_iterator find(const _K& _Kv) const
  269.         {return (_Tr.find(_Kv)); }
  270.     size_type count(const _K& _Kv) const
  271.         {return (_Tr.count(_Kv)); }
  272.     iterator lower_bound(const _K& _Kv)
  273.         {return (_Tr.lower_bound(_Kv)); }
  274.     const_iterator lower_bound(const _K& _Kv) const
  275.         {return (_Tr.lower_bound(_Kv)); }
  276.     iterator upper_bound(const _K& _Kv)
  277.         {return (_Tr.upper_bound(_Kv)); }
  278.     const_iterator upper_bound(const _K& _Kv) const
  279.         {return (_Tr.upper_bound(_Kv)); }
  280.     _Pairii equal_range(const _K& _Kv)
  281.         {return (_Tr.equal_range(_Kv)); }
  282.     _Paircc equal_range(const _K& _Kv) const
  283.         {return (_Tr.equal_range(_Kv)); }
  284. protected:
  285.     _Imp _Tr;
  286.     };
  287.         // multimap TEMPLATE OPERATORS
  288. template<class _K, class _Ty, class _Pr, class _A> inline
  289.     bool operator==(const multimap<_K, _Ty, _Pr, _A>& _X,
  290.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  291.     {return (_X.size() == _Y.size()
  292.         && equal(_X.begin(), _X.end(), _Y.begin())); }
  293. template<class _K, class _Ty, class _Pr, class _A> inline
  294.     bool operator!=(const multimap<_K, _Ty, _Pr, _A>& _X,
  295.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  296.     {return (!(_X == _Y)); }
  297. template<class _K, class _Ty, class _Pr, class _A> inline
  298.     bool operator<(const multimap<_K, _Ty, _Pr, _A>& _X,
  299.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  300.     {return (lexicographical_compare(_X.begin(), _X.end(),
  301.         _Y.begin(), _Y.end())); }
  302. template<class _K, class _Ty, class _Pr, class _A> inline
  303.     bool operator>(const multimap<_K, _Ty, _Pr, _A>& _X,
  304.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  305.     {return (_Y < _X); }
  306. template<class _K, class _Ty, class _Pr, class _A> inline
  307.     bool operator<=(const multimap<_K, _Ty, _Pr, _A>& _X,
  308.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  309.     {return (!(_Y < _X)); }
  310. template<class _K, class _Ty, class _Pr, class _A> inline
  311.     bool operator>=(const multimap<_K, _Ty, _Pr, _A>& _X,
  312.         const multimap<_K, _Ty, _Pr, _A>& _Y)
  313.     {return (!(_X < _Y)); }
  314. _STD_END
  315. #ifdef  _MSC_VER
  316. #pragma pack(pop)
  317. #endif  /* _MSC_VER */
  318.  
  319. #endif /* _MAP_ */
  320.  
  321. /*
  322.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  323.  * Consult your license regarding permissions and restrictions.
  324.  */
  325.  
  326. /*
  327.  * This file is derived from software bearing the following
  328.  * restrictions:
  329.  *
  330.  * Copyright (c) 1994
  331.  * Hewlett-Packard Company
  332.  *
  333.  * Permission to use, copy, modify, distribute and sell this
  334.  * software and its documentation for any purpose is hereby
  335.  * granted without fee, provided that the above copyright notice
  336.  * appear in all copies and that both that copyright notice and
  337.  * this permission notice appear in supporting documentation.
  338.  * Hewlett-Packard Company makes no representations about the
  339.  * suitability of this software for any purpose. It is provided
  340.  * "as is" without express or implied warranty.
  341.  */
  342.