-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsearch.go
165 lines (135 loc) Β· 3.53 KB
/
search.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
package fzf
import (
"sort"
"strings"
"sync"
"unicode/utf8"
)
var (
defaultSearchOption = searchOption{
caseSensitive: false,
}
)
type searchOption struct {
caseSensitive bool
}
// Items represents a list of items to be searched.
type Items interface {
// ItemString returns the string of the i-th item.
ItemString(i int) string
// Len returns the length of items.
Len() int
}
// Match represents a matched string.
type Match struct {
Str string
Index int
MatchedIndexes []int
}
// Matches is a slice of Match.
type Matches []Match
func (m Matches) sort() {
sort.Slice(m, func(i, j int) bool {
mi, mj := m[i].MatchedIndexes, m[j].MatchedIndexes
li, lj := len(mi), len(mj)
if li != lj {
return li < lj
}
for k := 0; k < li; k++ {
if mi[k] != mj[k] {
return mi[k] < mj[k]
}
}
return m[i].Index < m[j].Index
})
}
// SearchOption represents a option for a search.
type SearchOption func(o *searchOption)
func WithSearchCaseSensitive(c bool) SearchOption {
return func(o *searchOption) {
o.caseSensitive = c
}
}
// Search performs a fuzzy search for items.
func Search(items Items, search string, opts ...SearchOption) Matches {
// Apply the options.
o := defaultSearchOption
for _, opt := range opts {
opt(&o)
}
// If case-insensitive, convert the search string to lowercase.
if !o.caseSensitive {
search = strings.ToLower(search)
}
// Create a slice to store the search results.
result := make(Matches, 0, items.Len())
resultMutex := sync.Mutex{}
wg := sync.WaitGroup{}
// Set the number of workers and chunk size.
numWorkers := 8
chunkSize := (items.Len() + numWorkers - 1) / numWorkers
chunks := make(chan int, numWorkers)
// Launch the workers.
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Perform the search for each chunk.
for start := range chunks {
end := start + chunkSize
if end > items.Len() {
end = items.Len()
}
// Create a slice to store local matches for each chunk.
localMatches := make(Matches, 0)
// Perform the search for each item.
for index := start; index < end; index++ {
m, ok := fuzzySearch(items.ItemString(index), search, o)
if ok {
m.Index = index
localMatches = append(localMatches, m)
}
}
// Add the local matches to the result (while performing mutual exclusion).
resultMutex.Lock()
result = append(result, localMatches...)
resultMutex.Unlock()
}
}()
}
// Assign chunks to the workers.
for i := 0; i < items.Len(); i += chunkSize {
chunks <- i
}
//Once all chunks are assigned, close the channel.
close(chunks)
// Wait for all workers to complete their processing.
wg.Wait()
// Sort the search results and return them.
result.sort()
return result
}
func fuzzySearch(str, search string, option searchOption) (Match, bool) {
item := str
// If case-insensitive, convert the item to lowercase.
if !option.caseSensitive {
item = strings.ToLower(item)
}
// Create a slice to store the matched indexes.
matchedIndexes := make([]int, 0, utf8.RuneCountInString(search))
j := 0
// Check for matching between the item's characters and the search string.
searchRunes := []rune(search)
for i, r := range []rune(item) {
if j < len(searchRunes) && r == searchRunes[j] {
matchedIndexes = append(matchedIndexes, i)
j++
}
}
// Returns Match if all characters in the search string match.
if j == len(searchRunes) {
return Match{Str: str, MatchedIndexes: matchedIndexes}, true
} else {
return Match{}, false
}
}