package org.psjava.ds.graph;

import org.psjava.ds.Collection;
import org.psjava.ds.array.DynamicArray;
import org.psjava.ds.set.MutableSet;
import org.psjava.goods.GoodMutableSetFactory;
import org.psjava.util.AssertStatus;

public class MutableUndirectedWeightedGraph<V, W> implements Graph<V, UndirectedWeightedEdge<V, W>> {

	public static <V, W> MutableUndirectedWeightedGraph<V, W> create() {
		return new MutableUndirectedWeightedGraph<V, W>();
	}

	private MutableSet<V> vertices = GoodMutableSetFactory.getInstance().create();
	private DynamicArray<UndirectedWeightedEdge<V, W>> edges = DynamicArray.create();

	public void insertVertex(V v) {
		vertices.insert(v);
	}

	public void addEdge(V v1, V v2, W weight) {
		assertVertexExist(v1);
		assertVertexExist(v2);
		edges.addToLast(SimpleUndirectedWeightedEdge.create(v1, v2, weight));
	}

	private void assertVertexExist(V v) {
		AssertStatus.assertTrue(vertices.contains(v), "vertex is not in graph");
	}

	@Override
	public Collection<V> getVertices() {
		return vertices;
	}

	@Override
	public Iterable<UndirectedWeightedEdge<V, W>> getEdges() {
		return edges;
	}

	@Override
	public String toString() {
		return GraphToString.toString(this);
	}
}

@@ -63,136 +63,8 @@
 n;%0D%0A
-import org.psjava.ds.array.DynamicArray;%0D%0Aimport org.psjava.ds.set.MutableSet;%0D%0Aimport org.psjava.goods.GoodMutableSetFactory;%0D%0A
 impo
@@ -355,95 +355,17 @@
 able
-Set%3CV%3E vertices = GoodMutableSetFactory.getInstance().create();%0D%0A%09private DynamicArray%3C
+Graph%3CV, 
 Undi
@@ -394,28 +394,24 @@
 W%3E%3E 
-edges = DynamicArray
+g = MutableGraph
 .cre
@@ -460,23 +460,22 @@
 %0D%0A%09%09
-vertices
+g
 .insert
+Vertex
 (v);
@@ -586,23 +586,17 @@
 %0D%0A%09%09
-edges.addToLast
+g.addEdge
 (Sim
@@ -719,24 +719,31 @@
 ertTrue(
-v
+g.getV
 ertices
+()
 .contain
@@ -846,16 +846,23 @@
 urn 
-v
+g.getV
 ertices
+()
 ;%0D%0A%09
@@ -952,13 +952,20 @@
 urn 
-e
+g.getE
 dges
+()
 ;%0D%0A%09

refactoring MutableUndirectedWeightedGraph. use common class