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

  1. // utility standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _UTILITY_
  8. #define _UTILITY_
  9. #include <iosfwd>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14. _STD_BEGIN
  15.         // TEMPLATE STRUCT pair
  16. template<class _T1, class _T2> struct pair {
  17.     typedef _T1 first_type;
  18.     typedef _T2 second_type;
  19.     pair()
  20.         : first(_T1()), second(_T2()) {}
  21.     pair(const _T1& _V1, const _T2& _V2)
  22.         : first(_V1), second(_V2) {}
  23.     template<class U, class V> pair(const pair<U, V> &p)
  24.         : first(p.first), second(p.second) {}
  25.     _T1 first;
  26.     _T2 second;
  27.     };
  28. template<class _T1, class _T2> inline
  29.     bool __cdecl operator==(const pair<_T1, _T2>& _X,
  30.         const pair<_T1, _T2>& _Y)
  31.     {return (_X.first == _Y.first && _X.second == _Y.second); }
  32. template<class _T1, class _T2> inline
  33.     bool __cdecl operator!=(const pair<_T1, _T2>& _X,
  34.         const pair<_T1, _T2>& _Y)
  35.     {return (!(_X == _Y)); }
  36. template<class _T1, class _T2> inline
  37.     bool __cdecl operator<(const pair<_T1, _T2>& _X,
  38.         const pair<_T1, _T2>& _Y)
  39.     {return (_X.first < _Y.first ||
  40.         !(_Y.first < _X.first) && _X.second < _Y.second); }
  41. template<class _T1, class _T2> inline
  42.     bool __cdecl operator>(const pair<_T1, _T2>& _X,
  43.         const pair<_T1, _T2>& _Y)
  44.     {return (_Y < _X); }
  45. template<class _T1, class _T2> inline
  46.     bool __cdecl operator<=(const pair<_T1, _T2>& _X,
  47.         const pair<_T1, _T2>& _Y)
  48.     {return (!(_Y < _X)); }
  49. template<class _T1, class _T2> inline
  50.     bool __cdecl operator>=(const pair<_T1, _T2>& _X,
  51.         const pair<_T1, _T2>& _Y)
  52.     {return (!(_X < _Y)); }
  53. template<class _T1, class _T2> inline
  54.     pair<_T1, _T2> __cdecl make_pair(const _T1& _X, const _T2& _Y)
  55.     {return (pair<_T1, _T2>(_X, _Y)); }
  56.         // ITERATOR TAGS (from <iterator>)
  57. struct input_iterator_tag {};
  58. struct output_iterator_tag {};
  59. struct forward_iterator_tag
  60.     : public input_iterator_tag {};
  61. struct bidirectional_iterator_tag
  62.     : public forward_iterator_tag {};
  63. struct random_access_iterator_tag
  64.     : public bidirectional_iterator_tag  {};
  65.         // TEMPLATE CLASS iterator (from <iterator>)
  66. template<class _C, class _Ty, class _D = ptrdiff_t>
  67.     struct iterator {
  68.     typedef _C iterator_category;
  69.     typedef _Ty value_type;
  70.     typedef _D distance_type;
  71.     };
  72. template<class _Ty, class _D>
  73.     struct _Bidit : public iterator<bidirectional_iterator_tag,
  74.         _Ty, _D> {};
  75. template<class _Ty, class _D>
  76.     struct _Ranit : public iterator<random_access_iterator_tag,
  77.         _Ty, _D> {};
  78.         // TEMPLATE CLASS iterator_traits (from <iterator>)
  79. template<class _It>
  80.     struct iterator_traits {
  81.     typedef _It::iterator_category iterator_category;
  82.     typedef _It::value_type value_type;
  83.     typedef _It::distance_type distance_type;
  84.     };
  85.         // TEMPLATE FUNCTION _Iter_cat (from <iterator>)
  86. template<class _C, class _Ty, class _D> inline
  87.     _C __cdecl _Iter_cat(const iterator<_C, _Ty, _D>&)
  88.     {_C _IterCatTag;
  89.      _C* _pIterCatTag;
  90.     _pIterCatTag = &_IterCatTag;    // Workaround for C4700 warning
  91.     return (_IterCatTag); }
  92. template<class _Ty> inline
  93.     random_access_iterator_tag __cdecl _Iter_cat(const _Ty *)
  94.     {random_access_iterator_tag _RandIterTag;
  95.      random_access_iterator_tag* _pRandIterTag;
  96.     _pRandIterTag = &_RandIterTag;    // Workaround for C4700 warning
  97.     return (_RandIterTag); }
  98.         // TEMPLATE FUNCTION _Distance
  99. template<class _II> inline
  100.     _CNTSIZ(_II) __cdecl distance(_II _F, _II _L)
  101.     {_CNTSIZ(_II) _N = 0;
  102.     _Distance(_F, _L, _N, _Iter_cat(_F));
  103.     return (_N); }
  104. template<class _II, class _D> inline
  105.     void __cdecl _Distance(_II _F, _II _L, _D& _N)
  106.     {_Distance(_F, _L, _N, _Iter_cat(_F)); }
  107. template<class _II, class _D> inline
  108.     void __cdecl _Distance(_II _F, _II _L, _D& _N, input_iterator_tag)
  109.     {for (; _F != _L; ++_F)
  110.         ++_N; }
  111. template<class _II, class _D> inline
  112.     void __cdecl _Distance(_II _F, _II _L, _D& _N, forward_iterator_tag)
  113.     {for (; _F != _L; ++_F)
  114.         ++_N; }
  115. template<class _II, class _D> inline
  116.     void __cdecl _Distance(_II _F, _II _L, _D& _N,
  117.         bidirectional_iterator_tag)
  118.     {for (; _F != _L; ++_F)
  119.         ++_N; }
  120. template<class _RI, class _D> inline
  121.     void __cdecl _Distance(_RI _F, _RI _L, _D& _N,
  122.         random_access_iterator_tag)
  123.     {_N += _L - _F; }
  124.         // TEMPLATE CLASS reverse_iterator (from <iterator>)
  125. template<class _RI,
  126.     class _Ty,
  127.     class _Rt = _Ty&,
  128.     class _Pt = _Ty *,
  129.     class _D = ptrdiff_t>
  130.     class reverse_iterator : public _Ranit<_Ty, _D> {
  131. public:
  132.     typedef reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> _Myt;
  133.     typedef _RI iter_type;
  134.     typedef _Rt reference_type;
  135.     typedef _Pt pointer_type;
  136.     reverse_iterator()
  137.         {}
  138.     explicit reverse_iterator(_RI _X)
  139.         : current(_X) {}
  140.     _RI base() const
  141.         {return (current); }
  142.     _Rt operator*() const
  143.         {return (*(current - 1)); }
  144.     _Pt operator->() const
  145.         {return (&**this); }
  146.     _Myt& operator++()
  147.         {--current;
  148.         return (*this); }
  149.     _Myt operator++(int)
  150.         {_Myt _Tmp = *this;
  151.         --current;
  152.         return (_Tmp); }
  153.     _Myt& operator--()
  154.         {++current;
  155.         return (*this); }
  156.     _Myt operator--(int)
  157.         {_Myt _Tmp = *this;
  158.         ++current;
  159.         return (_Tmp); }
  160.     _Myt& operator+=(_D _N)
  161.         {current -= _N;
  162.         return (*this); }
  163.     _Myt operator+(_D _N) const
  164.         {return (_Myt(current - _N)); }
  165.     _Myt& operator-=(_D _N)
  166.         {current += _N;
  167.         return (*this); }
  168.     _Myt operator-(_D _N) const
  169.         {return (_Myt(current + _N)); }
  170.     _Rt operator[](_D _N) const
  171.         {return (*(*this + _N)); }
  172. protected:
  173.     _RI current;
  174.     };
  175. template<class _RI, class _Ty, class _Rt, class _Pt,
  176.     class _D> inline
  177.     bool __cdecl operator==(
  178.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  179.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  180.     {return (_X.base() == _Y.base()); }
  181. template<class _RI, class _Ty, class _Rt, class _Pt,
  182.     class _D> inline
  183.     bool __cdecl operator!=(
  184.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  185.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  186.     {return (!(_X == _Y)); }
  187. template<class _RI, class _Ty, class _Rt, class _Pt,
  188.     class _D> inline
  189.     bool __cdecl operator<(
  190.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  191.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  192.     {return (_Y.base() < _X.base()); }
  193. template<class _RI, class _Ty, class _Rt, class _Pt,
  194.     class _D> inline
  195.     bool __cdecl operator>(
  196.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  197.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  198.     {return (_Y < _X); }
  199. template<class _RI, class _Ty, class _Rt, class _Pt,
  200.     class _D> inline
  201.     bool __cdecl operator<=(
  202.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  203.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  204.     {return (!(_Y < _X)); }
  205. template<class _RI, class _Ty, class _Rt, class _Pt,
  206.     class _D> inline
  207.     bool __cdecl operator>=(
  208.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  209.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  210.     {return (!(_X < _Y)); }
  211. template<class _RI, class _Ty, class _Rt, class _Pt,
  212.     class _D> inline
  213.     _D __cdecl operator-(
  214.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _X,
  215.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  216.     {return (_Y.base() - _X.base()); }
  217. template<class _RI, class _Ty, class _Rt, class _Pt,
  218.     class _D> inline
  219.     reverse_iterator<_RI, _Ty, _Rt, _Pt, _D> __cdecl operator+(_D _N,
  220.         const reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>& _Y)
  221.     {return (reverse_iterator<_RI, _Ty, _Rt, _Pt, _D>(
  222.         _Y.base() - _N)); }
  223.         // TEMPLATE CLASS istreambuf_iterator (from <iterator>)
  224. template<class _E, class _Tr = char_traits<_E> >
  225.     class istreambuf_iterator
  226.         : public iterator<input_iterator_tag, _E, _Tr::off_type> {
  227. public:
  228.     typedef istreambuf_iterator<_E, _Tr> _Myt;
  229.     typedef _E char_type;
  230.     typedef _Tr traits_type;
  231.     typedef _Tr::int_type int_type;
  232.     typedef basic_streambuf<_E, _Tr> streambuf_type;
  233.     typedef basic_istream<_E, _Tr> istream_type;
  234.     istreambuf_iterator(streambuf_type *_Sb = 0) _THROW0()
  235.         : _Sbuf(_Sb), _Got(_Sb == 0) {}
  236.     istreambuf_iterator(istream_type& _I) _THROW0()
  237.         : _Sbuf(_I.rdbuf()), _Got(_I.rdbuf() == 0) {}
  238.     const _E& operator*() const
  239.         {if (!_Got)
  240.             ((_Myt *)this)->_Peek();
  241.         return (_Val); }
  242.     const _E *operator->() const
  243.         {return (&**this); }
  244.     _Myt& operator++()
  245.         {_Inc();
  246.         return (*this); }
  247.     _Myt operator++(int)
  248.         {if (!_Got)
  249.             _Peek();
  250.         _Myt _Tmp = *this;
  251.         _Inc();
  252.         return (_Tmp); }
  253.     bool equal(const _Myt& _X) const
  254.         {if (!_Got)
  255.             ((_Myt *)this)->_Peek();
  256.         if (!_X._Got)
  257.             ((_Myt *)&_X)->_Peek();
  258.         return (_Sbuf == 0 && _X._Sbuf == 0
  259.             || _Sbuf != 0 && _X._Sbuf != 0); }
  260. private:
  261.     void _Inc()
  262.         {if (_Sbuf == 0
  263.             || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sbumpc()))
  264.             _Sbuf = 0, _Got = true;
  265.         else
  266.             _Got = false; }
  267.     _E _Peek()
  268.         {int_type _C;
  269.         if (_Sbuf == 0
  270.             || _Tr::eq_int_type(_Tr::eof(), _C = _Sbuf->sgetc()))
  271.             _Sbuf = 0;
  272.         else
  273.             _Val = _Tr::to_char_type(_C);
  274.         _Got = true;
  275.         return (_Val); }
  276.     streambuf_type *_Sbuf;
  277.     bool _Got;
  278.     _E _Val;
  279.     };
  280. template<class _E, class _Tr> inline
  281.     bool __cdecl operator==(const istreambuf_iterator<_E, _Tr>& _X,
  282.         const istreambuf_iterator<_E, _Tr>& _Y)
  283.     {return (_X.equal(_Y)); }
  284. template<class _E, class _Tr> inline
  285.     bool __cdecl operator!=(const istreambuf_iterator<_E, _Tr>& _X,
  286.         const istreambuf_iterator<_E, _Tr>& _Y)
  287.     {return (!(_X == _Y)); }
  288.         // TEMPLATE CLASS ostreambuf_iterator (from <iterator>)
  289. template<class _E, class _Tr = char_traits<_E> >
  290.     class ostreambuf_iterator
  291.         : public iterator<output_iterator_tag, void, void> {
  292.     typedef ostreambuf_iterator<_E, _Tr> _Myt;
  293. public:
  294.     typedef _E char_type;
  295.     typedef _Tr traits_type;
  296.     typedef basic_streambuf<_E, _Tr> streambuf_type;
  297.     typedef basic_ostream<_E, _Tr> ostream_type;
  298.     ostreambuf_iterator(streambuf_type *_Sb) _THROW0()
  299.         : _Failed(false), _Sbuf(_Sb) {}
  300.     ostreambuf_iterator(ostream_type& _O) _THROW0()
  301.         : _Failed(false), _Sbuf(_O.rdbuf()) {}
  302.     _Myt& operator=(_E _X)
  303.         {if (_Sbuf == 0
  304.             || _Tr::eq_int_type(_Tr::eof(), _Sbuf->sputc(_X)))
  305.             _Failed = true;
  306.         return (*this); }
  307.     _Myt& operator*()
  308.         {return (*this); }
  309.     _Myt& operator++()
  310.         {return (*this); }
  311.     _Myt& operator++(int)
  312.         {return (*this); }
  313.     bool failed() const _THROW0()
  314.         {return (_Failed); }
  315. private:
  316.     bool _Failed;
  317.     streambuf_type *_Sbuf;
  318.     };
  319.         // TEMPLATE OPERATORS
  320.     namespace rel_ops {
  321. template<class _Ty> inline
  322.     bool __cdecl operator!=(const _Ty& _X, const _Ty& _Y)
  323.     {return (!(_X == _Y)); }
  324. template<class _Ty> inline
  325.     bool __cdecl operator>(const _Ty& _X, const _Ty& _Y)
  326.     {return (_Y < _X); }
  327. template<class _Ty> inline
  328.     bool __cdecl operator<=(const _Ty& _X, const _Ty& _Y)
  329.     {return (!(_Y < _X)); }
  330. template<class _Ty> inline
  331.     bool __cdecl operator>=(const _Ty& _X, const _Ty& _Y)
  332.     {return (!(_X < _Y)); }
  333.     _STD_END
  334. _STD_END
  335. #ifdef  _MSC_VER
  336. #pragma pack(pop)
  337. #endif  /* _MSC_VER */
  338.  
  339. #endif /* _UTILITY_ */
  340.  
  341. /*
  342.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  343.  * Consult your license regarding permissions and restrictions.
  344.  */
  345.  
  346. /*
  347.  * This file is derived from software bearing the following
  348.  * restrictions:
  349.  *
  350.  * Copyright (c) 1994
  351.  * Hewlett-Packard Company
  352.  *
  353.  * Permission to use, copy, modify, distribute and sell this
  354.  * software and its documentation for any purpose is hereby
  355.  * granted without fee, provided that the above copyright notice
  356.  * appear in all copies and that both that copyright notice and
  357.  * this permission notice appear in supporting documentation.
  358.  * Hewlett-Packard Company makes no representations about the
  359.  * suitability of this software for any purpose. It is provided
  360.  * "as is" without express or implied warranty.
  361.  */
  362.