merger.go
This file
The walk stopped after 200 commits: lines older than 76efddd point at it.
bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd
lines 1–183why, one step back1package fzf23import "fmt"45// EmptyMerger is a Merger with no data6func EmptyMerger(revision revision) *Merger {7 return NewMerger(nil, [][]Result{}, false, false, revision, 0, 0)8}910// Merger holds a set of locally sorted lists of items and provides the view of11// a single, globally-sorted list12type Merger struct {13 pattern *Pattern14 lists [][]Result15 merged []Result16 chunks *[]*Chunk17 cursors []int18 sorted bool19 tac bool20 final bool21 count int22 pass bool23 startIndex int24 revision revision25 minIndex int3226 maxIndex int3227}2829// PassMerger returns a new Merger that simply returns the items in the30// original order. startIndex items are skipped from the beginning.31func PassMerger(chunks *[]*Chunk, tac bool, revision revision, startIndex int32) *Merger {32 var minIndex, maxIndex int3233 if len(*chunks) > 0 {34 minIndex = (*chunks)[0].items[0].Index()35 maxIndex = (*chunks)[len(*chunks)-1].lastIndex(minIndex)36 }37 si := int(startIndex)38 mg := Merger{39 pattern: nil,40 chunks: chunks,41 tac: tac,42 count: 0,43 pass: true,44 startIndex: si,45 revision: revision,46 minIndex: minIndex + startIndex,47 maxIndex: maxIndex}4849 for _, chunk := range *mg.chunks {50 mg.count += chunk.count51 }52 mg.count = max(0, mg.count-si)53 return &mg54}5556// NewMerger returns a new Merger57func NewMerger(pattern *Pattern, lists [][]Result, sorted bool, tac bool, revision revision, minIndex int32, maxIndex int32) *Merger {58 mg := Merger{59 pattern: pattern,60 lists: lists,61 merged: []Result{},62 chunks: nil,63 cursors: make([]int, len(lists)),64 sorted: sorted,65 tac: tac,66 final: false,67 count: 0,68 revision: revision,69 minIndex: minIndex,70 maxIndex: maxIndex}7172 for _, list := range mg.lists {73 mg.count += len(list)74 }75 return &mg76}7778// Revision returns revision number79func (mg *Merger) Revision() revision {80 return mg.revision81}8283// Length returns the number of items84func (mg *Merger) Length() int {85 return mg.count86}8788func (mg *Merger) First() Result {89 if mg.tac && !mg.sorted {90 return mg.Get(mg.count - 1)91 }92 return mg.Get(0)93}9495// FindIndex returns the index of the item with the given item index96func (mg *Merger) FindIndex(itemIndex int32) int {97 index := -198 if mg.pass {99 index = int(itemIndex - mg.minIndex)100 if mg.tac {101 index = mg.count - index - 1102 }103 } else {104 for i := 0; i < mg.count; i++ {105 if mg.Get(i).item.Index() == itemIndex {106 index = i107 break108 }109 }110 }111 return index112}113114// Get returns the pointer to the Result object indexed by the given integer115func (mg *Merger) Get(idx int) Result {116 if mg.chunks != nil {117 if mg.tac {118 idx = mg.count - idx - 1119 }120 idx += mg.startIndex121 firstChunk := (*mg.chunks)[0]122 if firstChunk.count < chunkSize && idx >= firstChunk.count {123 idx -= firstChunk.count124125 chunk := (*mg.chunks)[idx/chunkSize+1]126 return Result{item: &chunk.items[idx%chunkSize]}127 }128 chunk := (*mg.chunks)[idx/chunkSize]129 return Result{item: &chunk.items[idx%chunkSize]}130 }131132 if mg.sorted {133 return mg.mergedGet(idx)134 }135136 if mg.tac {137 idx = mg.count - idx - 1138 }139 return mg.mergedGet(idx)140}141142func (mg *Merger) ToMap() map[int32]Result {143 ret := make(map[int32]Result, mg.count)144 for i := 0; i < mg.count; i++ {145 result := mg.Get(i)146 ret[result.Index()] = result147 }148 return ret149}150151func (mg *Merger) cacheable() bool {152 return mg.count < mergerCacheMax153}154155func (mg *Merger) mergedGet(idx int) Result {156 for i := len(mg.merged); i <= idx; i++ {157 minRank := minRank()158 minIdx := -1159 for listIdx, list := range mg.lists {160 cursor := mg.cursors[listIdx]161 if cursor < 0 || cursor == len(list) {162 mg.cursors[listIdx] = -1163 continue164 }165 if cursor >= 0 {166 rank := list[cursor]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 }172 }173174 if minIdx >= 0 {175 chosen := mg.lists[minIdx]176 mg.merged = append(mg.merged, chosen[mg.cursors[minIdx]])177 mg.cursors[minIdx]++178 } else {179 panic(fmt.Sprintf("Index out of bounds (sorted, %d/%d)", i, mg.count))180 }181 }182 return mg.merged[idx]183}