1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| package org.rblasch.convert.graph; |
17 |
| |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashSet; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.Set; |
22 |
| |
23 |
| public final class SparseWeightedDirectedGraph implements WeightedDirectedGraph { |
24 |
| private final Set vertices = new HashSet(); |
25 |
| private final Set edges = new HashSet(); |
26 |
| private final Set connections = new HashSet(); |
27 |
| |
28 |
73
| public Set getVertices() {
|
29 |
73
| return Collections.unmodifiableSet(vertices);
|
30 |
| } |
31 |
| |
32 |
0
| public Set getEdges() {
|
33 |
0
| return Collections.unmodifiableSet(edges);
|
34 |
| } |
35 |
| |
36 |
468
| public void addVertex(final Vertex v) {
|
37 |
468
| vertices.add(v);
|
38 |
| } |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
2981
| public void addConnection(final Vertex start, final Edge edge, final Vertex end) {
|
44 |
2981
| vertices.add(start);
|
45 |
2981
| edges.add(edge);
|
46 |
2981
| vertices.add(end);
|
47 |
2981
| connections.add(new Connection(start, edge, end));
|
48 |
| } |
49 |
| |
50 |
14
| public Set getAllConnections() {
|
51 |
14
| return Collections.unmodifiableSet(connections);
|
52 |
| } |
53 |
| |
54 |
12294
| public Set getConnections(final Vertex start, final Vertex end) {
|
55 |
12294
| final Set cons = new HashSet();
|
56 |
12294
| for (final Iterator i = connections.iterator(); i.hasNext();) {
|
57 |
1138537
| final Connection c = (Connection) i.next();
|
58 |
1138537
| if (c.getStart().equals(start) && c.getEnd().equals(end)) {
|
59 |
12294
| cons.add(c);
|
60 |
| } |
61 |
| } |
62 |
12294
| return cons;
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
5770
| public Set getOutbound(final Vertex vertex) {
|
69 |
5770
| final Set destinations = new HashSet();
|
70 |
| |
71 |
5770
| for (final Iterator i = connections.iterator(); i.hasNext();) {
|
72 |
518006
| final Connection c = (Connection) i.next();
|
73 |
518006
| if (c.getStart().equals(vertex)) {
|
74 |
12590
| destinations.add(c.getEnd());
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
5770
| return destinations;
|
79 |
| } |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
0
| public Set getInbound(final Vertex vertex) {
|
85 |
0
| final Set predecessors = new HashSet();
|
86 |
| |
87 |
0
| for (final Iterator i = connections.iterator(); i.hasNext();) {
|
88 |
0
| final Connection c = (Connection) i.next();
|
89 |
0
| if (c.getEnd().equals(vertex)) {
|
90 |
0
| predecessors.add(c.getStart());
|
91 |
| } |
92 |
| } |
93 |
| |
94 |
0
| return predecessors;
|
95 |
| } |
96 |
| |
97 |
0
| public boolean equals(final Object obj) {
|
98 |
0
| if (this == obj) return true;
|
99 |
0
| if (!(obj instanceof SparseWeightedDirectedGraph)) return false;
|
100 |
| |
101 |
0
| final SparseWeightedDirectedGraph rhs = (SparseWeightedDirectedGraph) obj;
|
102 |
0
| if (!edges.equals(rhs.edges)) return false;
|
103 |
0
| if (!vertices.equals(rhs.vertices)) return false;
|
104 |
| |
105 |
0
| return true;
|
106 |
| } |
107 |
| |
108 |
0
| public int hashCode() {
|
109 |
0
| int result = vertices.hashCode();
|
110 |
0
| result = 29 * result + edges.hashCode();
|
111 |
0
| return result;
|
112 |
| } |
113 |
| |
114 |
120
| public String toString() {
|
115 |
120
| return connections.toString();
|
116 |
| } |
117 |
| } |