/*
 * ExtendedNFA.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: Mar 25, 2013
 *      Author: Jan Travnicek
 */

#pragma once

#include <ostream>
#include <sstream>

#include <alib/multimap>
#include <alib/set>
#include <alib/algorithm>

#include <core/components.hpp>
#include <common/createUnique.hpp>

#include <common/DefaultStateType.h>
#include <common/DefaultSymbolType.h>

#include <label/InitialStateLabel.h>
#include <regexp/unbounded/UnboundedRegExpStructure.h>
#include <regexp/unbounded/UnboundedRegExpConcatenation.h>
#include <regexp/unbounded/UnboundedRegExpSymbol.h>

#include <automaton/AutomatonException.h>

#include <core/normalize.hpp>
#include <alphabet/common/SymbolNormalize.h>
#include <automaton/common/AutomatonNormalize.h>

#include "DFA.h"
#include "NFA.h"
#include "CompactNFA.h"
#include "EpsilonNFA.h"
#include "MultiInitialStateNFA.h"

namespace automaton {

class InputAlphabet;
class States;
class FinalStates;
class InitialState;

/**
 * \brief
 * Extended nondeterministic finite automaton. Accepts regular languages. The automaton has a regular expression on transitions.

 * \details
 * Definition is classical definition of finite automata.
 * A = (Q, T, I, \delta, F),
 * Q (States) = nonempty finite set of states,
 * T (TerminalAlphabet) = finite set of terminal symbols - having this empty won't let automaton do much though,
 * I (InitialState) = initial state,
 * \delta = transition function of the form A \times a -> P(Q), where A \in Q, a \in RegExpsOver(T), and P(Q) is a powerset of states,
 * F (FinalStates) = set of final states
 *
 * \tparam SymbolTypeT used for the terminal alphabet
 * \tparam StateTypeT used to the states, and the initial state of the automaton.
 */
template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
class ExtendedNFA final : public core::Components < ExtendedNFA < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
public:
	typedef SymbolTypeT SymbolType;
	typedef StateTypeT StateType;

private:
	/**
	 * Transition function as mapping from a state times a regular expression on the left hand side to a set of states.
	 */
	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > transitions;

public:
	/**
	 * \brief Creates a new instance of the automaton with a concrete initial state.
	 *
	 * \param initialState the initial state of the automaton
	 */
	explicit ExtendedNFA ( StateType initialState );

	/**
	 * \brief Creates a new instance of the automaton with a concrete set of states, input alphabet, initial state, and a set of final states.
	 *
	 * \param states the initial set of states of the automaton
	 * \param inputAlphabet the initial input alphabet
	 * \param initialState the initial state of the automaton
	 * \param finalStates the initial set of final states of the automaton
	 */
	explicit ExtendedNFA ( ext::set < StateType > states, ext::set < SymbolType > inputAlphabet, StateType initialState, ext::set < StateType > finalStates );

	/*
	 * \brief Creates a new instance of the automaton based on the Compact nondeterministic finite automaton.
	 *
	 * \param other the Compact nondeterministic finite automaton
	 */
	explicit ExtendedNFA ( const CompactNFA < SymbolType, StateType > & other );

	/*
	 * \brief Creates a new instance of the automaton based on the Multi Initial State Epsilon Nondeterministic finite automaton.
	 *
	 * \param other the Multi Initial State Epsilon Nondeterministic finite automaton
	 */
	explicit ExtendedNFA ( const MultiInitialStateEpsilonNFA < SymbolType, StateType > & other );

	/*
	 * \brief Creates a new instance of the automaton based on the Epsilon Nondeterministic finite automaton.
	 *
	 * \param other the Epsilon nondeterministic finite automaton
	 */
	explicit ExtendedNFA ( const EpsilonNFA < SymbolType, StateType > & other );

	/*
	 * \brief Creates a new instance of the automaton based on the Nondeterministic finite automaton with multiple initial states.
	 *
	 * \param other the Nondeterministic finite automaton with multiple initial states
	 */
	explicit ExtendedNFA ( const MultiInitialStateNFA < SymbolType, StateType > & other );

	/*
	 * \brief Creates a new instance of the automaton based on the Nondeterministic finite automaton.
	 *
	 * \param other the Nondeterministic finite automaton
	 */
	explicit ExtendedNFA ( const NFA < SymbolType, StateType > & other );

