home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _numeric.h < prev    next >
C/C++ Source or Header  |  2002-02-02  |  6KB  |  187 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Copyright (c) 1996,1997
  7.  * Silicon Graphics Computer Systems, Inc.
  8.  *
  9.  * Copyright (c) 1999 
  10.  * Boris Fomitchev
  11.  *
  12.  * This material is provided "as is", with absolutely no warranty expressed
  13.  * or implied. Any use is at your own risk.
  14.  *
  15.  * Permission to use or copy this software for any purpose is hereby granted 
  16.  * without fee, provided the above notices are retained on all copies.
  17.  * Permission to modify the code and to distribute modified code is granted,
  18.  * provided the above notices are retained, and a notice that the code was
  19.  * modified is included with the above copyright notice.
  20.  *
  21.  */
  22.  
  23. /* NOTE: This is an internal header file, included by other STL headers.
  24.  *   You should not attempt to use it directly.
  25.  */
  26.  
  27.  
  28. #ifndef _STLP_INTERNAL_NUMERIC_H
  29. #define _STLP_INTERNAL_NUMERIC_H
  30.  
  31. #ifndef _STLP_INTERNAL_FUNCTION_H
  32. # include <stl/_function_base.h>
  33. #endif
  34.  
  35. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  36. # include <stl/_iterator_base.h>
  37. #endif
  38.  
  39. _STLP_BEGIN_NAMESPACE
  40.  
  41. template <class _InputIterator, class _Tp>
  42. _STLP_INLINE_LOOP
  43. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init)
  44. {
  45.   _STLP_DEBUG_CHECK(__check_range(__first, __last))
  46.   for ( ; __first != __last; ++__first)
  47.     _Init = _Init + *__first;
  48.   return _Init;
  49. }
  50.  
  51. template <class _InputIterator, class _Tp, class _BinaryOperation>
  52. _STLP_INLINE_LOOP
  53. _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp _Init,
  54.                _BinaryOperation __binary_op)
  55. {
  56.   _STLP_DEBUG_CHECK(__check_range(__first, __last))
  57.   for ( ; __first != __last; ++__first)
  58.     _Init = __binary_op(_Init, *__first);
  59.   return _Init;
  60. }
  61.  
  62. template <class _InputIterator1, class _InputIterator2, class _Tp>
  63. _STLP_INLINE_LOOP
  64. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  65.                   _InputIterator2 __first2, _Tp _Init)
  66. {
  67.   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
  68.   for ( ; __first1 != __last1; ++__first1, ++__first2)
  69.     _Init = _Init + (*__first1 * *__first2);
  70.   return _Init;
  71. }
  72.  
  73. template <class _InputIterator1, class _InputIterator2, class _Tp,
  74.           class _BinaryOperation1, class _BinaryOperation2>
  75. _STLP_INLINE_LOOP
  76. _Tp inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  77.                   _InputIterator2 __first2, _Tp _Init, 
  78.                   _BinaryOperation1 __binary_op1,
  79.                   _BinaryOperation2 __binary_op2)
  80. {
  81.   _STLP_DEBUG_CHECK(__check_range(__first1, __last1))
  82.   for ( ; __first1 != __last1; ++__first1, ++__first2)
  83.     _Init = __binary_op1(_Init, __binary_op2(*__first1, *__first2));
  84.   return _Init;
  85. }
  86.  
  87. template <class _InputIterator, class _OutputIterator, class _Tp,
  88.           class _BinaryOperation>
  89. _OutputIterator 
  90. __partial_sum(_InputIterator __first, _InputIterator __last, 
  91.               _OutputIterator __result, _Tp*, _BinaryOperation __binary_op);
  92.  
  93.  
  94. template <class _InputIterator, class _OutputIterator>
  95. inline _OutputIterator 
  96. partial_sum(_InputIterator __first, _InputIterator __last,
  97.             _OutputIterator __result) {
  98.   return __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator),
  99.                        __plus(_STLP_VALUE_TYPE(__first, _InputIterator)));
  100. }
  101.  
  102. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  103. inline _OutputIterator 
  104. partial_sum(_InputIterator __first, _InputIterator __last,
  105.             _OutputIterator __result, _BinaryOperation __binary_op) {
  106.   return __partial_sum(__first, __last, __result, _STLP_VALUE_TYPE(__first, _InputIterator), 
  107.                        __binary_op);
  108. }
  109.  
  110.  
  111. template <class _InputIterator, class _OutputIterator, class _Tp, 
  112.           class _BinaryOperation>
  113. _OutputIterator
  114. __adjacent_difference(_InputIterator __first, _InputIterator __last, 
  115.                       _OutputIterator __result, _Tp*,
  116.                       _BinaryOperation __binary_op);
  117.  
  118. template <class _InputIterator, class _OutputIterator>
  119. inline _OutputIterator
  120. adjacent_difference(_InputIterator __first,
  121.                     _InputIterator __last, _OutputIterator __result) {
  122.   return __adjacent_difference(__first, __last, __result,
  123.                                _STLP_VALUE_TYPE(__first, _InputIterator),
  124.                                __minus(_STLP_VALUE_TYPE(__first, _InputIterator)));
  125. }
  126.  
  127. template <class _InputIterator, class _OutputIterator, class _BinaryOperation>
  128. _OutputIterator 
  129. adjacent_difference(_InputIterator __first, _InputIterator __last,
  130.                     _OutputIterator __result, _BinaryOperation __binary_op) {
  131.   return __adjacent_difference(__first, __last, __result,
  132.                                _STLP_VALUE_TYPE(__first, _InputIterator),
  133.                                __binary_op);
  134. }
  135.  
  136. template <class _Tp, class _Integer, class _MonoidOperation>
  137. _Tp __power(_Tp __x, _Integer __n, _MonoidOperation __opr);
  138.  
  139. # ifndef _STLP_NO_EXTENSIONS
  140.  
  141. // Returns __x ** __n, where __n >= 0.  _Note that "multiplication"
  142. // is required to be associative, but not necessarily commutative.
  143.  
  144. template <class _Tp, class _Integer>
  145. inline _Tp __power(_Tp __x, _Integer __n)
  146. {
  147.   return __power(__x, __n, multiplies<_Tp>());
  148. }
  149.  
  150. // Alias for the internal name __power.  Note that power is an extension,
  151. // not part of the C++ standard.
  152. template <class _Tp, class _Integer, class _MonoidOperation>
  153. inline _Tp power(_Tp __x, _Integer __n, _MonoidOperation __opr) {
  154.   return __power(__x, __n, __opr);
  155. }
  156.  
  157.  
  158. template <class _Tp, class _Integer>
  159. inline _Tp power(_Tp __x, _Integer __n) {
  160.   return __power(__x, __n, multiplies<_Tp>());
  161. }
  162.  
  163. // iota is not part of the C++ standard.  It is an extension.
  164.  
  165. template <class _ForwardIterator, class _Tp>
  166. _STLP_INLINE_LOOP
  167. void 
  168. iota(_ForwardIterator __first, _ForwardIterator __last, _Tp __val)
  169. {
  170.   _STLP_DEBUG_CHECK(__check_range(__first, __last))
  171.   while (__first != __last)
  172.     *__first++ = __val++;
  173. }
  174. # endif
  175.  
  176. _STLP_END_NAMESPACE
  177.  
  178. # if !defined (_STLP_LINK_TIME_INSTANTIATION)
  179. #  include <stl/_numeric.c>
  180. # endif
  181.  
  182. #endif /* _STLP_INTERNAL_NUMERIC_H */
  183.  
  184. // Local Variables:
  185. // mode:C++
  186. // End:
  187.