/*
 * FormalRTESymbolAlphabet.h
 *
 * This file is part of Algorithms library toolkit.
 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)

 * Algorithms library toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * Algorithms library toolkit 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 General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Algorithms library toolkit.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Created on: Feb 20, 2016
 *      Author: Tomas Pecka
 */

#ifndef FORMAL_RTE_SYMBOL_ALPHABET_H_
#define FORMAL_RTE_SYMBOL_ALPHABET_H_

#include "FormalRTESymbol.h"

#include <exception/CommonException.h>
#include <sstream>
#include <alib/utility>
#include <alphabet/common/SymbolNormalize.h>

namespace rte {

/**
 * \brief Represents the terminal symbol in the regular tree expression. The number of children must be the same as the arity of the symbol of the node.
 *
 * The structure is derived from VararyNode, which however does not check the number of children of the node.
 *
 * The node can be visited by the FormalRTEElement < SymbolType >::Visitor
 *
 * \tparam SymbolType used for the symbol part of the ranked symbol
 */
template < class SymbolType >
class FormalRTESymbolAlphabet : public FormalRTESymbol < SymbolType >, public ext::VararyNode < FormalRTEElement < SymbolType >, FormalRTESymbolAlphabet < SymbolType > > {
	/**
	 * @copydoc regexp::FormalRTEElement < SymbolType >::accept ( ) const
	 */
	void accept ( typename FormalRTEElement < SymbolType >::ConstVisitor & visitor ) const override {
		visitor.visit ( * this );
	}

	/**
	 * @copydoc regexp::FormalRTEElement < SymbolType >::accept ( ) const
	 */
	void accept ( typename FormalRTEElement < SymbolType >::Visitor & visitor ) override {
		visitor.visit ( * this );
	}

public:
	/**
	 * \brief Creates a new instance of the symbol node using the actual symbol to represent.
	 *
	 * \param symbol the value of the represented symbol
	 */
	explicit FormalRTESymbolAlphabet ( common::ranked_symbol < SymbolType > symbol, ext::ptr_vector < FormalRTEElement < SymbolType > > children );

	/**
	 * @copydoc FormalRTEElement < SymbolType >::clone ( ) const &
	 */
	FormalRTESymbolAlphabet < SymbolType > * clone ( ) const & override;

	/**
	 * @copydoc FormalRTEElement < SymbolType >::clone ( ) &&
	 */
	FormalRTESymbolAlphabet < SymbolType > * clone ( ) && override;

	/**
	 * @copydoc FormalRTEElement < SymbolType >::testSymbol ( const common::ranked_symbol < SymbolType > & ) const
	 */
	bool testSymbol ( const common::ranked_symbol < SymbolType > & symbol ) const override;

	/**
	 * @copydoc FormalRTEElement < SymbolType >::computeMinimalAlphabet ( ext::set < common::ranked_symbol < SymbolType > > &, ext::set < common::ranked_symbol < SymbolType > > & ) const
	 */
	void computeMinimalAlphabet ( ext::set < common::ranked_symbol < SymbolType > > & alphabetF, ext::set < common::ranked_symbol < SymbolType > > & alphabetK ) const override;

	/**
	 * @copydoc FormalRTEElement < SymbolType >::checkAlphabet ( const ext::set < common::ranked_symbol < SymbolType > > &, const ext::set < common::ranked_symbol < SymbolType > > & ) const
	 */
	bool checkAlphabet ( const ext::set < common::ranked_symbol < SymbolType > > & alphabetF, const ext::set < common::ranked_symbol < SymbolType > > & alphabetK ) const override;

	/**
	 * Getter of child nodes of the rte node
	 *
	 * \return child nodes
	 */
	const ext::ptr_vector < FormalRTEElement < SymbolType > > & getElements ( ) const;

	/**
	 * Getter of child nodes of the rte node
	 *
	 * \return child nodes
	 */
	const ext::ptr_vector < FormalRTEElement < SymbolType > > & getElements ( );

	/**
	 * Getter of child node on n-th position
	 *
	 * \return child node
	 */
	const FormalRTEElement < SymbolType > & getElement ( size_t index ) const;

	/**
	 * Getter of child node on n-th position
	 *
	 * \return child node
	 */
	FormalRTEElement < SymbolType > & getElement ( size_t index );

	/**
	 * Setter of the n-th child
	 *
	 * \param index the index of child to change
	 * \param element the iterated tree
	 */
	void setElement ( size_t index, const FormalRTEElement < SymbolType > & element );

	/**
	 * Setter of the n-th child
	 *
	 * \param index the index of child to change
	 * \param element the iterated tree
	 */
	void setElement ( size_t index, FormalRTEElement < SymbolType > && element );

	/**
	 * @copydoc base::CommonBase < FormalRTEElement < SymbolType > >::compare ( const FormalRTEElement < SymbolType > & ) const
	 */
	int compare ( const FormalRTEElement < SymbolType > & other ) const override {
		if ( ext::type_index ( typeid ( * this ) ) == ext::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other );

		return ext::type_index ( typeid ( * this ) ) - ext::type_index ( typeid ( other ) );
	}

	/**
	 * The actual compare method
	 *
	 * \param other the other instance
	 *
	 * \returns the actual relation between two by type same node instances
	 */
	int compare ( const FormalRTESymbolAlphabet & ) const;

	/**
	 * @copydoc base::CommonBase < FormalRTEElement < SymbolType > >::operator >> ( std::ostream & ) const
	 */
	void operator >>( std::ostream & out ) const override;

	/**
	 * @copydoc base::CommonBase < FormalRTEElement < SymbolType > >::operator std::string ( ) const
	 */
	explicit operator std::string ( ) const override;

