-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathda.go
More file actions
182 lines (148 loc) · 3.2 KB
/
da.go
File metadata and controls
182 lines (148 loc) · 3.2 KB
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
package double_array
import (
"bytes"
"compress/gzip"
"io/ioutil"
"github.com/vmihailenco/msgpack"
)
type ItemID int32
func Deserialize(obj DoubleArray, id ItemID, inverse InverseID) string {
da := obj.(*doubleArray)
rs := make([]rune, 0)
index := int32(id)
for index != 0 {
next := da.nodes[index].Check
base := da.nodes[next].Base
if base < 0 {
base = -base
}
runeID := index - base
r := inverse[runeID]
rs = append(rs, r)
index = next
}
return string(rs)
}
const (
ItemNotFound = ItemID(-1)
)
type node struct {
Base int32
Check int32
}
type doubleArray struct {
nodes []node
dict RuneID
}
func (da *doubleArray) traverse(index int32, branch int32) int32 {
base := da.nodes[index].Base
if base < 0 {
base = -base
}
next := base + branch
if next >= int32(len(da.nodes)) || da.nodes[next].Check != index {
return -1
} else {
return next
}
}
func (da *doubleArray) searchMaximumLengthMatchFromSuffix(text []rune) (ItemID, int) {
index := int32(0)
currentMaximumMatch := ItemNotFound
lastPos := -1
for i := len(text) - 1; i >= 0; i-- {
branch, ok := da.dict[text[i]]
if !ok {
return currentMaximumMatch, lastPos
}
next := da.traverse(index, branch)
if next < 0 {
return currentMaximumMatch, lastPos
}
if da.nodes[next].Base < 0 {
currentMaximumMatch = ItemID(next)
lastPos = i
}
index = next
}
return currentMaximumMatch, lastPos
}
func (da *doubleArray) Lookup(key []rune) ItemID {
index := int32(0)
for i := len(key) - 1; i >= 0; i-- {
branch, ok := da.dict[key[i]]
if !ok {
return ItemNotFound
}
next := da.traverse(index, branch)
if next < 0 {
return ItemNotFound
}
index = next
}
if da.nodes[index].Base < 0 {
return ItemID(index)
}
return ItemNotFound
}
func (da *doubleArray) Scan(text []rune, callback func(i, j int, id ItemID)) {
for i := len(text) - 1; i > 0; i-- {
id, pos := da.searchMaximumLengthMatchFromSuffix(text[:i])
if id >= 0 {
callback(pos, i, id)
i = pos
}
}
}
func (da *doubleArray) Serialize() ([]byte, error) {
serializable := da2serializableDA(da)
data, err := msgpack.Marshal(serializable)
if err != nil {
return nil, err
}
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
writer.Write(data)
writer.Flush()
writer.Close()
return buf.Bytes(), nil
}
type DoubleArray interface {
Serialize() ([]byte, error)
Lookup(key []rune) ItemID
Scan(text []rune, callback func(i, j int, id ItemID))
}
func NewDoubleArrayFromBytes(data []byte) (DoubleArray, error) {
buf := bytes.NewBuffer(data)
reader, err := gzip.NewReader(buf)
if err != nil {
return nil, err
}
rawData, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
var tda serializableDA
err = msgpack.Unmarshal(rawData, &tda)
if err != nil {
return nil, err
}
da := serializableDA2DA(&tda)
return da, err
}
func NewDoubleArray(data []Item) (DoubleArray, error) {
ivs, dict, err := preprocess(data)
if err != nil {
return nil, err
}
doubleArray := &doubleArray{
nodes: make([]node, len(data)*2),
dict: dict,
}
tail, err := constructDA(doubleArray, ivs, len(dict)+2)
if err != nil {
return nil, err
}
doubleArray.nodes = doubleArray.nodes[:tail+1]
return doubleArray, nil
}