m1.chocobarlite/src/chocobar/combi/EdgeBitSetModel.java
2009-05-01 08:07:06 +00:00

228 lines
4.8 KiB
Java

package chocobar.combi;
import java.util.*; // listes
import exception.*;
public class EdgeBitSetModel implements EdgeModel {
private int cardV;
// Bit Array of (x, y) -> (x * cardV + y)
private BitSet adjacent;
/**
* Default constructor
**/
public EdgeBitSetModel() {
this(10);
this.cardV=0;
}
/**
* Constructor with card (number of vertices) as a parameter
**/
public EdgeBitSetModel(int card) {
this.cardV=card;
this.adjacent=new BitSet(0, (long)card * card);
}
/**
* Fonction qui renvoie la dimension de cette matrice d'adjance
**/
public int getCardV(){
return this.cardV;
}
/** Fonction qui réinitialise à la taille donnée.
*/
public int setSize(int size)
{
cardV=size;
adjacent=new BitSet(0, (long)size * size);
return size;
}
/**
* Fonction qui rajoute un arc partant du sommet srcV
* vers sommet dstV
**/
public void addEdge(int srcV,int dstV)
throws OutOfRangeVerticeException
// plustard qu'on parse un graphe et rajoute des arcs sans
// passer par l'initialisation, il faut detecter le cas qu'on
// rajoute un arc n'importe quoi
{
if ((srcV<0) ||(dstV<0)
||(srcV>=cardV) || (dstV>=cardV)) {
throw new OutOfRangeVerticeException();
}
adjacent.setBit(srcV * cardV + dstV);
}
/**
* Fonction qui rajoute un arc partant du sommet srcV
* vers sommet dstV
**/
public void delEdge(int srcV,int dstV)
throws OutOfRangeVerticeException
// plustard qu'on parse un graphe et rajoute des arcs sans
// passer par l'initialisation, il faut detecter le cas qu'on
// rajoute un arc n'importe quoi
{
if ((srcV<0) ||(dstV<0)
||(srcV>=cardV) || (dstV>=cardV)) {
throw new OutOfRangeVerticeException();
}
adjacent.clearBit(srcV * cardV + dstV);
}
/**
* Fonction qui verifie l'existence d'un arc du sommet srcV
* vers sommet dstV
**/
public boolean isEdge(int srcV,int dstV){
return adjacent.testBit(srcV * cardV + dstV);
}
/**
* Fonction qui verifie si les sommets adjacents a src sont
* uniquement les sommets contenus dans le tableau dst
* Si non alors elle renvoie -1 et si oui elle renvoie le premier
* sommet dst adjacent a src
**/
public int withOnlyEdges(int src, Vector dst){
Collection srcAdj = getAdjacent(src);
int taille = srcAdj.size();
int adj=-1;
Object[] adjacents=srcAdj.toArray();
if (taille>dst.size()){
//y a des sommets qui ne sont pas dans le tableau
return -1;
} else {
for (int i=0; i<taille; i++){
if (!dst.contains(adjacents[i])){
return -1;
} else {
if (adj==-1){
adj=((Integer)adjacents[i]).intValue();
}
}
}
}
return adj;
}
/**
* Fonction qui renvoie la source (sommet racine) de ce graphe
* ou -1 s'il n'existe pas
**/
public int getSourceVertice(){
int i = -1;
if (this.cardV != 0){
i=0;
for (int j=0; j<this.cardV; j++){
if (isEdge(j, i)){
i=j;
}
}
for (int j=0; j<this.cardV; j++){
if (!isEdge(i, j)){
return -1;
}
}
for (int j=0; j<this.cardV; j++){
if (isEdge(j, i)){
return -1;
}
}
}
return i;
}
/**
* Fonction qui renvoie la puis (sommet final) de ce graphe
* ou -1 s'il n'existe pas
**/
public int getFinalVertice(){
int i = -1;
if (this.cardV != 0){
i=0;
for (int j=i+1; j<this.cardV; j++){
if (isEdge(i, j)){
i=j;
}
}
for (int j=0; j<this.cardV; j++){
if (isEdge(i, j)){
return -1;
}
}
for (int j=0; j<this.cardV; j++){
if (!isEdge(j, i)){
return -1;
}
}
}
return i;
}
/**
* Fonction qui renvoie une liste d'adjacence du sommet srcV
**/
public Collection getAdjacent(int srcV){
LinkedList res=new LinkedList();
for (int i=0; i<cardV; i++){
if (isEdge(srcV, i))
res.add(new Integer(i));
}
return res;
}
public Collection getIncident(int dstV){
//TODO: écrire la méthode !!!
return new Vector();
}
/**
* Fonction qui efface tous les arcs du graphe
**/
public void clear()
{
adjacent = new BitSet(0, (long)cardV * cardV);
}
/**
* Fonction qui cree tous les arcs possibles du graphe
**/
public void fill(){
// we add edges between every vertice
for (int i=0; i<this.cardV;i++){
for (int j=0;j<this.cardV;j++){
try { addEdge(i, j); } catch (OutOfRangeVerticeException e) { }
}
}
// but a vertice cannot have an edge to itself
for (int i=0;i<this.cardV;i++){
try { delEdge(i, i); } catch (OutOfRangeVerticeException e) { }
}
}
/**
* Fonction qui rajoute un vertice (sommet) a la liste des arcs du graphe
* les arcs sont encore VIDE
**/
public void addVertice()
{
}
public void removeVerticeAt(int rmIdx)
{
}
}