package iad;

import iad.*;
import java.util.Vector;

public class Tarjan {
	/**
	 *Atributs privés
	 */
	private Cellule representant;
	private Vector ensemble;
	/**
	 *Constructeur
	 *@param representant
	 */
	public Tarjan(Cellule representant) {
		this.representant=representant;
		this.ensemble=new Vector();
		ensemble.addElement(representant);
	}
	/**
	 *Retourne le représentant de l'ensemble
	 */
	public Cellule getRepresentant() {
		return this.representant;
	}
	/**
	 *Modifie le représentant de l'ensemble
	 *param representant
	 */
	public void setRepresentant(Cellule representant){
		this.representant=representant;
		for (int i=0; i<ensemble.size(); i++)
			((Cellule) ensemble.elementAt(i)).setEnsemble(this.representant.getEnsemble());
	}
	/**
	 *Retourne l'ensemble des cellules
	 *return ensemble
	 */
	public Vector getEnsemble(){
	  	return this.ensemble;
	}
	/**
	 *Test si une cellule appartient à cet ensemble
	 */
	 public boolean isPresent(Cellule c) {
	 	for (int i=0; i<ensemble.size(); i++) {
	 		if (c.getIdentificateur() == ((Cellule)ensemble.elementAt(i)).getIdentificateur()) {
	 			return true;
	 		}
	 	}
	 	return false;
	}
	/**
	 *Union de deux ensembles
	 *param ensemble Tarjan t
	 */
	public void union(Tarjan t) {
		t.setRepresentant(this.representant);
	  	for (int i=0; i<t.getEnsemble().size(); i++) {
	  		Cellule c = (Cellule) (t.getEnsemble().elementAt(i));
	  		c.setEnsemble(this.representant.getEnsemble());
	  		this.ensemble.add(c);
	  	}
	}
	/**
	 *Comparaison de deux cellules
	 *param cellule
	 *return true ou false
	 */
	 public boolean comparaison(Cellule c) {
	 	return (representant.getEnsemble() == c.getEnsemble());
	}
	/**
	 *Representation d'un ensemble de Tarjan
	 */
	 public String toString() {
	 	String s = "[<";
	 	s = s.concat(Integer.toString(representant.getIdentificateur()));
	 	s = s.concat(">");
	 	for (int i=0; i<ensemble.size(); i++){
	 		s = s.concat(", ");
	 		Cellule cel = (Cellule) ensemble.elementAt(i);
	 		s = s.concat(Integer.toString(cel.getIdentificateur()));
	 	}
	 	s = s.concat("]\n");
	 	return s;
	} 
	 		
}