/
ref.go
100 lines (87 loc) · 2.34 KB
/
ref.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
package graph
import "strconv"
type RefKey struct {
DefRepo string `json:",omitempty"`
DefUnitType string `json:",omitempty"`
DefUnit string `json:",omitempty"`
DefPath string `json:",omitempty"`
Def bool `json:",omitempty"`
Repo string `json:",omitempty"`
UnitType string `json:",omitempty"`
Unit string `json:",omitempty"`
File string `json:",omitempty"`
CommitID string `json:",omitempty"`
Start uint32 `json:",omitempty"`
End uint32 `json:",omitempty"`
}
func (r *RefKey) RefDefKey() RefDefKey {
return RefDefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
}
}
func (r *Ref) RefKey() RefKey {
return RefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
Def: r.Def,
Repo: r.Repo,
UnitType: r.UnitType,
Unit: r.Unit,
File: r.File,
Start: r.Start,
End: r.End,
}
}
func (r *Ref) RefDefKey() RefDefKey {
return RefDefKey{
DefRepo: r.DefRepo,
DefUnitType: r.DefUnitType,
DefUnit: r.DefUnit,
DefPath: r.DefPath,
}
}
func (r *Ref) DefKey() DefKey {
return DefKey{
Repo: r.DefRepo,
UnitType: r.DefUnitType,
Unit: r.DefUnit,
Path: r.DefPath,
}
}
func (r *Ref) SetFromDefKey(k DefKey) {
r.DefPath = k.Path
r.DefUnitType = k.UnitType
r.DefUnit = k.Unit
r.DefRepo = k.Repo
}
// Sorting
type Refs []*Ref
func (r *Ref) sortKey() string {
return r.DefPath + r.DefRepo + r.DefUnitType + r.DefUnit + r.Repo + r.UnitType + r.Unit + r.File + strconv.Itoa(int(r.Start)) + strconv.Itoa(int(r.End))
}
func (vs Refs) Len() int { return len(vs) }
func (vs Refs) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
func (vs Refs) Less(i, j int) bool { return vs[i].sortKey() < vs[j].sortKey() }
// RefSet is a set of Refs. It can used to determine whether a grapher emits
// duplicate refs.
type RefSet struct {
refs map[Ref]struct{}
}
func NewRefSet() *RefSet {
return &RefSet{make(map[Ref]struct{})}
}
// AddAndCheckUnique adds ref to the set of seen refs, and returns whether the
// ref already existed in the set.
func (c *RefSet) AddAndCheckUnique(ref Ref) (duplicate bool) {
_, present := c.refs[ref]
if present {
return true
}
c.refs[ref] = struct{}{}
return false
}