]> Cypherpunks repositories - gostls13.git/commitdiff
sort: split post-Go1.4 code into its own file
authorRuss Cox <rsc@golang.org>
Fri, 27 Oct 2017 14:21:46 +0000 (10:21 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 27 Oct 2017 16:01:43 +0000 (16:01 +0000)
This will let us build the latest sort when bootstrapping the compiler.
The compiler depends on the precise tie-breaks used by sort in
some cases, and it's easier to bring sort along than require checking
every sort call ever added to the compiler.

Change-Id: Idc622f89aedbb40d848708c76650fc28779d0c3c
Reviewed-on: https://go-review.googlesource.com/73951
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
src/sort/slice.go [new file with mode: 0644]
src/sort/sort.go

diff --git a/src/sort/slice.go b/src/sort/slice.go
new file mode 100644 (file)
index 0000000..206f121
--- /dev/null
@@ -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 !compiler_bootstrap go1.8
+
+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
+}
index 081b7007989b6e3a98f861e48a921811dc11476b..a7304af53de2bce3a5d6e99ab0fd5d88d5217625 100644 (file)
@@ -8,8 +8,6 @@
 // collections.
 package sort
 
-import "reflect"
-
 // A type, typically a collection, that satisfies sort.Interface can be
 // sorted by the routines in this package. The methods require that the
 // elements of the collection be enumerated by an integer index.
@@ -238,43 +236,6 @@ type lessSwap struct {
        Swap func(i, j int)
 }
 
-// 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
-}
-
 type reverse struct {
        // This embedded Interface permits Reverse to use the methods of
        // another Interface implementation.