/*
 *  Open BEAGLE
 *  Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact:
 *  Laboratoire de Vision et Systemes Numeriques
 *  Departement de genie electrique et de genie informatique
 *  Universite Laval, Quebec, Canada, G1V 4J7
 *  http://vision.gel.ulaval.ca
 *
 */

/*!
 *  \file   beagle/GP/Tree.hpp
 *  \brief  Definition of the type GP::Tree.
 *  \author Christian Gagne
 *  \author Marc Parizeau
 *  $Revision: 1.2 $
 *  $Date: 2004/01/15 21:08:56 $
 */

#ifndef Beagle_GP_Tree_hpp
#define Beagle_GP_Tree_hpp

#include <vector>

#include "beagle/config.hpp"
#include "beagle/macros.hpp"
#include "beagle/Object.hpp"
#include "beagle/Allocator.hpp"
#include "beagle/Pointer.hpp"
#include "beagle/Container.hpp"
#include "beagle/Genotype.hpp"
#include "beagle/GP/Primitive.hpp"


namespace Beagle {
namespace GP {

/*!
 *  \struct Node beagle/GP/Tree.hpp "beagle/GP/Tree.hpp"
 *  \brief GP tree node structure.
 *  \ingroup GPF
 *  \ingroup GPPop
 */
struct Node {
  Primitive::Handle mPrimitive;    //!< Smart pointer to the associated primitive.
  unsigned int      mSubTreeSize;  //!< Sub-tree size, including actual node.

  explicit Node(Primitive::Handle inPrimitive=NULL, unsigned int inSubTreeSize=0);
  bool operator==(const Node& inRightNode) const;
};


/*!
 *  \class Tree beagle/GP/Tree.hpp "beagle/GP/Tree.hpp"
 *  \brief The GP tree class, the genotype used in GP.
 *  \ingroup GPF
 *  \ingroup GPPop
 */
class Tree : public Genotype, public std::vector<Node> {

public:

  //! GP::Tree allocator type.
  typedef AllocatorT<Tree,Genotype::Alloc>
          Alloc;
  //! GP::Tree handle type.
  typedef PointerT<Tree,Genotype::Handle>
          Handle;
  //! GP::Tree bag type.
  typedef ContainerT<Tree,Genotype::Bag>
          Bag;

  explicit Tree(unsigned int inSize=0);
  virtual ~Tree() { }

          unsigned int getTreeDepth(unsigned int inNodeIndex=0) const;
          void         interpret(GP::Datum& outResult, GP::Context& ioContext);
  virtual bool         isEqual(const Object& inRightObj) const;
  virtual void         readWithContext(XMLNode::Handle& inNode, Beagle::Context& ioContext);
          void         setContextToNode(unsigned int inNodeIndex, GP::Context& ioContext) const;
          bool         validateSubTree(unsigned int inNodeIndex, GP::Context& ioContext) const;
  virtual void         write(XMLStreamer& ioStreamer) const;
          void         writeIndented(XMLStreamer& ioStreamer) const;

protected:
  unsigned int readSubTree(XMLNode::Handle& inNode, GP::Context& ioContext);
  unsigned int writeSubTree(XMLStreamer& ioStreamer,
                            unsigned int inN,
                            bool inIndent=false) const;

};

}
}

#endif // Beagle_GP_Tree_hpp
