-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
104 lines (91 loc) · 1.42 KB
/
utils.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
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
package netmap
func getNodes(b Bucket, path []Bucket) (nodes Nodes) {
if len(path) == 0 {
return b.Nodelist()
}
for _, p := range b.Children() {
if p.Equals(path[0]) {
return getNodes(p, path[1:])
}
}
return nil
}
func contains(nodes Nodes, n Node) bool {
for _, i := range nodes {
if i.N == n.N {
return true
}
}
return false
}
func intersect(a, b Nodes) Nodes {
if a == nil {
return b
}
var (
la, lb = len(a), len(b)
l = min(la, lb)
c = make(Nodes, 0, l)
)
for i, j := 0, 0; i < la && j < lb; {
switch true {
case a[i].N < b[j].N:
i++
case a[i].N > b[j].N:
j++
default:
c = append(c, a[i])
i++
j++
}
}
return c
}
func diff(a Nodes, exclude map[uint32]bool) (c Nodes) {
c = make(Nodes, 0, len(a))
for _, e := range a {
if excl, ok := exclude[e.N]; ok && !excl {
c = append(c, e)
}
}
return
}
func union(a, b Nodes) Nodes {
if a == nil {
return b
} else if b == nil {
return a
}
var (
la, lb = len(a), len(b)
l = la + lb
c = make(Nodes, 0, l)
i, j int
)
for i, j = 0, 0; i < la && j < lb; {
switch true {
case a[i].N < b[j].N:
c = append(c, a[i])
i++
case a[i].N > b[j].N:
c = append(c, b[j])
j++
default:
c = append(c, a[i])
i++
j++
}
}
if i == la {
c = append(c, b[j:]...)
} else {
c = append(c, a[i:]...)
}
return c
}
func min(a, b int) int {
if a < b {
return a
}
return b
}