[WIP] Useless code removal and code cleanup #5

Manually merged
regar42 merged 5 commits from fixUnneededCode into lastDijsktraClasses 2017-12-10 16:48:34 +00:00
10 changed files with 41 additions and 94 deletions

No files matched your search

+11 -3
View File
@@ -1,5 +1,7 @@
import dijkstra.Dijkstra; import java.util.ArrayList;
import maze.Maze;
import dijkstra.*;
import maze.*;
public class MainTest { public class MainTest {
@@ -7,7 +9,13 @@ public class MainTest {
Maze mazeTest = new Maze("data/labyrinthe.txt") ; Maze mazeTest = new Maze("data/labyrinthe.txt") ;
Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]) ; Previous previous = (Previous) Dijkstra.dijkstra(mazeTest, mazeTest.getMaze()[1][1]);
ArrayList<VertexInterface> shortest = previous.getShortestPathTo(mazeTest.getMaze()[5][5]);
for (VertexInterface vertex : shortest) {
System.out.println(vertex);
}
mazeTest.saveToTextFile("data/labyOutput.txt"); mazeTest.saveToTextFile("data/labyOutput.txt");
+3 -6
View File
@@ -2,8 +2,6 @@ package dijkstra;
import java.util.HashSet; import java.util.HashSet;
import maze.Box;
public class ASet implements ASetInterface { public class ASet implements ASetInterface {
private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ; private HashSet<VertexInterface> vertexHashSet = new HashSet<VertexInterface>() ;
@@ -11,20 +9,19 @@ public class ASet implements ASetInterface {
public ASet(HashSet<VertexInterface> vertexHashSet) { public ASet(HashSet<VertexInterface> vertexHashSet) {
this.vertexHashSet = vertexHashSet; this.vertexHashSet = new HashSet<VertexInterface>(vertexHashSet);
} }
public void add(VertexInterface vertex) { public void add(VertexInterface vertex) {
vertexHashSet.add((Box)vertex) ; vertexHashSet.add(vertex) ;
} }
public boolean contains(VertexInterface vertex) { public boolean contains(VertexInterface vertex) {
if (vertexHashSet.contains(vertex)) return true ; return vertexHashSet.contains(vertex);
return false ;
} }
+1 -1
View File
@@ -8,7 +8,7 @@ public class Pi implements PiInterface {
public Pi(Hashtable<VertexInterface, Integer> hashPi) { public Pi(Hashtable<VertexInterface, Integer> hashPi) {
this.hashPi = hashPi ; this.hashPi = new Hashtable<VertexInterface, Integer>(hashPi);
} }
+3 -9
View File
@@ -2,21 +2,16 @@ package dijkstra;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List;
import maze.Box;
public class Previous implements PreviousInterface { public class Previous implements PreviousInterface {
private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ; private Hashtable<VertexInterface, VertexInterface> hashPrevious = new Hashtable<VertexInterface, VertexInterface>() ;
private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ; private ArrayList<VertexInterface> shortestPath = new ArrayList<VertexInterface>() ;
public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) { public Previous(Hashtable<VertexInterface, VertexInterface> hashPrevious, ArrayList<VertexInterface> shortestPath) {
this.hashPrevious = hashPrevious; this.hashPrevious = new Hashtable<VertexInterface, VertexInterface>(hashPrevious);
this.shortestPath = shortestPath; this.shortestPath = new ArrayList<VertexInterface>(shortestPath);
} }
public VertexInterface getPrevious(VertexInterface vertex) { public VertexInterface getPrevious(VertexInterface vertex) {
@@ -39,8 +34,7 @@ public class Previous implements PreviousInterface {
if (getPrevious(father) == null) return shortestPath ; if (getPrevious(father) == null) return shortestPath ;
else { else {
return getShortestPathTo(father) ; return getShortestPathTo(father) ;
} }
} }
} }
+1 -4
View File
@@ -1,10 +1,7 @@
package dijkstra; package dijkstra;
import java.util.List;
public interface VertexInterface { public interface VertexInterface {
/** Compare a Vertex to another */
public boolean compareTo(VertexInterface vertex) ;
public String getLabel() ; public String getLabel() ;
} }
+1 -1
View File
@@ -1,6 +1,6 @@
package maze; package maze;
public class ABox extends Box{ public class ABox extends EBox{
public ABox (String label, int x, int y, Maze maze) { public ABox (String label, int x, int y, Maze maze) {
+4 -36
View File
@@ -1,6 +1,5 @@
package maze; package maze;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import dijkstra.VertexInterface; import dijkstra.VertexInterface;
@@ -11,7 +10,7 @@ public abstract class Box
private int y ; private int y ;
private String label = null ; private String label = null ;
private Maze maze ; private Maze maze ;
private List<Box> neighbourList = new ArrayList<Box>() ; private ArrayList<Box> neighbourList = new ArrayList<Box>() ;
public Box (String label, int x, int y, Maze maze) { public Box (String label, int x, int y, Maze maze) {
@@ -22,18 +21,11 @@ public abstract class Box
} }
public List<VertexInterface> generateNeighbors() { public ArrayList<VertexInterface> generateNeighbors() {
return maze.generateNeighbours(this) ; return maze.generateNeighbours(this) ;
} }
public boolean compareTo(VertexInterface vertex) {
if (this.getLabel() == vertex.getLabel()) {
return true ;
}
return false ;
}
public boolean isWalkable() { public boolean isWalkable() {
return true ; return true ;
} }
@@ -46,48 +38,24 @@ public abstract class Box
return false ; return false ;
} }
public List<Box> getNeighbourList() { public ArrayList<Box> getNeighbourList() {
return neighbourList; return neighbourList;
} }
public void setNeighbourList(List<Box> neighbourList) {
this.neighbourList = neighbourList;
}
public int getX() { public int getX() {
return x; return x;
} }
public void setX(int x) {
this.x = x;
}
public int getY() { public int getY() {
return y; return y;
} }
public void setY(int y) {
this.y = y;
}
public String getLabel() { public String getLabel() {
return label; return label;
} }
public void setLabel(String label) {
this.label = label;
}
public Maze getMaze() {
return maze;
}
public void setMaze(Maze maze) {
this.maze = maze;
}
public String toString() { public String toString() {
return label; return "Label: " + label + ", x: " + x + ", y: " + y;
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
package maze; package maze;
public class DBox extends Box{ public class DBox extends EBox{
public DBox (String label, int x, int y, Maze maze) { public DBox (String label, int x, int y, Maze maze) {
+15 -27
View File
@@ -21,21 +21,18 @@ public class Maze
} }
// public Maze(Box[][] maze){
// //Replacement for setMaze()
// //Todo : check if the given array is a proper maze
// //and then set dimensionX and dimensionX
// }
public int getCost(VertexInterface start, VertexInterface end) { public int getCost(VertexInterface start, VertexInterface end) {
Box startBox = (Box) start ; Box startBox = (Box) start ;
Box endBox = (Box) end ; Box endBox = (Box) end ;
if (startBox.getNeighbourList().contains(endBox)) { return startBox.getNeighbourList().contains(endBox) ? 1 : 0 ;
if (startBox.isWalkable() && endBox.isWalkable()) {
return 1 ;
}
else {
return 0 ;
}
}
return 0 ;
} }
public int getVertexNumber() { public int getVertexNumber() {
@@ -60,7 +57,12 @@ public class Maze
int x = box.getX() ; int x = box.getX() ;
int y = box.getY() ; int y = box.getY() ;
ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ; ArrayList<VertexInterface> neighbourList = new ArrayList<VertexInterface>() ;
// Always returns an empty list if the departure box is a wall
if (! box.isWalkable()) {
return neighbourList ;
}
if (x > 0 && maze[x-1][y].isWalkable()) { if (x > 0 && maze[x-1][y].isWalkable()) {
neighbourList.add(maze[x-1][y]) ; neighbourList.add(maze[x-1][y]) ;
} }
@@ -81,7 +83,7 @@ public class Maze
} }
public final void initFromTextFile(String filename) { private final void initFromTextFile(String filename) {
FileReader fr = null ; FileReader fr = null ;
BufferedReader br = null ; BufferedReader br = null ;
@@ -180,32 +182,18 @@ public class Maze
} finally { } finally {
try { pw.close();} catch(Exception e) {} try { pw.close();} catch(Exception e) {}
} }
} }
public int getDimensionX() { public int getDimensionX() {
return dimensionX; return dimensionX;
} }
public void setDimensionX(int dimensionX) {
this.dimensionX = dimensionX;
}
public int getDimensionY() { public int getDimensionY() {
return dimensionY; return dimensionY;
} }
public void setDimensionY(int dimensionY) {
this.dimensionY = dimensionY;
}
public Box[][] getMaze() { public Box[][] getMaze() {
return maze; return maze;
} }
public void setMaze(Box[][] maze) {
this.maze = maze;
}
} }
+1 -6
View File
@@ -3,17 +3,12 @@ package maze;
public class MazeReadingException public class MazeReadingException
extends Exception { extends Exception {
private String filename ; private static final long serialVersionUID = 1L;
public MazeReadingException (String filename) { public MazeReadingException (String filename) {
super("Invalid maze format : " + filename) ; super("Invalid maze format : " + filename) ;
this.filename = filename ;
} }
public String getFilename() {
return filename ;
}
} }