-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
42 lines (31 loc) · 794 Bytes
/
marshal.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
package internal
type GraphMarshaler[E any] struct {
Vertices map[string]E
Edges [][2]string
}
func (marshaler GraphMarshaler[E]) GenerateGraph() *Graph[E] {
graph := NewGraph[E]()
for name, v := range marshaler.Vertices {
graph.AddVertex(name, v)
}
for _, e := range marshaler.Edges {
graph.AddEdge(e[0], e[1])
}
return graph
}
func NewGraphMarshaler[E any](graph *Graph[E]) *GraphMarshaler[E] {
graph.Lock()
defer graph.Unlock()
marshaler := new(GraphMarshaler[E])
marshaler.Vertices = make(map[string]E)
marshaler.Edges = make([][2]string, 0)
for _, v := range graph.Vertices {
marshaler.Vertices[v.Name] = v.Elem
}
for e := range graph.Edges {
marshaler.Edges = append(marshaler.Edges, [2]string{
e.From.Name, e.To.Name,
})
}
return marshaler
}