home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 January / PCWorld_2000-01_cd.bin / Software / Servis / Devc / _SETUP.4 / Group3 / stl_numeric.h < prev    next >
C/C++ Source or Header  |  1998-03-08  |  6KB  |  197 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996,1997
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  */
  26.  
  27. /* NOTE: This is an internal header file, included by other STL headers.
  28.  *   You should not attempt to use it directly.
  29.  */
  30.  
  31.  
  32. #ifndef __SGI_STL_INTERNAL_NUMERIC_H
  33. #define __SGI_STL_INTERNAL_NUMERIC_H
  34.  
  35. __STL_BEGIN_NAMESPACE
  36.  
  37. template <class InputIterator, class T>
  38. T accumulate(InputIterator first, InputIterator last, T init) {
  39.   for ( ; first != last; ++first)
  40.     init = init + *first;
  41.   return init;
  42. }
  43.  
  44. template <class InputIterator, class T, class BinaryOperation>
  45. T accumulate(InputIterator first, InputIterator last, T init,
  46.              BinaryOperation binary_op) {
  47.   for ( ; first != last; ++first)
  48.     init = binary_op(init, *first);
  49.   return init;
  50. }
  51.  
  52. template <class InputIterator1, class InputIterator2, class T>
  53. T inner_product(InputIterator1 first1, InputIterator1 last1,
  54.                 InputIterator2 first2, T init) {
  55.   for ( ; first1 != last1; ++first1, ++first2)
  56.     init = init + (*first1 * *first2);
  57.   return init;
  58. }
  59.  
  60. template <class InputIterator1, class InputIterator2, class T,
  61.           class BinaryOperation1, class BinaryOperation2>
  62. T inner_product(InputIterator1 first1, InputIterator1 last1,
  63.                 InputIterator2 first2, T init, BinaryOperation1 binary_op1,
  64.                 BinaryOperation2 binary_op2) {
  65.   for ( ; first1 != last1; ++first1, ++first2)
  66.     init = binary_op1(init, binary_op2(*first1, *first2));
  67.   return init;
  68. }
  69.  
  70. template <class InputIterator, class OutputIterator, class T>
  71. OutputIterator __partial_sum(InputIterator first, InputIterator last,
  72.                              OutputIterator result, T*) {
  73.   T value = *first;
  74.   while (++first != last) {
  75.     value = value + *first;
  76.     *++result = value;
  77.   }
  78.   return ++result;
  79. }
  80.  
  81. template <class InputIterator, class OutputIterator>
  82. OutputIterator partial_sum(InputIterator first, InputIterator last,
  83.                            OutputIterator result) {
  84.   if (first == last) return result;
  85.   *result = *first;
  86.   return __partial_sum(first, last, result, value_type(first));
  87. }
  88.  
  89. template <class InputIterator, class OutputIterator, class T,
  90.           class BinaryOperation>
  91. OutputIterator __partial_sum(InputIterator first, InputIterator last,
  92.                              OutputIterator result, T*,
  93.                              BinaryOperation binary_op) {
  94.   T value = *first;
  95.   while (++first != last) {
  96.     value = binary_op(value, *first);
  97.     *++result = value;
  98.   }
  99.   return ++result;
  100. }
  101.  
  102. template <class InputIterator, class OutputIterator, class BinaryOperation>
  103. OutputIterator partial_sum(InputIterator first, InputIterator last,
  104.                            OutputIterator result, BinaryOperation binary_op) {
  105.   if (first == last) return result;
  106.   *result = *first;
  107.   return __partial_sum(first, last, result, value_type(first), binary_op);
  108. }
  109.  
  110. template <class InputIterator, class OutputIterator, class T>
  111. OutputIterator __adjacent_difference(InputIterator first, InputIterator last, 
  112.                                      OutputIterator result, T*) {
  113.   T value = *first;
  114.   while (++first != last) {
  115.     T tmp = *first;
  116.     *++result = tmp - value;
  117.     value = tmp;
  118.   }
  119.   return ++result;
  120. }
  121.  
  122. template <class InputIterator, class OutputIterator>
  123. OutputIterator adjacent_difference(InputIterator first, InputIterator last, 
  124.                                    OutputIterator result) {
  125.   if (first == last) return result;
  126.   *result = *first;
  127.   return __adjacent_difference(first, last, result, value_type(first));
  128. }
  129.  
  130. template <class InputIterator, class OutputIterator, class T, 
  131.           class BinaryOperation>
  132. OutputIterator __adjacent_difference(InputIterator first, InputIterator last, 
  133.                                      OutputIterator result, T*,
  134.                                      BinaryOperation binary_op) {
  135.   T value = *first;
  136.   while (++first != last) {
  137.     T tmp = *first;
  138.     *++result = binary_op(tmp, value);
  139.     value = tmp;
  140.   }
  141.   return ++result;
  142. }
  143.  
  144. template <class InputIterator, class OutputIterator, class BinaryOperation>
  145. OutputIterator adjacent_difference(InputIterator first, InputIterator last,
  146.                                    OutputIterator result,
  147.                                    BinaryOperation binary_op) {
  148.   if (first == last) return result;
  149.   *result = *first;
  150.   return __adjacent_difference(first, last, result, value_type(first),
  151.                                binary_op);
  152. }
  153.  
  154. // Returns x ** n, where n >= 0.  Note that "multiplication"
  155. //  is required to be associative, but not necessarily commutative.
  156.     
  157. template <class T, class Integer, class MonoidOperation>
  158. T power(T x, Integer n, MonoidOperation op) {
  159.   if (n == 0)
  160.     return identity_element(op);
  161.   else {
  162.     while ((n & 1) == 0) {
  163.       n >>= 1;
  164.       x = op(x, x);
  165.     }
  166.  
  167.     T result = x;
  168.     n >>= 1;
  169.     while (n != 0) {
  170.       x = op(x, x);
  171.       if ((n & 1) != 0)
  172.         result = op(result, x);
  173.       n >>= 1;
  174.     }
  175.     return result;
  176.   }
  177. }
  178.  
  179. template <class T, class Integer>
  180. inline T power(T x, Integer n) {
  181.   return power(x, n, multiplies<T>());
  182. }
  183.  
  184.  
  185. template <class ForwardIterator, class T>
  186. void iota(ForwardIterator first, ForwardIterator last, T value) {
  187.   while (first != last) *first++ = value++;
  188. }
  189.  
  190. __STL_END_NAMESPACE
  191.  
  192. #endif /* __SGI_STL_INTERNAL_NUMERIC_H */
  193.  
  194. // Local Variables:
  195. // mode:C++
  196. // End:
  197.