/*
 * UndistinguishableStates2.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: 27. 3. 2019
 *	  Author: Tomas Pecka
 */

#ifndef DISTINGUISHABLE_STATES_PARTITIONING_H_
#define DISTINGUISHABLE_STATES_PARTITIONING_H_

#include <alib/set>
#include <alib/map>

#include <automaton/FSM/DFA.h>
#include <automaton/TA/DFTA.h>

#include <automaton/properties/DistinguishableStates.h>

namespace automaton {

namespace properties {

/**
 * Partition undistinguishable states in automata
 *
 * @sa DistinguishableStates
 */
class UndistinguishableStates2 {
public:
	/**
	 * Creates state partitioning of undistinguishable states using DistinguishableStates algorithm
	 *
	 * @tparam T Type of the parameter automaton.
	 * @param dfta automaton which states are to be partitioned
	 *
	 * @sa DistinguishableStates
	 *
	 * @return state partitioning of undistinguishable states
	 */
	template < class T >
	static ext::set < ext::set < typename T::StateType > > undistinguishable ( const T & fsm );
};

template < class T >
ext::set < ext::set < typename T::StateType > > UndistinguishableStates2::undistinguishable ( const T & fsm ) {
	using StateType = typename T::StateType;

	const ext::set < ext::pair < StateType, StateType > > distinguishable = automaton::properties::DistinguishableStates::distinguishable ( fsm );
	ext::set < ext::set < StateType > > res;

	for ( const StateType & state : fsm.getStates ( ) ) {
		ext::set < StateType > partition;

		for ( const StateType & other : fsm.getStates ( ) )
			if ( distinguishable.count ( ext::make_pair ( state, other ) ) == 0 )
				partition.insert ( other );

		res.insert ( partition );
	}

	return res;
}

} /* namespace properties */

} /* namespace automaton */

#endif /* DISTINGUISHABLE_STATES_H_ */
