-
Notifications
You must be signed in to change notification settings - Fork 8
/
tree.go
121 lines (109 loc) · 2.9 KB
/
tree.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package pathutil
import (
"github.com/DeNA/unity-meta-check/util/typedpath"
"sort"
)
type PathTree[T interface{}] map[typedpath.BaseName]*PathTreeEntry[T]
type PathTreeEntry[T interface{}] struct {
Value *T
Subtree PathTree[T]
}
type PathPair[T interface{}] struct {
Path typedpath.SlashPath
Value T
}
type KeyValuePair[T interface{}] struct {
BaseName typedpath.BaseName
Entry *PathTreeEntry[T]
}
func NewPathTree(paths ...typedpath.SlashPath) PathTree[struct{}] {
pairs := make([]PathPair[struct{}], len(paths))
for i, p := range paths {
pairs[i] = struct {
Path typedpath.SlashPath
Value struct{}
}{p, struct{}{}}
}
return NewPathTreeWithValues(pairs...)
}
func NewPathTreeWithValues[T interface{}](pairs ...PathPair[T]) PathTree[T] {
root := make(PathTree[T])
for _, pair := range pairs {
tree := root
elements := SplitPathElements(pair.Path)
if len(elements) == 0 {
continue
}
var ok bool
var treeNode *PathTreeEntry[T]
for _, element := range elements {
treeNode, ok = tree[element]
if !ok {
treeNode = &PathTreeEntry[T]{Value: nil, Subtree: map[typedpath.BaseName]*PathTreeEntry[T]{}}
tree[element] = treeNode
}
tree = treeNode.Subtree
}
// NOTE: Use copied pointer instead of pointer for the loop variable.
val := pair.Value
treeNode.Value = &val
}
return root
}
// Member returns whether the pathElements is a member of the tree.
// Notably, returns false if the pathElements point at the tree.
func (t PathTree[T]) Member(pathElements []typedpath.BaseName) bool {
if len(pathElements) == 0 {
return false
}
child, ok := t[pathElements[0]]
if !ok {
return false
}
return child.member(pathElements[1:])
}
func (t PathTree[T]) Postorder(f func(typedpath.SlashPath, PathTreeEntry[T]) error) error {
for _, kv := range sortDict(t) {
if err := kv.Entry.postorder(".", kv.BaseName, f); err != nil {
return err
}
}
return nil
}
func (e PathTreeEntry[T]) member(pathElements []typedpath.BaseName) bool {
if len(pathElements) == 0 {
return false
}
if len(e.Subtree) == 0 {
return true
}
child, ok := e.Subtree[pathElements[0]]
if !ok {
return false
}
return child.member(pathElements[1:])
}
func (e PathTreeEntry[T]) postorder(relPath typedpath.SlashPath, baseName typedpath.BaseName, f func(typedpath.SlashPath, PathTreeEntry[T]) error) error {
var path typedpath.SlashPath
if relPath == "." {
path = typedpath.SlashPath(baseName)
} else {
path = relPath.JoinBaseName(baseName)
}
for _, kv := range sortDict(e.Subtree) {
if err := kv.Entry.postorder(path, kv.BaseName, f); err != nil {
return err
}
}
return f(path, e)
}
func sortDict[T interface{}](tree PathTree[T]) []KeyValuePair[T] {
kvs := make([]KeyValuePair[T], 0, len(tree))
for baseName, entry := range tree {
kvs = append(kvs, KeyValuePair[T]{baseName, entry})
}
sort.Slice(kvs, func(i, j int) bool {
return kvs[i].BaseName < kvs[j].BaseName
})
return kvs
}