-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooling.go
52 lines (39 loc) · 1.22 KB
/
tooling.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
package depgraph
import (
"sort"
mapset "github.com/deckarep/golang-set"
)
// push accepts a vertex as an uint64 and its dependency
// edges as a mapset.Set and inserts them into the DependencyGraph
func (dgraph *DependencyGraph) push(ptr uint64, set mapset.Set) {
dgraph.mutex.Lock()
defer dgraph.mutex.Unlock()
dgraph.graph[ptr] = set
}
// pop accepts a vertex as an uint64 and removes it from the graph
func (dgraph *DependencyGraph) pop(ptr uint64) {
dgraph.mutex.Lock()
defer dgraph.mutex.Unlock()
delete(dgraph.graph, ptr)
}
// peek returns the edge dependencies for a given vertex as a
// mapset.Set along with a boolean indicating if the vertex existed.
func (dgraph *DependencyGraph) peek(ptr uint64) (mapset.Set, bool) {
dgraph.mutex.RLock()
defer dgraph.mutex.RUnlock()
set, ok := dgraph.graph[ptr]
return set, ok
}
// sorted returns the vertices of the DependencyGraph as a sorted slice of uint64
func (dgraph *DependencyGraph) sorted() []uint64 {
dgraph.mutex.RLock()
defer dgraph.mutex.RUnlock()
sorted := make([]uint64, 0, len(dgraph.graph))
for ptr := range dgraph.graph {
sorted = append(sorted, ptr)
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i] < sorted[j]
})
return sorted
}