diff options
Diffstat (limited to 'libgo/go/sort/sort.go')
-rw-r--r-- | libgo/go/sort/sort.go | 74 |
1 files changed, 35 insertions, 39 deletions
diff --git a/libgo/go/sort/sort.go b/libgo/go/sort/sort.go index 30b1819af2d..0a4a4375f05 100644 --- a/libgo/go/sort/sort.go +++ b/libgo/go/sort/sort.go @@ -2,10 +2,12 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package sort provides primitives for sorting arrays and user-defined +// Package sort provides primitives for sorting slices and user-defined // collections. package sort +import "math" + // 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. @@ -82,7 +84,7 @@ func doPivot(data Interface, lo, hi int) (midlo, midhi int) { // data[d <= i < hi] = pivot // // Once b meets c, can swap the "= pivot" sections - // into the middle of the array. + // into the middle of the slice. pivot := lo a, b, c, d := lo+1, lo+1, hi, hi for b < c { @@ -141,7 +143,6 @@ func quickSort(data Interface, a, b int) { func Sort(data Interface) { quickSort(data, 0, data.Len()) } - func IsSorted(data Interface) bool { n := data.Len() for i := n - 1; i > 0; i-- { @@ -152,55 +153,50 @@ func IsSorted(data Interface) bool { return true } - // Convenience types for common cases -// IntArray attaches the methods of Interface to []int, sorting in increasing order. -type IntArray []int +// IntSlice attaches the methods of Interface to []int, sorting in increasing order. +type IntSlice []int -func (p IntArray) Len() int { return len(p) } -func (p IntArray) Less(i, j int) bool { return p[i] < p[j] } -func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p IntSlice) Len() int { return len(p) } +func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // Sort is a convenience method. -func (p IntArray) Sort() { Sort(p) } - +func (p IntSlice) Sort() { Sort(p) } -// Float64Array attaches the methods of Interface to []float64, sorting in increasing order. -type Float64Array []float64 +// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order. +type Float64Slice []float64 -func (p Float64Array) Len() int { return len(p) } -func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] } -func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p Float64Slice) Len() int { return len(p) } +func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] || math.IsNaN(p[i]) && !math.IsNaN(p[j]) } +func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // Sort is a convenience method. -func (p Float64Array) Sort() { Sort(p) } +func (p Float64Slice) Sort() { Sort(p) } +// StringSlice attaches the methods of Interface to []string, sorting in increasing order. +type StringSlice []string -// StringArray attaches the methods of Interface to []string, sorting in increasing order. -type StringArray []string - -func (p StringArray) Len() int { return len(p) } -func (p StringArray) Less(i, j int) bool { return p[i] < p[j] } -func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p StringSlice) Len() int { return len(p) } +func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // Sort is a convenience method. -func (p StringArray) Sort() { Sort(p) } - +func (p StringSlice) Sort() { Sort(p) } // Convenience wrappers for common cases -// SortInts sorts an array of ints in increasing order. -func SortInts(a []int) { Sort(IntArray(a)) } -// SortFloat64s sorts an array of float64s in increasing order. -func SortFloat64s(a []float64) { Sort(Float64Array(a)) } -// SortStrings sorts an array of strings in increasing order. -func SortStrings(a []string) { Sort(StringArray(a)) } - - -// IntsAreSorted tests whether an array of ints is sorted in increasing order. -func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) } -// Float64sAreSorted tests whether an array of float64s is sorted in increasing order. -func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) } -// StringsAreSorted tests whether an array of strings is sorted in increasing order. -func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) } +// Ints sorts a slice of ints in increasing order. +func Ints(a []int) { Sort(IntSlice(a)) } +// Float64s sorts a slice of float64s in increasing order. +func Float64s(a []float64) { Sort(Float64Slice(a)) } +// Strings sorts a slice of strings in increasing order. +func Strings(a []string) { Sort(StringSlice(a)) } + +// IntsAreSorted tests whether a slice of ints is sorted in increasing order. +func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) } +// Float64sAreSorted tests whether a slice of float64s is sorted in increasing order. +func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) } +// StringsAreSorted tests whether a slice of strings is sorted in increasing order. +func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) } |