//
// 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 _BASE_LIST_EXP_H
#define _BASE_LIST_EXP_H

#include "IExp.h"
#include "../Utils/Utils.h"

#include <vector>
#include <assert.h>

namespace planning
{
  /// <summary>
  /// Base class for list expressions, i.e. expressions which contain other expressions.
  /// </summary>
  /// <typeparam name="T">The type of the expressions.</typeparam>
  template <class T>
  class BaseListExp : public virtual IExp
  {
  public:
    /// <summary>
    /// Creates a new list expression which will store the specified expressions.
    /// </summary>
    /// <param name="image">The image of this list expression.</param>
    /// <param name="exps">The expressions to store.</param>
    BaseListExp(const std::string& image)
      : m_image(image)
    {
    }

    ~BaseListExp()
    {
      memory::release(this, m_elements.begin(), m_elements.end());
    }

    typedef typename std::vector<const T*>::const_iterator const_iterator;
    typedef typename std::vector<const T*>::const_reverse_iterator const_reverse_iterator;

    const_iterator begin() const { return m_elements.begin(); }
    const_iterator end() const { return m_elements.end(); }
    const_iterator rbegin() const { return m_elements.rbegin(); }
    const_iterator rend() const { return m_elements.rend(); }

  protected:
    virtual void _output(std::ostream& out) const
    {
      out << "(" << m_image;
      for (const_iterator it = m_elements.begin();
           it != m_elements.end();
           ++it)
      {
        out << " " << **it;
      }
      out << ")";
    }

    typedef typename std::vector<const T*>::iterator iterator;
    typedef typename std::vector<const T*>::reverse_iterator reverse_iterator;

    /// <summary>
    /// Adds a new expression to this list expression.
    /// Note that this method is protected and should be called only in the constructor.
    /// The default implementation is to add the expression to the list.
    /// </summary>
    /// <param name="elt">The new expression to add to this list expression.</param>
    void _pushBack(const T* element)
    {
      element->acquire(this);
      m_elements.push_back(element);
    }

    //virtual void pushFront(const T* element)
    //{
    //  element->acquire(this);
    //  m_elements.push_front(element);
    //}

    virtual int _internalCompareTo(const IExp& other) const
    {
      const BaseListExp& exp = dynamic_cast<const BaseListExp&>(other);

      COMPARE(m_elements, exp.m_elements);

      return 0;
    }

    /// <summary>
    /// The list of expressions contained in this list expression.
    /// </summary>
    std::vector<const T*> m_elements;

  private:
    /// <summary>
    /// The image of this list expression.
    /// </summary>
    const std::string& m_image;
  
    ///// <summary>
    ///// Substitutes all occurrences of the variables that occur in this list
    ///// expression by their corresponding bindings.
    ///// </summary>
    ///// <param name="bindings">The variable bindings.</param>
    ///// <returns>A substituted copy of this list expression.</returns>
    //public override IExp Apply(ParameterBindings bindings)
    //{
    //  ListExp<T> other = (ListExp<T>)base.Apply(bindings);
    //  other.m_expressions = new List<T>(this.m_expressions.Count);
    //  foreach (T exp in this)
    //  {
    //    other.m_expressions.Add((T)exp.Apply(bindings));
    //  }
    //  return other;
    //}

    ///// <summary>
    ///// Standardizes all occurrences of the variables that occur in this
    ///// expression. The IDictionary argument is used to store the variable already
    ///// standardized. Remember that free variables are existentially quantified.
    ///// </summary>
    ///// <param name="images">The object that maps old variable images to the standardize
    ///// image.</param>
    ///// <returns>A standardized copy of this expression.</returns>
    //public override IExp Standardize(IDictionary<string, string> images)
    //{
    //  ListExp<T> other = (ListExp<T>)base.Standardize(images);
    //  other.m_expressions = new List<T>(this.m_expressions.Count);
    //  foreach (T exp in this.m_expressions)
    //  {
    //    other.m_expressions.Add((T)exp.Standardize(images));
    //  }
    //  return other;
    //}

