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

  1. // numeric standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _NUMERIC_
  8. #define _NUMERIC_
  9. #include <iterator>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14. _STD_BEGIN
  15.         // TEMPLATE FUNCTION accumulate
  16. template<class _II, class _Ty> inline
  17.     _Ty accumulate(_II _F, _II _L, _Ty _V)
  18.     {for (; _F != _L; ++_F)
  19.         _V = _V + *_F;
  20.     return (_V); }
  21.         // TEMPLATE FUNCTION accumulate WITH BINOP
  22. template<class _II, class _Ty, class _Bop> inline
  23.     _Ty accumulate(_II _F, _II _L, _Ty _V, _Bop _B)
  24.     {for (; _F != _L; ++_F)
  25.         _V = _B(_V, *_F);
  26.     return (_V); }
  27.         // TEMPLATE FUNCTION inner_product
  28. template<class _II1, class _II2, class _Ty> inline
  29.     _Ty inner_product(_II1 _F, _II1 _L, _II2 _X, _Ty _V)
  30.     {for (; _F != _L; ++_F, ++_X)
  31.         _V = _V + *_F * *_X;
  32.     return (_V); }
  33.         // TEMPLATE FUNCTION inner_product WITH BINOPS
  34. template<class _II1, class _II2, class _Ty,
  35.     class _Bop1, class _Bop2> inline
  36.     _Ty inner_product(_II1 _F, _II1 _L, _II2 _X, _Ty _V,
  37.         _Bop1 _B1, _Bop2 _B2)
  38.     {for (; _F != _L; ++_F, ++_X)
  39.         _V = _B1(_V, _B2(*_F, *_X));
  40.     return (_V); }
  41.         // TEMPLATE FUNCTION partial_sum
  42. template<class _II, class _OI> inline
  43.     _OI partial_sum(_II _F, _II _L, _OI _X)
  44.     {return (_F == _L ? _X
  45.         : _Partial_sum(_F, _L, _X, _Val_type(_F))); }
  46. template<class _II, class _OI, class _Ty> inline
  47.     _OI _Partial_sum(_II _F, _II _L, _OI _X, _Ty *)
  48.     {_Ty _V = *_F;
  49.     for (*_X = _V; ++_F != _L; *++_X = _V)
  50.         _V = _V + *_F;
  51.     return (++_X); }
  52.         // TEMPLATE FUNCTION partial_sum WITH BINOP
  53. template<class _II, class _OI, class _Bop> inline
  54.     _OI partial_sum(_II _F, _II _L, _OI _X, _Bop _B)
  55.     {return (_F == _L ? _X
  56.         : _Partial_sum(_F, _L, _X, _B, _Val_type(_F))); }
  57. template<class _II, class _OI, class _Bop, class _Ty> inline
  58.     _OI _Partial_sum(_II _F, _II _L, _OI _X, _Bop _B, _Ty *)
  59.     {_Ty _V = *_F;
  60.     for (*_X = _V; ++_F != _L; *++_X = _V)
  61.         _V = _B(_V, *_F);
  62.     return (++_X); }
  63.         // TEMPLATE FUNCTION adjacent_difference
  64. template<class _II, class _OI> inline
  65.     _OI adjacent_difference(_II _F, _II _L, _OI _X)
  66.     {return (_F == _L ? _X
  67.         : _Adjacent_difference(_F, _L, _X, _Val_type(_F))); }
  68. template<class _II, class _OI, class _Ty> inline
  69.     _OI _Adjacent_difference(_II _F, _II _L, _OI _X, _Ty *)
  70.     {_Ty _V = *_F;
  71.     for (*_X = _V; ++_F != _L; )
  72.         {_Ty _Tmp = *_F;
  73.         *++_X = _Tmp - _V;
  74.         _V = _Tmp; }
  75.     return (++_X); }
  76.         // TEMPLATE FUNCTION adjacent_difference WITH BINOP
  77. template<class _II, class _OI, class _Bop> inline
  78.     _OI adjacent_difference(_II _F, _II _L, _OI _X, _Bop _B)
  79.     {return (_F == _L ? _X
  80.         : _Adjacent_difference(_F, _L, _X, _B, _Val_type(_F))); }
  81. template<class _II, class _OI, class _Bop, class _Ty> inline
  82.     _OI _Adjacent_difference(_II _F, _II _L, _OI _X,
  83.         _Bop _B, _Ty *)
  84.     {_Ty _V = *_F;
  85.     for (*_X = _V; ++_F != _L; )
  86.         {_Ty _Tmp = *_F;
  87.         *++_X = _B(_Tmp, _V);
  88.         _V = _Tmp; }
  89.     return (++_X); }
  90. _STD_END
  91. #ifdef  _MSC_VER
  92. #pragma pack(pop)
  93. #endif  /* _MSC_VER */
  94.  
  95. #endif /* _NUMERIC_ */
  96.  
  97. /*
  98.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  99.  * Consult your license regarding permissions and restrictions.
  100.  */
  101.  
  102. /*
  103.  * This file is derived from software bearing the following
  104.  * restrictions:
  105.  *
  106.  * Copyright (c) 1994
  107.  * Hewlett-Packard Company
  108.  *
  109.  * Permission to use, copy, modify, distribute and sell this
  110.  * software and its documentation for any purpose is hereby
  111.  * granted without fee, provided that the above copyright notice
  112.  * appear in all copies and that both that copyright notice and
  113.  * this permission notice appear in supporting documentation.
  114.  * Hewlett-Packard Company makes no representations about the
  115.  * suitability of this software for any purpose. It is provided
  116.  * "as is" without express or implied warranty.
  117.  */
  118.