home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / NUMERIC.H < prev    next >
C/C++ Source or Header  |  1997-02-14  |  8KB  |  213 lines

  1. #ifndef __STD_NUMERIC
  2. #define __STD_NUMERIC
  3. /* $Revision:   8.1  $ */
  4.  
  5. /***************************************************************************
  6.  *
  7.  * numeric - Declarations for the Standard Library algorithms
  8.  *
  9.  * $Id: numeric,v 1.6 1995/08/23 23:54:42 lijewski Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED
  29.  *
  30.  * The software and information contained herein are proprietary to, and
  31.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  32.  * intends to preserve as trade secrets such software and information.
  33.  * This software is furnished pursuant to a written license agreement and
  34.  * may be used, copied, transmitted, and stored only in accordance with
  35.  * the terms of such license and with the inclusion of the above copyright
  36.  * notice.  This software and information or any other copies thereof may
  37.  * not be provided or otherwise made available to any other person.
  38.  *
  39.  * Notwithstanding any other lease or license that may pertain to, or
  40.  * accompany the delivery of, this computer software and information, the
  41.  * rights of the Government regarding its use, reproduction and disclosure
  42.  * are as set forth in Section 52.227-19 of the FARS Computer
  43.  * Software-Restricted Rights clause.
  44.  *
  45.  * Use, duplication, or disclosure by the Government is subject to
  46.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  47.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  48.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  49.  * P.O. Box 2328, Corvallis, Oregon 97339.
  50.  *
  51.  * This computer software and information is distributed with "restricted
  52.  * rights."  Use, duplication or disclosure is subject to restrictions as
  53.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  54.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  55.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  56.  * then the "Alternate III" clause applies.
  57.  *
  58.  **************************************************************************/
  59.  
  60. #include <stdcomp.h>
  61.  
  62. #include <function>
  63. #include <iterator>
  64.  
  65. #ifndef RWSTD_NO_NEW_HEADER
  66. #include <cstdlib>
  67. #else
  68. #include <stdlib.h>
  69. #endif
  70.  
  71. #ifndef RWSTD_NO_NAMESPACE
  72. namespace std {
  73. #endif
  74.  
  75. template <class InputIterator, class T>
  76. T accumulate (InputIterator first, InputIterator last, T init)
  77. {
  78.     while (first != last) init = init + *first++;
  79.     return init;
  80. }
  81.  
  82. template <class InputIterator, class T, class BinaryOperation>
  83. T accumulate (InputIterator first, InputIterator last, T init,
  84.               BinaryOperation binary_op)
  85. {
  86.     while (first != last) init = binary_op(init, *first++);
  87.     return init;
  88. }
  89.  
  90. template <class InputIterator1, class InputIterator2, class T>
  91. T inner_product (InputIterator1 first1, InputIterator1 last1,
  92.                  InputIterator2 first2, T init)
  93. {
  94.     while (first1 != last1) init = init + (*first1++ * *first2++);
  95.     return init;
  96. }
  97.  
  98. template <class InputIterator1, class InputIterator2, class T,
  99.           class BinaryOperation1, class BinaryOperation2>
  100. T inner_product (InputIterator1 first1, InputIterator1 last1,
  101.                  InputIterator2 first2, T init, BinaryOperation1 binary_op1,
  102.                  BinaryOperation2 binary_op2)
  103. {
  104.     while (first1 != last1)
  105.         init = binary_op1(init, binary_op2(*first1++, *first2++));
  106.     return init;
  107. }
  108.  
  109. template <class InputIterator, class OutputIterator, class T>
  110. OutputIterator __partial_sum (InputIterator first, InputIterator last,
  111.                               OutputIterator result, T*)
  112. {
  113.     T value = *first;
  114.     while (++first != last)
  115.     {
  116.         value = value + *first;
  117.         *++result = value;
  118.     }
  119.     return ++result;
  120. }
  121.  
  122. template <class InputIterator, class OutputIterator>
  123. inline OutputIterator partial_sum (InputIterator first, InputIterator last,
  124.                                    OutputIterator result)
  125. {
  126.     return first == last ? result :
  127.         (*result = *first,
  128.         __partial_sum(first, last, result, RWSTD_VALUE_TYPE(first)));
  129. }
  130.  
  131. template <class InputIterator, class OutputIterator, class T,
  132.           class BinaryOperation>
  133. OutputIterator __partial_sum (InputIterator first, InputIterator last,
  134.                               OutputIterator result, T*,
  135.                               BinaryOperation binary_op)
  136. {
  137.     T value = *first;
  138.     while (++first != last)
  139.     {
  140.         value = binary_op(value, *first);
  141.         *++result = value;
  142.     }
  143.     return ++result;
  144. }
  145.  
  146. template <class InputIterator, class OutputIterator, class BinaryOperation>
  147. inline OutputIterator partial_sum (InputIterator first, InputIterator last,
  148.                                    OutputIterator result,
  149.                                    BinaryOperation binary_op)
  150. {
  151.     return first == last ? result :
  152.         (*result = *first,
  153.         __partial_sum(first, last, result, RWSTD_VALUE_TYPE(first),binary_op));
  154. }
  155.  
  156. template <class InputIterator, class OutputIterator, class T>
  157. OutputIterator __adjacent_difference (InputIterator first, InputIterator last,
  158.                                       OutputIterator result, T*)
  159. {
  160.     T value = *first;
  161.     while (++first != last)
  162.     {
  163.         T tmp = *first;
  164.         *++result = tmp - value;
  165.         value = tmp;
  166.     }
  167.     return ++result;
  168. }
  169.  
  170. template <class InputIterator, class OutputIterator>
  171. inline OutputIterator adjacent_difference (InputIterator first,
  172.                                            InputIterator last,
  173.                                            OutputIterator result)
  174. {
  175.     return first == last ? result :
  176.         (*result = *first,
  177.         __adjacent_difference(first, last, result, RWSTD_VALUE_TYPE(first)));
  178. }
  179.  
  180. template <class InputIterator, class OutputIterator, class T,
  181.           class BinaryOperation>
  182. OutputIterator __adjacent_difference (InputIterator first, InputIterator last,
  183.                                       OutputIterator result, T*,
  184.                                       BinaryOperation binary_op)
  185. {
  186.     T value = *first;
  187.     while (++first != last)
  188.     {
  189.         T tmp = *first;
  190.         *++result = binary_op(tmp, value);
  191.         value = tmp;
  192.     }
  193.     return ++result;
  194. }
  195.  
  196. template <class InputIterator, class OutputIterator, class BinaryOperation>
  197. inline OutputIterator adjacent_difference (InputIterator first,
  198.                                            InputIterator last,
  199.                                            OutputIterator result,
  200.                                            BinaryOperation binary_op)
  201. {
  202.     return first == last ? result :
  203.         (*result = *first,
  204.         __adjacent_difference(first, last, result, RWSTD_VALUE_TYPE(first),
  205.                               binary_op));
  206. }
  207.  
  208. #ifndef RWSTD_NO_NAMESPACE
  209. }
  210. #endif
  211.  
  212. #endif
  213.