156 lines
3.8 KiB
Java
156 lines
3.8 KiB
Java
package chocobar.bpl;
|
|
|
|
import java.util.Vector;
|
|
import java.util.LinkedList;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
|
|
import graph.Vertice;
|
|
|
|
public class ChocoBarSet implements VerticeSet {
|
|
private int size;
|
|
private ChocoBarSetNode CBSroot;
|
|
private ChocoBar prevSearch;
|
|
private ChocoBar prevResultCB;
|
|
private boolean prevResultContains;
|
|
private int prevResultId;
|
|
private Vector assoc;
|
|
private boolean assocTainted;
|
|
|
|
public int size(){
|
|
return this.size;
|
|
}
|
|
|
|
public ChocoBarSet(){
|
|
this.size=0;
|
|
this.assoc=null;
|
|
this.assocTainted=true;
|
|
this.CBSroot=new ChocoBarSetNode();
|
|
}
|
|
|
|
public int add(Vertice vCB){
|
|
ChocoBar extCB=(ChocoBar)vCB;
|
|
int addId=this.size;
|
|
this.size=this.size+1;
|
|
ChocoBarSetNode addedCBSN=this.CBSroot.add(extCB,0,addId);
|
|
if (this.assocTainted){
|
|
this.buildAssoc();
|
|
} else {
|
|
this.assoc.add(addedCBSN);
|
|
}
|
|
if (this.prevSearch==vCB){
|
|
this.prevSearch=null;
|
|
}
|
|
return addId;
|
|
}
|
|
|
|
public void remove(Vertice vCB){
|
|
ChocoBar extCB=(ChocoBar)vCB;
|
|
int extId=this.getIdOf(extCB);
|
|
// TODO: supprimer l'element extCB/extId
|
|
for (int i=extId+1;i<this.assoc.size();i++){
|
|
ChocoBarSetNode cbsn=(ChocoBarSetNode)this.assoc.elementAt(i);
|
|
cbsn.setId(cbsn.getId()-1);
|
|
}
|
|
// décaler de -1 tous les Id suppérieurs
|
|
this.assocTainted=true;
|
|
}
|
|
|
|
public int getIdOf(Vertice vCB){
|
|
ChocoBar extCB=(ChocoBar)vCB;
|
|
if (prevSearch==extCB){
|
|
// we already made something about this
|
|
//System.out.println("Using prev value");
|
|
return this.prevResultId;
|
|
} else {
|
|
this.updateSearchValues(extCB);
|
|
return this.prevResultId;
|
|
}
|
|
}
|
|
|
|
private void updateSearchValues(ChocoBar extCB){
|
|
//System.out.println("Looking for :");
|
|
//extCB.display();
|
|
ChocoBarSetNode foundCBSN=this.CBSroot.getSimilar(extCB,0);
|
|
this.prevSearch=extCB;
|
|
if (foundCBSN==null){
|
|
//System.out.println("Not Found!");
|
|
this.prevResultCB=null;
|
|
this.prevResultId=-1;
|
|
this.prevResultContains=false;
|
|
} else {
|
|
this.prevResultCB=foundCBSN.getChocoBar();
|
|
//System.out.println("Found:");
|
|
//this.prevResultCB.display();
|
|
this.prevResultId=foundCBSN.getId();
|
|
this.prevResultContains=true;
|
|
}
|
|
}
|
|
|
|
public boolean contains(Vertice vCB){
|
|
ChocoBar extCB=(ChocoBar)vCB;
|
|
if (prevSearch==extCB){
|
|
// we already made something about this
|
|
//System.out.println("Using prev value");
|
|
return this.prevResultContains;
|
|
} else {
|
|
this.updateSearchValues(extCB);
|
|
return this.prevResultContains;
|
|
}
|
|
}
|
|
|
|
public Vertice getSimilar(Vertice vCB){
|
|
ChocoBar extCB=(ChocoBar)vCB;
|
|
if (prevSearch==extCB){
|
|
// we already made something about this
|
|
//System.out.println("Using prev value");
|
|
return (Vertice)this.prevResultCB;
|
|
} else {
|
|
this.updateSearchValues(extCB);
|
|
return (Vertice)this.prevResultCB;
|
|
}
|
|
}
|
|
|
|
private void buildAssoc(){
|
|
System.out.println("Building assoc for "+this.size);
|
|
Vector newAssoc=new Vector(this.size+1);
|
|
synchronized(newAssoc){
|
|
for (int i=0;i<this.size;i++){newAssoc.add(null); }
|
|
LinkedList fifo=new LinkedList();
|
|
fifo.addFirst(this.CBSroot);
|
|
ChocoBarSetNode curCBSN=null;
|
|
ChocoBarSetNode nextCBSN=null;
|
|
while(fifo.size()>0){
|
|
curCBSN=(ChocoBarSetNode)fifo.removeFirst();
|
|
if (curCBSN.getId()<0){
|
|
// content of CBSN is not initialized
|
|
} else {
|
|
newAssoc.setElementAt(curCBSN,curCBSN.getId());
|
|
}
|
|
Collection c=curCBSN.getSubTree().values();
|
|
Iterator i=c.iterator();
|
|
while(i.hasNext()){
|
|
nextCBSN=(ChocoBarSetNode)i.next();
|
|
fifo.addFirst(nextCBSN);
|
|
}
|
|
|
|
}
|
|
}
|
|
this.assoc=newAssoc;
|
|
this.assocTainted=false;
|
|
}
|
|
|
|
public Vertice elementAt(int eIdx){
|
|
if (this.assocTainted){
|
|
//tout reconstruire
|
|
this.buildAssoc();
|
|
return this.elementAt(eIdx);
|
|
} else {
|
|
// renvoyer l'element eIdx de this.assoc
|
|
ChocoBarSetNode cbsn=(ChocoBarSetNode)this.assoc.elementAt(eIdx);
|
|
return cbsn.getChocoBar();
|
|
}
|
|
}
|
|
|
|
}
|