Skip to content
Myr

Fix --no-sort jumpiness by merging results in index order

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #4717

authored by Junegunn Choi · committed by Junegunn Choi · parent b80059e · Browse files · 2 files+19 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+19 added−11 removed

  • other:+19 added−11 removed

2 files+19 added−11 removed

modified:src/merger_test.go+17 added-2 removed
52 unmodified lines
53 return lists, items54}5556func TestMergerUnsorted(t *testing.T) {57 lists, items := buildLists(false)57 lists, _ := buildLists(false)5859 // Sort each list by index to simulate real worker behavior60 // (workers process chunks in ascending order via nextChunk.Add(1))61 for _, list := range lists {62 sort.Slice(list, func(i, j int) bool {63 return list[i].item.Index() < list[j].item.Index()64 })65 }66 items := []Result{}67 for _, list := range lists {68 items = append(items, list...)69 }70 sort.Slice(items, func(i, j int) bool {71 return items[i].item.Index() < items[j].item.Index()72 })73 cnt := len(items)7460 // Not sorted: same order75 // Not sorted: items in ascending index order76 mg := NewMerger(nil, lists, false, false, revision{}, 0, 0)77 assert(t, cnt == mg.Length(), "Invalid Length")78 for i := range cnt {79 assert(t, items[i] == mg.Get(i), "Invalid Get")
25 unmodified lines
modified:src/merger.go+2 added-9 removed
134 unmodified lines
135136 if mg.tac {137 idx = mg.count - idx - 1138 }139 for _, list := range mg.lists {140 numItems := len(list)141 if idx < numItems {142 return list[idx]143 }144 idx -= numItems145 }146 panic(fmt.Sprintf("Index out of bounds (unsorted, %d/%d)", idx, mg.count))139 return mg.mergedGet(idx)140}141142func (mg *Merger) ToMap() map[int32]Result {143 ret := make(map[int32]Result, mg.count)
19 unmodified lines
163 continue164 }165 if cursor >= 0 {166 rank := list[cursor]174 if minIdx < 0 || compareRanks(rank, minRank, mg.tac) {167 if minIdx < 0 || mg.sorted && compareRanks(rank, minRank, mg.tac) || !mg.sorted && rank.item.Index() < minRank.item.Index() {168 minRank = rank169 minIdx = listIdx170 }171 }
12 unmodified lines