-- AUTORA: Elvira Mayordomo Cámara
-- PROYECTO: módulo de implementación del TAD
-- 	  lrestricciones solución
--	     del examen de febrero de 2001 de la asignatura de 
--	     Estructuras de Datos y Algoritmos
-- FICHERO: lrestricciones.adb
-- FECHA: 6-2-01; 12-1-04

package body lrestricciones is

	function
	h(palabra:ustring;tam:integer) return integer is
  	-- función hash por el método de la división con módulo numMax 
	-- coste en tiempo O(longitud(palabra))=O(1)
  	res:integer;
  	begin
  		res:=0;
		for i in 1..Length(palabra) loop
		
res:=(res+character'pos(element(palabra,i)))mod
tam;
		end loop;				  
  		return res;
  	end h;
     
  procedure vacio(p:out planEstudios) is
	-- coste en tiempo O(numMax)
  aux:ptnodo;
  begin
  	tablavacia(p.asig);
	aux:=new nodo'(nulo,-1);
	p.lalista:=new rnombre'(U("titulo"),aux,null);
	modificar(p.asig,U("titulo"),aux);
	p.titulo:=aux;
  end vacio;
  
  procedure anyadir(p:in out planEstudios; x:in prerequisito) is
  -- Pre: x.pres(0) está en p
	-- coste en tiempo O(1)
  aux,aux2:ptnodo;
  begin
  	aux:=consultar(p.asig,x.pres(0));
  	for i in 1..x.numasig loop
		if esta(p.asig,x.pres(i)) then 
			aux2:=consultar(p.asig,x.pres(i));
			aux.ptrequisitos(i):=aux2;
		else
			aux2:=new nodo'(nulo,-1);
			modificar(p.asig,x.pres(i),aux2);
			aux.ptrequisitos(i):=aux2;
			p.lalista:=new rnombre'(x.pres(i),aux2,p.lalista);
		end if;
	end loop;
  end anyadir;

  procedure cortar(l:in ustring;x:out prerequisito) is
  num,prin,j,n:integer;
  -- guarda la restricción de l en x
  -- es puro formato, no interesa
  begin
  	if element(l,1)='<' then
		n:=4;
		x.pres(0):=U("titulo");
	else
		n:=1;
		while element(l,n)/='<' loop
			n:=n+1;
		end loop;
		x.pres(0):= To_Unbounded_String(n-2);
		n:=n+4;
		for i in 1..Length(x.pres(0)) loop
			replace_element(x.pres(0),i,element(l,i));
		end loop;
	end if;
	prin:=n;
	x.numasig:=0;
	for i in n..Length(l) loop
		if (element(l,i)=';')  then
			x.numasig:=x.numasig+1; 
			x.pres(x.numasig):=To_Unbounded_String(i-prin);
			prin:=i+2;
		end if;
	end loop;			
	num:=0;
	j:=1;
	for i in n..Length(l) loop
		if element(l,i)=';' then
			num:=num+1;
			j:=1;
		elsif element(l,i)/=' ' then
			replace_element(x.pres(num),j,element(l,i));
			j:=j+1;
		end if;
	end loop;				
  end cortar;
	  
  procedure leer(p:out planEstudios) is
  -- pide el nombre de un fichero y copia la información del
  -- mismo en el plan de estudios p
  f:file_type;
  nombre,linea:ustring;
  x:prerequisito;
  begin
  put_line("Nombre del fichero donde está:");
  get_line(nombre);
  open(f,in_file,S(nombre));
  vacio(p);
  while not end_of_file(f) loop
      get_line(f,linea);
     cortar(linea,x);
      skip_line(f);
      anyadir(p,x);
  end loop;
  close(f);
  end leer;
  
  function maxSemestres(p:planEstudios) return integer is
	-- coste en tiempo O(n^2), donde n es
	-- el número de asignaturas
  aux,ptitulo:ptnodo;
  terminado,calculada:boolean;
  altura:integer;
  laux:lista;
  begin
  	terminado:=false;
	while not terminado loop
		terminado:=true;
  		laux:=p.lalista;
		while laux/=null loop
			if laux.posicion.altura=-1 then
			altura:=0;
			calculada:=true;
			for i in 1..4 loop
				if laux.posicion.ptrequisitos(i)=null then
					laux.posicion.altura:=integer'max(0,laux.posicion.altura);
				elsif laux.posicion.ptrequisitos(i).altura/=-1 then
			
				
laux.posicion.altura:=integer'max(laux.posicion.altura,laux.posicion.ptrequisitos(i).altura+1);
				else
					calculada:=false;
				end if;
			end loop;
			terminado:=terminado and calculada;	
			end if;
			laux:=laux.sig;
		end loop;
	end loop;	
  	ptitulo:=consultar(p.asig,U("titulo"));
    return ptitulo.altura;
  end maxSemestres;

end lrestricciones;


syntax highlighted by Code2HTML, v. 0.9.1