(* ThrghBldUp.nb -------------------------------------------------------- *)

(* For a given n, all naturally labeled posets of size n-1 are read in.
	Files are created that contain the following for posets of size n:
	1. All naturally labeled posets,
	2. Standard forms of the posets,
	3. Inverse order extensions for the standard posets,
	4. Equations for the inverse rule for putting a poset in standard form,
	5. Equations for the positions of the standard posets in File 2. *)

(* Utils Needed: AntiChnsIdls[],Relabl[],CmprssBiList[],NoSpcs[],
	WrtLstOfLst[]. *)
(* Function Needed: BasInvExts[]. *)
(* Data Needed: natlabs* for * = n-1. *)

(* User must specify the directory and size of posets considered. *)

(* Change the directory for the files: *)
SetDirectory["G:/.isis/home/c/a/cagann/public_html/Posets"];

(* Change the size, n, of posets: *)    pstsiz = 1;

<< GenrlUtils.m;    << PosetUtils.m;    << InvExIsoFncts.m;

stmem = MaxMemoryUsed[]; sttim = AbsoluteTime[]; stcpu = TimeUsed[];
lststds = {}; lstinvs = {}; lstisos = {};

num = ToString[pstsiz];
outf1 = StringJoin["NatLabs/natlabs",num];
outf2 = StringJoin["ThrStds/thrstds",num];
outf3 = StringJoin["InvExts/invexts",num];
outf4 = StringJoin["StdIsos/stdisos",num];
outf5 = StringJoin["Lookups/lookups",num];

(* Extend naturally labeled posets of length pstsiz-1 to form all
	those of the desired size. *)
Which[
	pstsiz \[Equal] 1,
	augmentatns = {{{}}},
	pstsiz > 1,
	augmentatns = Sort[Flatten[
		Map[Function[x,
			Map[ Append[x,#]&, AntiChnsIdls[x][[1]] ]
		], ReadList[StringJoin["NatLabs/natlabs",ToString[pstsiz-1]]] ]
	,1]]
];
	
inputlist = augmentatns;

(* First occurrence of a poset is added to the list of standard forms. Then 
	all alternate labelings of that poset will be removed from inputlist.  
	Loop ends when all posets generated above have been removed. *)

While[inputlist \[NotEqual] {},
	AppendTo[lststds,inputlist[[1]]];
	
	(* Find all alternate naturally labeled posets for inputlist[[1]]. *)
	invext = BasInvExts[ inputlist[[1]] ];
	relabelpairs = Sort[ Map[{Relabl[inputlist[[1]],#],#}&,invext] ];
	irredunds = CmprssBiList[ relabelpairs,1,#[[1]]& ];
	alterns = Map[#[[1]]&,irredunds];
	
	crrntlngth = Length[inputlist];
	(* Elements with same standard form are removed from inputlist. *)
	inputlist = Complement[inputlist,alterns];

	(* Check to see if we previously generated all the alternate
		forms. If so, we add the desired info to the relevant lists *)
	Which[
		Length[inputlist] \[Equal] (crrntlngth - Length[alterns]),
		AppendTo[lstinvs,invext];
		AppendTo[lstisos,irredunds],

		Length[inputlist] \[NotEqual] (crrntlngth - Length[alterns]),
		Print["Error: Not all naturally labeled posets were generated"];
		Return   	
	];
];

lstisos = Flatten[lstisos,1];

WriteLstOfLst[outf1,augmentatns];
WriteLstOfLst[outf2,lststds];
WriteLstOfLst[outf3,lstinvs];
OpenWrite[outf4, FormatType\[Rule]OutputForm];
Map[WriteString[ outf4,
	StringJoin[ "s[", ToString[NoSpcs[#[[1]]]], "] = ",		
	ToString[NoSpcs[#[[2]]]], ";", "\n" ] ]&,
lstisos];
Close[outf4];
OpenWrite[outf5, FormatType\[Rule]OutputForm];
Map[WriteString[ outf5,
	StringJoin[ "m[", ToString[NoSpcs[#]], "] = ",		
	ToString[Position[lststds,#][[1,1]]], ";", "\n" ] ]&,
lststds];
Close[outf5];

emem = MaxMemoryUsed[]; etim = AbsoluteTime[]; ecpu = TimeUsed[];
PrntCnsmptn[stmem,emem,sttim,etim,stcpu,ecpu];

(* End of ThrghBldUp.nb. *)