| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
package edu.uci.ics.jung.graph; |
| 13 | |
|
| 14 | |
import java.util.Comparator; |
| 15 | |
import java.util.Map; |
| 16 | |
import java.util.Set; |
| 17 | |
import java.util.TreeMap; |
| 18 | |
import java.util.TreeSet; |
| 19 | |
|
| 20 | |
import org.apache.commons.collections15.Factory; |
| 21 | |
import org.apache.commons.collections15.comparators.ComparableComparator; |
| 22 | |
|
| 23 | |
import edu.uci.ics.jung.graph.util.Pair; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
@SuppressWarnings("serial") |
| 34 | |
public class SortedSparseMultigraph<V,E> |
| 35 | |
extends OrderedSparseMultigraph<V,E> |
| 36 | |
implements MultiGraph<V,E> |
| 37 | |
{ |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public static <V,E> Factory<Graph<V,E>> getFactory() |
| 44 | |
{ |
| 45 | 2 | return new Factory<Graph<V,E>> () |
| 46 | |
{ |
| 47 | 6 | public Graph<V,E> create() |
| 48 | |
{ |
| 49 | 4 | return new SortedSparseMultigraph<V,E>(); |
| 50 | |
} |
| 51 | |
}; |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
protected Comparator<V> vertex_comparator; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
protected Comparator<E> edge_comparator; |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public SortedSparseMultigraph(Comparator<V> vertex_comparator, Comparator<E> edge_comparator) |
| 71 | 68 | { |
| 72 | 68 | this.vertex_comparator = vertex_comparator; |
| 73 | 68 | this.edge_comparator = edge_comparator; |
| 74 | 68 | vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator); |
| 75 | 68 | edges = new TreeMap<E, Pair<V>>(edge_comparator); |
| 76 | 68 | directedEdges = new TreeSet<E>(edge_comparator); |
| 77 | 68 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
@SuppressWarnings("unchecked") |
| 84 | |
public SortedSparseMultigraph() |
| 85 | |
{ |
| 86 | 68 | this(new ComparableComparator(), new ComparableComparator()); |
| 87 | 68 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
public void setVertexComparator(Comparator<V> vertex_comparator) |
| 94 | |
{ |
| 95 | 0 | this.vertex_comparator = vertex_comparator; |
| 96 | 0 | Map<V, Pair<Set<E>>> tmp_vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator); |
| 97 | 0 | for (Map.Entry<V, Pair<Set<E>>> entry : vertices.entrySet()) |
| 98 | 0 | tmp_vertices.put(entry.getKey(), entry.getValue()); |
| 99 | 0 | this.vertices = tmp_vertices; |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
@Override |
| 103 | |
public boolean addVertex(V vertex) { |
| 104 | 364 | if(vertex == null) { |
| 105 | 2 | throw new IllegalArgumentException("vertex may not be null"); |
| 106 | |
} |
| 107 | 362 | if (!containsVertex(vertex)) |
| 108 | |
{ |
| 109 | 328 | vertices.put(vertex, new Pair<Set<E>>(new TreeSet<E>(edge_comparator), |
| 110 | |
new TreeSet<E>(edge_comparator))); |
| 111 | 328 | return true; |
| 112 | |
} |
| 113 | |
else |
| 114 | |
{ |
| 115 | 2 | return false; |
| 116 | |
} |
| 117 | |
} |
| 118 | |
} |