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 fzf
import (
"fmt"
"sort"
)
type Offset [2]int32
type Item struct {
text *string
origText *string
offsets []Offset
rank Rank
transformed *Transformed
}
type Rank struct {
matchlen uint16
strlen uint16
index uint32
}
func (i *Item) Rank() Rank {
if i.rank.matchlen > 0 || i.rank.strlen > 0 {
return i.rank
}
sort.Sort(ByOrder(i.offsets))
matchlen := 0
prevEnd := 0
for _, offset := range i.offsets {
begin := int(offset[0])
end := int(offset[1])
if prevEnd > begin {
begin = prevEnd
}
if end > prevEnd {
prevEnd = end
}
if end > begin {
matchlen += end - begin
}
}
i.rank = Rank{uint16(matchlen), uint16(len(*i.text)), i.rank.index}
return i.rank
}
func (i *Item) Print() {
if i.origText != nil {
fmt.Println(*i.origText)
} else {
fmt.Println(*i.text)
}
}
type ByOrder []Offset
func (a ByOrder) Len() int {
return len(a)
}
func (a ByOrder) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByOrder) Less(i, j int) bool {
ioff := a[i]
joff := a[j]
return (ioff[0] < joff[0]) || (ioff[0] == joff[0]) && (ioff[1] <= joff[1])
}
type ByRelevance []*Item
func (a ByRelevance) Len() int {
return len(a)
}
func (a ByRelevance) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a ByRelevance) Less(i, j int) bool {
irank := a[i].Rank()
jrank := a[j].Rank()
return compareRanks(irank, jrank)
}
func compareRanks(irank Rank, jrank Rank) bool {
if irank.matchlen < jrank.matchlen {
return true
} else if irank.matchlen > jrank.matchlen {
return false
}
if irank.strlen < jrank.strlen {
return true
} else if irank.strlen > jrank.strlen {
return false
}
if irank.index <= jrank.index {
return true
}
return false
}
func SortMerge(partialResults [][]*Item) []*Item {
if len(partialResults) == 1 {
return partialResults[0]
}
merged := []*Item{}
for len(partialResults) > 0 {
minRank := Rank{0, 0, 0}
minIdx := -1
for idx, partialResult := range partialResults {
if len(partialResult) > 0 {
rank := partialResult[0].Rank()
if minIdx < 0 || compareRanks(rank, minRank) {
minRank = rank
minIdx = idx
}
}
}
if minIdx >= 0 {
merged = append(merged, partialResults[minIdx][0])
partialResults[minIdx] = partialResults[minIdx][1:]
}
nonEmptyPartialResults := make([][]*Item, 0, len(partialResults))
for _, partialResult := range partialResults {
if len(partialResult) > 0 {
nonEmptyPartialResults = append(nonEmptyPartialResults, partialResult)
}
}
partialResults = nonEmptyPartialResults
}
return merged
}
|