Debian Codegraph @

src/merger.go

raw
1 package fzf
2
3 import "fmt"
4
5 // EmptyMerger is a Merger with no data
6 func EmptyMerger(revision revision) *Merger {
7 return NewMerger(nil, [][]Result{}, false, false, revision, 0, 0)
8 }
9
10 // Merger holds a set of locally sorted lists of items and provides the view of
11 // a single, globally-sorted list
12 type Merger struct {
13 pattern *Pattern
14 lists [][]Result
15 merged []Result
16 chunks *[]*Chunk
17 cursors []int
18 sorted bool
19 tac bool
20 final bool
21 count int
22 pass bool
23 startIndex int
24 revision revision
25 minIndex int32
26 maxIndex int32
27 }
28
29 // PassMerger returns a new Merger that simply returns the items in the
30 // original order. startIndex items are skipped from the beginning.
31 func PassMerger(chunks *[]*Chunk, tac bool, revision revision, startIndex int32) *Merger {
32 var minIndex, maxIndex int32
33 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}
48
49 for _, chunk := range *mg.chunks {
50 mg.count += chunk.count
51 }
52 mg.count = max(0, mg.count-si)
53 return &mg
54 }
55
56 // NewMerger returns a new Merger
57 func 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}
71
72 for _, list := range mg.lists {
73 mg.count += len(list)
74 }
75 return &mg
76 }
77
78 // Revision returns revision number
79 func (mg *Merger) Revision() revision {
80 return mg.revision
81 }
82
83 // Length returns the number of items
84 func (mg *Merger) Length() int {
85 return mg.count
86 }
87
88 func (mg *Merger) First() Result {
89 if mg.tac && !mg.sorted {
90 return mg.Get(mg.count - 1)
91 }
92 return mg.Get(0)
93 }
94
95 // FindIndex returns the index of the item with the given item index
96 func (mg *Merger) FindIndex(itemIndex int32) int {
97 index := -1
98 if mg.pass {
99 index = int(itemIndex - mg.minIndex)
100 if mg.tac {
101 index = mg.count - index - 1
102 }
103 } else {
104 for i := 0; i < mg.count; i++ {
105 if mg.Get(i).item.Index() == itemIndex {
106 index = i
107 break
108 }
109 }
110 }
111 return index
112 }
113
114 // Get returns the pointer to the Result object indexed by the given integer
115 func (mg *Merger) Get(idx int) Result {
116 if mg.chunks != nil {
117 if mg.tac {
118 idx = mg.count - idx - 1
119 }
120 idx += mg.startIndex
121 firstChunk := (*mg.chunks)[0]
122 if firstChunk.count < chunkSize && idx >= firstChunk.count {
123 idx -= firstChunk.count
124
125 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 }
131
132 if mg.sorted {
133 return mg.mergedGet(idx)
134 }
135
136 if mg.tac {
137 idx = mg.count - idx - 1
138 }
139 return mg.mergedGet(idx)
140 }
141
142 func (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()] = result
147 }
148 return ret
149 }
150
151 func (mg *Merger) cacheable() bool {
152 return mg.count < mergerCacheMax
153 }
154
155 func (mg *Merger) mergedGet(idx int) Result {
156 for i := len(mg.merged); i <= idx; i++ {
157 minRank := minRank()
158 minIdx := -1
159 for listIdx, list := range mg.lists {
160 cursor := mg.cursors[listIdx]
161 if cursor < 0 || cursor == len(list) {
162 mg.cursors[listIdx] = -1
163 continue
164 }
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 = rank
169 minIdx = listIdx
170 }
171 }
172 }
173
174 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 }