Skip to content

Commit

Permalink
Fixed sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Harshil Goel committed Sep 13, 2023
1 parent b0a1bc3 commit e10910a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ require (
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.8.4
github.com/twpayne/go-geom v1.0.5
github.com/wangjohn/quickselect v0.0.0-20161129230411-ed8402a42d5f
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
go.etcd.io/etcd/raft/v3 v3.5.9
go.opencensus.io v0.24.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,6 @@ github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
github.com/vektah/gqlparser/v2 v2.1.0/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=
github.com/wangjohn/quickselect v0.0.0-20161129230411-ed8402a42d5f h1:9DDCDwOyEy/gId+IEMrFHLuQ5R/WV0KNxWLler8X2OY=
github.com/wangjohn/quickselect v0.0.0-20161129230411-ed8402a42d5f/go.mod h1:8sdOQnirw1PrcnTJYkmW1iOHtUmblMmGdUOHyWYycLI=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I=
github.com/xdg/stringprep v1.0.3 h1:cmL5Enob4W83ti/ZHuZLuKD/xqJfus4fVPwE+/BDm+4=
Expand Down
68 changes: 63 additions & 5 deletions types/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package types

import (
"container/heap"
"sort"
"time"

"github.com/pkg/errors"
"github.com/wangjohn/quickselect"
"golang.org/x/text/collate"
"golang.org/x/text/language"

Expand Down Expand Up @@ -97,6 +97,67 @@ func IsSortable(tid TypeID) bool {
}
}

type dataHeap struct {
heapIndices []int
data byValue
}

func (h dataHeap) Len() int { return len(h.heapIndices) }
func (h dataHeap) Less(i, j int) bool { return h.data.Less(h.heapIndices[j], h.heapIndices[i]) }
func (h dataHeap) Swap(i, j int) {
h.heapIndices[i], h.heapIndices[j] = h.heapIndices[j], h.heapIndices[i]
}

func (h *dataHeap) Push(x interface{}) {
h.heapIndices = append(h.heapIndices, x.(int))
}

func (h *dataHeap) Pop() interface{} {
old := h.heapIndices
n := len(old)
x := old[n-1]
h.heapIndices = old[0 : n-1]
return x
}

type IntSlice []int

func (t IntSlice) Len() int {
return len(t)
}

func (t IntSlice) Less(i, j int) bool {
return t[i] < t[j]
}

func (t IntSlice) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

func heapSelectionFinding(data byValue, k int) {
heapIndices := make([]int, k)
for i := 0; i < k; i++ {
heapIndices[i] = i
}

h := &dataHeap{heapIndices, data}
heap.Init(h)
var currentHeapMax int
for i := k; i < data.Len(); i++ {
currentHeapMax = h.heapIndices[0]

if data.Less(i, currentHeapMax) {
heap.Push(h, i)
heap.Pop(h)
}
}

sort.Sort(IntSlice(h.heapIndices))
for i := 0; i < len(h.heapIndices); i++ {
data.Swap(i, h.heapIndices[i])
}
}

// SortWithFacet sorts the given array in-place and considers the given facets to calculate
// the proper ordering.
func SortTopN(v [][]Val, ul *[]uint64, desc []bool, lang string, n int) error {
Expand All @@ -121,10 +182,7 @@ func SortTopN(v [][]Val, ul *[]uint64, desc []bool, lang string, n int) error {

b := sortBase{v, desc, ul, nil, cl}
toBeSorted := byValue{b}
err := quickselect.QuickSelect(toBeSorted, n)
if err != nil {
return err
}
heapSelectionFinding(toBeSorted, n)

toBeSorted.values = toBeSorted.values[:n]
sort.Sort(toBeSorted)
Expand Down

0 comments on commit e10910a

Please sign in to comment.