	/*
	 * \brief Creates a new instance of the automaton based on the Deterministic finite automaton.
	 *
	 * \param other the Deterministic finite automaton
	 */
	explicit ExtendedNFA ( const DFA < SymbolType, StateType > & other );

	/**
	 * Getter of the initial state.
	 *
	 * \returns the initial state of the automaton
	 */
	const StateType & getInitialState ( ) const & {
		return this->template accessComponent < InitialState > ( ).get ( );
	}

	/**
	 * Getter of the initial state.
	 *
	 * \returns the initial state of the automaton
	 */
	StateType && getInitialState ( ) && {
		return std::move ( this->template accessComponent < InitialState > ( ).get ( ) );
	}

	/**
	 * Setter of the initial state.
	 *
	 * \param state new initial state of the automaton
	 *
	 * \returns true if the initial state was indeed changed
	 */
	bool setInitialState ( StateType state ) {
		return this->template accessComponent < InitialState > ( ).set ( std::move ( state ) );
	}

	/**
	 * Getter of states.
	 *
	 * \returns the states of the automaton
	 */
	const ext::set < StateType > & getStates ( ) const & {
		return this->template accessComponent < States > ( ).get ( );
	}

	/**
	 * Getter of states.
	 *
	 * \returns the states of the automaton
	 */
	ext::set < StateType > && getStates ( ) && {
		return std::move ( this->template accessComponent < States > ( ).get ( ) );
	}

	/**
	 * Adder of a state.
	 *
	 * \param state the new state to be added to a set of states
	 *
	 * \returns true if the state was indeed added
	 */
	bool addState ( StateType state ) {
		return this->template accessComponent < States > ( ).add ( std::move ( state ) );
	}

	/**
	 * Setter of states.
	 *
	 * \param states completely new set of states
	 */
	void setStates ( ext::set < StateType > states ) {
		this->template accessComponent < States > ( ).set ( std::move ( states ) );
	}

	/**
	 * Remover of a state.
	 *
	 * \param state a state to be removed from a set of states
	 *
	 * \returns true if the state was indeed removed
	 */
	void removeState ( const StateType & state ) {
		this->template accessComponent < States > ( ).remove ( state );
	}

	/**
	 * Getter of final states.
	 *
	 * \returns the final states of the automaton
	 */
	const ext::set < StateType > & getFinalStates ( ) const & {
		return this->template accessComponent < FinalStates > ( ).get ( );
	}

