Commit 2b08e952 authored by Rob Pike's avatar Rob Pike

sort: change the name of Float64Array to Float64Slice.

I missed this before because I used the wrong regexp. What a moron.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/4639041
parent b955448d
...@@ -15,7 +15,7 @@ package sort ...@@ -15,7 +15,7 @@ package sort
// Search calls f(i) only for i in the range [0, n). // Search calls f(i) only for i in the range [0, n).
// //
// A common use of Search is to find the index i for a value x in // A common use of Search is to find the index i for a value x in
// a sorted, indexable data structure like an array or slice. // a sorted, indexable data structure such as an array or slice.
// In this case, the argument f, typically a closure, captures the value // In this case, the argument f, typically a closure, captures the value
// to be searched for, and how the data structure is indexed and // to be searched for, and how the data structure is indexed and
// ordered. // ordered.
...@@ -75,7 +75,7 @@ func Search(n int, f func(int) bool) int { ...@@ -75,7 +75,7 @@ func Search(n int, f func(int) bool) int {
// Convenience wrappers for common cases. // Convenience wrappers for common cases.
// SearchInts searches for x in a sorted slice of ints and returns the index // SearchInts searches for x in a sorted slice of ints and returns the index
// as specified by Search. The array must be sorted in ascending order. // as specified by Search. The slice must be sorted in ascending order.
// //
func SearchInts(a []int, x int) int { func SearchInts(a []int, x int) int {
return Search(len(a), func(i int) bool { return a[i] >= x }) return Search(len(a), func(i int) bool { return a[i] >= x })
...@@ -83,15 +83,15 @@ func SearchInts(a []int, x int) int { ...@@ -83,15 +83,15 @@ func SearchInts(a []int, x int) int {
// SearchFloat64s searches for x in a sorted slice of float64s and returns the index // SearchFloat64s searches for x in a sorted slice of float64s and returns the index
// as specified by Search. The array must be sorted in ascending order. // as specified by Search. The slice must be sorted in ascending order.
// //
func SearchFloat64s(a []float64, x float64) int { func SearchFloat64s(a []float64, x float64) int {
return Search(len(a), func(i int) bool { return a[i] >= x }) return Search(len(a), func(i int) bool { return a[i] >= x })
} }
// SearchStrings searches for x in a sorted slice of strings and returns the index // SearchStrings searches for x slice a sorted slice of strings and returns the index
// as specified by Search. The array must be sorted in ascending order. // as specified by Search. The slice must be sorted in ascending order.
// //
func SearchStrings(a []string, x string) int { func SearchStrings(a []string, x string) int {
return Search(len(a), func(i int) bool { return a[i] >= x }) return Search(len(a), func(i int) bool { return a[i] >= x })
...@@ -103,7 +103,7 @@ func (p IntSlice) Search(x int) int { return SearchInts(p, x) } ...@@ -103,7 +103,7 @@ func (p IntSlice) Search(x int) int { return SearchInts(p, x) }
// Search returns the result of applying SearchFloat64s to the receiver and x. // Search returns the result of applying SearchFloat64s to the receiver and x.
func (p Float64Array) Search(x float64) int { return SearchFloat64s(p, x) } func (p Float64Slice) Search(x float64) int { return SearchFloat64s(p, x) }
// Search returns the result of applying SearchStrings to the receiver and x. // Search returns the result of applying SearchStrings to the receiver and x.
......
...@@ -108,7 +108,7 @@ var wrappertests = []struct { ...@@ -108,7 +108,7 @@ var wrappertests = []struct {
{"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4}, {"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4},
{"SearchStrings", SearchStrings(sdata, ""), 0}, {"SearchStrings", SearchStrings(sdata, ""), 0},
{"IntSlice.Search", IntSlice(data).Search(0), 2}, {"IntSlice.Search", IntSlice(data).Search(0), 2},
{"Float64Array.Search", Float64Array(fdata).Search(2.0), 3}, {"Float64Slice.Search", Float64Slice(fdata).Search(2.0), 3},
{"StringSlice.Search", StringSlice(sdata).Search("x"), 3}, {"StringSlice.Search", StringSlice(sdata).Search("x"), 3},
} }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // 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. // collections.
package sort package sort
...@@ -82,7 +82,7 @@ func doPivot(data Interface, lo, hi int) (midlo, midhi int) { ...@@ -82,7 +82,7 @@ func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
// data[d <= i < hi] = pivot // data[d <= i < hi] = pivot
// //
// Once b meets c, can swap the "= pivot" sections // Once b meets c, can swap the "= pivot" sections
// into the middle of the array. // into the middle of the slice.
pivot := lo pivot := lo
a, b, c, d := lo+1, lo+1, hi, hi a, b, c, d := lo+1, lo+1, hi, hi
for b < c { for b < c {
...@@ -166,15 +166,15 @@ func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } ...@@ -166,15 +166,15 @@ func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p IntSlice) Sort() { Sort(p) } func (p IntSlice) Sort() { Sort(p) }
// Float64Array attaches the methods of Interface to []float64, sorting in increasing order. // Float64Slice attaches the methods of Interface to []float64, sorting in increasing order.
type Float64Array []float64 type Float64Slice []float64
func (p Float64Array) Len() int { return len(p) } func (p Float64Slice) Len() int { return len(p) }
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] } func (p Float64Slice) 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) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method. // 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. // StringSlice attaches the methods of Interface to []string, sorting in increasing order.
...@@ -190,17 +190,17 @@ func (p StringSlice) Sort() { Sort(p) } ...@@ -190,17 +190,17 @@ func (p StringSlice) Sort() { Sort(p) }
// Convenience wrappers for common cases // Convenience wrappers for common cases
// SortInts sorts an array of ints in increasing order. // SortInts sorts a slice of ints in increasing order.
func SortInts(a []int) { Sort(IntSlice(a)) } func SortInts(a []int) { Sort(IntSlice(a)) }
// SortFloat64s sorts an array of float64s in increasing order. // SortFloat64s sorts a slice of float64s in increasing order.
func SortFloat64s(a []float64) { Sort(Float64Array(a)) } func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
// SortStrings sorts an array of strings in increasing order. // SortStrings sorts a slice of strings in increasing order.
func SortStrings(a []string) { Sort(StringSlice(a)) } func SortStrings(a []string) { Sort(StringSlice(a)) }
// IntsAreSorted tests whether an array of ints is sorted in increasing order. // IntsAreSorted tests whether a slice of ints is sorted in increasing order.
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) } func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
// Float64sAreSorted tests whether an array of float64s is sorted in increasing order. // Float64sAreSorted tests whether a slice of float64s is sorted in increasing order.
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) } func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
// StringsAreSorted tests whether an array of strings is sorted in increasing order. // StringsAreSorted tests whether a slice of strings is sorted in increasing order.
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) } func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }
...@@ -26,9 +26,9 @@ func TestSortIntSlice(t *testing.T) { ...@@ -26,9 +26,9 @@ func TestSortIntSlice(t *testing.T) {
} }
} }
func TestSortFloat64Array(t *testing.T) { func TestSortFloat64Slice(t *testing.T) {
data := float64s data := float64s
a := Float64Array(data[0:]) a := Float64Slice(data[0:])
Sort(a) Sort(a)
if !IsSorted(a) { if !IsSorted(a) {
t.Errorf("sorted %v", float64s) t.Errorf("sorted %v", float64s)
...@@ -161,7 +161,7 @@ func (d *testingData) Len() int { return len(d.data) } ...@@ -161,7 +161,7 @@ func (d *testingData) Len() int { return len(d.data) }
func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j] } func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j] }
func (d *testingData) Swap(i, j int) { func (d *testingData) Swap(i, j int) {
if d.nswap >= d.maxswap { if d.nswap >= d.maxswap {
d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data)) d.t.Errorf("%s: used %d swaps sorting slice of %d", d.desc, d.nswap, len(d.data))
d.t.FailNow() d.t.FailNow()
} }
d.nswap++ d.nswap++
...@@ -255,13 +255,13 @@ func TestBentleyMcIlroy(t *testing.T) { ...@@ -255,13 +255,13 @@ func TestBentleyMcIlroy(t *testing.T) {
Sort(d) Sort(d)
// If we were testing C qsort, we'd have to make a copy // If we were testing C qsort, we'd have to make a copy
// of the array and sort it ourselves and then compare // of the slice and sort it ourselves and then compare
// x against it, to ensure that qsort was only permuting // x against it, to ensure that qsort was only permuting
// the data, not (for example) overwriting it with zeros. // the data, not (for example) overwriting it with zeros.
// //
// In go, we don't have to be so paranoid: since the only // In go, we don't have to be so paranoid: since the only
// mutating method Sort can call is TestingData.swap, // mutating method Sort can call is TestingData.swap,
// it suffices here just to check that the final array is sorted. // it suffices here just to check that the final slice is sorted.
if !IntsAreSorted(mdata) { if !IntsAreSorted(mdata) {
t.Errorf("%s: ints not sorted", desc) t.Errorf("%s: ints not sorted", desc)
t.Errorf("\t%v", mdata) t.Errorf("\t%v", mdata)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment