-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreftree.go
executable file
·225 lines (194 loc) · 3.32 KB
/
preftree.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
package linguo
type List struct {
begin, end *ListRecBase
}
func NewList() *List {
return &List{
begin: nil,
end: nil,
}
}
/*
func (this *List) push(c rune, wordEnd bool) interface {} {
n := this.find(c)
if n == nil {
if !wordEnd {
n = NewListRecBase(c)
} else {
n = NewListRecBase(c)
}
if this.end != nil {
this.end.next = n
} else {
this.begin = n
}
this.end = n
} else if wordEnd {
tmp := this.begin
var prev *ListRecBase = nil
for tmp != n {
prev = tmp
tmp = tmp.next
}
if n == this.end {
this.end = nil
}
ntmp := n.next
l := n.nextList
n = NewListRecData(c)
n.next = ntmp
n.nextList = l
if prev != nil {
prev.next = n
} else {
this.begin = n
}
if this.end == nil {
this.end = n
}
}
if !wordEnd {
if n.nextList == nil {
n.nextList = NewList()
}
return n.nextList
} else {
return n
}
}
*/
func (this *List) find(c rune) *ListRecBase {
if this.begin == nil {
return nil
}
tmp := this.begin
for tmp != nil && tmp.symb != c {
if tmp.next != nil {
if tmp.next.symb == c {
return tmp.next
}
tmp = tmp.next.next
} else {
tmp = nil
break
}
}
return If(tmp != nil, tmp, nil).(*ListRecBase)
}
type ListRecBase struct {
symb rune
next *ListRecBase
nextList *List
}
func NewListRecBase(s rune) *ListRecBase {
return &ListRecBase{
symb: s,
next: nil,
nextList: nil,
}
}
/*
func (this *List) find(c rune) *ListRecBase {
if this.begin == nil {
return nil
}
tmp := this.begin
for tmp.symb != c {
if tmp.next != nil {
if tmp.next.symb == c {
return tmp.next
}
tmp = tmp.next.next
} else {
tmp = nil
break
}
}
return If(tmp != nil, tmp, nil).(*ListRecBase)
}
*/
type ListRec struct {
*ListRecBase
}
func NewListRec(s rune) *ListRec {
return &ListRec{
ListRecBase: NewListRecBase(s),
}
}
type ListRecEnd struct {
*ListRecBase
}
func NewListRecEnd(s rune) *ListRec {
return &ListRec{
ListRecBase: NewListRecBase(s),
}
}
type ListRecData struct {
*ListRecBase
value []rune
}
func NewListRecData(s rune) *ListRec {
return &ListRec{
ListRecBase: NewListRecBase(s),
}
}
func (this *ListRecData) setValue(value []rune) {
this.value = value
}
func (this *ListRecData) getValue() []rune {
return this.value
}
func (this *ListRecData) delete() {
this.value = nil
}
type PrefTree struct {
root *List
DELIM_LEN int
}
func NewPrefTree() *PrefTree {
return &PrefTree{
root: NewList(),
DELIM_LEN: len(" "),
}
}
/*
func (this *PrefTree) addWord(word string, lr *ListRecBase, sIn string) {
if word == "" {
return
}
var l *List = nil
if lr == nil {
l = this.root
for w := 0; w < len(word); w++ {
l = l.push(rune(word[w]),w < len(word)).(*List)
}
} else {
//l = lr.(*List)
}
var ldata *ListRecData = l.(*ListRecData)
data := ldata.getValue()
var newStr []rune
s := 0
if data != nil {
s = len(data) + this.DELIM_LEN + len(sIn) + 1
newStr = make([]rune, 0)
newStr = append(newStr, data...)
newStr = append(newStr, " "[0])
} else {
s = len(sIn) + 1
newStr = make([]rune,1)
newStr[0] = 0
}
newStr = append(newStr, sIn)
ldata.setValue(newStr)
}
*/
/*
func (this *PrefTree) findWord(word string) *ListRecBase {
n := this.root
var lr *ListRecBase = n.find(word[0])
for word != "" && n != nil && lr != nil {
n = lr.next
}
}
*/