-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdagger.go
More file actions
1255 lines (1128 loc) · 32.3 KB
/
dagger.go
File metadata and controls
1255 lines (1128 loc) · 32.3 KB
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Package dagger is a collection of generic, concurrency safe datastructures including a Directed Acyclic Graph and others.
Datastructures are implemented using generics in Go 1.18.
Supported Datastructures:
DAG: thread safe directed acyclic graph
Queue: unbounded thread safe fifo queue
Stack: unbounded thread safe lifo stack
BoundedQueue: bounded thread safe fifo queue with a fixed capacity
PriorityQueue: thread safe priority queue
HashMap: thread safe hashmap
Set: thread safe set
ChannelGroup: thread safe group of channels for broadcasting 1 value to N channels
MultiContext: thread safe context for coordinating the cancellation of multiple contexts
Borrower: thread safe object ownership manager
*/
package dagger
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"image"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/goccy/go-graphviz"
"github.com/goccy/go-graphviz/cgraph"
"golang.org/x/sync/errgroup"
"github.com/autom8ter/async"
)
// UniqueID returns a unique identifier with the given prefix
func UniqueID(prefix string) string {
b := make([]byte, 4)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
if prefix == "" {
prefix = "id"
}
return fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(b))
}
// GraphEdge is a relationship between two nodes
type GraphEdge[T Node] struct {
// ID is the unique identifier of the edge
id string
// Metadata is the metadata of the edge
metadata map[string]string
// From returns the root node of the edge
from *GraphNode[T]
// To returns the target node of the edge
to *GraphNode[T]
// Relationship is the relationship between the two nodes
relationship string
edge *cgraph.Edge
}
// ID returns the unique identifier of the node
func (n *GraphEdge[T]) ID() string {
return n.id
}
// Metadata returns the metadata of the node
func (n *GraphEdge[T]) Metadata() map[string]string {
return n.metadata
}
// From returns the from node of the edge
func (n *GraphEdge[T]) From() *GraphNode[T] {
return n.from
}
// To returns the to node of the edge
func (n *GraphEdge[T]) To() *GraphNode[T] {
return n.to
}
// Relationship returns the relationship between the two nodes
func (n *GraphEdge[T]) Relationship() string {
return n.relationship
}
// SetMetadata sets the metadata of the node
func (n *GraphEdge[T]) SetMetadata(metadata map[string]string) {
for k, v := range metadata {
n.metadata[k] = v
}
}
// Node is a node in the graph. It can be connected to other nodes via edges.
type Node interface {
// ID returns the unique identifier of the node
ID() string
// Metadata returns the metadata of the node
Metadata() map[string]string
// SetMetadata sets the metadata of the node
SetMetadata(metadata map[string]string)
}
// GraphNode is a node in the graph. It can be connected to other nodes via edges.
type GraphNode[T Node] struct {
Node
edgesFrom *HashMap[string, *GraphEdge[T]]
edgesTo *HashMap[string, *GraphEdge[T]]
graph *DAG[T]
node *cgraph.Node
}
// DFS performs a depth-first search on the graph starting from the current node
func (n *GraphNode[T]) DFS(ctx context.Context, reverse bool, fn GraphSearchFunc[T]) error {
return n.graph.DFS(ctx, reverse, n, fn)
}
// BFS performs a breadth-first search on the graph starting from the current node
func (n *GraphNode[T]) BFS(ctx context.Context, reverse bool, fn GraphSearchFunc[T]) error {
return n.graph.BFS(ctx, reverse, n, fn)
}
// EdgesFrom iterates over the edges from the current node to other nodes with the given relationship.
// If the relationship is empty, all relationships will be iterated over.
func (n *GraphNode[T]) EdgesFrom(relationship string, fn func(e *GraphEdge[T]) bool) {
n.graph.mu.RLock()
defer n.graph.mu.RUnlock()
n.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
if relationship != "" && edge.Relationship() != relationship {
return true
}
return fn(edge)
})
}
// EdgesTo iterates over the edges from other nodes to the current node with the given relationship.
// If the relationship is empty, all relationships will be iterated over.
func (n *GraphNode[T]) EdgesTo(relationship string, fn func(e *GraphEdge[T]) bool) {
n.graph.mu.RLock()
defer n.graph.mu.RUnlock()
n.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
if relationship != "" && edge.Relationship() != relationship {
return true
}
return fn(edge)
})
}
// SetEdge sets an edge from the current node to the node with the given nodeID.
// If the nodeID does not exist, an error is returned.
// If the edgeID is empty, a unique id will be generated.
// If the metadata is nil, an empty map will be used.
func (n *GraphNode[T]) SetEdge(relationship string, toNode Node, metadata map[string]string) (*GraphEdge[T], error) {
if metadata == nil {
metadata = make(map[string]string)
}
to, ok := n.graph.nodes.Get(toNode.ID())
if !ok {
to = n.graph.SetNode(toNode)
}
n.graph.mu.Lock()
defer n.graph.mu.Unlock()
e := &GraphEdge[T]{
id: strings.ReplaceAll(strings.ToLower(fmt.Sprintf("%v-(%v)-%v", n.ID(), relationship, toNode.ID())), " ", "-"),
metadata: metadata,
from: n,
to: to,
}
n.graph.edges.Set(e.ID(), e)
to.edgesTo.Set(e.ID(), e)
n.edgesFrom.Set(e.ID(), e)
if n.graph.options.vizualize {
ge, err := n.graph.viz.CreateEdge(e.ID(), n.node, to.node)
if err != nil {
return nil, err
}
ge.SetLabel(e.ID())
if label, ok := metadata["label"]; ok {
ge.SetLabel(label)
}
if color, ok := metadata["color"]; ok {
ge.SetColor(color)
}
if fontColor, ok := metadata["fontcolor"]; ok {
ge.SetFontColor(fontColor)
}
if weight, ok := metadata["weight"]; ok {
weightFloat, _ := strconv.ParseFloat(weight, 64)
ge.SetWeight(weightFloat)
}
if penWidth, ok := metadata["penwidth"]; ok {
penWidthFloat, _ := strconv.ParseFloat(penWidth, 64)
ge.SetPenWidth(penWidthFloat)
}
e.edge = ge
}
return e, nil
}
// RemoveEdge removes an edge from the current node by edgeID
func (n *GraphNode[T]) RemoveEdge(edgeID string) {
n.graph.mu.Lock()
defer n.graph.mu.Unlock()
n.removeEdge(edgeID)
}
func (n *GraphNode[T]) removeEdge(edgeID string) {
edge, ok := n.graph.edges.Get(edgeID)
if !ok {
return
}
n.graph.edges.Delete(edgeID)
n.edgesFrom.Delete(edgeID)
n.edgesTo.Delete(edgeID)
if edge.edge != nil {
n.graph.viz.DeleteEdge(edge.edge)
}
}
// Remove removes the current node from the graph
func (n *GraphNode[T]) Remove() error {
n.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
n.removeEdge(edge.ID())
return true
})
n.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
n.removeEdge(edge.ID())
return true
})
n.graph.nodes.Delete(n.ID())
if n.graph.options.vizualize {
n.graph.viz.DeleteNode(n.node)
}
return nil
}
// DirectedGraph returns the graph the node belongs to
func (n *GraphNode[T]) Graph() *DAG[T] {
return n.graph
}
// Ancestors returns the ancestors of the current node
func (n *GraphNode[T]) Ancestors(fn func(node *GraphNode[T]) bool) {
n.graph.mu.RLock()
defer n.graph.mu.RUnlock()
visited := NewSet[string]()
n.ancestors(visited, fn)
}
func (n *GraphNode[T]) ancestors(visited *Set[string], fn func(node *GraphNode[T]) bool) {
n.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
if visited.Contains(edge.From().ID()) {
return true
}
visited.Add(edge.From().ID())
if !fn(edge.From()) {
return false
}
edge.From().ancestors(visited, fn)
return true
})
}
// Descendants returns the descendants of the current node
func (n *GraphNode[T]) Descendants(fn func(node *GraphNode[T]) bool) {
n.graph.mu.RLock()
defer n.graph.mu.RUnlock()
visited := NewSet[string]()
n.descendants(visited, fn)
}
func (n *GraphNode[T]) descendants(visited *Set[string], fn func(node *GraphNode[T]) bool) {
n.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
if visited.Contains(edge.To().ID()) {
return true
}
visited.Add(edge.To().ID())
if !fn(edge.To()) {
return false
}
edge.To().descendants(visited, fn)
return true
})
}
// IsConnectedTo returns true if the current node is connected to the given node in any direction
func (n *GraphNode[T]) IsConnectedTo(node *GraphNode[T]) bool {
var result bool
n.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
if edge.To() == node {
result = true
return false
}
return true
})
n.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
if edge.From() == node {
result = true
return false
}
return true
})
return result
}
// DAG is a concurrency safe, mutable, in-memory directed graph
type DAG[T Node] struct {
nodes *HashMap[string, *GraphNode[T]]
edges *HashMap[string, *GraphEdge[T]]
gviz *graphviz.Graphviz
viz *cgraph.Graph
mu sync.RWMutex
options *dagOpts
}
type dagOpts struct {
vizualize bool
}
// DagOpt is an option for configuring a DAG
type DagOpt func(*dagOpts)
// WithVizualization enables graphviz visualization on the DAG
func WithVizualization() DagOpt {
return func(opts *dagOpts) {
opts.vizualize = true
}
}
// NewDAG creates a new Directed Acyclic Graph instance
func NewDAG[T Node](opts ...DagOpt) (*DAG[T], error) {
var err error
options := &dagOpts{}
for _, opt := range opts {
opt(options)
}
g := &DAG[T]{
nodes: NewHashMap[string, *GraphNode[T]](),
edges: NewHashMap[string, *GraphEdge[T]](),
gviz: graphviz.New(),
options: options,
}
if options.vizualize {
graph, _ := g.gviz.Graph()
g.viz = graph
}
return g, err
}
// SetNode sets a node in the graph - it will use the node's ID as the key and overwrite any existing node with the same ID
func (g *DAG[T]) SetNode(node Node) *GraphNode[T] {
n := &GraphNode[T]{
Node: node,
edgesTo: NewHashMap[string, *GraphEdge[T]](),
edgesFrom: NewHashMap[string, *GraphEdge[T]](),
graph: g,
}
g.nodes.Set(node.ID(), n)
if g.options.vizualize {
gn, err := g.viz.CreateNode(fmt.Sprintf("%v", node.ID()))
if err != nil {
panic(err)
}
gn.SetLabel(fmt.Sprintf("%v", node.ID()))
if label, ok := node.Metadata()["label"]; ok {
gn.SetLabel(label)
}
if color, ok := node.Metadata()["color"]; ok {
gn.SetColor(color)
}
n.node = gn
}
return n
}
// HasNode returns true if the node with the given id exists in the graph
func (g *DAG[T]) HasNode(id string) bool {
_, ok := g.nodes.Get(id)
return ok
}
// HasEdge returns true if the edge with the given id exists in the graph
func (g *DAG[T]) HasEdge(id string) bool {
_, ok := g.edges.Get(id)
return ok
}
// GetNode returns the node with the given id
func (g *DAG[T]) GetNode(id string) (*GraphNode[T], bool) {
val, ok := g.nodes.Get(id)
return val, ok
}
// Size returns the number of nodes and edges in the graph
func (g *DAG[T]) Size() (int, int) {
return g.nodes.Len(), g.edges.Len()
}
// GetNodes returns all nodes in the graph
func (g *DAG[T]) GetNodes() []*GraphNode[T] {
nodes := make([]*GraphNode[T], 0, g.nodes.Len())
g.nodes.Range(func(key string, val *GraphNode[T]) bool {
nodes = append(nodes, val)
return true
})
return nodes
}
// GetEdges returns all edges in the graph
func (g *DAG[T]) GetEdges() []*GraphEdge[T] {
edges := make([]*GraphEdge[T], 0, g.edges.Len())
g.edges.Range(func(key string, val *GraphEdge[T]) bool {
edges = append(edges, val)
return true
})
return edges
}
// GetEdge returns the edge with the given id
func (g *DAG[T]) GetEdge(id string) (*GraphEdge[T], bool) {
val, ok := g.edges.Get(id)
return val, ok
}
// RangeEdges iterates over all edges in the graph
func (g *DAG[T]) RangeEdges(fn func(e *GraphEdge[T]) bool) {
g.edges.Range(func(key string, val *GraphEdge[T]) bool {
return fn(val)
})
}
// RangeNodes iterates over all nodes in the graph
func (g *DAG[T]) RangeNodes(fn func(n *GraphNode[T]) bool) {
g.nodes.Range(func(key string, val *GraphNode[T]) bool {
return fn(val)
})
}
// GraphSearchFunc is a function that is called on each node in the graph during a search
type GraphSearchFunc[T Node] func(ctx context.Context, relationship string, node *GraphNode[T]) bool
// BFS executes a depth first search on the graph starting from the current node.
// The reverse parameter determines whether the search is reversed or not.
// The fn parameter is a function that is called on each node in the graph. If the function returns false, the search is stopped.
func (g *DAG[T]) BFS(ctx context.Context, reverse bool, start *GraphNode[T], search GraphSearchFunc[T]) error {
var visited = NewSet[string]()
stack := NewStack[*searchItem[T]]()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := g.breadthFirstSearch(ctx, &breadthFirstSearchState[T]{
visited: visited,
stack: stack,
reverse: reverse,
root: start,
next: start,
relationship: "",
}); err != nil {
return err
}
stack.Range(func(element *searchItem[T]) bool {
if element.node.ID() != start.ID() {
return search(ctx, element.relationship, element.node)
}
return true
})
return nil
}
// DFS executes a depth first search on the graph starting from the current node.
// The reverse parameter determines whether the search is reversed or not.
// The fn parameter is a function that is called on each node in the graph. If the function returns false, the search is stopped.
func (g *DAG[T]) DFS(ctx context.Context, reverse bool, start *GraphNode[T], fn GraphSearchFunc[T]) error {
ctx, cancel := context.WithCancel(ctx)
var visited = NewSet[string]()
queue := NewBoundedQueue[*searchItem[T]](0)
egp1, ctx := errgroup.WithContext(ctx)
egp1.Go(func() error {
egp2, ctx := errgroup.WithContext(ctx)
if err := g.depthFirstSearch(ctx, &depthFirstSearchState[T]{
visited: visited,
egp: egp2,
queue: queue,
reverse: reverse,
root: start,
next: start,
relationship: "",
}); err != nil {
return err
}
if err := egp2.Wait(); err != nil {
return err
}
cancel()
return nil
})
egp1.Go(func() error {
queue.RangeContext(ctx, func(element *searchItem[T]) bool {
if element.node.ID() != start.ID() {
return fn(ctx, element.relationship, element.node)
}
return true
})
return nil
})
if err := egp1.Wait(); err != nil {
return err
}
return nil
}
// searchItem is an item that is used in the search queue/stack in DFS/BFS
type searchItem[T Node] struct {
node *GraphNode[T]
relationship string
}
type breadthFirstSearchState[T Node] struct {
visited *Set[string]
stack *Stack[*searchItem[T]]
reverse bool
root *GraphNode[T]
next *GraphNode[T]
relationship string
}
func (g *DAG[T]) breadthFirstSearch(ctx context.Context, state *breadthFirstSearchState[T]) error {
if ctx.Err() != nil {
return ctx.Err()
}
if state.next == nil {
state.next = state.root
}
if !state.visited.Contains(state.next.ID()) {
state.visited.Add(state.next.ID())
state.stack.Push(&searchItem[T]{
node: state.next,
relationship: state.relationship,
})
if state.reverse {
state.next.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
g.breadthFirstSearch(ctx, &breadthFirstSearchState[T]{
stack: state.stack,
reverse: state.reverse,
root: state.root,
next: edge.From(),
relationship: edge.Relationship(),
visited: state.visited,
})
return state.visited.Len() < g.nodes.Len() && ctx.Err() == nil
})
} else {
state.next.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
g.breadthFirstSearch(ctx, &breadthFirstSearchState[T]{
visited: state.visited,
stack: state.stack,
reverse: state.reverse,
root: state.root,
next: edge.To(),
relationship: edge.Relationship(),
})
return state.visited.Len() < g.nodes.Len() && ctx.Err() == nil
})
}
}
return ctx.Err()
}
type depthFirstSearchState[T Node] struct {
visited *Set[string]
egp *errgroup.Group
queue *BoundedQueue[*searchItem[T]]
reverse bool
root *GraphNode[T]
next *GraphNode[T]
relationship string
}
func (g *DAG[T]) depthFirstSearch(ctx context.Context, state *depthFirstSearchState[T]) error {
if ctx.Err() != nil {
return ctx.Err()
}
if state.next == nil {
state.next = state.root
}
if !state.visited.Contains(state.next.ID()) {
state.visited.Add(state.next.ID())
state.queue.Push(&searchItem[T]{
node: state.next,
relationship: state.relationship,
})
if state.reverse {
state.egp.Go(func() error {
state.next.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
g.depthFirstSearch(ctx, &depthFirstSearchState[T]{
visited: state.visited,
egp: state.egp,
queue: state.queue,
reverse: state.reverse,
root: state.root,
next: edge.From(),
relationship: edge.Relationship(),
})
return state.visited.Len() < g.nodes.Len() && ctx.Err() == nil
})
return nil
})
} else {
state.egp.Go(func() error {
state.next.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
g.depthFirstSearch(ctx, &depthFirstSearchState[T]{
visited: state.visited,
egp: state.egp,
queue: state.queue,
reverse: state.reverse,
root: state.root,
next: edge.To(),
relationship: edge.Relationship(),
})
return state.visited.Len() < g.nodes.Len() && ctx.Err() == nil
})
return nil
})
}
}
return ctx.Err()
}
// Acyclic returns true if the graph contains no cycles.
func (g *DAG[T]) Acyclic() bool {
isAcyclic := true
g.nodes.Range(func(key string, node *GraphNode[T]) bool {
if node.edgesFrom.Len() > 0 {
visited := NewSet[string]()
onStack := NewSet[string]()
if g.isCyclic(node, visited, onStack) {
isAcyclic = false
return false
}
}
return true
})
return isAcyclic
}
// isAcyclic returns true if the graph contains no cycles.
func (g *DAG[T]) isCyclic(node *GraphNode[T], visited *Set[string], onStack *Set[string]) bool {
visited.Add(node.ID())
onStack.Add(node.ID())
result := false
node.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
if visited.Contains(edge.To().ID()) {
if g.isCyclic(edge.To(), visited, onStack) {
result = true
return false
}
} else if onStack.Contains(edge.To().ID()) {
result = true
return false
}
return true
})
return result
}
func (g *DAG[T]) TopologicalSort(reverse bool) ([]*GraphNode[T], error) {
if !g.Acyclic() {
return nil, fmt.Errorf("topological sort cannot be computed on cyclical graph")
}
stack := NewStack[*GraphNode[T]]()
permanent := NewSet[string]()
temporary := NewSet[string]()
g.nodes.Range(func(key string, node *GraphNode[T]) bool {
g.topology(true, stack, node, permanent, temporary)
return true
})
var sorted []*GraphNode[T]
for stack.Len() > 0 {
val, _ := stack.Pop()
sorted = append(sorted, val)
}
if reverse {
for i, j := 0, len(sorted)-1; i < j; i, j = i+1, j-1 {
sorted[i], sorted[j] = sorted[j], sorted[i]
}
}
return sorted, nil
}
func (g *DAG[T]) topology(reverse bool, stack *Stack[*GraphNode[T]], node *GraphNode[T], permanent, temporary *Set[string]) {
if permanent.Contains(node.ID()) {
return
}
if temporary.Contains(node.ID()) {
panic("not a DAG")
}
temporary.Add(node.ID())
if reverse {
node.edgesTo.Range(func(key string, edge *GraphEdge[T]) bool {
g.topology(reverse, stack, edge.From(), permanent, temporary)
return true
})
} else {
node.edgesFrom.Range(func(key string, edge *GraphEdge[T]) bool {
g.topology(reverse, stack, edge.From(), permanent, temporary)
return true
})
}
temporary.Remove(node.ID())
permanent.Add(node.ID())
stack.Push(node)
}
// GraphViz returns a graphviz image
func (g *DAG[T]) GraphViz() (image.Image, error) {
if g.viz == nil {
return nil, fmt.Errorf("graphviz not configured")
}
g.mu.RLock()
defer g.mu.RUnlock()
img, err := g.gviz.RenderImage(g.viz)
if err != nil {
return nil, err
}
return img, nil
}
// NewHashMap creates a new generic hash map
func NewHashMap[K comparable, V any]() *HashMap[K, V] {
return &HashMap[K, V]{
data: sync.Map{},
}
}
// HashMap is a thread safe map
type HashMap[K comparable, V any] struct {
data sync.Map
}
// Len returns the length of the map
func (n *HashMap[K, V]) Len() int {
count := 0
n.data.Range(func(key, value interface{}) bool {
count++
return true
})
return count
}
// Get gets the value from the key
func (n *HashMap[K, V]) Get(key K) (V, bool) {
c, ok := n.data.Load(key)
if !ok {
return *new(V), ok
}
return c.(V), ok
}
// Set sets the key to the value
func (n *HashMap[K, V]) Set(key K, value V) {
n.data.Store(key, value)
}
// Delete deletes the key from the map
func (n *HashMap[K, V]) Delete(key K) {
n.data.Delete(key)
}
// Exists returns true if the key exists in the map
func (n *HashMap[K, V]) Exists(key K) bool {
_, ok := n.Get(key)
return ok
}
// Clear clears the map
func (n *HashMap[K, V]) Clear() {
n.data.Range(func(key, value interface{}) bool {
n.data.Delete(key)
return true
})
}
// Keys returns a copy of the keys in the map as a slice
func (n *HashMap[K, V]) Keys() []K {
var keys []K
n.data.Range(func(key, value interface{}) bool {
keys = append(keys, key.(K))
return true
})
return keys
}
// Values returns a copy of the values in the map as a slice
func (n *HashMap[K, V]) Values() []V {
var values []V
n.data.Range(func(key, value interface{}) bool {
values = append(values, value.(V))
return true
})
return values
}
// Range ranges over the map with a function until false is returned
func (n *HashMap[K, V]) Range(f func(key K, value V) bool) {
n.data.Range(func(key, value interface{}) bool {
return f(key.(K), value.(V))
})
}
// Filter returns a new hashmap with the values that return true from the function
func (n *HashMap[K, V]) Filter(f func(key K, value V) bool) *HashMap[K, V] {
filtered := NewHashMap[K, V]()
n.data.Range(func(key, value interface{}) bool {
if f(key.(K), value.(V)) {
filtered.Set(key.(K), value.(V))
}
return true
})
return filtered
}
// Map returns a copy of the hashmap as a map[string]T
func (n *HashMap[K, V]) Map() map[K]V {
copied := map[K]V{}
n.data.Range(func(key, value interface{}) bool {
copied[key.(K)] = value.(V)
return true
})
return copied
}
// priorityQueueItem is an item in the priority queue
type priorityQueueItem[T any] struct {
value T
priority float64
}
// PriorityQueue is a thread safe priority queue
type PriorityQueue[T any] struct {
items []*priorityQueueItem[T]
mu sync.RWMutex
}
// NewPriorityQueue creates a new priority queue
func NewPriorityQueue[T any]() *PriorityQueue[T] {
return &PriorityQueue[T]{
items: []*priorityQueueItem[T]{},
mu: sync.RWMutex{},
}
}
func (q *PriorityQueue[T]) UpdatePriority(value T, priority float64) {
q.mu.Lock()
defer q.mu.Unlock()
for i, item := range q.items {
if reflect.DeepEqual(item.value, value) {
q.items[i].priority = priority
sort.Slice(q.items, func(i, j int) bool {
return q.items[i].priority < q.items[j].priority
})
return
}
}
}
// Len returns the length of the queue
func (q *PriorityQueue[T]) Len() int {
q.mu.RLock()
defer q.mu.RUnlock()
return len(q.items)
}
// Push pushes an item onto the queue
func (q *PriorityQueue[T]) Push(item T, weight float64) {
q.mu.Lock()
defer q.mu.Unlock()
q.items = append(q.items, &priorityQueueItem[T]{value: item, priority: weight})
sort.Slice(q.items, func(i, j int) bool {
return q.items[i].priority < q.items[j].priority
})
}
// Pop pops an item off the queue
func (q *PriorityQueue[T]) Pop() (T, bool) {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.items) == 0 {
return *new(T), false
}
item := q.items[0]
q.items = q.items[1:]
return item.value, true
}
// Peek returns the next item in the queue without removing it
func (q *PriorityQueue[T]) Peek() (T, bool) {
q.mu.RLock()
defer q.mu.RUnlock()
if len(q.items) == 0 {
return *new(T), false
}
return q.items[0].value, true
}
// BoundedQueue is a basic FIFO BoundedQueue based on a buffered channel
type BoundedQueue[T any] struct {
closeOnce sync.Once
ch *async.Channel[T]
}
// NewBoundedQueue returns a new BoundedQueue with the given max size. When the max size is reached, the queue will block until a value is removed.
// If maxSize is 0, the queue will always block until a value is removed. The BoundedQueue is concurrent-safe.
func NewBoundedQueue[T any](maxSize int) *BoundedQueue[T] {
return &BoundedQueue[T]{ch: async.NewChannel[T](context.Background(), async.WithBufferSize[T](maxSize))}
}
// Range executes a provided function once for each BoundedQueue element until it returns false.
func (q *BoundedQueue[T]) Range(fn func(element T) bool) {
for {
value, ok := q.ch.Recv(context.Background())
if !ok {
return
}
if !fn(value) {
return
}
}
}
// RangeContext executes a provided function once for each BoundedQueue element until it returns false or a value is sent to the done channel.
// Use this function when you want to continuously process items from the queue until a done signal is received.
func (q *BoundedQueue[T]) RangeContext(ctx context.Context, fn func(element T) bool) {
for {
value, ok := q.ch.Recv(ctx)
if !ok {
return
}
if !fn(value) {
return
}
}
}
// Close closes the BoundedQueue channel.
func (q *BoundedQueue[T]) Close() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
q.ch.Close(ctx)
}
// Push adds an element to the end of the BoundedQueue and returns a channel that will block until the element is added.
// If the queue is full, it will block until an element is removed.
func (q *BoundedQueue[T]) Push(val T) bool {
return q.ch.Send(context.Background(), val)
}
// PushContext adds an element to the end of the BoundedQueue and returns a channel that will block until the element is added.
// If the queue is full, it will block until an element is removed or the context is cancelled.
func (q *BoundedQueue[T]) PushContext(ctx context.Context, val T) bool {
return q.ch.Send(ctx, val)
}
// Pop removes and returns an element from the beginning of the BoundedQueue.
func (q *BoundedQueue[T]) Pop() (T, bool) {
return q.ch.Recv(context.Background())
}
// PopContext removes and returns an element from the beginning of the BoundedQueue.
// If no element is available, it will block until an element is available or the context is cancelled.
func (q *BoundedQueue[T]) PopContext(ctx context.Context) (T, bool) {
return q.ch.Recv(ctx)
}
// Len returns the number of elements in the BoundedQueue.
func (q *BoundedQueue[T]) Len() int {
return q.ch.Len()
}
// Queue is a thread safe non-blocking queue
type Queue[T any] struct {
mu sync.RWMutex
values []T
}