graph.impl.AdjacencyListGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of astra-compiler Show documentation
Show all versions of astra-compiler Show documentation
Core compiler artifact for the ASTRA Language
package graph.impl;
import graph.core.Edge;
import graph.core.Graph;
import graph.core.InvalidVertexException;
import graph.core.Vertex;
import graph.util.LinkedList;
import graph.util.List;
import graph.util.Position;
import java.util.Iterator;
public class AdjacencyListGraph implements Graph {
protected class AdjacencyListVertex implements Vertex {
Position position;
V element;
List> incidentEdges;
public AdjacencyListVertex(V element) {
this.element = element;
incidentEdges = new LinkedList>();
}
@Override
public V element() {
return element;
}
public String toString() {
return element.toString();
}
}
protected class AdjacencyListEdge implements Edge {
Position position;
E element;
AdjacencyListVertex start, end;
public Position> startPosition, endPosition;
public AdjacencyListEdge(AdjacencyListVertex start, AdjacencyListVertex end, E element) {
this.start = start;
this.end = end;
this.element = element;
}
@Override
public E element() {
return element;
}
public String toString() {
return element.toString();
}
}
private List vertices;
private List edges;
public AdjacencyListGraph() {
vertices = new LinkedList();
edges = new LinkedList();
}
@SuppressWarnings("unchecked")
@Override
public Vertex[] endVertices(Edge e) {
AdjacencyListEdge edge = (AdjacencyListEdge) e;
Vertex[] endpoints = (Vertex[]) new Vertex[2];
endpoints[0] = edge.start;
endpoints[1] = edge.end;
return endpoints;
}
@Override
public Vertex opposite(Vertex v, Edge e) {
Vertex[] endpoints = endVertices(e);
if (endpoints[0].equals(v)) {
return endpoints[1];
} else if (endpoints[1].equals(v)) {
return endpoints[0];
}
throw new InvalidVertexException();
}
@Override
public boolean areAdjacent(Vertex v, Vertex w) {
for (AdjacencyListEdge edge: edges) {
if ((edge.start.equals(v)) && (edge.end.equals(w))) return true;
if ((edge.end.equals(v)) && (edge.start.equals(w))) return true;
}
return false;
}
@Override
public V replace(Vertex v, V x) {
AdjacencyListVertex vertex = (AdjacencyListVertex) v;
V temp = vertex.element;
vertex.element = x;
return temp;
}
@Override
public E replace(Edge e, E x) {
AdjacencyListEdge edge = (AdjacencyListEdge) e;
E temp = edge.element;
edge.element = x;
return temp;
}
@Override
public Vertex insertVertex(V v) {
AdjacencyListVertex vertex = new AdjacencyListVertex(v);
Position position = vertices.insertLast(vertex);
vertex.position = position;
return vertex;
}
@Override
public Edge insertEdge(Vertex v, Vertex w, E o) {
AdjacencyListEdge edge = new AdjacencyListEdge((AdjacencyListVertex) v, (AdjacencyListVertex) w, o);
Position position = edges.insertLast(edge);
edge.position = position;
position.element().startPosition = ((AdjacencyListVertex) v).incidentEdges.insertLast(position.element());
position.element().endPosition = ((AdjacencyListVertex) w).incidentEdges.insertLast(position.element());
return edge;
}
@Override
public V removeVertex(Vertex v) {
AdjacencyListVertex vertex = (AdjacencyListVertex) v;
if (vertex.position == null) return null;
Iterator> it = incidentEdges(v).iterator();
while (it.hasNext()) it.remove();
vertices.remove(vertex.position);
vertex.position = null;
return vertex.element;
}
@Override
public E removeEdge(Edge e) {
AdjacencyListEdge edge = (AdjacencyListEdge) e;
edge.start.incidentEdges.remove(edge.startPosition);
edge.end.incidentEdges.remove(edge.endPosition);
edges.remove(edge.position);
return edge.element;
}
@Override
public List> incidentEdges(Vertex v) {
LinkedList> list = new LinkedList>();
for (Edge edge : ((AdjacencyListVertex) v).incidentEdges) {
list.insertLast(edge);
}
return list;
}
@Override
public List> vertices() {
LinkedList> list = new LinkedList>();
for (AdjacencyListVertex vertex : vertices) {
list.insertLast(vertex);
}
return list;
}
@Override
public List> edges() {
LinkedList> list = new LinkedList>();
for (AdjacencyListEdge edge : edges) {
list.insertLast(edge);
}
return list;
}
public String toString() {
String out = "";
for (AdjacencyListVertex vertex : vertices) {
out += vertex.toString() + " [";
boolean first = true;
for (Edge edge : incidentEdges(vertex)) {
if (first) first=false; else out += ", ";
AdjacencyListEdge e = (AdjacencyListEdge) edge;
if (e.start == vertex) {
out += " -> " + e.end.element();
} else {
out += " <- " + e.start.element();
}
}
out +=" ]\n";
}
return out;
}
}