Remove access to underlying containers inside Graph and Node.

This commit is contained in:
Holt59
2018-03-22 19:47:01 +01:00
parent 797a4e1c8c
commit bdb378c079
12 changed files with 206 additions and 129 deletions

View File

@@ -24,7 +24,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
ShortestPathData data = getInputData();
Graph graph = data.getGraph();
final int nbNodes = graph.getNodes().size();
final int nbNodes = graph.size();
// Initialize array of distances.
double[] distances = new double[nbNodes];
@@ -41,8 +41,8 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
boolean found = false;
for (int i = 0; !found && i < nbNodes; ++i) {
found = true;
for (Node node: graph.getNodes()) {
for (Arc arc: node.getSuccessors()) {
for (Node node: graph) {
for (Arc arc: node) {
// Small test to check allowed roads...
if (!data.isAllowed(arc)) {

View File

@@ -1,9 +1,9 @@
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void insert( x ) --> Insert x
// Comparable deleteMin( )--> Return and remove smallest item
// Comparable findMin( ) --> Return smallest item
// boolean isEmpty( ) --> Return true if empty; else false
// Comparable findMin( ) --> Return smallest item
// boolean isEmpty( ) --> Return true if empty; else false
// ******************ERRORS********************************
// Throws RuntimeException for findMin and deleteMin when empty
@@ -60,20 +60,6 @@ public class BinaryHeap<E extends Comparable<E>> {
}
}
/**
* @return true if the heap is empty, false otherwise.
*/
public boolean isEmpty() {
return this.currentSize == 0;
}
/**
* @return Current size (number of elements) of this heap.
*/
public int size() {
return this.currentSize;
}
/**
* @return Index of the parent of the given index.
*/
@@ -88,17 +74,6 @@ public class BinaryHeap<E extends Comparable<E>> {
return index * 2 + 1;
}
/**
* Insert the given element into the heap.
*
* @param x Item to insert.
*/
public void insert(E x) {
int index = this.currentSize++;
this.arraySet(index, x);
this.percolateUp(index);
}
/**
* Internal method to percolate up in the heap.
*
@@ -151,6 +126,41 @@ public class BinaryHeap<E extends Comparable<E>> {
}
}
/**
* @return true if the heap is empty, false otherwise.
*/
public boolean isEmpty() {
return this.currentSize == 0;
}
/**
* @return Current size (number of elements) of this heap.
*/
public int size() {
return this.currentSize;
}
/**
* Insert the given element into the heap.
*
* @param x Item to insert.
*/
public void add(E x) {
int index = this.currentSize++;
this.arraySet(index, x);
this.percolateUp(index);
}
/**
* Tell the binary heap that the given element has been modified and should be
* re-positioned inside the heap.
*
* @param x Item to update.
*/
public void update(E x) {
// TODO:
}
/**
* Find the smallest item in the heap.
*
@@ -160,7 +170,7 @@ public class BinaryHeap<E extends Comparable<E>> {
*/
public E findMin() throws RuntimeException {
if (isEmpty())
throw new RuntimeException("Empty binary heap");
throw new RuntimeException("Empty binary heap.");
return this.array.get(0);
}

View File

@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.insa.algo.AbstractAlgorithm;
@@ -72,14 +71,14 @@ public class WeaklyConnectedComponentsAlgorithm
* graph.
*/
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
int nNodes = getInputData().getGraph().getNodes().size();
int nNodes = getInputData().getGraph().size();
ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
for (int i = 0; i < nNodes; ++i) {
res.add(new HashSet<Integer>());
}
for (Node node: getInputData().getGraph().getNodes()) {
for (Arc arc: node.getSuccessors()) {
for (Node node: getInputData().getGraph()) {
for (Arc arc: node) {
res.get(node.getId()).add(arc.getDestination().getId());
if (arc.getRoadInformation().isOneWay()) {
res.get(arc.getDestination().getId()).add(node.getId());
@@ -100,27 +99,27 @@ public class WeaklyConnectedComponentsAlgorithm
* @return
*/
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
List<Node> nodes = getInputData().getGraph().getNodes();
Graph graph = getInputData().getGraph();
ArrayList<Node> component = new ArrayList<Node>();
// Using a queue because we are doing a BFS
Queue<Integer> queue = new LinkedList<Integer>();
// Notify observers about the current component.
notifyStartComponent(nodes.get(cur));
notifyStartComponent(graph.get(cur));
// Add original node and loop until the queue is empty.
queue.add(cur);
marked[cur] = true;
while (!queue.isEmpty()) {
Node node = nodes.get(queue.remove());
Node node = graph.get(queue.remove());
component.add(node);
// Notify observers
notifyNewNodeInComponent(node);
for (Integer destId: ugraph.get(node.getId())) {
Node dest = nodes.get(destId);
Node dest = graph.get(destId);
if (!marked[dest.getId()]) {
queue.add(destId);
marked[destId] = true;
@@ -138,7 +137,7 @@ public class WeaklyConnectedComponentsAlgorithm
Graph graph = getInputData().getGraph();
ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
boolean[] marked = new boolean[graph.getNodes().size()];
boolean[] marked = new boolean[graph.size()];
Arrays.fill(marked, false);
ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();

View File

@@ -1,7 +1,7 @@
package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
@@ -11,7 +11,7 @@ import java.util.List;
* holds a list of nodes and each node holds a list of its successors.
*
*/
public class Graph {
public class Graph implements Iterable<Node> {
// Map identifier.
private final String mapId;
@@ -48,10 +48,28 @@ public class Graph {
}
/**
* @return Immutable view of the list of nodes of this graph.
* Fetch the node with the given ID.
*
* Complexity: O(1).
*
* @param id ID of the node to fetch.
*
* @return Node with the given ID.
*/
public List<Node> getNodes() {
return Collections.unmodifiableList(nodes);
public Node get(int id) {
return this.nodes.get(id);
}
/**
* @return Number of nodes in this graph.
*/
public int size() {
return this.nodes.size();
}
@Override
public Iterator<Node> iterator() {
return this.nodes.iterator();
}
/**
@@ -78,7 +96,7 @@ public class Graph {
}
for (Node node: nodes) {
Node orig = trNodes.get(node.getId());
for (Arc arc: node.getSuccessors()) {
for (Arc arc: node) {
if (arc.getRoadInformation().isOneWay()) {
Node dest = trNodes.get(arc.getDestination().getId());
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),

View File

@@ -2,7 +2,7 @@ package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
/**
* Class representing a Node in a {@link Graph}.
@@ -13,7 +13,7 @@ import java.util.List;
* Nodes are comparable based on their ID.
*
*/
public class Node implements Comparable<Node> {
public class Node implements Comparable<Node>, Iterable<Arc> {
/**
* Link the two given nodes with one or two arcs (depending on roadInformation),
@@ -94,10 +94,22 @@ public class Node implements Comparable<Node> {
}
/**
* @return Immutable view of the list of successors of this node.
* @return Number of successors of this node.
*/
public List<Arc> getSuccessors() {
return Collections.unmodifiableList(successors);
public int getNumberOfSuccessors() {
return this.successors.size();
}
/**
* @return true if this node has at least one successor.
*/
public boolean hasSuccessors() {
return !this.successors.isEmpty();
}
@Override
public Iterator<Arc> iterator() {
return this.successors.iterator();
}
/**

View File

@@ -70,7 +70,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
* @throws IndexOutOfBoundsException if the node is not in the graph.
*/
protected Node readNode(Graph graph) throws IOException {
return graph.getNodes().get(dis.readInt());
return graph.get(dis.readInt());
}
}

View File

@@ -602,8 +602,8 @@ public class MainWindow extends JFrame {
// name that are right-to-left (e.g. arabic names).
info += " - " + graph.getMapName() + "\u200e";
}
info += ", " + graph.getNodes().size() + " nodes, "
+ graph.getGraphInformation().getArcCount() + " arcs.";
info += ", " + graph.size() + " nodes, " + graph.getGraphInformation().getArcCount()
+ " arcs.";
graphInfoPanel.setText(info);
drawGraph();

View File

@@ -65,7 +65,7 @@ public class NodesInputPanel extends JPanel
public Node findClosestNode(Point point) {
Node minNode = null;
double minDis = Double.POSITIVE_INFINITY;
for (Node node: graph.getNodes()) {
for (Node node: graph) {
double dlon = point.getLongitude() - node.getPoint().getLongitude();
double dlat = point.getLatitude() - node.getPoint().getLatitude();
double dis = dlon * dlon + dlat * dlat; // No need to square
@@ -308,7 +308,7 @@ public class NodesInputPanel extends JPanel
*/
protected Node getNodeForInput(JTextField textfield) {
try {
Node node = graph.getNodes().get(Integer.valueOf(textfield.getText().trim()));
Node node = graph.get(Integer.valueOf(textfield.getText().trim()));
return node;
}
catch (IllegalArgumentException | IndexOutOfBoundsException ex) {

View File

@@ -614,7 +614,7 @@ public class BasicDrawing extends JPanel implements Drawing {
@Override
public void drawGraph(Graph graph, GraphPalette palette) {
int repaintModulo = Math.max(1, graph.getNodes().size() / 100);
int repaintModulo = Math.max(1, graph.size() / 100);
// Initialize the buffered image
@@ -625,8 +625,8 @@ public class BasicDrawing extends JPanel implements Drawing {
this.removeMouseMotionListener(zoomAndPanListener);
this.removeMouseWheelListener(zoomAndPanListener);
for (Node node: graph.getNodes()) {
for (Arc arc: node.getSuccessors()) {
for (Node node: graph) {
for (Arc arc: node) {
// Draw arcs only if there are one-way arcs or if origin is lower than
// destination, avoid drawing two-ways arc twice.
if (arc.getRoadInformation().isOneWay()