(* GenrlUtils.m --------------------------------------------------------- *)


(* CmprssBiList[] ------------------------------------------------------- *)

(* Compresses lstprs in the first or second slot indicated by slt (=1 or 2), 
	and combines the entries in the other slot according to the rule fnct. *)

CmprssBiList[lstprs_,slt_,fnct_] :=
	Map[
		Function[x,
			Switch[slt,
				1,{x,fnct[Map[#[[2]]&,Select[lstprs,(#[[1]]\[Equal]x)&]]]},
				2,{fnct[Map[#[[1]]&,Select[lstprs,(#[[2]]\[Equal]x)&]]],x}
			]
		],
		Union[Map[#[[slt]]&,lstprs]]
	];



(* NoSpcs[] ------------------------------------------------------------- *)

(* Given a nested list-of-lists outlst with varying depths,
	WriteString[stream,NoSpcs[outlst],"\n"] will write outlst in one 
	record with no embedded spaces, followed by a newline command.  *)

Format[NoSpcs[lsstt_]] :=
	Map[If[Length[#]\[Equal]0,
		"{}",
		"{"<>Apply[ StringJoin,
			MapAt[StringJoin[#,","]&,#,
				Drop[Position[#,_String,{1}],-1]] ]<>"}"
		]&,
		Map[ToString,lsstt,{-1}],
		{0,-2}
	];


(* WriteLstOfLst[] ------------------------------------------------------ *)

(* Writes a list of lists to a file, one entry per line. *)

WriteLstOfLst[file_,lsst_] := Module[{},
	OpenWrite[file, FormatType\[Rule]OutputForm];
	Map[ WriteString[file,NoSpcs[#],"\n"]&, lsst ];
	Close[file]
];



(* PrntCnsmptn[] -------------------------------------------------------- *)

(* Prints change in memory from strtmem to endmem, change in actual time 
	from strttim to endtim, change in cpu time from strtcpu to endcpu. *)

PrntCnsmptn[strtmem_,endmem_,strttim_,endtim_,strtcpu_,endcpu_] := Module[
	{mm=(endmem-strtmem)/1000000, tm=(endtim-strttim)/60, 
		cp=(endcpu-strtcpu)/60},
	
	Print["MB of Memory Used: "];
	Print[ToString[ScientificForm[mm//N,3]]];

	Print["Minutes of real time: "];
	Print[ToString[ScientificForm[tm,3]]]; 

	Print["Minutes of CPU Time Used: "];
	Print[ToString[ScientificForm[cp,3]]];
];