/
ac.go
280 lines (253 loc) · 5.74 KB
/
ac.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package ac
import (
"bufio"
"bytes"
"io"
"os"
"unsafe"
)
/*
*************************************************************\
* Constant *
\*************************************************************
*/
const (
BufSize = 64 * 4096
RootState = 1
FailState = -1
MaskBegin = 0xFF000000
MaskEnd = 0x00FF0000
MaskType = 0x0000C000
MaskLength = 0x00003FFF
TypeMovie = 1
TypeMusic = 2
TypeBoth = 3
DictSize = 106264
maxRune = 260
maxByte = 384
)
/**************************************************************\
* Global variable *
\**************************************************************/
// Cache
var (
Buf [maxByte]byte // max 383
Cache [maxRune]rune // max 257
BSP = 0 // Buf Stack Pointer
)
// states
var (
Base []int
Check []int
Fail []int
Info []int
)
// WriteRune put rune to global buffer (extreme ver without err check)
func WriteRune(r rune) {
switch i := uint32(r); {
case i <= 127:
Buf[BSP] = byte(r)
BSP++
case i <= 2047:
Buf[BSP] = 0xC0 | byte(r>>6)
BSP++
Buf[BSP] = 0x80 | byte(r)&0x3F
BSP++
case i <= 65535:
Buf[BSP] = 0xE0 | byte(r>>12)
BSP++
Buf[BSP] = 0x80 | byte(r>>6)&0x3F
BSP++
Buf[BSP] = 0x80 | byte(r)&0x3F
BSP++
default:
Buf[BSP] = 0xF0 | byte(r>>18)
BSP++
Buf[BSP] = 0x80 | byte(r>>12)&0x3F
BSP++
Buf[BSP] = 0x80 | byte(r>>6)&0x3F
BSP++
Buf[BSP] = 0x80 | byte(r)&0x3F
BSP++
}
}
// WriteByType put write target string to buffer via match type
func WriteByType(match int) {
Buf[BSP] = 45
BSP++
Buf[BSP] = 42
BSP++
switch (match & MaskType) >> 14 {
case TypeMovie:
WriteRune(30005)
WriteRune(24433)
case TypeMusic:
WriteRune(38899)
WriteRune(20048)
case TypeBoth:
WriteRune(38899)
WriteRune(20048)
WriteRune(38)
WriteRune(30005)
WriteRune(24433)
}
Buf[BSP] = 42
BSP++
Buf[BSP] = 45
BSP++
}
/**************************************************************\
* Line Processor *
\**************************************************************/
// HandleLine take one line , process and write it
func HandleLine(input []byte) (output []byte) {
var Matches [5]int
var nMatch, match, rCursor, wCursor, info, mLength, mBegin int
var overlap bool
state := RootState
// stage 1 : find all match
for _, c := range *(*string)(unsafe.Pointer(&input)) {
transfer:
Cache[rCursor] = c
// state transfer
if t := state + int(c) + RootState; t < len(Check) { // 219169484
if state == Check[t] { // 53870341
state = Base[t]
goto match
}
if state == RootState { // 117354923
state = RootState
goto match
}
}
if state == RootState {
goto done
}
// reach fail state
state = Fail[state]
goto transfer
match:
if info = Info[state]; info != 0 {
// there's a match , check it out
mLength = info & MaskLength
match = (rCursor-mLength+1)<<24 | (rCursor << 16) | info
// lastMatch.End < match.Begin : add new match
if nMatch == 0 || ((Matches[nMatch-1]&MaskEnd)>>16) < (match&MaskBegin)>>24 {
Matches[nMatch] = match
match = 0
nMatch++
goto done
}
// newMatch.Begin <= lastMatch.Begin : abandon old match
overlap = false
for ; nMatch > 0 && ((match&MaskBegin)>>24) <= ((Matches[nMatch-1])&MaskBegin)>>24; nMatch-- {
overlap = true
}
if overlap {
Matches[nMatch] = match
match = 0
nMatch++
goto done
}
// miss: omit new match
}
done:
rCursor++
}
// stage 2 : replace all match
if nMatch == 0 {
// no match, write line back
return input
} else {
// reset buf
BSP = 0
matchIndex := 0
match = Matches[matchIndex]
mBegin = (match & MaskBegin) >> 24
mLength = match & MaskLength
for wCursor = 0; wCursor < rCursor; {
if wCursor < mBegin {
WriteRune(Cache[wCursor])
wCursor++
continue
}
if wCursor == mBegin {
WriteByType(match)
wCursor += match & MaskLength
if matchIndex+1 < nMatch {
matchIndex++
match = Matches[matchIndex]
mBegin = (match & MaskBegin) >> 24
mLength = match & MaskLength
}
continue
} else {
WriteRune(Cache[wCursor])
wCursor++
}
}
return Buf[:BSP]
}
}
/**************************************************************\
* Driver *
\**************************************************************/
// Run program with given parameter
func Run(inputPath, outputPath, dictPath string) {
// dict
ac := FromFile(dictPath)
Base = ac.Base
Check = ac.Check
Fail = ac.Failure
Info = ac.Output
// input
inputFile, err := os.Open(inputPath)
if err != nil {
panic(err)
}
defer inputFile.Close()
reader := bufio.NewReaderSize(inputFile, BufSize)
// output
outputFile, err := os.Create(outputPath)
if err != nil {
panic(err)
}
defer outputFile.Close()
writer := bufio.NewWriterSize(outputFile, BufSize)
// process loop
var line []byte
for err = nil; err != io.EOF; line, err = reader.ReadSlice('\n') {
writer.Write(HandleLine(line))
}
writer.Flush()
}
/*
*************************************************************\
* Load From File *
\*************************************************************
*/
func FromFile(filename string) *AC {
dict := make(map[string]int, 1115)
f, err := os.OpenFile(filename, os.O_RDONLY, 0660)
if err != nil {
panic(err)
}
r := bufio.NewReader(f)
for {
l, err := r.ReadBytes('\n')
if err != nil {
break
}
piece := bytes.Split(bytes.TrimSpace(l), []byte("\t"))
key := string(piece[0])
switch string(piece[1]) {
case StrMovie:
dict[key] = TypeMovie
case StrMusic:
dict[key] = TypeMusic
case StrBoth:
dict[key] = TypeBoth
}
}
return FromDict(dict)
}