// CuddPlaLoad.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include "PlaFile.h"
#include "malloc.h"
#include "CuddPlaLoad.h"


int Cudd_PlaLoad(DdManager *manager, char *path, BddArray *result){
	DdNode *tmp,*var,*fAdd;	
	PlaData data;
	PlaOpen(path, &data);
	result->fRoot = (DdNode**)malloc(data.o*sizeof(DdNode));
	result->i = data.i;
	result->o = data.o;
	for(int i=0;i<data.o;i++) result->fRoot[i]=Cudd_ReadLogicZero(manager);
	for (i=0; i<data.p;i++){
		fAdd=Cudd_ReadOne(manager);
		Cudd_Ref(fAdd);
		for (int j=0;j<data.i;j++){
			var=Cudd_bddIthVar(manager,j);			
			if (data.iArray[i][j]==1) tmp=Cudd_bddAnd(manager, var,fAdd);
			else if (data.iArray[i][j]==0) tmp=Cudd_bddAnd(manager, Cudd_Not(var),fAdd);			
			if(data.iArray[i][j]== 1 || data.iArray[i][j]==0) {
				Cudd_Ref(tmp);			
				Cudd_RecursiveDeref(manager, fAdd);			
				fAdd = tmp;
			}
		}
		for(j=0;j<data.o;j++){
			if(data.oArray[i][j] == 1) {
				tmp = Cudd_bddOr(manager,result->fRoot[j],fAdd);						
				Cudd_Ref(tmp);
				Cudd_RecursiveDeref(manager, result->fRoot[j]);
				result->fRoot[j] = tmp;
			}
		}
		Cudd_RecursiveDeref(manager, fAdd);
	}
	return 1;
}


