-
Notifications
You must be signed in to change notification settings - Fork 458
/
dedupe.go
44 lines (40 loc) · 866 Bytes
/
dedupe.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
package selector
import (
"sort"
"github.com/spiffe/spire/proto/spire/common"
)
func Dedupe(selectorSets ...[]*common.Selector) []*common.Selector {
var deduped []*common.Selector
for _, selectorSet := range selectorSets {
for _, selector := range selectorSet {
deduped = insertSelector(deduped, selector)
}
}
return deduped
}
func insertSelector(ss []*common.Selector, s *common.Selector) []*common.Selector {
// find the insertion index
i, found := sort.Find(len(ss), func(i int) int {
switch {
case s.Type < ss[i].Type:
return -1
case s.Type > ss[i].Type:
return 1
case s.Value < ss[i].Value:
return -1
case s.Value > ss[i].Value:
return 1
default:
return 0
}
})
if found {
// already inserted
return ss
}
// otherwise, shift and insert
ss = append(ss, nil)
copy(ss[i+1:], ss[i:])
ss[i] = s
return ss
}