diff options
Diffstat (limited to 'src/pkg/sort')
-rw-r--r-- | src/pkg/sort/search.go | 16 | ||||
-rw-r--r-- | src/pkg/sort/search_test.go | 6 | ||||
-rw-r--r-- | src/pkg/sort/sort.go | 64 | ||||
-rw-r--r-- | src/pkg/sort/sort_test.go | 42 |
4 files changed, 64 insertions, 64 deletions
diff --git a/src/pkg/sort/search.go b/src/pkg/sort/search.go index 6828e19b6..7d468da8a 100644 --- a/src/pkg/sort/search.go +++ b/src/pkg/sort/search.go @@ -15,7 +15,7 @@ package sort // 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 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 // to be searched for, and how the data structure is indexed and // ordered. @@ -75,7 +75,7 @@ func Search(n int, f func(int) bool) int { // Convenience wrappers for common cases. // 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 { return Search(len(a), func(i int) bool { return a[i] >= x }) @@ -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 -// 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 { 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 -// as specified by Search. The array must be sorted in ascending order. +// SearchStrings searches for x slice a sorted slice of strings and returns the index +// as specified by Search. The slice must be sorted in ascending order. // func SearchStrings(a []string, x string) int { return Search(len(a), func(i int) bool { return a[i] >= x }) @@ -99,12 +99,12 @@ func SearchStrings(a []string, x string) int { // Search returns the result of applying SearchInts to the receiver and x. -func (p IntArray) Search(x int) int { return SearchInts(p, x) } +func (p IntSlice) Search(x int) int { return SearchInts(p, 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. -func (p StringArray) Search(x string) int { return SearchStrings(p, x) } +func (p StringSlice) Search(x string) int { return SearchStrings(p, x) } diff --git a/src/pkg/sort/search_test.go b/src/pkg/sort/search_test.go index 939f66af3..2a9a85854 100644 --- a/src/pkg/sort/search_test.go +++ b/src/pkg/sort/search_test.go @@ -107,9 +107,9 @@ var wrappertests = []struct { {"SearchInts", SearchInts(data, 11), 8}, {"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4}, {"SearchStrings", SearchStrings(sdata, ""), 0}, - {"IntArray.Search", IntArray(data).Search(0), 2}, - {"Float64Array.Search", Float64Array(fdata).Search(2.0), 3}, - {"StringArray.Search", StringArray(sdata).Search("x"), 3}, + {"IntSlice.Search", IntSlice(data).Search(0), 2}, + {"Float64Slice.Search", Float64Slice(fdata).Search(2.0), 3}, + {"StringSlice.Search", StringSlice(sdata).Search("x"), 3}, } diff --git a/src/pkg/sort/sort.go b/src/pkg/sort/sort.go index 30b1819af..daed61ea8 100644 --- a/src/pkg/sort/sort.go +++ b/src/pkg/sort/sort.go @@ -2,7 +2,7 @@ // 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 @@ -82,7 +82,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 { @@ -155,52 +155,52 @@ func IsSorted(data Interface) bool { // 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] } +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) } -// StringArray attaches the methods of Interface to []string, sorting in increasing order. -type StringArray []string +// StringSlice attaches the methods of Interface to []string, sorting in increasing order. +type StringSlice []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)) } +// 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 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)) } +// 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)) } diff --git a/src/pkg/sort/sort_test.go b/src/pkg/sort/sort_test.go index 3d7337fd0..4da262637 100644 --- a/src/pkg/sort/sort_test.go +++ b/src/pkg/sort/sort_test.go @@ -16,9 +16,9 @@ var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, var float64s = [...]float64{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8} var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"} -func TestSortIntArray(t *testing.T) { +func TestSortIntSlice(t *testing.T) { data := ints - a := IntArray(data[0:]) + a := IntSlice(data[0:]) Sort(a) if !IsSorted(a) { t.Errorf("sorted %v", ints) @@ -26,9 +26,9 @@ func TestSortIntArray(t *testing.T) { } } -func TestSortFloat64Array(t *testing.T) { +func TestSortFloat64Slice(t *testing.T) { data := float64s - a := Float64Array(data[0:]) + a := Float64Slice(data[0:]) Sort(a) if !IsSorted(a) { t.Errorf("sorted %v", float64s) @@ -36,9 +36,9 @@ func TestSortFloat64Array(t *testing.T) { } } -func TestSortStringArray(t *testing.T) { +func TestSortStringSlice(t *testing.T) { data := strings - a := StringArray(data[0:]) + a := StringSlice(data[0:]) Sort(a) if !IsSorted(a) { t.Errorf("sorted %v", strings) @@ -46,27 +46,27 @@ func TestSortStringArray(t *testing.T) { } } -func TestSortInts(t *testing.T) { +func TestInts(t *testing.T) { data := ints - SortInts(data[0:]) + Ints(data[0:]) if !IntsAreSorted(data[0:]) { t.Errorf("sorted %v", ints) t.Errorf(" got %v", data) } } -func TestSortFloat64s(t *testing.T) { +func TestFloat64s(t *testing.T) { data := float64s - SortFloat64s(data[0:]) + Float64s(data[0:]) if !Float64sAreSorted(data[0:]) { t.Errorf("sorted %v", float64s) t.Errorf(" got %v", data) } } -func TestSortStrings(t *testing.T) { +func TestStrings(t *testing.T) { data := strings - SortStrings(data[0:]) + Strings(data[0:]) if !StringsAreSorted(data[0:]) { t.Errorf("sorted %v", strings) t.Errorf(" got %v", data) @@ -85,7 +85,7 @@ func TestSortLarge_Random(t *testing.T) { if IntsAreSorted(data) { t.Fatalf("terrible rand.rand") } - SortInts(data) + Ints(data) if !IntsAreSorted(data) { t.Errorf("sort didn't sort - 1M ints") } @@ -99,7 +99,7 @@ func BenchmarkSortString1K(b *testing.B) { data[i] = strconv.Itoa(i ^ 0x2cc) } b.StartTimer() - SortStrings(data) + Strings(data) b.StopTimer() } } @@ -112,7 +112,7 @@ func BenchmarkSortInt1K(b *testing.B) { data[i] = i ^ 0x2cc } b.StartTimer() - SortInts(data) + Ints(data) b.StopTimer() } } @@ -125,7 +125,7 @@ func BenchmarkSortInt64K(b *testing.B) { data[i] = i ^ 0xcccc } b.StartTimer() - SortInts(data) + Ints(data) b.StopTimer() } } @@ -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) Swap(i, j int) { 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.nswap++ @@ -241,9 +241,9 @@ func TestBentleyMcIlroy(t *testing.T) { for i := 0; i < n; i++ { mdata[i] = data[i] } - // SortInts is known to be correct + // Ints is known to be correct // because mode Sort runs after mode _Copy. - SortInts(mdata) + Ints(mdata) case _Dither: for i := 0; i < n; i++ { mdata[i] = data[i] + i%5 @@ -255,13 +255,13 @@ func TestBentleyMcIlroy(t *testing.T) { Sort(d) // 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 // the data, not (for example) overwriting it with zeros. // // In go, we don't have to be so paranoid: since the only // 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) { t.Errorf("%s: ints not sorted", desc) t.Errorf("\t%v", mdata) |