-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstream.go
144 lines (110 loc) · 3 KB
/
stream.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
package ahocorasick
import (
"compress/gzip"
"encoding/binary"
"io"
)
// Encode writes a Trie to w in gzip compressed binary format.
func Encode(w io.Writer, trie *Trie) error {
enc := newEncoder(w)
return enc.encode(trie)
}
// Decode reads a Trie in gzip compressed binary format from r.
func Decode(r io.Reader) (*Trie, error) {
dec := newDecoder(r)
return dec.decode()
}
type encoder struct {
w io.Writer
}
func newEncoder(w io.Writer) *encoder {
return &encoder{
w,
}
}
func (enc *encoder) encode(trie *Trie) error {
w := gzip.NewWriter(enc.w)
defer w.Close()
if err := binary.Write(w, binary.LittleEndian, uint64(len(trie.dict))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(trie.trans))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(trie.failLink))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(trie.dictLink))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, uint64(len(trie.pattern))); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, trie.dict); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, trie.trans); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, trie.failLink); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, trie.dictLink); err != nil {
return err
}
if err := binary.Write(w, binary.LittleEndian, trie.pattern); err != nil {
return err
}
return nil
}
type decoder struct {
r io.Reader
}
func newDecoder(r io.Reader) *decoder {
return &decoder{
r,
}
}
func (dec *decoder) decode() (*Trie, error) {
r, err := gzip.NewReader(dec.r)
if err != nil {
return nil, err
}
var dictLen, transLen, dictLinkLen, failLinkLen, patternLen uint64
if err := binary.Read(r, binary.LittleEndian, &dictLen); err != nil {
return nil, err
}
if err := binary.Read(r, binary.LittleEndian, &transLen); err != nil {
return nil, err
}
if err := binary.Read(r, binary.LittleEndian, &dictLinkLen); err != nil {
return nil, err
}
if err := binary.Read(r, binary.LittleEndian, &failLinkLen); err != nil {
return nil, err
}
if err := binary.Read(r, binary.LittleEndian, &patternLen); err != nil {
return nil, err
}
dict := make([]int64, dictLen)
if err := binary.Read(r, binary.LittleEndian, dict); err != nil {
return nil, err
}
trans := make([][256]int64, transLen)
if err := binary.Read(r, binary.LittleEndian, trans); err != nil {
return nil, err
}
failLink := make([]int64, failLinkLen)
if err := binary.Read(r, binary.LittleEndian, failLink); err != nil {
return nil, err
}
dictLink := make([]int64, dictLinkLen)
if err := binary.Read(r, binary.LittleEndian, dictLink); err != nil {
return nil, err
}
pattern := make([]int64, patternLen)
if err := binary.Read(r, binary.LittleEndian, pattern); err != nil {
return nil, err
}
return &Trie{dict, trans, failLink, dictLink, pattern}, nil
}