-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmac_engine_common.go
80 lines (68 loc) · 1.22 KB
/
smac_engine_common.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
// Copyright Piero de Salvia.
// All Rights Reserved
package smac
type wordAccepts struct {
Word string
Accepts int
}
type wordHit struct {
word string
accepts int
next *wordHit
}
type sOLILI struct {
start *wordHit
end *wordHit
}
func (list *sOLILI) insert(word string, accepts int) {
hit := &wordHit{
word: word,
accepts: accepts,
}
if list.start == nil {
list.start = hit
list.end = hit
return
}
if accepts == 0 {
list.end.next = hit
list.end = hit
return
}
if hit.accepts > list.start.accepts {
hit.next = list.start
list.start = hit
return
}
cursor := list.start
for cursor.next != nil {
if hit.accepts > cursor.next.accepts {
break
}
cursor = cursor.next
}
hit.next = cursor.next
cursor.next = hit
}
func (list *sOLILI) flush() []string {
slice := []string{}
if list.start != nil {
cursor := list.start
for cursor != nil {
slice = append(slice, cursor.word)
cursor = cursor.next
}
}
return slice
}
func (list *sOLILI) flushL(limit int) []string {
slice := []string{}
if list.start != nil {
cursor := list.start
for i := 0; cursor != nil && i < limit; i++ {
slice = append(slice, cursor.word)
cursor = cursor.next
}
}
return slice
}