	/**
	 * Getter of final states.
	 *
	 * \returns the final states of the automaton
	 */
	ext::set < StateType > && getFinalStates ( ) && {
		return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) );
	}

	/**
	 * Adder of a final state.
	 *
	 * \param state the new state to be added to a set of final states
	 *
	 * \returns true if the state was indeed added
	 */
	bool addFinalState ( StateType state ) {
		return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
	}

	/**
	 * Setter of final states.
	 *
	 * \param states completely new set of final states
	 */
	void setFinalStates ( ext::set < StateType > states ) {
		this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
	}

	/**
	 * Remover of a final state.
	 *
	 * \param state a state to be removed from a set of final states
	 *
	 * \returns true if the state was indeed removed
	 */
	void removeFinalState ( const StateType & state ) {
		this->template accessComponent < FinalStates > ( ).remove ( state );
	}

	/**
	 * Getter of the input alphabet.
	 *
	 * \returns the input alphabet of the automaton
	 */
	const ext::set < SymbolType > & getInputAlphabet ( ) const & {
		return this->template accessComponent < InputAlphabet > ( ).get ( );
	}

	/**
	 * Getter of the input alphabet.
	 *
	 * \returns the input alphabet of the automaton
	 */
	ext::set < SymbolType > && getInputAlphabet ( ) && {
		return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) );
	}

	/**
	 * Adder of a input symbol.
	 *
	 * \param symbol the new symbol to be added to an input alphabet
	 *
	 * \returns true if the symbol was indeed added
	 */
	bool addInputSymbol ( SymbolType symbol ) {
		return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
	}

	/**
	 * Adder of input symbols.
	 *
	 * \param symbols new symbols to be added to an input alphabet
	 */
	void addInputSymbols ( ext::set < SymbolType > symbols ) {
		this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
	}

	/**
	 * Setter of input alphabet.
	 *
	 * \param symbols completely new input alphabet
	 */
	void setInputAlphabet ( ext::set < SymbolType > symbols ) {
		this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
	}

	/**
	 * Remover of an input symbol.
	 *
	 * \param symbol a symbol to be removed from an input alphabet
	 *
	 * \returns true if the symbol was indeed removed
	 */
	void removeInputSymbol ( const SymbolType & symbol ) {
		this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
	}

	/**
	 * \brief Add a transition to the automaton.
	 *
	 * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in RegExpsOver(T)
	 *
	 * \param current the source state (A)
	 * \param input the regexp (a)
	 * \param next the target state (B)
	 *
	 * \throws AutomatonException when transition contains state or symbol not present in the automaton components
	 *
	 * \returns true if the transition was indeed added
	 */
	bool addTransition ( StateType from, regexp::UnboundedRegExpStructure < SymbolType > input, StateType to );

	/**
	 * \brief Removes a transition from the automaton.
	 *
	 * \details The transition is in a form A \times a -> B, where A, B \in Q and a \in RegExpsOver(T)
	 *
	 * \param current the source state (A)
	 * \param input the regexp (a)
	 * \param next the target state (B)
	 *
	 * \returns true if the transition was indeed removed
	 */
	bool removeTransition ( const StateType & from, const regexp::UnboundedRegExpStructure < SymbolType > & input, const StateType & to );

	/**
	 * Get the transition function of the automaton in its natural form.
	 *
	 * \returns transition function of the automaton
	 */
	const ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & getTransitions ( ) const &;

	/**
	 * Get the transition function of the automaton in its natural form.
	 *
	 * \returns transition function of the automaton
	 */
	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > && getTransitions ( ) &&;

	/**
	 * Get a subset of the transition function of the automaton, with the source state fixed in the transitions natural representation.
	 *
	 * \param from filter the transition function based on this state as a source state
	 *
	 * \returns a subset of the transition function of the automaton with the source state fixed
	 */
	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > getTransitionsFromState ( const StateType & from ) const;

	/**
	 * Get a subset of the transition function of the automaton, with the target state fixed in the transitions natural representation.
	 *
	 * \param to filter the transition function based on this state as a source state
	 *
	 * \returns a subset of the transition function of the automaton with the target state fixed
	 */
	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > getTransitionsToState ( const StateType & to ) const;

	/**
	 * The three way comparison implementation
	 *
	 * \param other the other instance
	 *
	 * \returns the ordering between this object and the @p other.
	 */
	auto operator <=> ( const ExtendedNFA & other ) const {
		return std::tie ( getStates ( ), getInputAlphabet ( ), getInitialState ( ), getFinalStates ( ), transitions ) <=> std::tie ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ), other.getTransitions ( ) );
	}

	/**
	 * The equality comparison implementation.
	 *
	 * \param other the other object to compare with.
	 *
	 * \returns true if this and other objects are equal, false othervise
	 */
	bool operator == ( const ExtendedNFA & other ) const {
		return std::tie ( getStates ( ), getInputAlphabet ( ), getInitialState ( ), getFinalStates ( ), transitions ) == std::tie ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ), other.getTransitions ( ) );
	}

	/**
	 * Print this object as raw representation to ostream.
	 *
	 * \param out ostream where to print
	 * \param instance object to print
	 *
	 * \returns modified output stream
	 */
	friend std::ostream & operator << ( std::ostream & out, const ExtendedNFA & instance ) {
		return out << "(ExtendedNFA "
			   << "states = " << instance.getStates ( )
			   << "inputAlphabet = " << instance.getInputAlphabet ( )
			   << "initialState = " << instance.getInitialState ( )
			   << "finalStates = " << instance.getFinalStates ( )
			   << "transitions = " << instance.getTransitions ( )
			   << ")";
	}

	/**
	 * Casts this instance to as compact as possible string representation.
	 *
	 * \returns string representation of the object
	 */
	explicit operator std::string ( ) const;
};

/**
 * Trait to detect whether the type parameter T is or is not ExtendedNFA. Derived from std::false_type.
 *
 * \tparam T the tested type parameter
 */
template < class T >
class isExtendedNFA_impl : public std::false_type {};

/**
 * Trait to detect whether the type parameter T is or is not ExtendedNFA. Derived from std::true_type.
 *
 * Specialisation for ExtendedNFA.
 *
 * \tparam SymbolType used for the terminal alphabet of the automaton
 * \tparam StateType used for the terminal alphabet of the automaton
 */
template < class SymbolType, class StateType >
class isExtendedNFA_impl < ExtendedNFA < SymbolType, StateType > > : public std::true_type {};

