-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
67 lines (53 loc) · 993 Bytes
/
find.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
package decide
import (
"fmt"
)
type stack struct {
s []string
}
func (t *stack) Push(v string) {
t.s = append(t.s, v)
}
func (t *stack) Pop() {
l := len(t.s)
if l != 0 {
t.s = t.s[:l-1]
}
}
func (t *stack) Len() int {
return len(t.s)
}
func (t stack) String() string {
return fmt.Sprintf("%v", t.s)
}
func (t Tree) Find(payload string) ([]string, bool) {
path := &stack{make([]string, 0, 20)}
found := t.root.Find(0, path, payload)
return path.s, found
}
func (t Node) Find(depth int, path *stack, object string) bool {
path.Push(t.Expression.String())
if t.Contains(object) {
return true
}
if len(t.True) > 0 {
for _, trueNode := range t.True {
if trueNode.Find(depth+1, path, object) {
return true
}
}
}
path.Pop()
if t.False != nil && t.False.Find(depth+1, path, object) {
return true
}
return false
}
func (t Node) Contains(object string) bool {
for _, p := range t.Payload {
if p == object {
return true
}
}
return false
}