home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Include / NUMERIC.H < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  7.6 KB  |  219 lines

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