/**
 * Constexpr true if the type parameter T is ExtendedNFA, false otherwise.
 *
 * \tparam T the tested type parameter
 */
template < class T >
constexpr bool isExtendedNFA = isExtendedNFA_impl < T > { };

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( ext::set < StateType > states, ext::set < SymbolType > inputAlphabet, StateType initialState, ext::set < StateType > finalStates ) : core::Components < ExtendedNFA, ext::set < SymbolType >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates >, StateType, component::Value, InitialState > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ), std::move ( initialState ) ) {
}

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( StateType initialState ) : ExtendedNFA ( ext::set < StateType > { initialState }, ext::set < SymbolType > { }, initialState, ext::set < StateType > { } ) {
}

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const CompactNFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		regexp::UnboundedRegExpConcatenation < SymbolType > con;

		for ( auto & symbol : transition.first.second )
			con.appendElement ( regexp::UnboundedRegExpSymbol < SymbolType > ( symbol ) );

		ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( std::move ( con ) ) );
		transitions.insert ( key, transition.second );
	}
}

template < class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const MultiInitialStateEpsilonNFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ) + ext::set < StateType > { common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), other.getStates ( ) ) }, other.getInputAlphabet ( ), common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), other.getStates ( ) ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		if ( transition.first.second.is_epsilon ( ) ) {
			ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpEpsilon < SymbolType > ( ) ) );
			transitions.insert ( key, transition.second );
		} else {
			ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpSymbol < SymbolType > ( transition.first.second.getSymbol ( ) ) ) );
			transitions.insert ( key, transition.second );
		}
	}

	ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( this->getInitialState ( ), regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpEpsilon < SymbolType > ( ) ) );
	for ( const StateType & to : other.getInitialStates ( ) )
		transitions.insert ( key, to );
}

template < class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const EpsilonNFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		if ( transition.first.second.is_epsilon ( ) ) {
			ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpEpsilon < SymbolType > ( ) ) );
			transitions.insert ( key, transition.second );
		} else {
			ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpSymbol < SymbolType > ( transition.first.second.getSymbol ( ) ) ) );
			transitions.insert ( key, transition.second );
		}
	}
}

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const MultiInitialStateNFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ) + ext::set < StateType > { common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), other.getStates ( ) ) }, other.getInputAlphabet ( ), common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), other.getStates ( ) ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpSymbol < SymbolType > ( transition.first.second ) ) );
		transitions.insert ( key, transition.second );
	}

	ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( this->getInitialState ( ), regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpEpsilon < SymbolType > ( ) ) );
	for ( const StateType & state : other.getInitialStates ( ) )
		transitions.insert ( key, state );
}

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const NFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpSymbol < SymbolType > ( transition.first.second ) ) );
		transitions.insert ( key, transition.second );
	}
}

template<class SymbolType, class StateType >
ExtendedNFA < SymbolType, StateType >::ExtendedNFA ( const DFA < SymbolType, StateType > & other ) : ExtendedNFA ( other.getStates ( ), other.getInputAlphabet ( ), other.getInitialState ( ), other.getFinalStates ( ) ) {
	for ( const auto & transition : other.getTransitions ( ) ) {
		ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( transition.first.first, regexp::UnboundedRegExpStructure < SymbolType > ( regexp::UnboundedRegExpSymbol < SymbolType > ( transition.first.second ) ) );
		transitions.insert ( key, transition.second );
	}
}

template<class SymbolType, class StateType >
bool ExtendedNFA < SymbolType, StateType >::addTransition ( StateType from, regexp::UnboundedRegExpStructure < SymbolType > input, StateType to ) {
	if ( !getStates ( ).count ( from ) )
		throw AutomatonException ( "State \"" + ext::to_string ( from ) + "\" doesn't exist." );

	ext::set < SymbolType > inputRegExpAlphabet = input.getStructure ( ).computeMinimalAlphabet ( );

	// Transition regexp's alphabet must be subset of automaton's alphabet
	if ( !std::includes ( getInputAlphabet ( ).begin ( ), getInputAlphabet ( ).end ( ), inputRegExpAlphabet.begin ( ), inputRegExpAlphabet.end ( ) ) )
		throw AutomatonException ( "Input string is over different alphabet than automaton" );

	if ( !getStates ( ).count ( to ) )
		throw AutomatonException ( "State \"" + ext::to_string ( to ) + "\" doesn't exist." );

	auto upper_bound = transitions.upper_bound ( ext::tie ( from, input ) );
	auto lower_bound = transitions.lower_bound ( ext::tie ( from, input ) );
	auto iter = std::lower_bound ( lower_bound, upper_bound, to, [ ] ( const auto & transition, const auto & target ) { return transition.second < target; } );
	if ( iter != upper_bound && to >= iter->second )
		return false;

	ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > > key = ext::make_pair ( std::move ( from ), std::move ( input ) );
	transitions.insert ( iter, std::make_pair ( std::move ( key ), std::move ( to ) ) );
	return true;
}

