-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnoconflict.go
255 lines (232 loc) · 5.48 KB
/
noconflict.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package godat
import (
"fmt"
"sort"
"unicode/utf8"
)
// 无冲突build双数组
type Words []rune
//
// 去重
func (gd *GoDat) toWords() []Words {
var (
cnt int
m = make(map[string]bool)
)
ws := make([]Words, len(gd.pats))
for _, pat := range gd.pats {
if _, ok := m[pat]; ok {
continue
}
m[pat] = true
ru := make(Words, 0)
for len(pat) > 0 {
r, size := utf8.DecodeRuneInString(pat)
pat = pat[size:]
ru = append(ru, r)
}
ws[cnt] = ru
cnt++
}
return ws[0:cnt]
}
func (gd *GoDat) ncBuild(ws []Words) error {
type buildStack struct {
endRow int
column int
s int
}
var (
s = 0
t = 0
cursor = 0
codes []int
stacks = make([]buildStack, 0)
stack buildStack
err error
)
// 首字符
codes = gd.prefixRune(ws, -1, 0)
stacks = append(stacks, buildStack{len(codes), 0, 0})
if err = gd.insertCodes(0, codes); err != nil {
return err
}
for cursor < len(ws) {
w := ws[cursor]
stack = stacks[len(stacks)-1]
if stack.endRow == 1 {
stacks = stacks[0 : len(stacks)-1]
} else {
stacks[len(stacks)-1].endRow--
}
//fmt.Printf("cursor: %d string to be inserted: %v stack: %v stacks: %v base[stack.s]: %d\n", cursor, w, stack, stacks, gd.base[stack.s])
w = w[stack.column:]
if gd.base[stack.s] < 0 {
s = -1*gd.base[stack.s] + gd.auxiliary[w[0]]
} else {
//println("cursor:", cursor, "w length:", len(w), stack.column, len(ws[cursor]), string(ws[cursor]))
s = gd.base[stack.s] + gd.auxiliary[w[0]]
}
if s >= len(gd.base) {
if err = gd.extend(0); err != nil {
return err
}
}
// s是当前字符的位置, t是下一个字符的位置
for i, r := range w {
// codes 不包括r
codes = gd.prefixRune(ws[cursor:], i+stack.column, r)
//fmt.Printf(" i=%d w=%v s=%d t=%d r=%v(%d) codes=%v\n", i, w, s, t, r, gd.auxiliary[r], codes)
if i == len(w)-1 {
// 所有模式中,无与当前相同的前缀
// 最后一个字符
if len(codes) == 0 {
gd.base[s] = DAT_END_POS
} else {
gd.base[s] = -gd.base[s]
stacks = append(stacks, buildStack{len(codes), stack.column + i + 1, s})
if err = gd.insertCodes(s, codes); err != nil {
return err
}
}
//fmt.Printf(" i=%d s=%d t=%d r=%v codes=%v stack.column=%d\n", i, s, t, r, codes, stack.column)
//fmt.Println(" ", gd.base, "\n ", gd.check)
//fmt.Println(" stacks:", len(stacks), stacks)
break
} else {
allcodes := codes
nextChar := gd.auxiliary[w[i+1]]
if len(codes) == 0 {
allcodes = []int{nextChar}
} else {
if i != len(w)-1 {
allcodes = append(codes, nextChar) //gd.auxiliary[r])
}
stacks = append(stacks, buildStack{len(codes), stack.column + i + 1, s})
}
if err = gd.insertCodes(s, allcodes); err != nil {
return err
}
t = gd.base[s] + nextChar //gd.auxiliary[r]
}
s = t
}
cursor++
if cursor%1000 == 0 {
fmt.Printf("build %d patterns\n", cursor)
}
}
return nil
}
// 比较两个slice中的每一个元素是否都相同
// 长度相同
func equalSlic(src, dst Words) bool {
for i, _ := range src {
if src[i] != dst[i] {
return false
}
}
return true
}
// ws已经排过序,且ws的第一条记录应该是符合ws[startPos]==r的
// ws中有多少条记录ws[startPos]==r
// endRow: 记录条数
// codes: 符合条件的Words的下一个rune对应的code
// 特殊情况:
// startPos = -1:
// 返回值endRow=1,codes=[],只有ws[0]符合条件,且ws[0]以r结尾
func (gd *GoDat) prefixRune(ws []Words, startPos int, r rune) []int {
var (
i int
w Words
nextChar rune
rm = make(map[rune]bool)
codes = make([]int, 0)
)
if len(ws) <= 1 {
return codes
}
if startPos != -1 && startPos <= len(ws[0])-2 {
nextChar = ws[0][startPos+1]
}
//fmt.Printf(" +++prefixRune: ws=%v startPos=%v nextChar=%v r=%v\n", ws, startPos, nextChar, r)
prefix := ws[0]
if startPos != -1 {
i = 1
}
for ; i < len(ws); i++ {
w = ws[i]
if len(w) < startPos+1 || (startPos >= 0 && equalSlic(w[0:startPos+1], prefix[0:startPos+1]) == false) {
break
}
if len(w) > startPos+1 {
r := w[startPos+1]
if r == nextChar {
continue
}
if _, ok := rm[r]; !ok {
rm[r] = true
codes = append(codes, gd.auxiliary[w[startPos+1]])
}
}
}
return codes
}
// codes已经排好序
// 为code中的每一个值,选择一个位置
// 确定s位置的base值
// 类似于findNewBase
func (gd *GoDat) insertCodes(s int, codes []int) error {
var (
start = gd.check[0]
pos int
base int
found bool
err error
)
if len(codes) == 0 {
return nil
}
sort.Sort(sort.IntSlice(codes))
if gd.idles == 0 {
if err = gd.extend(0); err != nil {
return err
}
}
for pos = start; ; pos = -1 * gd.check[pos] {
if -1*gd.check[pos] == start {
if err = gd.extend(0); err != nil {
return err
}
}
base = pos - codes[0]
if (s == 0 && base < 0) || (s > 0 && base <= 0) {
continue
}
found = true
for i := 1; i < len(codes); i++ {
if base+codes[i] >= len(gd.check) {
if err = gd.extend(base + codes[i]); err != nil {
return err
}
}
if gd.check[base+codes[i]] >= 0 {
found = false
break
}
}
if found {
break
}
}
// found
//fmt.Println(" ----base:", base, ", s:", s, ", codes:", codes, len(gd.base))
gd.base[s] = base
for _, c := range codes {
//fmt.Println(" --------delLink:", base+c, gd.base[base+c], gd.check[base+c])
gd.delLink(base + c)
gd.check[base+c] = s
gd.base[base+c] = 0
}
return nil
}