This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuilder.go
169 lines (144 loc) · 4.35 KB
/
builder.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gogl
/* Graph type constants. Used primarily for specs. */
// Describes the properties of a graph as a bitfield.
type GraphProperties uint16
const (
// Edge directedness. Flags are provided for both, though gogl does not really support
// a hybrid graph containing both directed and undirected edges. Algorithms would have
// undefined results.
G_UNDIRECTED = 1 << iota
G_DIRECTED
// Edge type. Basic (untyped edges, represented solely by the Edge interface) is the implied zero-value.
G_BASIC
G_LABELED
G_WEIGHTED
G_DATA
// Multiplicity. Simple (no loops or multiple edges) is the implied zero-value.
G_SIMPLE
G_LOOPS
G_PARALLEL
// Mutability. Immutable is the implied zero-value.
G_IMMUTABLE
G_MUTABLE
G_PERSISTENT = 1<<iota | G_MUTABLE // Persistent graphs are, kinda weirdly, both.
)
/*
TODO go back to using zero vals; see if the following can be made to work
const (
G_UNDIRECTED = ^G_DIRECTED
G_BASIC = ^(G_LABELED | G_WEIGHTED | G_DATA)
G_SIMPLE = ^(G_LOOPS | G_PARALLEL)
G_IMMUTABLE = ^G_MUTABLE
)
*/
type GraphSpec struct {
Props GraphProperties
Source GraphSource
}
// Create a graph spec, which allows specification and creation of a graph through
// a fluent builder-style interface.
func Spec() GraphSpec {
b := GraphSpec{Props: G_UNDIRECTED | G_SIMPLE | G_BASIC | G_MUTABLE}
return b
}
// Specify that the graph should be populated from the provided source "graph".
//
// The GraphSource interface is used here because this is an ideal place
// at which to load in, for example, graph data exported into a flat file;
// a GraphSource can represent that data and only implement the minimal interface.
func (b GraphSpec) Using(g GraphSource) GraphSpec {
b.Source = g
return b
}
// Specify that the graph should have undirected edges.
func (b GraphSpec) Undirected() GraphSpec {
b.Props &^= G_DIRECTED
b.Props |= G_UNDIRECTED
return b
}
// Specify that the graph should have directed edges (be a digraph).
func (b GraphSpec) Directed() GraphSpec {
b.Props &^= G_UNDIRECTED
b.Props |= G_DIRECTED
return b
}
// Specify that the edges should be "basic" - no weights, labels, or data.
func (b GraphSpec) Basic() GraphSpec {
b.Props &^= G_LABELED | G_WEIGHTED | G_DATA
b.Props |= G_BASIC
return b
}
// Specify that the edges should be labeled. See LabeledEdge
func (b GraphSpec) Labeled() GraphSpec {
b.Props &^= G_BASIC
b.Props |= G_LABELED
return b
}
// Specify that the edges should be weighted. See WeightedEdge
func (b GraphSpec) Weighted() GraphSpec {
b.Props &^= G_BASIC
b.Props |= G_WEIGHTED
return b
}
// Specify that the edges should contain arbitrary data. See DataEdge
func (b GraphSpec) DataEdges() GraphSpec {
b.Props &^= G_BASIC
b.Props |= G_DATA
return b
}
// Specify that the graph should be simple - have no loops or multiple edges.
func (b GraphSpec) SimpleGraph() GraphSpec {
b.Props &^= G_LOOPS | G_PARALLEL
b.Props |= G_SIMPLE
return b
}
// Specify that the graph is a multigraph - allows parallel edges, but no loops.
func (b GraphSpec) MultiGraph() GraphSpec {
b.Props &^= G_SIMPLE | G_LOOPS
b.Props |= G_PARALLEL
return b
}
// Specify that the graph is a pseudograph - allows both loops and parallel edges.
func (b GraphSpec) PseudoGraph() GraphSpec {
b.Props &^= G_SIMPLE
b.Props |= G_LOOPS | G_PARALLEL
return b
}
// Specify that the graph allows parallel edges.
func (b GraphSpec) Parallel() GraphSpec {
b.Props &^= G_SIMPLE
b.Props |= G_PARALLEL
return b
}
// Specify that the graph allows loops - edges connecting a vertex to itself.
func (b GraphSpec) Loop() GraphSpec {
b.Props &^= G_SIMPLE
b.Props |= G_LOOPS
return b
}
// Specify that the graph is mutable.
func (b GraphSpec) Mutable() GraphSpec {
b.Props &^= G_IMMUTABLE | G_PERSISTENT
b.Props |= G_MUTABLE
return b
}
// Specify that the graph is immutable.
func (b GraphSpec) Immutable() GraphSpec {
b.Props &^= G_PERSISTENT | G_MUTABLE // redundant, but being thorough
b.Props |= G_IMMUTABLE
return b
}
// Specify that the graph is persistent.
// TODO Commented out until this actually gets implemented
//func (b GraphSpec) Persistent() GraphSpec {
//b.Props &^= G_IMMUTABLE
//b.Props |= G_PERSISTENT
//return b
//}
// Creates a graph from the spec, using the provided creator function.
//
// This is just a convenience method; the creator function can always
// be called directly.
func (b GraphSpec) Create(f func(GraphSpec) Graph) Graph {
return f(b)
}