template<class SymbolType, class StateType >
bool ExtendedNFA < SymbolType, StateType >::removeTransition ( const StateType & from, const regexp::UnboundedRegExpStructure < SymbolType > & input, const StateType & to ) {
	auto upper_bound = transitions.upper_bound ( ext::tie ( from, input ) );
	auto lower_bound = transitions.lower_bound ( ext::tie ( from, input ) );
	auto iter = std::find_if ( lower_bound, upper_bound, [ & ] ( const auto & transition ) { return transition.second == to; } );
	if ( iter == upper_bound )
		return false;

	transitions.erase ( iter );
	return true;
}

template<class SymbolType, class StateType >
const ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & ExtendedNFA < SymbolType, StateType >::getTransitions ( ) const & {
	return transitions;
}

template<class SymbolType, class StateType >
ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > && ExtendedNFA < SymbolType, StateType >::getTransitions ( ) && {
	return std::move ( transitions );
}

template<class SymbolType, class StateType >
ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > ExtendedNFA < SymbolType, StateType >::getTransitionsFromState ( const StateType & from ) const {
	if ( !getStates ( ).count ( from ) )
		throw AutomatonException ( "State \"" + ext::to_string ( from ) + "\" doesn't exist" );

	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > transitionsFromState;

	for ( const std::pair < const ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & transition : transitions )
		if ( transition.first.first == from )
			transitionsFromState.insert ( transition );

	return transitionsFromState;
}

template<class SymbolType, class StateType >
ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > ExtendedNFA < SymbolType, StateType >::getTransitionsToState ( const StateType & to ) const {
	if ( !getStates ( ).count ( to ) )
		throw AutomatonException ( "State \"" + ext::to_string ( to ) + "\" doesn't exist" );

	ext::multimap < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > transitionsToState;

	for ( const std::pair < const ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & transition : transitions )
		if ( transition.second == to )
			transitionsToState.insert ( transition );

	return transitionsToState;
}

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

} /* namespace automaton */

namespace core {

/**
 * Helper class specifying constraints for the automaton's internal input alphabet component.
 *
 * \tparam SymbolType used for the terminal alphabet of the automaton.
 * \tparam StateType used for the terminal alphabet of the automaton.
 */
template<class SymbolType, class StateType >
class SetConstraint< automaton::ExtendedNFA < SymbolType, StateType >, SymbolType, automaton::InputAlphabet > {
public:
	/**
	 * Returns true if the symbol is still used in some transition of the automaton.
	 *
	 * \param automaton the tested automaton
	 * \param symbol the tested symbol
	 *
	 * \returns true if the symbol is used, false othervise
	 */
	static bool used ( const automaton::ExtendedNFA < SymbolType, StateType > & automaton, const SymbolType & symbol ) {
		for ( const std::pair < const ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & transition : automaton.getTransitions ( ) ) {
			ext::set < SymbolType > alphabet = transition.first.second.getStructure ( ).computeMinimalAlphabet ( );
			if ( alphabet.count ( symbol ) )
				return true;
		}

		return false;
	}

	/**
	 * Returns true as all symbols are possibly available to be elements of the input alphabet.
	 *
	 * \param automaton the tested automaton
	 * \param symbol the tested symbol
	 *
	 * \returns true
	 */
	static bool available ( const automaton::ExtendedNFA < SymbolType, StateType > &, const SymbolType & ) {
		return true;
	}

