aboutsummaryrefslogtreecommitdiff
path: root/src/sort
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2019-03-25 17:39:11 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2019-03-27 04:58:23 +0000
commit39a51a4b0d698491baaa252e21be2a51516379ea (patch)
treee14c5dce3e27e69783779ae5b81a0668a36ab5d7 /src/sort
parentf0e0be6e9020caff0b44e0dcb44c8b2e707710f0 (diff)
downloadgo-39a51a4b0d698491baaa252e21be2a51516379ea.tar.xz
sort, internal/reflectlite: flesh out reflectlite enough for use by sort
Now the net package is back to no longer depending on unicode. And lock that in with a test. Fixes #30440 Change-Id: I18b89b02f7d96488783adc07308da990f505affd Reviewed-on: https://go-review.googlesource.com/c/go/+/169137 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/sort')
-rw-r--r--src/sort/slice.go16
-rw-r--r--src/sort/slice_pre113.go46
2 files changed, 55 insertions, 7 deletions
diff --git a/src/sort/slice.go b/src/sort/slice.go
index 206f12173d..5196affcfd 100644
--- a/src/sort/slice.go
+++ b/src/sort/slice.go
@@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// +build !compiler_bootstrap go1.8
+// +build !compiler_bootstrap go1.13
package sort
-import "reflect"
+import (
+ "internal/reflectlite"
+)
// Slice sorts the provided slice given the provided less function.
//
@@ -15,8 +17,8 @@ import "reflect"
//
// The function panics if the provided interface is not a slice.
func Slice(slice interface{}, less func(i, j int) bool) {
- rv := reflect.ValueOf(slice)
- swap := reflect.Swapper(slice)
+ rv := reflectlite.ValueOf(slice)
+ swap := reflectlite.Swapper(slice)
length := rv.Len()
quickSort_func(lessSwap{less, swap}, 0, length, maxDepth(length))
}
@@ -26,8 +28,8 @@ func Slice(slice interface{}, less func(i, j int) bool) {
//
// The function panics if the provided interface is not a slice.
func SliceStable(slice interface{}, less func(i, j int) bool) {
- rv := reflect.ValueOf(slice)
- swap := reflect.Swapper(slice)
+ rv := reflectlite.ValueOf(slice)
+ swap := reflectlite.Swapper(slice)
stable_func(lessSwap{less, swap}, rv.Len())
}
@@ -35,7 +37,7 @@ func SliceStable(slice interface{}, less func(i, j int) bool) {
//
// The function panics if the provided interface is not a slice.
func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool {
- rv := reflect.ValueOf(slice)
+ rv := reflectlite.ValueOf(slice)
n := rv.Len()
for i := n - 1; i > 0; i-- {
if less(i, i-1) {
diff --git a/src/sort/slice_pre113.go b/src/sort/slice_pre113.go
new file mode 100644
index 0000000000..4d5f759a92
--- /dev/null
+++ b/src/sort/slice_pre113.go
@@ -0,0 +1,46 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.8,!go1.13
+
+package sort
+
+import "reflect"
+
+// Slice sorts the provided slice given the provided less function.
+//
+// The sort is not guaranteed to be stable. For a stable sort, use
+// SliceStable.
+//
+// The function panics if the provided interface is not a slice.
+func Slice(slice interface{}, less func(i, j int) bool) {
+ rv := reflect.ValueOf(slice)
+ swap := reflect.Swapper(slice)
+ length := rv.Len()
+ quickSort_func(lessSwap{less, swap}, 0, length, maxDepth(length))
+}
+
+// SliceStable sorts the provided slice given the provided less
+// function while keeping the original order of equal elements.
+//
+// The function panics if the provided interface is not a slice.
+func SliceStable(slice interface{}, less func(i, j int) bool) {
+ rv := reflect.ValueOf(slice)
+ swap := reflect.Swapper(slice)
+ stable_func(lessSwap{less, swap}, rv.Len())
+}
+
+// SliceIsSorted tests whether a slice is sorted.
+//
+// The function panics if the provided interface is not a slice.
+func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool {
+ rv := reflect.ValueOf(slice)
+ n := rv.Len()
+ for i := n - 1; i > 0; i-- {
+ if less(i, i-1) {
+ return false
+ }
+ }
+ return true
+}