package chocobar.bpl; import java.util.*; // listes import exception.*; class EdgeListModel implements EdgeModel { private HashMap[] adjacent; private HashMap[] incident; private int cardV; /** * Constructeur par defaut **/ public EdgeListModel(){ this(10); this.cardV=0; } /** * Constructeur d'un modele de graphe avec nbVertices sommets **/ public EdgeListModel(int nbVertices){ if (nbVertices>=0){ this.cardV=nbVertices; this.adjacent=new HashMap[this.cardV+(this.cardV/3)]; this.incident=new HashMap[this.cardV+(this.cardV/3)]; } } /** * Fonction qui renvoie le nombre de sommets de cette liste * d'adjacence **/ public int getCardV(){ return this.cardV; } /** Fonction qui réinitialise à la taille donnée. */ public int setSize(int size) { cardV=size; adjacent=new HashMap[this.cardV+(this.cardV/3)]; incident=new HashMap[this.cardV+(this.cardV/3)]; return size; } /** * Fonction qui rajoute un arc de srcV vers dstV **/ public void addEdge(int srcV,int dstV) throws OutOfRangeVerticeException { // on vérifie que les arcs ajoutés joignent des // sommets existants dans le graphe if ((srcV<0) ||(dstV<0) ||(srcV>=cardV) || (dstV>=cardV)) { throw new OutOfRangeVerticeException(); } else { if (this.adjacent[srcV]==null){ this.adjacent[srcV]=new HashMap(); } if (this.incident[dstV]==null){ this.incident[dstV]=new HashMap(); } HashMap srcList=this.adjacent[srcV]; HashMap dstList=this.incident[dstV]; Integer d=new Integer(dstV); Integer s=new Integer(srcV); if (!srcList.containsKey(d)){srcList.put(d,null);} if (!dstList.containsKey(s)){dstList.put(s,null);} } } /** * Fonction qui verifie s'il existe un arc * de srcV vers dstV **/ public boolean isEdge(int srcV,int dstV){ if (this.adjacent[srcV]==null){ return false; } //works too with incident... HashMap srcAdj=this.adjacent[srcV]; if (srcAdj.containsKey(new Integer(dstV))){ return true; } else { return false; } } /** * 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){ HashMap srcAdj=this.adjacent[src]; int taille=srcAdj.size(); int adj=-1; Object[] adjacents=srcAdj.keySet().toArray(); if (taille>dst.size()){ //y a des sommets qui ne sont pas dans le tableau return -1; } else { for (int i=0; i cardV < 3/4 * adj.length if ((4*this.cardV) > (3*this.adjacent.length)){ System.out.println("ADDVERTICE: duplicating size: "+ this.cardV+"/"+this.adjacent.length+"..."); int extra=2*this.adjacent.length; HashMap[] adjCopy = new HashMap[this.adjacent.length + extra]; HashMap[] incCopy = new HashMap[this.adjacent.length + extra]; System.arraycopy(this.adjacent,0,adjCopy,0,this.adjacent.length); System.arraycopy(this.incident,0,incCopy,0,this.incident.length); this.adjacent=adjCopy; this.incident=incCopy; System.out.println("ADDVERTICE: duplicating done."); } this.cardV++; } public void removeVerticeAt(int index) throws OutOfRangeVerticeException{ //TODO: écrire!!! /* if (index >= this.cardV){ throw (new OutOfRangeVerticeException()); } else { }*/ } }