-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedmap.go
69 lines (60 loc) · 988 Bytes
/
fedmap.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
package main
import (
"sort"
)
type FedMap struct {
name map[int]string
edges map[int][]int
nEdges int
}
func NewFedMap() *FedMap {
return &FedMap{
name: make(map[int]string, 0),
edges: make(map[int][]int, 0),
}
}
func (f *FedMap) AddNode(id int, name string) {
f.name[id] = name
}
func (f *FedMap) AddEdge(from, to int) {
fe, ok := f.edges[from]
if ok {
fe = append(fe, to)
} else {
fe = []int{to}
}
f.edges[from] = fe
te, ok := f.edges[to]
if ok {
te = append(te, from)
} else {
te = []int{from}
}
f.edges[to] = te
f.nEdges++
}
func (f *FedMap) Nodes() []int {
r := make([]int, 0, len(f.name))
for k, _ := range f.name {
r = append(r, k)
}
sort.Sort(sort.IntSlice(r))
return r
}
func (f *FedMap) Name(i int) string {
name, ok := f.name[i]
if !ok {
return ""
}
return name
}
func (f *FedMap) Edges(i int) []int {
edges, ok := f.edges[i]
if !ok {
return nil
}
return edges
}
func (f *FedMap) NEdges() int {
return f.nEdges
}