// EnsembleDeMots.java

import java.util.Enumeration;
import java.util.Vector;

public class EnsembleDeMots {
   private Vector ensembleDeMots;

   EnsembleDeMots() {
      this.ensembleDeMots = new Vector();
   }

   public void ajouter(Mot x) {
      this.ensembleDeMots.addElement(x);
   }

   public Enumeration elements() {
      return this.ensembleDeMots.elements();
   }

   public PetitAutomate petitAutomate(Alphabet alphabet) {
      int init = 0, term = 0, p = 1;
      int[] masq = new int[alphabet.getCard()];
      Enumeration e = this.elements();
      Mot x;

      while (e.hasMoreElements()) {
         init |= p;
         x = (Mot)e.nextElement();
         for (int i = 0; i < x.getLongueur(); ++i) {
            masq[alphabet.rang(x.lettreA(i))] |= p;
            p <<= 1;
         }
         term |= (p >> 1);
      }
      return new PetitAutomate(init, masq, term);
   }

   Automate arbre() {
      Automate automate = new Automate();
      Enumeration e = this.elements();
      Etat p, t;
      Mot x;

      while (e.hasMoreElements()) {
         t = automate.getInitial();
         x = (Mot)e.nextElement();
         for (int i = 0; i < x.getLongueur(); ++i) {
            p = t.getCible(x.lettreA(i));
            if (p == null) {
               p = new Etat();
               t.setCible(x.lettreA(i), p);
            }
            t = p;
         }
         t.setTerminal(true);
      }
      return(automate);
   }

}