home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Internet 2000 May / MICD_2000_05.iso / CBuilder5 / INSTALL / DATA1.CAB / Program_Built_Files / Include / numeric.h < prev    next >
C/C++ Source or Header  |  2000-02-01  |  7KB  |  210 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.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  29.  *
  30.  * This computer software is owned by Rogue Wave Software, Inc. and is
  31.  * protected by U.S. copyright laws and other laws and by international
  32.  * treaties.  This computer software is furnished by Rogue Wave Software,
  33.  * Inc. pursuant to a written license agreement and may be used, copied,
  34.  * transmitted, and stored only in accordance with the terms of such
  35.  * license and with the inclusion of the above copyright notice.  This
  36.  * computer software or any other copies thereof may not be provided or
  37.  * otherwise made available to any other person.
  38.  *
  39.  * U.S. Government Restricted Rights.  This computer software is provided
  40.  * with Restricted Rights.  Use, duplication, or disclosure by the
  41.  * Government is subject to restrictions as set forth in subparagraph (c)
  42.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  43.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  44.  * Commercial Computer Software û Restricted Rights at 48 CFR 52.227-19,
  45.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  46.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  47.  *
  48.  **************************************************************************/
  49.  
  50. #include <stdcomp.h>
  51.  
  52. #include <functional>
  53. #include <iterator>
  54.  
  55. #ifndef _RWSTD_NO_NEW_HEADER
  56. #include <cstdlib>
  57. #else
  58. #include <stdlib.h>
  59. #endif
  60.  
  61. #ifndef _RWSTD_NO_NAMESPACE
  62. namespace std {
  63. #endif
  64.  
  65.   template <class InputIterator, class T>
  66.   T accumulate (InputIterator first, InputIterator last, T init)
  67.   {
  68.     while (first != last) init = init + *first++;
  69.     return init;
  70.   }
  71.  
  72.   template <class InputIterator, class T, class BinaryOperation>
  73.   T accumulate (InputIterator first, InputIterator last, T init,
  74.                 BinaryOperation binary_op)
  75.   {
  76.     while (first != last) init = binary_op(init, *first++);
  77.     return init;
  78.   }
  79.  
  80.   template <class InputIterator1, class InputIterator2, class T>
  81.   T inner_product (InputIterator1 first1, InputIterator1 last1,
  82.                    InputIterator2 first2, T init)
  83.   {
  84.     while (first1 != last1) init = init + (*first1++ * *first2++);
  85.     return init;
  86.   }
  87.  
  88.   template <class InputIterator1, class InputIterator2, class T,
  89.   class BinaryOperation1, class BinaryOperation2>
  90.   T inner_product (InputIterator1 first1, InputIterator1 last1,
  91.                    InputIterator2 first2, T init, BinaryOperation1 binary_op1,
  92.                    BinaryOperation2 binary_op2)
  93.   {
  94.     while (first1 != last1) 
  95.       init = binary_op1(init, binary_op2(*first1++, *first2++));
  96.     return init;
  97.   }
  98.  
  99.   template <class InputIterator, class OutputIterator, class T>
  100.   OutputIterator __partial_sum (InputIterator first, InputIterator last,
  101.                                 OutputIterator result, T*)
  102.   {
  103.     T value = *first;
  104.     while (++first != last)
  105.     {
  106.       value = value + *first;
  107.       *++result = value;
  108.     }
  109.     return ++result;
  110.   }
  111.  
  112.   template <class InputIterator, class OutputIterator>
  113.   inline OutputIterator partial_sum (InputIterator first, InputIterator last,
  114.                                      OutputIterator result)
  115.   {
  116.     return first == last ? result :
  117.     (*result = *first,
  118.      __partial_sum(first, last, result, _RWSTD_VALUE_TYPE(first)));
  119.   }
  120.  
  121.   template <class InputIterator, class OutputIterator, class T,
  122.   class BinaryOperation>
  123.   OutputIterator __partial_sum (InputIterator first, InputIterator last,
  124.                                 OutputIterator result, T*,
  125.                                 BinaryOperation binary_op)
  126.   {
  127.     T value = *first;
  128.     while (++first != last)
  129.     {
  130.       value = binary_op(value, *first);
  131.       *++result = value;
  132.     }
  133.     return ++result;
  134.   }
  135.  
  136.   template <class InputIterator, class OutputIterator, class BinaryOperation>
  137.   inline OutputIterator partial_sum (InputIterator first, InputIterator last,
  138.                                      OutputIterator result,
  139.                                      BinaryOperation binary_op)
  140.   {
  141.     return first == last ? result :
  142.     (*result = *first,
  143.      __partial_sum(first, last, result, _RWSTD_VALUE_TYPE(first),binary_op));
  144.   }
  145.  
  146.   template <class InputIterator, class OutputIterator, class T>
  147.   OutputIterator __adjacent_difference (InputIterator first, InputIterator last, 
  148.                                         OutputIterator result, T*)
  149.   {
  150.     T value = *first;
  151.     while (++first != last)
  152.     {
  153.       T tmp = *first;
  154.       *++result = tmp - value;
  155.       value = tmp;
  156.     }
  157.     return ++result;
  158.   }
  159.  
  160.   template <class InputIterator, class OutputIterator>
  161.   inline OutputIterator adjacent_difference (InputIterator first,
  162.                                              InputIterator last, 
  163.                                              OutputIterator result)
  164.   {
  165.     return first == last ? result :
  166.     (*result = *first,
  167.      __adjacent_difference(first, last, result, _RWSTD_VALUE_TYPE(first)));
  168.   }
  169.  
  170.   template <class InputIterator, class OutputIterator, class T, 
  171.   class BinaryOperation>
  172.   OutputIterator __adjacent_difference (InputIterator first, InputIterator last, 
  173.                                         OutputIterator result, T*,
  174.                                         BinaryOperation binary_op)
  175.   {
  176.     T value = *first;
  177.     while (++first != last)
  178.     {
  179.       T tmp = *first;
  180.       *++result = binary_op(tmp, value);
  181.       value = tmp;
  182.     }
  183.     return ++result;
  184.   }
  185.  
  186.   template <class InputIterator, class OutputIterator, class BinaryOperation>
  187.   inline OutputIterator adjacent_difference (InputIterator first,
  188.                                              InputIterator last,
  189.                                              OutputIterator result,
  190.                                              BinaryOperation binary_op)
  191.   {
  192.     return first == last ? result :
  193.     (*result = *first,
  194.      __adjacent_difference(first, last, result, _RWSTD_VALUE_TYPE(first),
  195.                            binary_op));
  196.   }
  197.  
  198. #ifndef _RWSTD_NO_NAMESPACE
  199. }
  200. #endif
  201.  
  202. #endif
  203.  
  204. #ifndef __USING_STD_NAMES__
  205.   using namespace std;
  206. #endif
  207.  
  208. #pragma option pop
  209. #endif /* __NUMERIC_H */
  210.