//
// Copyright (c) 2009 Froduald Kabanza and the Université de Sherbrooke.
// Use of this software is permitted for non-commercial research purposes, and
// it may be copied or applied only for that use. All copies must include this
// copyright message.
// 
// This is a research prototype that has not gone through intensive tests and
// is delivered as is. It may still contain bugs. Froduald Kabanza and the
// Université de Sherbrooke disclaim any responsibility for damage that may be
// caused by using it.
//
// Please note that some files were inspired in part by the PDDL4J library:
// http://www.math-info.univ-paris5.fr/~pellier/software/software.php 
//
// Implementation: Simon Chamberland
// Project Manager: Froduald Kabanza
//

#ifndef _NARITY_DIVIDE_H
#define _NARITY_DIVIDE_H

#include "FunctionExp.h"
#include "../../Exception/NumericException.h"
#include <numeric>

namespace planning
{
  /// <summary>
  /// This class implements a n-ary division.
  /// If only one argument is given, the result is the multiplicative inverse.
  /// </summary>
  class NArityDivide : public FunctionExp
  {
  public:
    /// <summary>
    /// Creates a new division function with the specified arguments.
    /// </summary>
    /// <param name="args">The arguments of the new division function.</param>
    template <class Iterator>
    NArityDivide(size_t n, Iterator argumentsBegin, Iterator argumentsEnd)
      : FunctionExp(n, s_image, argumentsBegin, argumentsEnd)
    {
      assert(m_arguments.size() >= 1);

      INITIALIZE_HASH_CODE();
    }

    NArityDivide(const INumericExp* exp1, const INumericExp* exp2)
      : FunctionExp(s_image, exp1, exp2)
    {
      INITIALIZE_HASH_CODE();
    }

    /// <summary>
    /// Calculates the left-associative quotient of the arguments.
    /// If only one argument is given, the result is the multiplicative inverse.
    /// </summary>
    /// <param name="args">The arguments to call the function with.</param>
    /// <returns>The left-associative quotient of the arguments.</returns>
    /// <exception cref="PDDLParser.Exception.NumericException">A NumericException is thrown if a division
    /// by 0 occurs.</exception>
    virtual double calculate(evaluated_argument_const_iterator begin,
                             evaluated_argument_const_iterator end) const
    {
      //const size_t arity = getArity();
      //const double result = (arity == 1) ? 
      //  1.0 / arguments[0] :
      //  std::accumulate(arguments + 1, arguments + getArity(), 
      //                  arguments[0], std::divides<double>());

      const double result = (getArity() == 1) ?
        1.0 / *begin :
        std::accumulate(begin + 1, end, *begin, std::divides<double>());

      if ( !(boost::math::isfinite)(result) )
      {
        throw NumericException(this, begin, end);
      }
      return result;
    }

  protected:
    virtual size_t _calculateHashCode() const
    {
      return CALCULATE_HASH_CODE(FunctionExp)
           + 12199572;
    }

    const IExp* _internalApply(const Bindings* bindings) const;

  private:
    static const std::string s_image;
  };
}

#endif