	/**
	 * All symbols are valid as input symbols.
	 *
	 * \param automaton the tested automaton
	 * \param symbol the tested symbol
	 */
	static void valid ( const automaton::ExtendedNFA < SymbolType, StateType > &, const SymbolType & ) {
	}
};

/**
 * Helper class specifying constraints for the automaton's internal states component.
 *
 * \tparam SymbolType used for the terminal alphabet of the automaton.
 * \tparam StateType used for the terminal alphabet of the automaton.
 */
template<class SymbolType, class StateType >
class SetConstraint< automaton::ExtendedNFA < SymbolType, StateType >, StateType, automaton::States > {
public:
	/**
	 * Returns true if the state is still used in some transition of the automaton.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 *
	 * \returns true if the state is used, false othervise
	 */
	static bool used ( const automaton::ExtendedNFA < SymbolType, StateType > & automaton, const StateType & state ) {
		if ( automaton.getInitialState ( ) == state )
			return true;

		if ( automaton.getFinalStates ( ).count ( state ) )
			return true;

		for ( const std::pair < const ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > & transition : automaton.getTransitions ( ) )
			if ( transition.first.first == state || transition.second == state )
				return true;

		return false;
	}

	/**
	 * Returns true as all states are possibly available to be elements of the states.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 *
	 * \returns true
	 */
	static bool available ( const automaton::ExtendedNFA < SymbolType, StateType > &, const StateType & ) {
		return true;
	}

	/**
	 * All states are valid as a state of the automaton.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 */
	static void valid ( const automaton::ExtendedNFA < SymbolType, StateType > &, const StateType & ) {
	}
};

/**
 * Helper class specifying constraints for the automaton's internal final states component.
 *
 * \tparam SymbolType used for the terminal alphabet of the automaton.
 * \tparam StateType used for the terminal alphabet of the automaton.
 */
template<class SymbolType, class StateType >
class SetConstraint< automaton::ExtendedNFA < SymbolType, StateType >, StateType, automaton::FinalStates > {
public:
	/**
	 * Returns false. Final state is only a mark that the automaton itself does require further.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 *
	 * \returns false
	 */
	static bool used ( const automaton::ExtendedNFA < SymbolType, StateType > &, const StateType & ) {
	return false;
}

	/**
	 * Determines whether the state is available in the automaton's states set.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 *
	 * \returns true if the state is already in the set of states of the automaton
	 */
	static bool available ( const automaton::ExtendedNFA < SymbolType, StateType > & automaton, const StateType & state ) {
		return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
	}

	/**
	 * All states are valid as a final state of the automaton.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 */
	static void valid ( const automaton::ExtendedNFA < SymbolType, StateType > &, const StateType & ) {
	}
};

/**
 * Helper class specifying constraints for the automaton's internal initial state element.
 *
 * \tparam SymbolType used for the terminal alphabet of the automaton.
 * \tparam StateType used for the terminal alphabet of the automaton.
 */
template<class SymbolType, class StateType >
class ElementConstraint< automaton::ExtendedNFA < SymbolType, StateType >, StateType, automaton::InitialState > {
public:
	/**
	 * Determines whether the state is available in the automaton's states set.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 *
	 * \returns true if the state is already in the set of states of the automaton
	 */
	static bool available ( const automaton::ExtendedNFA < SymbolType, StateType > & automaton, const StateType & state ) {
		return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
	}

	/**
	 * All states are valid as an initial state of the automaton.
	 *
	 * \param automaton the tested automaton
	 * \param state the tested state
	 */
	static void valid ( const automaton::ExtendedNFA < SymbolType, StateType > &, const StateType & ) {
	}
};

/**
 * Helper for normalisation of types specified by templates used as internal datatypes of symbols and states.
 *
 * \returns new instance of the automaton with default template parameters or unmodified instance if the template parameters were already the default ones
 */
template < class SymbolType, class StateType >
struct normalize < automaton::ExtendedNFA < SymbolType, StateType > > {
	static automaton::ExtendedNFA < > eval ( automaton::ExtendedNFA < SymbolType, StateType > && value ) {
		ext::set < DefaultSymbolType > alphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getInputAlphabet ( ) );
		ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
		DefaultStateType initialState = automaton::AutomatonNormalize::normalizeState ( std::move ( value ).getInitialState ( ) );
		ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );

		automaton::ExtendedNFA < > res ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) );

		for ( std::pair < ext::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, StateType > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
			DefaultStateType from = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) );
			regexp::UnboundedRegExpStructure < DefaultSymbolType > input = automaton::AutomatonNormalize::normalizeRegExp ( std::move ( transition.first.second ) );
			DefaultStateType target = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );

			res.addTransition ( std::move ( from ), std::move ( input ), std::move ( target ) );
		}

		return res;
	}
};

} /* namespace core */

extern template class automaton::ExtendedNFA < >;