    ///// <summary>
    ///// Returns true if the expression is ground, i.e. it does not contain any variables.
    ///// </summary>
    ///// <returns>Whether the expression is ground.</returns>
    //public override bool IsGround()
    //{
    //  foreach (T exp in this)
    //  {
    //    if (!exp.IsGround())
    //      return false;
    //  }
    //  return true;
    //}

    ///// <summary>
    ///// Returns the free variables in this expression.
    ///// </summary>
    ///// <returns>The free variables in this expression.</returns>
    //public override HashSet<Variable> GetFreeVariables()
    //{
    //  HashSet<Variable> vars = new HashSet<Variable>();
    //  foreach (T exp in this.m_expressions)
    //  {
    //    vars.UnionWith(exp.GetFreeVariables());  
    //  }
    //  return vars;
    //}

    ///// <summary>
    ///// Returns true if this list expression is equal to a specified object.
    ///// By default, two list expressions are equal if they have the same arguments in
    ///// the same order.
    ///// </summary>
    ///// <param name="obj">Object to test for equality.</param>
    ///// <returns>True if this list expression is equal to the specified objet.</returns>
    //public override bool Equals(object obj)
    //{
    //  if (obj == this)
    //  {
    //    return true;
    //  }
    //  else if (obj.GetType() == this.GetType())
    //  {
    //    ListExp<T> other = (ListExp<T>)obj;
    //    return this.m_expressions.SequenceEqual(other.m_expressions);
    //  }
    //  else
    //  {
    //    return false;
    //  }
    //}

    ///// <summary>
    ///// Returns the hash code of this list expression.
    ///// </summary>
    ///// <returns>The hash code of this list expression.</returns>
    //public override int GetHashCode()
    //{
    //  return this.m_expressions.GetOrderedEnumerableHashCode();
    //}

    ///// <summary>
    ///// Returns a clone of this list expression.
    ///// </summary>
    ///// <returns>A clone of this list expression.</returns>
    //public override object Clone()
    //{
    //  ListExp<T> other = (ListExp<T>)base.Clone();
    //  other.m_expressions = new List<T>(this.m_expressions);

    //  return other;
    //}

    ///// <summary>
    ///// Returns a string representation of this conjunctive expression.
    ///// </summary>
    ///// <returns>A string representation of this conjunctive expression.</returns>
    //public override string ToString()
    //{
    //  StringBuilder str = new StringBuilder();
    //  str.Append("(");
    //  str.Append(m_image);
    //  foreach (T elt in this.m_expressions)
    //  {
    //    str.Append(" ");
    //    str.Append(elt.ToString());
    //  }
    //  str.Append(")");
    //  return str.ToString();
    //}

    ///// <summary>
    ///// Returns a typed string representation of this conjunctive expression.
    ///// </summary>
    ///// <returns>A typed string representation of this conjunctive expression.</returns>
    //public override string ToTypedString()
    //{
    //  StringBuilder str = new StringBuilder();
    //  str.Append("(");
    //  str.Append(m_image);
    //  foreach (T elt in this.m_expressions)
    //  {
    //    str.Append(" ");
    //    str.Append(elt.ToTypedString());
    //  }
    //  str.Append(")");
    //  return str.ToString();
    //}

    //#region IComparable<IExp> Members

    ///// <summary>
    ///// Compares this list expression with another expression.
    ///// </summary>
    ///// <param name="obj">The other expression to compare this expression to.</param>
    ///// <returns>An integer representing the total order relation between the two expressions.
    ///// </returns>
    //public override int CompareTo(IExp obj)
    //{
    //  int value = base.CompareTo(obj);
    //  if (value != 0)
    //    return value;

    //  ListExp<T> other = (ListExp<T>)obj;
    //  value = this.m_expressions.Count.CompareTo(other.m_expressions.Count);
    //  if (value != 0)
    //    return value;

    //  for (int i = 0; i < this.m_expressions.Count; ++i)
    //  {
    //    value = this.m_expressions[i].CompareTo(other.m_expressions[i]);
    //    if (value != 0)
    //      return value;
    //  }
    //  return value;
    //}

    //#endregion

private :
  //const member prevent the use of the assigment operator
  BaseListExp<T>& operator=(const BaseListExp<T>&);
  };
}

#endif