Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions runtime/sam/expr/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,40 +252,3 @@ func (s *sortStableReader) Read() (*super.Value, error) {
s.indices = s.indices[1:]
return val, nil
}

type RecordSlice struct {
vals []super.Value
compare CompareFn
}

func NewRecordSlice(compare CompareFn) *RecordSlice {
return &RecordSlice{compare: compare}
}

// Swap implements sort.Interface for *Record slices.
func (r *RecordSlice) Len() int { return len(r.vals) }

// Swap implements sort.Interface for *Record slices.
func (r *RecordSlice) Swap(i, j int) { r.vals[i], r.vals[j] = r.vals[j], r.vals[i] }

// Less implements sort.Interface for *Record slices.
func (r *RecordSlice) Less(i, j int) bool {
return r.compare(r.vals[i], r.vals[j]) < 0
}

// Push adds x as element Len(). Implements heap.Interface.
func (r *RecordSlice) Push(rec any) {
r.vals = append(r.vals, rec.(super.Value))
}

// Pop removes the first element in the array. Implements heap.Interface.
func (r *RecordSlice) Pop() any {
rec := r.vals[len(r.vals)-1]
r.vals = r.vals[:len(r.vals)-1]
return rec
}

// Index returns the ith record.
func (r *RecordSlice) Index(i int) super.Value {
return r.vals[i]
}
41 changes: 39 additions & 2 deletions runtime/sam/op/top/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Op struct {
guessReverse bool

eos bool
records *expr.RecordSlice
records *recordSlice
compare expr.CompareFn
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func (o *Op) consume(rec super.Value) {
// package heap implements a min-heap. Invert the comparison result to get a max-heap.
o.compare = func(a, b super.Value) int { return comparator.Compare(a, b) * -1 }
}
o.records = expr.NewRecordSlice(o.compare)
o.records = newRecordSlice(o.compare)
heap.Init(o.records)
}
if o.records.Len() < o.limit || o.compare(o.records.Index(0), rec) < 0 {
Expand All @@ -85,3 +85,40 @@ func (o *Op) sorted() sbuf.Batch {
o.records = nil
return sbuf.NewArray(out)
}

type recordSlice struct {
vals []super.Value
compare expr.CompareFn
}

func newRecordSlice(compare expr.CompareFn) *recordSlice {
return &recordSlice{compare: compare}
}

// Swap implements sort.Interface for *Record slices.
func (r *recordSlice) Len() int { return len(r.vals) }

// Swap implements sort.Interface for *Record slices.
func (r *recordSlice) Swap(i, j int) { r.vals[i], r.vals[j] = r.vals[j], r.vals[i] }

// Less implements sort.Interface for *Record slices.
func (r *recordSlice) Less(i, j int) bool {
return r.compare(r.vals[i], r.vals[j]) < 0
}

// Push adds x as element Len(). Implements heap.Interface.
func (r *recordSlice) Push(rec any) {
r.vals = append(r.vals, rec.(super.Value))
}

// Pop removes the first element in the array. Implements heap.Interface.
func (r *recordSlice) Pop() any {
rec := r.vals[len(r.vals)-1]
r.vals = r.vals[:len(r.vals)-1]
return rec
}

// Index returns the ith record.
func (r *recordSlice) Index(i int) super.Value {
return r.vals[i]
}
Loading