-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
532 lines (429 loc) · 9.98 KB
/
graph.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package ds
import (
"fmt"
"strings"
"code.google.com/p/go-uuid/uuid"
"github.com/influx6/flux"
)
//Values defines an interface with Values
type Values interface {
Value() interface{}
}
//Nodes represents the standard Graph interface
type Nodes interface {
Values
Equalers
//Represents the archs/edges of this nodes
Sockets() *DeferList
//Bind the supplied node to this node
Connect(Nodes, int) *Socket
//Unbind the supplied node from this one
GetEdge(Nodes) (*Socket, error)
HasEdge(Nodes) bool
RemovalEdges()
Disconnect(Nodes)
DisconnectOne(Nodes)
//ChangeGraph changes the underline graph this nodes belongs to
ChangeGraph(Graphs)
//Graph returns the graph of the node
Graph() Graphs
Arcs() DeferIterator
String() string
}
//Socket represents a connection between two nodes
type Socket struct {
flux.Collector
Attrs *StringSet
To Nodes
From Nodes
Weight int
}
//NewSocket creates a new socket between two nodes with a set weight
func NewSocket(from, to Nodes, weight int) *Socket {
return &Socket{
Collector: flux.NewCollector(),
Attrs: NewStringSet(),
To: to,
From: from,
Weight: weight,
}
}
//Close closes and destroys the socket between the two nodes
func (s *Socket) Close() {
s.To.Disconnect(s.From)
s.From = nil
s.To = nil
s.Clear()
}
//Node represents an element in the graph
type Node struct {
data interface{}
arcs *DeferList
graph Graphs
}
//ChangeValue changes the value of the node
func (n *Node) ChangeValue(d interface{}) {
n.data = d
}
//ChangeGraph changes the graph this nodes is attached to
func (n *Node) ChangeGraph(g Graphs) {
if n.graph == g {
return
}
n.graph = g
itr := n.arcs.Iterator()
for itr.Next() == nil {
cur, ok := itr.Value().(*Socket)
if ok {
cur.Close()
}
}
n.arcs.Clear()
}
//Graph returns the graph we are connected with
func (n *Node) Graph() Graphs {
return n.graph
}
//Equals returns bool if the value is equal
func (n *Node) Equals(d interface{}) bool {
dx, ok := d.(Nodes)
if ok {
if dx == n {
return true
}
return dx.Value() == n.data
}
return n.data == d
}
//Sockets returns the list of arcs/sockets
func (n *Node) Sockets() *DeferList {
return n.arcs
}
//DisconnectOne removes all edges of the giving node
func (n *Node) DisconnectOne(r Nodes) {
n.disconnect(r, true)
}
//Disconnect removes all edges of the giving node
func (n *Node) Disconnect(r Nodes) {
n.disconnect(r, false)
}
//GetEdge returns the socket that connects a node is connected to this node
func (n *Node) GetEdge(r Nodes) (*Socket, error) {
if r == nil {
return nil, ErrNoEdge
}
itr := n.arcs.Iterator()
for itr.Next() == nil {
sock, _ := itr.Value().(*Socket)
if sock.To != r {
continue
}
return sock, nil
}
return nil, ErrNoEdge
}
//HasEdge returns if a node is connected to this node
func (n *Node) HasEdge(r Nodes) bool {
itr := n.arcs.Iterator()
for itr.Next() == nil {
sock, _ := itr.Value().(*Socket)
if sock.To == r {
return true
}
}
return false
}
//RemovalEdges removes all edges of this node
func (n *Node) RemovalEdges() {
itr := n.arcs.Iterator()
for itr.Next() == nil {
sock, _ := itr.Value().(*Socket)
sock.Close()
}
n.arcs.Clear()
}
func (n *Node) disconnect(r Nodes, one bool) {
itr := n.arcs.Iterator()
for itr.Next() == nil {
sock, _ := itr.Value().(*Socket)
if sock.To == r {
key, ok := itr.Key().(*DeferNode)
if ok {
key.Reset()
}
sock.Close()
if one {
break
}
}
}
}
//Connect the provide node to itself
func (n *Node) Connect(r Nodes, weight int) *Socket {
var socket *Socket
var err error
socket, err = n.GetEdge(r)
if err == nil {
return socket
}
socket = NewSocket(n, r, weight)
// _ = r.Connect(n, weight)
n.arcs.AppendElement(socket)
return socket
}
//NewGraphNode returns a new graph node
func NewGraphNode(d interface{}, g Graphs) *Node {
return &Node{
data: d,
arcs: List(),
graph: g,
}
}
//String returns the string value
func (n *Node) String() string {
return fmt.Sprintf("%+v", n.Value())
}
//Arcs returns an iterator of all the arcs/edges of this node
func (n *Node) Arcs() DeferIterator {
itr := n.arcs.Iterator()
return itr.(DeferIterator)
}
//Value returns the value of the node
func (n *Node) Value() interface{} {
return n.data
}
//Graphs represents the standard Graph interface
type Graphs interface {
// sequence.SizableSequencable
UID() string
Contains(interface{}) bool
Get(interface{}) Nodes
Add(...interface{})
AddNode(Nodes)
AddForeignNode(r Nodes)
Bind(interface{}, interface{}, int) (*Socket, bool)
UnBind(interface{}, interface{}) bool
BindNodes(Nodes, Nodes, int) (*Socket, bool)
UnBindNodes(Nodes, Nodes) bool
IsBound(interface{}, interface{}) bool
nodeSet() *NodeSet
Length() int
// UnBindAll(interface{}, interface{}) bool
}
//Graph represent a standard structure of nodes
type Graph struct {
nodes *NodeSet
uid string
}
//Length returns the size of the graph
func (n *Graph) Length() int {
return n.nodes.Length()
}
//Get as a node into the graph
func (n *Graph) Get(r interface{}) Nodes {
nz, _ := n.nodes.GetNode(r)
return nz
}
//Contains returns true wether the graph has the element
func (n *Graph) Contains(r interface{}) bool {
_, f := n.nodes.GetNode(r)
return f
}
//AddNode as a node into the graph and sets the node graph to this graph,thereby clearing all previos connection
func (n *Graph) AddNode(r Nodes) {
r.ChangeGraph(n)
n.nodes.AddNode(r)
}
//AddForeignNode as a node into the graph without setting the node graph to this graph,thereby clearing all previos connection but note if this nodes value is the same with another node in this,this will be rejected
func (n *Graph) AddForeignNode(r Nodes) {
if !n.Contains(r.Value()) {
// r.ChangeGraph(n)
n.nodes.AddNode(r)
}
}
//Length returns the total size of nodes in this graph
// func (n *Graph) Length() int {
// return n.nodes.Length()
// }
//UID returns the auto generated uuid for this graph
func (n *Graph) UID() string {
return n.uid
}
func (n *Graph) String() string {
return strings.Join([]string{
fmt.Sprintf("<Graph UID='%s'>\n", n.uid),
"-> Nodes Length",
fmt.Sprintf("%d", n.Length()),
"</Graph>",
}, " ")
}
//Add as a new node into the graph
func (n *Graph) Add(r ...interface{}) {
for _, v := range r {
n.nodes.AddNode(NewGraphNode(v, n))
}
}
//BindNodes binds the two nodes of this values
func (n *Graph) BindNodes(r, f Nodes, we int) (*Socket, bool) {
if r.Graph() != n || f.Graph() != n {
return nil, false
}
return r.Connect(f, we), true
}
//UnBindNodes binds the two nodes of this values
func (n *Graph) UnBindNodes(r, f Nodes) bool {
if r.Graph() != n || f.Graph() != n {
return false
}
r.Disconnect(f)
return true
}
//Bind binds the two nodes of this values
func (n *Graph) Bind(r, f interface{}, we int) (*Socket, bool) {
rx, rk := n.nodes.GetNode(r)
fx, fk := n.nodes.GetNode(f)
if !rk || !fk {
return nil, false
}
return rx.Connect(fx, we), true
}
//nodeSet returns the internal graph nodeset
func (n *Graph) nodeSet() *NodeSet {
return n.nodes
}
//UnBind unbinds the two nodes of this values
func (n *Graph) UnBind(r, f interface{}) bool {
rx, rk := n.nodes.GetNode(r)
fx, fk := n.nodes.GetNode(f)
if !rk || !fk {
return false
}
rx.Disconnect(fx)
return true
}
//IsBound returns true if both nodes are bound
func (n *Graph) IsBound(r, f interface{}) bool {
rx, rk := n.nodes.GetNode(r)
fx, fk := n.nodes.GetNode(f)
if !rk || !fk {
return false
}
return rx.HasEdge(fx)
}
//NewGraph returns a new graph instance
func NewGraph() *Graph {
return &Graph{
nodes: NewNodeSet(),
uid: uuid.New(),
}
}
//UnvisitedUtil returns the current set of unvisited nodes
func UnvisitedUtil(g Graphs, visited NodeMaps) []Nodes {
unvs := []Nodes{}
if len(visited) <= 0 {
return g.nodeSet().AllNodes()
}
g.nodeSet().EachNode(func(nx Nodes) {
if !visited[nx] {
unvs = append(unvs, nx)
}
})
return unvs
}
//Reset resets the cache
func (d *NodeMaps) Reset() {
*d = make(map[Nodes]bool)
}
//Add removes the last node cache
func (d *NodeMaps) Add(n Nodes) {
(*d)[n] = true
}
//Valid removes the last node cache
func (d *NodeMaps) Valid(n Nodes) bool {
return (*d)[n]
}
//Length returns the length node cache
func (d *NodeMaps) Length() int {
return len(*d)
}
//Length returns the length the cache
func (d *NodeCaches) Length() int {
return len(*d)
}
//Reset resets the cache
func (d *NodeCaches) Reset() {
*d = (*d)[:0]
}
//UncacheRight removes the first node cache and closes the cache object
func (d *NodeCaches) UncacheRight() {
nx := (*d)[0]
*d = (*d)[1:]
nx.Close()
}
//Uncache removes the last node cache and closes the cache object
func (d *NodeCaches) Uncache() {
nx := (*d)[len(*d)-1]
*d = (*d)[:len(*d)-1]
nx.Close()
}
//NthCache let you get a item at a point in the cache
func (d *NodeCaches) NthCache(ind int) (*NodeCache, error) {
if ind >= d.Length() {
return nil, ErrBadIndex
}
var loc int
if ind < 0 {
loc = d.Length() - ind
} else {
loc = ind
}
return (*d)[loc], nil
}
//FirstCache returns the last node cached
func (d *NodeCaches) FirstCache() (*NodeCache, error) {
clen := len(*d)
if clen <= 0 {
return nil, ErrBadIndex
}
return (*d)[0], nil
}
//LastCache returns the last node cached
func (d *NodeCaches) LastCache() (*NodeCache, error) {
clen := len(*d)
if clen <= 0 {
return nil, ErrBadIndex
}
return (*d)[clen-1], nil
}
//AddCache adds a node to the cache
func (d *NodeCaches) AddCache(n Nodes) {
*d = append(*d, NewNodeCache(n))
}
//VisitMaps returns a new NodeCache
func VisitMaps() NodeMaps {
return make(NodeMaps)
}
//NewCache returns a new NodeCache
func NewCache() NodeCaches {
return make(NodeCaches, 0)
}
//NewNodeCache returns a new NodeCache
func NewNodeCache(n Nodes) *NodeCache {
return &NodeCache{
Node: n,
Itr: n.Arcs(),
}
}
//String returns the string of the node
func (n *NodeCache) String() string {
if n.Node != nil {
return fmt.Sprintf("%+s", n.Node.Value())
}
return ""
}
//Close destroys this cache
func (n *NodeCache) Close() error {
n.Node = nil
n.Itr = nil
return nil
}