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