2015-01-09 11:06:08 -05:00
|
|
|
package fzf
|
|
|
|
|
2015-01-10 00:24:12 -05:00
|
|
|
import "fmt"
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Merger with no data
|
|
|
|
var EmptyMerger = NewMerger([][]*Item{}, false)
|
2015-01-09 11:06:08 -05:00
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Merger holds a set of locally sorted lists of items and provides the view of
|
|
|
|
// a single, globally-sorted list
|
2015-01-09 11:06:08 -05:00
|
|
|
type Merger struct {
|
|
|
|
lists [][]*Item
|
|
|
|
merged []*Item
|
|
|
|
cursors []int
|
2015-01-10 00:24:12 -05:00
|
|
|
sorted bool
|
|
|
|
count int
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// NewMerger returns a new Merger
|
2015-01-09 11:06:08 -05:00
|
|
|
func NewMerger(lists [][]*Item, sorted bool) *Merger {
|
|
|
|
mg := Merger{
|
|
|
|
lists: lists,
|
|
|
|
merged: []*Item{},
|
|
|
|
cursors: make([]int, len(lists)),
|
2015-01-10 00:24:12 -05:00
|
|
|
sorted: sorted,
|
|
|
|
count: 0}
|
|
|
|
|
|
|
|
for _, list := range mg.lists {
|
|
|
|
mg.count += len(list)
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
|
|
|
return &mg
|
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Length returns the number of items
|
2015-01-09 11:06:08 -05:00
|
|
|
func (mg *Merger) Length() int {
|
2015-01-10 00:24:12 -05:00
|
|
|
return mg.count
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
|
|
|
|
2015-01-11 13:01:24 -05:00
|
|
|
// Get returns the pointer to the Item object indexed by the given integer
|
2015-01-09 11:06:08 -05:00
|
|
|
func (mg *Merger) Get(idx int) *Item {
|
2015-01-10 00:24:12 -05:00
|
|
|
if len(mg.lists) == 1 {
|
2015-01-09 11:06:08 -05:00
|
|
|
return mg.lists[0][idx]
|
2015-01-10 00:24:12 -05:00
|
|
|
} else if !mg.sorted {
|
|
|
|
for _, list := range mg.lists {
|
|
|
|
numItems := len(list)
|
|
|
|
if idx < numItems {
|
|
|
|
return list[idx]
|
|
|
|
}
|
|
|
|
idx -= numItems
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("Index out of bounds (unsorted, %d/%d)", idx, mg.count))
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
2015-01-10 00:24:12 -05:00
|
|
|
return mg.mergedGet(idx)
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
|
|
|
|
2015-01-10 00:24:12 -05:00
|
|
|
func (mg *Merger) mergedGet(idx int) *Item {
|
|
|
|
for i := len(mg.merged); i <= idx; i++ {
|
2015-01-09 11:06:08 -05:00
|
|
|
minRank := Rank{0, 0, 0}
|
|
|
|
minIdx := -1
|
|
|
|
for listIdx, list := range mg.lists {
|
|
|
|
cursor := mg.cursors[listIdx]
|
|
|
|
if cursor < 0 || cursor == len(list) {
|
|
|
|
mg.cursors[listIdx] = -1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cursor >= 0 {
|
2015-01-11 09:49:12 -05:00
|
|
|
rank := list[cursor].Rank(false)
|
2015-01-09 11:06:08 -05:00
|
|
|
if minIdx < 0 || compareRanks(rank, minRank) {
|
|
|
|
minRank = rank
|
|
|
|
minIdx = listIdx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mg.cursors[listIdx] = cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
if minIdx >= 0 {
|
|
|
|
chosen := mg.lists[minIdx]
|
|
|
|
mg.merged = append(mg.merged, chosen[mg.cursors[minIdx]])
|
2015-01-11 13:01:24 -05:00
|
|
|
mg.cursors[minIdx]++
|
2015-01-09 11:06:08 -05:00
|
|
|
} else {
|
2015-01-10 00:24:12 -05:00
|
|
|
panic(fmt.Sprintf("Index out of bounds (sorted, %d/%d)", i, mg.count))
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|
|
|
|
}
|
2015-01-10 00:24:12 -05:00
|
|
|
return mg.merged[idx]
|
2015-01-09 11:06:08 -05:00
|
|
|
}
|