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

#include "BaseListExp.h"

#include <typeinfo>

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 FlatListExp : public BaseListExp<T>
  {
  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>
    template <class Iterator>
    FlatListExp(size_t, const std::string& image, Iterator elementsBegin, Iterator elementsEnd)
      : BaseListExp<T>(image)
    {
      for (; elementsBegin != elementsEnd; ++elementsBegin)
      {
        _pushBack(*elementsBegin);
      }
      INITIALIZE_HASH_CODE();
    }

    FlatListExp(const std::string& image, const T* t1, const T* t2)
      : BaseListExp<T>(image)
    {
      _pushBack(t1);
      _pushBack(t2);

      INITIALIZE_HASH_CODE();
    }

  protected:
    virtual size_t _calculateHashCode() const
    {
      return calculateSequenceHashCode(m_elements.begin(), m_elements.end());
    }

    ///// <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 pushFront(const T* element)
    //{
    //  const std::type_info &type1 = typeid(*this);
    //  const std::type_info &type2 = typeid(*element);

    //  if (type1 == type2)
    //  {
    //    const FlatListExp<T>* list = dynamic_cast<const FlatListExp<T>*>(element);
    //    assert(list != NULL);

    //    for (const_reverse_iterator it = list->m_elements.rbegin();
    //         it != list->m_elements.rend();
    //         ++it)
    //    {
    //      pushFront(*it);
    //    }
    //    // Although apparently useless, this ensures 'element' deletes itself if necessary
    //    element->delete();
    //  }
    //  else
    //  {      
    //    BaseListExp<T>::pushFront(element);
    //  }
    //}
  protected:
    void _pushBack(const T* element)
    {
      assert(element != NULL);

      const std::type_info &type1 = typeid(*this);
      const std::type_info &type2 = typeid(*element);

      if (type1 == type2)
      {
        const FlatListExp<T>* list = dynamic_cast<const FlatListExp<T>*>(element);
        assert(list != NULL);

        for (const_iterator it = list->m_elements.begin();
             it != list->m_elements.end();
             ++it)
        {
          _pushBack(*it);
        }
        // Although apparently useless, this ensures 'element' deletes itself if necessary
        memory::considerDeletion(element);
      }
      else
      {      
        BaseListExp<T>::_pushBack(element);
      }
    }
  };
}

#endif