	/**
	 * @copydoc FormalRTEElement < SymbolType >::normalize ( ) &&
	 */
	ext::smart_ptr < FormalRTEElement < DefaultSymbolType > > normalize ( ) && override {
		ext::ptr_vector < FormalRTEElement < DefaultSymbolType > > children;
		for ( FormalRTEElement < SymbolType > && element : ext::make_mover ( std::move ( * this ).getChildren ( ) ) )
			children.push_back ( std::move ( * std::move ( element ).normalize ( ) ) );

		return ext::smart_ptr < FormalRTEElement < DefaultSymbolType > > ( new FormalRTESymbolAlphabet < DefaultSymbolType > ( alphabet::SymbolNormalize::normalizeRankedSymbol ( std::move ( this->m_symbol ) ), std::move ( children ) ) );
	}
};

template < class SymbolType >
FormalRTESymbolAlphabet < SymbolType >::FormalRTESymbolAlphabet ( common::ranked_symbol < SymbolType > symbol, ext::ptr_vector < FormalRTEElement < SymbolType > > children ) : FormalRTESymbol < SymbolType > ( symbol ), ext::VararyNode < FormalRTEElement < SymbolType >, FormalRTESymbolAlphabet < SymbolType > > ( std::move ( children ) ) {
	if ( this->getChildren ( ).size ( ) != ( size_t ) this->getSymbol ( ).getRank ( ) )
		throw exception::CommonException ( "Symbol's rank and number of children differ. Rank is " + ext::to_string ( this->getSymbol ( ).getRank ( ) ) + ", number of children is " + ext::to_string ( this->getChildren ( ).size ( ) ) + "." );

}

template < class SymbolType >
FormalRTESymbolAlphabet < SymbolType > * FormalRTESymbolAlphabet < SymbolType >::clone ( ) const & {
	return new FormalRTESymbolAlphabet ( * this );
}

template < class SymbolType >
FormalRTESymbolAlphabet < SymbolType > * FormalRTESymbolAlphabet < SymbolType >::clone ( ) && {
	return new FormalRTESymbolAlphabet ( std::move ( * this ) );
}

template < class SymbolType >
const ext::ptr_vector < FormalRTEElement < SymbolType > > & FormalRTESymbolAlphabet < SymbolType >::getElements ( ) const {
	return this->getChildren ( );
}

template < class SymbolType >
const ext::ptr_vector < FormalRTEElement < SymbolType > > & FormalRTESymbolAlphabet < SymbolType >::getElements ( ) {
	return this->getChildren ( );
}

template < class SymbolType >
const FormalRTEElement < SymbolType > & FormalRTESymbolAlphabet < SymbolType >::getElement ( size_t index ) const {
	return this->getChild ( index );
}

template < class SymbolType >
FormalRTEElement < SymbolType > & FormalRTESymbolAlphabet < SymbolType >::getElement ( size_t index ) {
	return this->getChild ( index );
}

template < class SymbolType >
void FormalRTESymbolAlphabet < SymbolType >::setElement ( size_t index, const FormalRTEElement < SymbolType > & element ) {
	setElement ( index, ext::move_copy ( element ) );
}

template < class SymbolType >
void FormalRTESymbolAlphabet < SymbolType >::setElement ( size_t index, FormalRTEElement < SymbolType > && element ) {
	this->setChild ( std::move ( element ), index );
}

template < class SymbolType >
int FormalRTESymbolAlphabet < SymbolType >::compare ( const FormalRTESymbolAlphabet < SymbolType > & other ) const {
	auto first = ext::tie ( this->getSymbol ( ), this->getChildren ( ) );
	auto second = ext::tie ( other.getSymbol ( ), other.getChildren ( ) );

	static ext::compare < decltype ( first ) > comp;

	return comp ( first, second );
}

template < class SymbolType >
void FormalRTESymbolAlphabet < SymbolType >::operator >>( std::ostream & out ) const {
	out << "(FormalRTESymbolAlphabet " << " symbol = " << this->getSymbol ( ) << " children = " << this->getChildren ( ) << "})";
}

template < class SymbolType >
bool FormalRTESymbolAlphabet < SymbolType >::testSymbol ( const common::ranked_symbol < SymbolType > & symbol ) const {
	return symbol == this->getSymbol ( );
}

template < class SymbolType >
void FormalRTESymbolAlphabet < SymbolType >::computeMinimalAlphabet ( ext::set < common::ranked_symbol < SymbolType > > & alphabetF, ext::set < common::ranked_symbol < SymbolType > > & alphabetK ) const {
	alphabetF.insert ( this->getSymbol ( ) );

	for ( const FormalRTEElement < SymbolType > & child : this->getElements ( ) )
		child.computeMinimalAlphabet ( alphabetF, alphabetK );
}

template < class SymbolType >
bool FormalRTESymbolAlphabet < SymbolType >::checkAlphabet ( const ext::set < common::ranked_symbol < SymbolType > > & alphabetF, const ext::set < common::ranked_symbol < SymbolType > > & alphabetK ) const {
	return alphabetF.count ( this->getSymbol ( ) ) > 0 && std::all_of ( getElements ( ).begin ( ), getElements ( ).end ( ), [&] ( const FormalRTEElement < SymbolType > & e ) {
			return e.checkAlphabet ( alphabetF, alphabetK );
		} );
}

template < class SymbolType >
FormalRTESymbolAlphabet < SymbolType >::operator std::string ( ) const {
	std::stringstream ss;
	ss << * this;
	return ss.str ( );
}

} /* namespace rte */

extern template class rte::FormalRTESymbolAlphabet < DefaultSymbolType >;

#endif /* FORMAL_RTE_SYMBOL_ALPHABET_H_ */
