Skip to content

Commit

Permalink
go1.20 changed sorts up again
Browse files Browse the repository at this point in the history
  • Loading branch information
puellanivis committed Mar 2, 2023
1 parent 3338e73 commit b7166c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/sort/unsafe_go119.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !go1.20
// +build !go1.20

package sort

import (
Expand Down
25 changes: 25 additions & 0 deletions lib/sort/unsafe_go120.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sort

import (
"math/bits"
"sort"
_ "unsafe" // this is to explicitly signal this file is unsafe.
)

//go:linkname heapSort sort.heapSort
func heapSort(data sort.Interface, a, b int)

// quickSort is just an aliased call into pdqsort,
// this means sort.go doesn’t need to be different between go1.19 and earlier.
func quickSort(data sort.Interface, a, b, _ int) {
heapSort(data, a, b)
}

// maxDepth returns a threashold at which quicksort should switch to heapsort.
// It returns 2 × ceil( log₂(n+1) )
func maxDepth(n int) int {
if n <= 0 {
return 0
}
return 2 * bits.Len(uint(n))
}

0 comments on commit b7166c6

Please sign in to comment.