/* File: PotentialList.h */


#ifndef __PotentialList_h__
#define __PotentialList_h__

using namespace std;

namespace BIOS
{

Container<PotentialTable, ListOfPointers>* Container<PotentialTable, ListOfPointers>::product(Container<PotentialTable, ListOfPointers>* otherPotentialList, bool product)
{
Container<PotentialTable, ListOfPointers>::NodePointer p=otherPotentialList->GetFirst();
Container<PotentialTable, ListOfPointers>::NodePointer p2=GetFirst();
Container<PotentialTable, ListOfPointers>* result=new Container<PotentialTable, ListOfPointers>();
bool found;
PotentialTable* pt;
while(p!=NULL)
{
found=false;
while(p2!=NULL && !found)
{
 if (GetElement(p2)->varList->includes(otherPotentialList->GetElement(p)->varList))
{
found=true;
pt=GetElement(p2)->product(otherPotentialList->GetElement(p), product);
result->insertElement(pt);
}
p2=GetNext(p2);
}
p=otherPotentialList->GetNext(p);
}
return result;
}

/*____________________________________________________________________________________ */
 
Container<PotentialTable, ListOfPointers>* Container<PotentialTable, ListOfPointers>::operator/(Container<PotentialTable, ListOfPointers>* otherPotentialList)
{
product(otherPotentialList, false);
}
/*____________________________________________________________________________________ */
 
Container<PotentialTable, ListOfPointers>* Container<PotentialTable, ListOfPointers>::operator*(Container<PotentialTable, ListOfPointers>* otherPotentialList)
{
product(otherPotentialList, true);
}
/*____________________________________________________________________________________ */

typedef Container<PotentialTable, ListOfPointers> PotentialList;

PotentialList* PotentialList::marginalize(intList marginalList, intList* inputPattern, ListOfAttributes* listOfAttributes);

 
Container<PotentialTable, ListOfPointers>* Container<PotentialTable, ListOfPointers>::marginalize(intList marginalList, intList* inputPattern, ListOfAttributes* listOfAttributes)
{
PotentialTable* marginal;
Container<PotentialTable, ListOfPointers>* marginalPotentials=new Container<PotentialTable, ListOfPointers>();
Container<PotentialTable, ListOfPointers>::NodePointer p=GetFirst();
intList* adaptedMarginalList, *extendedVarlist;
while (p!=NULL)
{
adaptedMarginalList=GetElement(p)->varList->copyElementsIn(marginalList);
extendedVarlist=GetElement(p)->addMissingVarList(adaptedMarginalList, inputPattern, listOfAttributes);
marginal=GetElement(p)->marginalize(adaptedMarginalList);
marginalPotentials->insertElement(marginal);
zap(marginal);
p=GetNext(p);
}
return marginalPotentials;
}
};  // Fin del Namespace

#endif