Simplify Item structure
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This commit compensates for the performance overhead from the extended tiebreak option.
authored by Junegunn Choi · committed by Junegunn Choi · parent 1d2d32c · Browse files · 8 files·+43 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+43 added−58 removed
- other:+43 added−58 removed
Provenance
- Commit
8d3a302- Tree
a5b9536- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1d2d32c
8 files changed+43 added−58 removed
modified:src/chunklist_test.go+2 added-2 removed
5 unmodified lines6 )7 8 func TestChunkList(t *testing.T) {9 // FIXME global10- sortCriteria = []criterion{byMatchLen, byLength, byIndex}10+ sortCriteria = []criterion{byMatchLen, byLength}11 12 cl := NewChunkList(func(s []byte, i int) *Item {13 return &Item{text: []rune(string(s)), rank: buildEmptyRank(int32(i * 2))}14 })23 unmodified lines38 chunk1 := snapshot[0]39 if len(*chunk1) != 2 {40 t.Error("Snapshot should contain only two items")41 }42- last := func(arr []int32) int32 {42+ last := func(arr [5]int32) int32 {43 return arr[len(arr)-1]44 }45 if string((*chunk1)[0].text) != "hello" || last((*chunk1)[0].rank) != 0 ||46 string((*chunk1)[1].text) != "world" || last((*chunk1)[1].rank) != 2 {34 unmodified linesmodified:src/core.go-2 removed
101 unmodified lines102 }103 runes, colors := ansiProcessor(data)104 return &Item{105 text: runes,106- index: int32(index),106 colors: colors,107 rank: buildEmptyRank(int32(index))}108 })109 } else {8 unmodified lines118 }119 item := Item{120 text: joinTokens(trans),121 origText: &runes,123- index: int32(index),122 colors: nil,123 rank: buildEmptyRank(int32(index))}124 125 trimmed, colors := ansiProcessorRunes(item.text)166 unmodified linesmodified:src/item_test.go+16 added-16 removed
21 unmodified lines22 }23 }24 25 func TestRankComparison(t *testing.T) {26- if compareRanks([]int32{3, 0, 5}, []int32{2, 0, 7}, false) ||27- !compareRanks([]int32{3, 0, 5}, []int32{3, 0, 6}, false) ||28- !compareRanks([]int32{1, 2, 3}, []int32{1, 3, 2}, false) ||29- !compareRanks([]int32{0, 0, 0}, []int32{0, 0, 0}, false) {26+ if compareRanks([5]int32{3, 0, 0, 0, 5}, [5]int32{2, 0, 0, 0, 7}, false) ||27+ !compareRanks([5]int32{3, 0, 0, 0, 5}, [5]int32{3, 0, 0, 0, 6}, false) ||28+ !compareRanks([5]int32{1, 2, 0, 0, 3}, [5]int32{1, 3, 0, 0, 2}, false) ||29+ !compareRanks([5]int32{0, 0, 0, 0, 0}, [5]int32{0, 0, 0, 0, 0}, false) {30 t.Error("Invalid order")31 }32 33- if compareRanks([]int32{3, 0, 5}, []int32{2, 0, 7}, true) ||34- !compareRanks([]int32{3, 0, 5}, []int32{3, 0, 6}, false) ||35- !compareRanks([]int32{1, 2, 3}, []int32{1, 3, 2}, true) ||36- !compareRanks([]int32{0, 0, 0}, []int32{0, 0, 0}, false) {33+ if compareRanks([5]int32{3, 0, 0, 0, 5}, [5]int32{2, 0, 0, 0, 7}, true) ||34+ !compareRanks([5]int32{3, 0, 0, 0, 5}, [5]int32{3, 0, 0, 0, 6}, false) ||35+ !compareRanks([5]int32{1, 2, 0, 0, 3}, [5]int32{1, 3, 0, 0, 2}, true) ||36+ !compareRanks([5]int32{0, 0, 0, 0, 0}, [5]int32{0, 0, 0, 0, 0}, false) {37 t.Error("Invalid order (tac)")38 }39 }40 41 // Match length, string length, index42 func TestItemRank(t *testing.T) {43 // FIXME global44- sortCriteria = []criterion{byMatchLen, byLength, byIndex}44+ sortCriteria = []criterion{byMatchLen, byLength}45 46 strs := [][]rune{[]rune("foo"), []rune("foobar"), []rune("bar"), []rune("baz")}47- item1 := Item{text: strs[0], index: 1, offsets: []Offset{}}47+ item1 := Item{text: strs[0], offsets: []Offset{}, rank: [5]int32{0, 0, 0, 0, 1}}48 rank1 := item1.Rank(true)49- if rank1[0] != math.MaxInt32 || rank1[1] != 3 || rank1[2] != 1 {49+ if rank1[0] != math.MaxInt32 || rank1[1] != 3 || rank1[4] != 1 {50 t.Error(item1.Rank(true))51 }52 // Only differ in index53- item2 := Item{text: strs[0], index: 0, offsets: []Offset{}}53+ item2 := Item{text: strs[0], offsets: []Offset{}}54 55 items := []*Item{&item1, &item2}56 sort.Sort(ByRelevance(items))57 if items[0] != &item2 || items[1] != &item1 {7 unmodified lines65 t.Error(items)66 }67 68 // Sort by relevance69- item3 := Item{text: strs[1], rank: []int32{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}70- item4 := Item{text: strs[1], rank: []int32{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}71- item5 := Item{text: strs[2], rank: []int32{0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}72- item6 := Item{text: strs[2], rank: []int32{0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}69+ item3 := Item{text: strs[1], rank: [5]int32{0, 0, 0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}70+ item4 := Item{text: strs[1], rank: [5]int32{0, 0, 0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}71+ item5 := Item{text: strs[2], rank: [5]int32{0, 0, 0, 0, 2}, offsets: []Offset{Offset{1, 3}, Offset{5, 7}}}72+ item6 := Item{text: strs[2], rank: [5]int32{0, 0, 0, 0, 2}, offsets: []Offset{Offset{1, 2}, Offset{6, 7}}}73 items = []*Item{&item1, &item2, &item3, &item4, &item5, &item6}74 sort.Sort(ByRelevance(items))75 if items[0] != &item6 || items[1] != &item4 ||76 items[2] != &item5 || items[3] != &item3 ||32 unmodified linesmodified:src/item.go+17 added-23 removed
18 unmodified lines19 type Item struct {20 text []rune21 origText *[]rune22 transformed []Token23- index int3223 offsets []Offset24 colors []ansiOffset26- rank []int3225+ rank [5]int3226 }27 28 // Sort criteria to use. Never changes once fzf is started.29 var sortCriteria []criterion30 32-func isRankValid(rank []int32) bool {31+func isRankValid(rank [5]int32) bool {32 // Exclude ordinal index34- for i := 0; i < len(rank)-1; i++ {35- if rank[i] > 0 {33+ for _, r := range rank[:4] {34+ if r > 0 {35 return true36 }37 }38 return false39 }40 42-func buildEmptyRank(index int32) []int32 {43- len := len(sortCriteria)44- arr := make([]int32, len)45- arr[len-1] = index46- return arr41+func buildEmptyRank(index int32) [5]int32 {42+ return [5]int32{0, 0, 0, 0, index}43 }44 45+func (item *Item) Index() int32 {46+ return item.rank[4]47+}48+49 // Rank calculates rank of the Item50-func (item *Item) Rank(cache bool) []int32 {50+func (item *Item) Rank(cache bool) [5]int32 {51 if cache && isRankValid(item.rank) {52 return item.rank53 }54 matchlen := 055 prevEnd := 056 lenSum := 057- minBegin := math.MaxUint1657+ minBegin := math.MaxInt3258 for _, offset := range item.offsets {59 begin := int(offset[0])60 end := int(offset[1])61 trimLen := int(offset[2])13 unmodified lines75 }76 if matchlen == 0 {77 matchlen = math.MaxInt3278 }79- rank := make([]int32, len(sortCriteria))79+ rank := buildEmptyRank(item.Index())80 for idx, criterion := range sortCriteria {81 var val int3282 switch criterion {83 case byMatchLen:15 unmodified lines99 } else {100 // Empty offsets due to inverse terms.101 val = 1102 }103- case byIndex:104- val = item.index103 }104 rank[idx] = val105 }106 if cache {159 unmodified lines266 267 return compareRanks(irank, jrank, true)268 }269 272-func compareRanks(irank []int32, jrank []int32, tac bool) bool {273- lastIdx := len(irank) - 1274- for idx, left := range irank {270+func compareRanks(irank [5]int32, jrank [5]int32, tac bool) bool {271+ for idx := 0; idx < 4; idx++ {272+ left := irank[idx]273 right := jrank[idx]276- if tac && idx == lastIdx {277- left = left * -1278- right = right * -1279- }274 if left < right {275 return true276 } else if left > right {277 return false278 }279 }286- return true280+ return (irank[4] <= jrank[4]) != tac281 }modified:src/merger_test.go+1 added-1 removed
21 unmodified lines22 offsets[idx] = Offset{sidx, eidx}23 }24 return &Item{25 text: []rune(str),26- index: rand.Int31(),26+ rank: buildEmptyRank(rand.Int31()),27 offsets: offsets}28 }29 30 func TestEmptyMerger(t *testing.T) {63 unmodified linesmodified:src/options.go+1 added-6 removed
81 unmodified lines82 byMatchLen criterion = iota83 byLength84 byBegin85 byEnd86- byIndex86 )87 88 func defaultMargin() [4]string {89 return [4]string{"0", "0", "0", "0"}55 unmodified lines145 WithNth: make([]Range, 0),146 Delimiter: Delimiter{},147 Sort: 1000,148 Tac: false,150- Criteria: []criterion{byMatchLen, byLength, byIndex},149+ Criteria: []criterion{byMatchLen, byLength},150 Multi: false,151 Ansi: false,152 Mouse: true,153 Theme: defaultTheme(),226 unmodified lines380 for _, str := range strings.Split(strings.ToLower(str), ",") {381 switch str {382 case "index":383 check(&hasIndex, "index")385- criteria = append(criteria, byIndex)384 case "length":385 check(&hasLength, "length")386 criteria = append(criteria, byLength)387 case "begin":5 unmodified lines393 default:394 errorExit("invalid sort criterion: " + str)395 }396 }399- if !hasIndex {400- criteria = append(criteria, byIndex)401- }397 return criteria398 }399 400 func dupeTheme(theme *curses.ColorTheme) *curses.ColorTheme {543 unmodified linesmodified:src/pattern.go+1 added-2 removed
304 unmodified lines305 return &Item{306 text: item.text,307 origText: item.origText,308 transformed: item.transformed,309- index: item.index,309 offsets: offsets,310 colors: item.colors,312- rank: buildEmptyRank(item.index)}311+ rank: buildEmptyRank(item.Index())}312 }313 314 func (p *Pattern) basicMatch(item *Item) (int, int, int) {315 input := p.prepareInput(item)57 unmodified linesmodified:src/terminal.go+5 added-6 removed
462 unmodified lines463 trimmed, colors, newState := extractColor(lineStr, state)464 state = newState465 item := &Item{466 text: []rune(trimmed),467- index: 0,467 colors: colors,468 rank: buildEmptyRank(0)}469 470 t.move(line, 2, true)18 unmodified lines489 }490 }491 492 func (t *Terminal) printItem(item *Item, current bool) {494- _, selected := t.selected[item.index]493+ _, selected := t.selected[item.Index()]494 if current {495 C.CPrint(C.ColCursor, true, ">")496 if selected {497 C.CPrint(C.ColSelected, true, ">")336 unmodified lines834 }835 }836 }837 selectItem := func(item *Item) bool {839- if _, found := t.selected[item.index]; !found {840- t.selected[item.index] = selectedItem{time.Now(), item.StringPtr(t.ansi)}838+ if _, found := t.selected[item.Index()]; !found {839+ t.selected[item.Index()] = selectedItem{time.Now(), item.StringPtr(t.ansi)}840 return true841 }842 return false843 }844 toggleY := func(y int) {845 item := t.merger.Get(y)846 if !selectItem(item) {848- delete(t.selected, item.index)847+ delete(t.selected, item.Index())848 }849 }850 toggle := func() {851 if t.cy < t.merger.Length() {80 unmodified lines932 case actDeselectAll:933 if t.multi {934 for i := 0; i < t.merger.Length(); i++ {935 item := t.merger.Get(i)937- delete(t.selected, item.index)936+ delete(t.selected, item.Index())937 }938 req(reqList, reqInfo)939 }940 case actToggle:214 unmodified lines