/
node.go
175 lines (146 loc) · 3.63 KB
/
node.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fst
import (
"context"
"errors"
"fmt"
)
type Node interface {
IsCompiled() bool
}
var _ Node = &CompiledNode{}
type CompiledNode struct {
node int64
}
func NewCompiledNode() *CompiledNode {
return &CompiledNode{}
}
func (*CompiledNode) IsCompiled() bool {
return true
}
func (r *CompiledNode) Code() int64 {
return r.node
}
var _ Node = &UnCompiledNode{}
// UnCompiledNode
// TODO:
// instead of recording isFinal/output on the
// node, maybe we should use -1 arc to mean "end" (like
// we do when reading the FST). Would simplify much
// code here...
type UnCompiledNode struct {
Arcs []*PendingArc
Output Output
IsFinal bool
InputCount int
Depth int // This node's depth, starting from the automaton root.
builder *Builder
}
// PendingArc
// Expert: holds a pending (seen but not yet serialized) arc.
type PendingArc struct {
Label int
Target Node
IsFinal bool
Output Output
NextFinalOutput Output
}
func (u *UnCompiledNode) NumArcs() int {
return len(u.Arcs)
}
func NewUnCompiledNode(builder *Builder, depth int) *UnCompiledNode {
return &UnCompiledNode{
builder: builder,
Arcs: make([]*PendingArc, 0),
Output: builder.noOutput,
Depth: depth,
}
}
func (u *UnCompiledNode) IsCompiled() bool {
return false
}
func (u *UnCompiledNode) Code() int64 {
return -1
}
func (u *UnCompiledNode) Clear() {
u.Arcs = u.Arcs[:0]
u.IsFinal = false
u.Output = u.builder.noOutput
u.InputCount = 0
// We don't clear the depth here because it never changes
// for nodes on the frontier (even when reused).
}
func (u *UnCompiledNode) GetLastOutput() Output {
return u.lastArc().Output
}
func (u *UnCompiledNode) lastArc() *PendingArc {
return u.Arcs[len(u.Arcs)-1]
}
func (u *UnCompiledNode) AddArc(label int, target Node) {
u.Arcs = append(u.Arcs, &PendingArc{
Label: label,
Target: target,
Output: u.builder.noOutput,
NextFinalOutput: u.builder.noOutput,
IsFinal: false,
})
}
// DeleteLast 移除目标arc
func (u *UnCompiledNode) DeleteLast(ctx context.Context, label int, target Node) error {
if len(u.Arcs) <= 0 {
return errors.New("arcs size is 0")
}
lastArc := u.lastArc()
if label != lastArc.Label {
return errors.New("label not match")
}
if target != lastArc.Target {
return errors.New("target not match")
}
u.Arcs = u.Arcs[:len(u.Arcs)-1]
return nil
}
// SetLastOutput 设置最后arc的output对象
func (u *UnCompiledNode) SetLastOutput(ctx context.Context, label int, newOutput Output) error {
if len(u.Arcs) <= 0 {
return errors.New("arcs size is 0")
}
lastArc := u.lastArc()
if lastArc.Label != label {
return errors.New("label not match")
}
lastArc.Output = newOutput
return nil
}
// ReplaceLast 替换最后的arc的内部数据
func (u *UnCompiledNode) ReplaceLast(labelToMatch int, target Node, nextFinalOutput Output, isFinal bool) error {
if len(u.Arcs) <= 0 {
return fmt.Errorf("arcs size is 0")
}
lastArc := u.lastArc()
if lastArc.Label != labelToMatch {
return fmt.Errorf("arc.label=%d vs %d", lastArc.Label, labelToMatch)
}
lastArc.Target = target
lastArc.NextFinalOutput = nextFinalOutput
lastArc.IsFinal = isFinal
return nil
}
// PrependOutput pushes an output prefix forward onto all arcs
// 所有的边都增加一个output前缀
func (u *UnCompiledNode) PrependOutput(outputPrefix Output) error {
for i := range u.Arcs {
output, err := outputPrefix.Add(u.Arcs[i].Output)
if err != nil {
return err
}
u.Arcs[i].Output = output
}
if u.IsFinal {
output, err := outputPrefix.Add(u.Output)
if err != nil {
return err
}
u.Output = output
}
return nil
}