...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package graph
17
18 import (
19 "github.com/go-logr/logr"
20 )
21
22
23 type Edge struct {
24 Source uint32
25 Target uint32
26 Next *Edge
27 }
28
29
30 type Graph struct {
31 head map[uint32]*Edge
32 }
33
34
35 func NewGraph() *Graph {
36 return &Graph{
37 head: make(map[uint32]*Edge),
38 }
39 }
40
41
42 func (g *Graph) Insert(source uint32, target uint32) {
43 newEdge := Edge{
44 Source: source,
45 Target: target,
46 Next: g.head[source],
47 }
48 g.head[source] = &newEdge
49 }
50
51
52 func (g *Graph) IterFrom(source uint32) *Edge {
53 return g.head[source]
54 }
55
56
57 func (g *Graph) Flatten(source uint32, log logr.Logger) []uint32 {
58 current := g.head[source]
59
60 var flatTree []uint32
61 for {
62 if current == nil {
63 break
64 }
65
66 children := g.Flatten(current.Target, log)
67 flatTree = append(flatTree, current.Target)
68 flatTree = append(flatTree, children...)
69
70 current = current.Next
71 }
72
73 log.Info("get flatTree", "source", source, "flatTree", flatTree)
74 return flatTree
75 }
76