summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/graph/AlgGraph.java
blob: 96a984f9f94c13bcc43eb16c4b91969480c66513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package bjc.utils.graph;

import bjc.typeclasses.Container;

/**
 * A directed algebraic graph
 * 
 * (ref. Algebraic Graphs with Class, Andrey Mokhov}
 * 
 * @author bjcul
 *
 * @param <Vertex> The type of the vertexes
 * @param <PGraph> Containing type parameter
 */
public interface AlgGraph<Vertex, PGraph extends AlgGraph<?, PGraph>> extends Container<Vertex, AlgGraph<?, PGraph>> {
	/**
	 * Create a empty algebraic graph.
	 * 
	 * @param <B> The type of the vertices
	 * 
	 * @return A new empty graph.
	 */
	<B> AlgGraph<B, PGraph> empty();
	
	/**
	 * Create a algebraic graph with a single vertex
	 * 
	 * @param val The value for the vertex
	 * 
	 * @return A new graph with the given vertex
	 */
	<B> AlgGraph<B, PGraph> vertex(B val);
	
	/**
	 * Overlay a graph onto this one, adding its vertices and edges.
	 * 
	 * @param other The graph to overlay
	 */
	void overlay(AlgGraph<Vertex, PGraph> other);

	/**
	 * Connect a graph to this one, adding its vertices and edges; as well as
	 * establishing an edge from each node in this graph, to each node in the other
	 * graph.
	 * 
	 * @param other The graph to connect
	 */
	void connect(AlgGraph<Vertex, PGraph> other);
	
	/**
	 * Overlay two graphs into a new graph.
	 * 
	 * @param <Vertex> The type of the vertices.
	 * 
	 * @param left     The graph to overlay onto
	 * @param right    The graph being overlaid
	 * 
	 * @return The overlaid graph.
	 */
	public static <Vertex, G extends AlgGraph<Vertex, G>> G overlay(G left, G right) {
		@SuppressWarnings("unchecked")
		// We know this cast is good, java just can't tell
		G result = (G) left.empty();
		result.overlay(left);
		result.overlay(right);

		return result;
	}

	/**
	 * Connect two graphs into a new graph.
	 * 
	 * @param <Vertex> The type of the vertices.
	 * 
	 * @param left     The graph to connect to
	 * @param right    The graph being connected
	 * 
	 * @return The connected graph.
	 */
	public static <Vertex, G extends AlgGraph<Vertex, G>> G connect(G left, G right) {
		@SuppressWarnings("unchecked")
		// We know this cast is good, java just can't tell
		G result = (G) left.empty();
		result.overlay(left);
		result.connect(right);
		return result;
	}
}