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 com.google.common.base.Supplier;
21 import com.google.common.collect.Ordering;
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> Supplier<Graph<V,E>> getFactory()
44 {
45 return new Supplier<Graph<V,E>> ()
46 {
47 public Graph<V,E> get()
48 {
49 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
71
72 public SortedSparseMultigraph(Comparator<V> vertex_comparator, Comparator<E> edge_comparator)
73 {
74 this.vertex_comparator = vertex_comparator;
75 this.edge_comparator = edge_comparator;
76 vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
77 edges = new TreeMap<E, Pair<V>>(edge_comparator);
78 directedEdges = new TreeSet<E>(edge_comparator);
79 }
80
81
82
83
84
85 public SortedSparseMultigraph()
86 {
87 this(new Ordering<V>(){
88 @SuppressWarnings("unchecked")
89 public int compare(V v1, V v2) {
90 return ((Comparable<V>) v1).compareTo(v2);
91 }},
92 new Ordering<E>(){
93 @SuppressWarnings("unchecked")
94 public int compare(E e1, E e2) {
95 return ((Comparable<E>) e1).compareTo(e2);
96 }});
97 }
98
99
100
101
102
103 public void setVertexComparator(Comparator<V> vertex_comparator)
104 {
105 this.vertex_comparator = vertex_comparator;
106 Map<V, Pair<Set<E>>> tmp_vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
107 for (Map.Entry<V, Pair<Set<E>>> entry : vertices.entrySet())
108 tmp_vertices.put(entry.getKey(), entry.getValue());
109 this.vertices = tmp_vertices;
110 }
111
112 @Override
113 public boolean addVertex(V vertex) {
114 if(vertex == null) {
115 throw new IllegalArgumentException("vertex may not be null");
116 }
117 if (!containsVertex(vertex))
118 {
119 vertices.put(vertex, new Pair<Set<E>>(new TreeSet<E>(edge_comparator),
120 new TreeSet<E>(edge_comparator)));
121 return true;
122 }
123 else
124 {
125 return false;
126 }
127 }
128 }