//  numeric : headerfile for the free standard C++ library
//  
//  Copyright (C) 1999 by the free standard C++ Library Team
//                        see AUTHORS for more details
//
//  Homepage : http://www.inf.fu-berlin.de/~mkrueger/fscl/
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either	
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Library General Public License for more details.
//  You should have received a copy of the GNU Library General Public
//  License along with this library; if not, write to the Free
//  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//  version : 0.1 last modified : 17.09.99

#ifndef __CPP_NUMERIC
#define __CPP_NUMERIC

#include<functional>

namespace std {

	// 26.4.1 Accumulate
	template <class InputIterator, class T>
	T accumulate(InputIterator first, InputIterator last, T init)
	{
		return accumulate(first, last, init, plus<T>());
	}
	
	template <class InputIterator, class T, class BinaryOperation>
	T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op)
	{
		while (first != last)
			init = op(init, *first++);
		return init;
	}

	// 26.4.2 Inner product
	template <class InputIterator1, class InputIterator2, class T>
	T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
	{
		return inner_product(first, last, init, plus<T>(), multiplies<T>());
	}

	template <class InputIterator1, class InputIterator2, class T, class BinaryOperation1, class BinaryOperation2>
	T inner_product(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2)
	{
		while (first != last)
			init = op(init, op2(*first++, *first2++));
		return init;
	}

	// 26.4.3 Partial sum
	template <class InputIterator, class OutputIterator>
	OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result)
	{
		return partial_sum(first, last, result, plus<typename InputIterator::value_type>());
	}

	template <class InputIterator, class OutputIterator, class BinaryOperation>
	OutputIterator partial_sum(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op)
	{
		if (first == last)
			return result;
		*result = *first;
		typename InputIterator::value_type v = *first;
		while (++first != last) {
			v = op(value, *first);
			*++result = v;
		}
		return ++result;
	}

	// 26.4.4 Adjacent difference
	template <class InputIterator, class OutputIterator>
	OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result)
	{
		return adjacent_difference(first, last, result, minus<typename InputIterator::value_type>());
	}

	template <class InputIterator, class OutputIterator, class BinaryOperation>
	OutputIterator adjacent_difference(InputIterator first, InputIterator last, OutputIterator result, BinaryOperation binary_op)
	{
		if (first == last)
			return result;
		*result = *first;
		typename InputIterator::value_type v = *first;
		while (++first != last) {
			typename InputIterator::value_type tmp = *first;
			*++result = op(tmp, v);
			v = tmp;
		}
		return ++result;
	}
}

#endif
