package org.psjava.ds.graph;

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

public class BipartiteGraph<V> {

    private MutableSet<V> left = GoodMutableSetFactory.getInstance().create();
    private MutableSet<V> right = GoodMutableSetFactory.getInstance().create();
    private DynamicArray<BipartiteGraphEdge<V>> edges = DynamicArray.create();

    public void insertLeftVertex(V v) {
        AssertStatus.assertTrue(!right.contains(v));
        left.addIfAbsent(v);
    }

    public void insertRightVertex(V v) {
        AssertStatus.assertTrue(!left.contains(v));
        right.addIfAbsent(v);
    }

    public void addEdge(final V leftv, final V rightv) {
        assertVertexExist(leftv, left);
        assertVertexExist(rightv, right);
        edges.addToLast(SimpleBipartiteGraphEdge.create(leftv, rightv));
    }

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

    public PSCollection<V> getLeftVertices() {
        return left;
    }

    public PSCollection<V> getRightVertices() {
        return right;
    }

    public Iterable<BipartiteGraphEdge<V>> getEdges() {
        return edges;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + ":" + IterableToString.toString(getEdges());
    }
}

@@ -47,151 +47,117 @@
 ava.
-ds.PSCollection;%0D%0Aimport org.psjava.ds.array.DynamicArray;%0D%0Aimport org.psjava.ds.set.MutableSet;%0D%0Aimport org.psjava.goods.GoodMutableSetFactory
+util.AssertStatus;%0D%0A%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.Collection;%0D%0Aimport java.util.Collections
 ;%0D%0Ai
@@ -154,38 +154,32 @@
 ctions;%0D%0Aimport 
-org.ps
 java.util.Assert
@@ -172,28 +172,47 @@
 va.util.
-AssertStatus
+HashSet;%0D%0Aimport java.util.List
 ;%0D%0Aimpor
@@ -205,38 +205,32 @@
 l.List;%0D%0Aimport 
-org.ps
 java.util.Iterab
@@ -223,32 +223,19 @@
 va.util.
-IterableToString
+Set
 ;%0D%0A%0D%0Apub
@@ -275,31 +275,24 @@
     private 
-Mutable
 Set%3CV%3E left 
@@ -294,53 +294,25 @@
 left
+s
  = 
-GoodMutableSetFactory.getInstance().create
+new HashSet%3C%3E
 ();%0D
@@ -324,23 +324,16 @@
 private 
-Mutable
 Set%3CV%3E r
@@ -340,53 +340,25 @@
 ight
+s
  = 
-GoodMutableSetFactory.getInstance().create
+new HashSet%3C%3E
 ();%0D
@@ -370,28 +370,20 @@
 private 
-DynamicArray
+List
 %3CBiparti
@@ -410,27 +410,23 @@
 s = 
-DynamicArray.create
+new ArrayList%3C%3E
 ();%0D
@@ -507,16 +507,17 @@
 e(!right
+s
 .contain
@@ -536,28 +536,21 @@
     left
+s
 .add
-IfAbsent
 (v);%0D%0A  
@@ -635,16 +635,17 @@
 ue(!left
+s
 .contain
@@ -669,20 +669,13 @@
 ight
+s
 .add
-IfAbsent
 (v);
@@ -721,17 +721,16 @@
 l V left
-v
 , final 
@@ -736,17 +736,16 @@
  V right
-v
 ) %7B%0D%0A   
@@ -775,15 +775,15 @@
 left
-v
 , left
+s
 );%0D%0A
@@ -813,24 +813,24 @@
 st(right
-v
 , right
+s
 );%0D%0A    
@@ -846,14 +846,8 @@
 .add
-ToLast
 (Sim
@@ -883,17 +883,15 @@
 left
-v
 , right
-v
 ));%0D
@@ -944,15 +944,8 @@
  v, 
-Mutable
 Set%3C
@@ -1044,34 +1044,32 @@
 %7D%0D%0A%0D%0A    public 
-PS
 Collection%3CV%3E ge
@@ -1102,20 +1102,57 @@
  return 
-left
+Collections.unmodifiableCollection(lefts)
 ;%0D%0A    %7D
@@ -1170,10 +1170,8 @@
 lic 
-PS
 Coll
@@ -1217,21 +1217,58 @@
  return 
+Collections.unmodifiableCollection(
 right
+s)
 ;%0D%0A    %7D
@@ -1282,24 +1282,26 @@
  public 
-Iterable
+Collection
 %3CBiparti
@@ -1349,163 +1349,48 @@
 urn 
-edges;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public String toString() %7B%0D%0A        return this.getClass().getSimpleName() + %22:%22 + IterableToString.toString(getE
+Collections.unmodifiableCollection(e
 dges
-()
 );%0D%0A

remove using own data structure in BipartiteGraph