(* JDTLRQ[] ------------------------------------------------------------- *)

(* Checks to see if psst has the JDT property and the LR property. *)

(* Utils Needed: AntiChnsIdls[], Ideel[], InvStdTblx[]. *)
(* Function Needed: InvExtsWRI[]. *)
(* Must be run within JDTLRscan.nb for the variables invrsex[*] and szord,
	and for the equations from stdisos* and lookups*. *)

JDTLRQ[psst_] := Module[{Slde,Migrt,nb=Length[psst],
	nbset,idbysz,jdt=True,lr=True,colmn},

	nbset = Range[nb];
	
	Slde[redss_,bubbl_] := Slde[redss,bubbl] = Module[
		{rds=redss,bub=bubbl,temp,mvup},
		While[ Intersection[psst[[bub]],rds] \[NotEqual] {},
			temp = bub;
			mvup = Max[Flatten[
				Position[rds, x_ /; MemberQ[psst[[bub]],x], {1}]
			]];
			bub = rds[[mvup]];
			rds = ReplacePart[rds,temp,mvup]
		];
		{bub,rds}
	];
	Migrt[lwgrs_,rrdds_,upgrs_ /; upgrs\[Equal]{}] := {lwgrs,rrdds};
	Migrt[lwgrs_,rrdds_,upgrs_] := Migrt[
		Append[lwgrs,Slde[rrdds,upgrs[[1]]][[1]]],
		Slde[rrdds,upgrs[[1]]][[2]],
		Drop[upgrs,1]
	];

	idbysz = Table[
		Select[ AntiChnsIdls[psst][[2]], (Length[#]\[Equal]iw)& ],
		{iw,2,nb-2}
	];

	Do[  (* Runs through sizes of ideals in the order determined by szord. *)
		siz = szord[[szindx]];
	
		Do[                 (* Runs through all ideals of the current size. *)
			ideal = idbysz[[siz-1,jid]];
			filter = Complement[nbset,ideal];
	  		(* Compute inverse extensions of the current ideal and filter. *)
			redies = InvStdTblx[psst,ideal,InvExtsWRI];
			grnies = InvStdTblx[psst,filter,InvExtsWRI];
			
			(* Compute all resulting labelings of psst when the elements of the
				filter are slid past the elements of the ideal. *)
			rslts = Outer[ Migrt[{},#1,#2]&, redies, grnies, 1 ];

			Do[
				If[ !jdt, Break[] ];
				(* Determine whether for a given red labeling of the ideal, all 
					green labelings of the filter lead to the same final red 
					labeling. *)
				jdt = Apply[ Equal, Map[#[[2]]&,rslts[[rw]]] ]
			,{rw,Length[redies]}];

			If[ lr,
				Do[
				(* Compare the results for cl-th green labeling of the filter
					to the list of ordered pairs of distinct lower green 
					outputs and the red labelings of the complementary filter. *) 
					lr = Equal[  
					        (* Output green/red pairs for this green labeling. *)
						Sort[ colmn = Map[#[[cl]]&,rslts] ], 
						Sort[
							Flatten[
						(* Creates all green/red pairs for given green output. *)
								Map[
									Function[x,
										Map[
											{x,#}&,
											InvStdTblx[ 
												psst,
												Complement[nbset,x],
												InvExtsWRI
											]
										]
									],                        (* End of Function[x *)
									      (* Distinct lower green output tableaux. *)
									Union[Map[#[[1]]&,colmn]] 
								],                           (* End of Outer Map[ *)
							1]                                (* End of Flatten[ *)
						]                                       (* End of Sort[ *)
					];                                        (* End of Equal[ *)	
	
					If[ lr, Break[] ] 
				,{cl,Length[grnies]}];           (* End of loop thru columns. *)
			];

			If[ !jdt&&!lr, Break[] ]     (* Exit jid loop if both are false. *)

		,{jid,Length[idbysz[[siz-1]]]}];   (* End of ideals-this-size loop. *)

		If[ !jdt&&!lr, Break[] ]      (* Exit outer loop if both are false. *)

	,{szindx,nb-3}];                       (* End of loop for ideal sizes. *)

	{jdt,lr}                       (* Return ordered pair of test results. *)
];