Skip to content

Commit

Permalink
lib/sort: updates for go1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
puellanivis committed Nov 28, 2022
1 parent a055cd5 commit 3499d1d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/sort/unsafe_go119.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 pdqsort sort.pdqsort
func pdqsort(data sort.Interface, a, b, maxDepth 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, maxDepth int) {
pdqsort(data, a, b, maxDepth/2)
}

// 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))
}
3 changes: 3 additions & 0 deletions lib/sort/unsafe_pre119.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !go1.19
// +build !go1.19

package sort

import (
Expand Down

0 comments on commit 3499d1d

Please sign in to comment.