-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
338 lines (311 loc) · 6.88 KB
/
path.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2021 Cloud Privacy Labs, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lpg
import (
"fmt"
"strings"
)
// A Path can be a node, or node-edge-node...-edge-node sequence.
type Path struct {
only *Node
path []PathElement
}
type PathElement struct {
Edge *Edge
Reverse bool
}
func NewPathFromElements(elements ...PathElement) *Path {
return &Path{path: elements}
}
func NewPathElementsFromEdges(edges []*Edge) []PathElement {
pe := make([]PathElement, len(edges))
for idx, e := range edges {
pe[idx].Edge = e
}
return pe
}
// PathFromNode creates a path containing a single node
func PathFromNode(node *Node) *Path {
if node == nil {
panic("Nil node in path")
}
return &Path{
only: node,
}
}
// Clone returns a copy of the path
func (p *Path) Clone() *Path {
ret := Path{
only: p.only,
path: make([]PathElement, len(p.path)),
}
copy(ret.path, p.path)
return &ret
}
// SetOnlyNode sets the path to a single node
func (p *Path) SetOnlyNode(node *Node) *Path {
p.only = node
p.path = nil
return p
}
// Clear the path
func (p *Path) Clear() *Path {
p.only = nil
p.path = nil
return p
}
// Last returns the last node of the path
func (p *Path) Last() *Node {
if p.only != nil {
return p.only
}
if len(p.path) != 0 {
return p.path[len(p.path)-1].GetTargetNode()
}
return nil
}
// First returns the first node of the path
func (p *Path) First() *Node {
if p.only != nil {
return p.only
}
if len(p.path) != 0 {
return p.path[0].GetSourceNode()
}
return nil
}
func (p PathElement) GetSourceNode() *Node {
if p.Reverse {
return p.Edge.GetTo()
}
return p.Edge.GetFrom()
}
func (p PathElement) GetTargetNode() *Node {
if p.Reverse {
return p.Edge.GetFrom()
}
return p.Edge.GetTo()
}
// Append an edge to the end of the path. The edge must be outgoing from the last node of the path
func (p *Path) Append(path ...PathElement) *Path {
if len(path) == 0 {
return p
}
if p.NumNodes() == 0 {
p.path = make([]PathElement, len(path))
copy(p.path, path)
return p
}
last := p.Last()
if last != nil && last != path[0].GetSourceNode() {
fmt.Println(last.GetLabels(), path[0].GetSourceNode().GetLabels())
fmt.Println(p.String())
panic("Appended edge is disconnected from path")
}
if p.only != nil {
p.only = nil
}
for _, pe := range path {
if pe.GetSourceNode() != last {
panic("Appended edges are disconnected")
}
last = pe.GetTargetNode()
}
p.path = append(p.path, path...)
return p
}
// AppendPath can append a single node or a path to the callers path.
// If the caller does not contain a path, the passed path is copied into the receiver
func (p *Path) AppendPath(path *Path) *Path {
switch p.NumNodes() {
case 0:
p.path = make([]PathElement, len(path.path))
copy(p.path, path.path)
return p
case 1:
if p.only != nil {
p.only = nil
}
default:
switch path.NumNodes() {
case 0:
return p
case 1:
if path.only != nil {
path.only = nil
}
}
}
return p.Append(path.path...)
}
// GetEdge returns the nth edge
func (p *Path) GetEdge(n int) *Edge {
if p.only != nil {
return nil
}
if n < len(p.path) {
return p.path[n].Edge
}
return nil
}
// GetNode returns the nth node
func (p *Path) GetNode(n int) *Node {
if p.only != nil && n == 0 {
return p.only
}
if n == 0 {
e := p.First()
if e != nil {
return e
}
panic("Invalid node index")
}
return p.path[n-1].GetTargetNode()
}
// String returns the path as a string
func (p *Path) String() string {
sb := strings.Builder{}
for _, p := range p.path {
sb.WriteString(p.Edge.GetFrom().String() + "->" + p.Edge.GetTo().String() + " ")
if p.Reverse {
sb.WriteString("Reverse ")
}
// p.Edge.GetFrom().String()
// if p.Reverse {
// sb.WriteString(p.GetSourceNode().String() + "<-" + p.GetTargetNode().String())
// } else {
// sb.WriteString(p.GetSourceNode().String() + "->" + p.GetTargetNode().String())
// }
}
return strings.TrimSpace(sb.String())
}
// HasPrefix return if all edges of p1 exist in path
func (p *Path) HasPrefix(p1 []PathElement) bool {
if p.NumNodes() < 2 {
return false
}
if len(p1) == 0 {
return true
}
if len(p1) > p.NumEdges() {
return false
}
for path1Idx, e1 := range p1 {
if e1 != p.path[path1Idx] {
return false
}
}
return true
}
// HasPrefixPaths returns if all paths elements of p1 exist in path
func (p *Path) HasPrefixPath(p1 *Path) bool {
switch p.NumNodes() {
case 0:
return p1.NumNodes() == 0
case 1:
switch p1.NumNodes() {
case 0:
return true
case 1:
return p.only == p1.only
default:
return false
}
default:
switch p1.NumNodes() {
case 0:
return true
case 1:
return p.First() == p1.First()
default:
return p.HasPrefix(p1.path)
}
}
}
// Slice returns a copy of p partitioned by the args start and end index
func (p *Path) Slice(startNodeIndex, endNodeIndex int) *Path {
if p.NumNodes() == 0 {
panic("index error")
}
if startNodeIndex == 0 && endNodeIndex == 0 {
return &Path{
only: p.First(),
}
}
if startNodeIndex > p.NumNodes() {
panic("start index greater than length of num nodes")
}
if endNodeIndex > p.NumNodes() || endNodeIndex == -1 {
endNodeIndex = p.NumNodes()
}
if endNodeIndex < startNodeIndex {
panic("")
}
if startNodeIndex == endNodeIndex {
return &Path{
only: p.path[startNodeIndex].GetSourceNode(),
}
}
if startNodeIndex == p.NumNodes()-1 {
return &Path{
only: p.Last(),
}
}
pth := make([]PathElement, endNodeIndex-startNodeIndex-1)
copy(pth, p.path[startNodeIndex:endNodeIndex-1])
return &Path{path: pth}
}
// RemoveLast removes the last edge from the path. If the path has one
// edge, the path becomes a single node containing the source. If the
// path has only a node, path becomes empty
func (p *Path) RemoveLast() *Path {
if p.only != nil {
p.only = nil
return p
}
if len(p.path) > 0 {
p.path[len(p.path)-1].Edge.Remove()
p.path = p.path[:len(p.path)-1]
}
return p
}
// RemoveFirst removes the first edge from the graph. If the path has
// only a node, path becomes empty
func (p *Path) RemoveFirst() *Path {
if p.only != nil {
p.only = nil
return p
}
if len(p.path) > 0 {
p.path[0].Edge.Remove()
p.path = p.path[1:]
}
return p
}
func (p *Path) NumEdges() int {
return len(p.path)
}
func (p *Path) NumNodes() int {
if p.only != nil {
return 1
}
n := p.NumEdges()
if n == 0 {
return 0
}
return n + 1
}
func (p *Path) IsEmpty() bool {
return p.only == nil && len(p.path) == 0
}