summaryrefslogtreecommitdiff
path: root/src/item.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-11 23:49:12 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-11 23:49:12 +0900
commit9dbf6b02d24b52ae43e36905bbb1e83087e1dfe9 (patch)
tree37e627f5bb4a3037ea769aad1033593baec4eb00 /src/item.go
parent1db68a3976cfb10ed7d6ab88d7b468bb1b93ee34 (diff)
downloadfzf-9dbf6b02d24b52ae43e36905bbb1e83087e1dfe9.tar.gz
Fix race conditions
- Wait for completions of goroutines when cancelling a search - Remove shared access to rank field of Item
Diffstat (limited to 'src/item.go')
-rw-r--r--src/item.go22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/item.go b/src/item.go
index 9f90b8d4..41aa34bd 100644
--- a/src/item.go
+++ b/src/item.go
@@ -1,9 +1,6 @@
package fzf
-import (
- "fmt"
- "sort"
-)
+import "fmt"
type Offset [2]int32
@@ -11,6 +8,7 @@ type Item struct {
text *string
origText *string
transformed *Transformed
+ index uint32
offsets []Offset
rank Rank
}
@@ -21,11 +19,10 @@ type Rank struct {
index uint32
}
-func (i *Item) Rank() Rank {
- if i.rank.matchlen > 0 || i.rank.strlen > 0 {
+func (i *Item) Rank(cache bool) Rank {
+ if cache && (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 {
@@ -41,8 +38,11 @@ func (i *Item) Rank() Rank {
matchlen += end - begin
}
}
- i.rank = Rank{uint16(matchlen), uint16(len(*i.text)), i.rank.index}
- return i.rank
+ rank := Rank{uint16(matchlen), uint16(len(*i.text)), i.index}
+ if cache {
+ i.rank = rank
+ }
+ return rank
}
func (i *Item) Print() {
@@ -80,8 +80,8 @@ func (a ByRelevance) Swap(i, j int) {
}
func (a ByRelevance) Less(i, j int) bool {
- irank := a[i].Rank()
- jrank := a[j].Rank()
+ irank := a[i].Rank(true)
+ jrank := a[j].Rank(true)
return compareRanks(irank, jrank)
}