mirror of
https://github.com/golang/go.git
synced 2025-11-01 01:00:56 +00:00
sort: rename helpers: s/Sort// in sort.Sort[Float64s|Ints|Strings]
Includes 'sorthelpers' gofix and updates to tree. R=golang-dev, gri CC=golang-dev https://golang.org/cl/4631098
This commit is contained in:
parent
e67a2504a1
commit
5bcbcab311
19 changed files with 126 additions and 33 deletions
|
|
@ -168,7 +168,7 @@ func loadCodewalk(filename string) (*Codewalk, os.Error) {
|
||||||
cw.File[i] = f
|
cw.File[i] = f
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.SortStrings(cw.File)
|
sort.Strings(cw.File)
|
||||||
|
|
||||||
return cw, nil
|
return cw, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -954,7 +954,7 @@ func (list positionList) Swap(i, j int) { list[i], list[j] = list[j], list[
|
||||||
|
|
||||||
// unique returns the list sorted and with duplicate entries removed
|
// unique returns the list sorted and with duplicate entries removed
|
||||||
func unique(list []int) []int {
|
func unique(list []int) []int {
|
||||||
sort.SortInts(list)
|
sort.Ints(list)
|
||||||
var last int
|
var last int
|
||||||
i := 0
|
i := 0
|
||||||
for _, x := range list {
|
for _, x := range list {
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ func (m *Mapping) PrefixList() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the list and remove duplicate entries
|
// sort the list and remove duplicate entries
|
||||||
sort.SortStrings(list)
|
sort.Strings(list)
|
||||||
i := 0
|
i := 0
|
||||||
prev := ""
|
prev := ""
|
||||||
for _, path := range list {
|
for _, path := range list {
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func canonicalizePaths(list []string, filter func(path string) bool) []string {
|
||||||
list = list[0:i]
|
list = list[0:i]
|
||||||
|
|
||||||
// sort the list and remove duplicate entries
|
// sort the list and remove duplicate entries
|
||||||
sort.SortStrings(list)
|
sort.Strings(list)
|
||||||
i = 0
|
i = 0
|
||||||
prev := ""
|
prev := ""
|
||||||
for _, path := range list {
|
for _, path := range list {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ GOFILES=\
|
||||||
procattr.go\
|
procattr.go\
|
||||||
reflect.go\
|
reflect.go\
|
||||||
signal.go\
|
signal.go\
|
||||||
|
sorthelpers.go\
|
||||||
sortslice.go\
|
sortslice.go\
|
||||||
stringssplit.go\
|
stringssplit.go\
|
||||||
typecheck.go\
|
typecheck.go\
|
||||||
|
|
|
||||||
47
src/cmd/gofix/sorthelpers.go
Normal file
47
src/cmd/gofix/sorthelpers.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/ast"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
register(fix{
|
||||||
|
"sorthelpers",
|
||||||
|
sorthelpers,
|
||||||
|
`Adapt code from sort.Sort[Ints|Float64s|Strings] to sort.[Ints|Float64s|Strings].
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func sorthelpers(f *ast.File) (fixed bool) {
|
||||||
|
if !imports(f, "sort") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(f, func(n interface{}) {
|
||||||
|
s, ok := n.(*ast.SelectorExpr)
|
||||||
|
if !ok || !isTopName(s.X, "sort") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch s.Sel.String() {
|
||||||
|
case "SortFloat64s":
|
||||||
|
s.Sel.Name = "Float64s"
|
||||||
|
case "SortInts":
|
||||||
|
s.Sel.Name = "Ints"
|
||||||
|
case "SortStrings":
|
||||||
|
s.Sel.Name = "Strings"
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fixed = true
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
45
src/cmd/gofix/sorthelpers_test.go
Normal file
45
src/cmd/gofix/sorthelpers_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
addTestCases(sorthelpersTests)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sorthelpersTests = []testCase{
|
||||||
|
{
|
||||||
|
Name: "sortslice.0",
|
||||||
|
In: `package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s []string
|
||||||
|
sort.SortStrings(s)
|
||||||
|
var i []ints
|
||||||
|
sort.SortInts(i)
|
||||||
|
var f []float64
|
||||||
|
sort.SortFloat64s(f)
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
Out: `package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s []string
|
||||||
|
sort.Strings(s)
|
||||||
|
var i []ints
|
||||||
|
sort.Ints(i)
|
||||||
|
var f []float64
|
||||||
|
sort.Float64s(f)
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -176,7 +176,7 @@ func main() {
|
||||||
list[i] = f
|
list[i] = f
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.SortStrings(list)
|
sort.Strings(list)
|
||||||
for _, f := range list {
|
for _, f := range list {
|
||||||
fmt.Printf("%s\n", f)
|
fmt.Printf("%s\n", f)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ func (t *T) MSort(m map[string]int) []string {
|
||||||
keys[i] = k
|
keys[i] = k
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
sort.SortStrings(keys)
|
sort.Strings(keys)
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -551,7 +551,7 @@ func (doc *docReader) newDoc(importpath string, filenames []string) *PackageDoc
|
||||||
p := new(PackageDoc)
|
p := new(PackageDoc)
|
||||||
p.PackageName = doc.pkgName
|
p.PackageName = doc.pkgName
|
||||||
p.ImportPath = importpath
|
p.ImportPath = importpath
|
||||||
sort.SortStrings(filenames)
|
sort.Strings(filenames)
|
||||||
p.Filenames = filenames
|
p.Filenames = filenames
|
||||||
p.Doc = CommentText(doc.doc)
|
p.Doc = CommentText(doc.doc)
|
||||||
// makeTypeDocs may extend the list of doc.values and
|
// makeTypeDocs may extend the list of doc.values and
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) os.Error {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.SortStrings(keys)
|
sort.Strings(keys)
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
for _, v := range h[k] {
|
for _, v := range h[k] {
|
||||||
v = strings.Replace(v, "\n", " ", -1)
|
v = strings.Replace(v, "\n", " ", -1)
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int) {
|
||||||
if len(indices) == 0 {
|
if len(indices) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sort.SortInts(indices)
|
sort.Ints(indices)
|
||||||
pairs := make([]int, 2*len(indices))
|
pairs := make([]int, 2*len(indices))
|
||||||
result = make([][]int, len(indices))
|
result = make([][]int, len(indices))
|
||||||
count := 0
|
count := 0
|
||||||
|
|
@ -159,7 +159,7 @@ func (x *Index) FindAllIndex(r *regexp.Regexp, n int) (result [][]int) {
|
||||||
if len(indices) == 0 {
|
if len(indices) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sort.SortInts(indices)
|
sort.Ints(indices)
|
||||||
result = result[0:0]
|
result = result[0:0]
|
||||||
prev := 0
|
prev := 0
|
||||||
for _, i := range indices {
|
for _, i := range indices {
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ func testLookup(t *testing.T, tc *testCase, x *Index, s string, n int) {
|
||||||
// we cannot simply check that the res and exp lists are equal
|
// we cannot simply check that the res and exp lists are equal
|
||||||
|
|
||||||
// check that each result is in fact a correct match and there are no duplicates
|
// check that each result is in fact a correct match and there are no duplicates
|
||||||
sort.SortInts(res)
|
sort.Ints(res)
|
||||||
for i, r := range res {
|
for i, r := range res {
|
||||||
if r < 0 || len(tc.source) <= r {
|
if r < 0 || len(tc.source) <= r {
|
||||||
t.Errorf("test %q, lookup %q, result %d (n = %d): index %d out of range [0, %d[", tc.name, s, i, n, r, len(tc.source))
|
t.Errorf("test %q, lookup %q, result %d (n = %d): index %d out of range [0, %d[", tc.name, s, i, n, r, len(tc.source))
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ func TestLookupHost(t *testing.T) {
|
||||||
// duplicate addresses (a common bug due to the way
|
// duplicate addresses (a common bug due to the way
|
||||||
// getaddrinfo works).
|
// getaddrinfo works).
|
||||||
addrs, _ := LookupHost("localhost")
|
addrs, _ := LookupHost("localhost")
|
||||||
sort.SortStrings(addrs)
|
sort.Strings(addrs)
|
||||||
for i := 0; i+1 < len(addrs); i++ {
|
for i := 0; i+1 < len(addrs); i++ {
|
||||||
if addrs[i] == addrs[i+1] {
|
if addrs[i] == addrs[i+1] {
|
||||||
t.Fatalf("LookupHost(\"localhost\") = %v, has duplicate addresses", addrs)
|
t.Fatalf("LookupHost(\"localhost\") = %v, has duplicate addresses", addrs)
|
||||||
|
|
|
||||||
|
|
@ -272,7 +272,7 @@ func glob(dir, pattern string, matches []string) (m []string, e os.Error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sort.SortStrings(names)
|
sort.Strings(names)
|
||||||
|
|
||||||
for _, n := range names {
|
for _, n := range names {
|
||||||
matched, err := Match(pattern, n)
|
matched, err := Match(pattern, n)
|
||||||
|
|
|
||||||
|
|
@ -190,12 +190,12 @@ func (p StringSlice) Sort() { Sort(p) }
|
||||||
|
|
||||||
// Convenience wrappers for common cases
|
// Convenience wrappers for common cases
|
||||||
|
|
||||||
// SortInts sorts a slice of ints in increasing order.
|
// Ints sorts a slice of ints in increasing order.
|
||||||
func SortInts(a []int) { Sort(IntSlice(a)) }
|
func Ints(a []int) { Sort(IntSlice(a)) }
|
||||||
// SortFloat64s sorts a slice of float64s in increasing order.
|
// Float64s sorts a slice of float64s in increasing order.
|
||||||
func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
|
func Float64s(a []float64) { Sort(Float64Slice(a)) }
|
||||||
// SortStrings sorts a slice of strings in increasing order.
|
// Strings sorts a slice of strings in increasing order.
|
||||||
func SortStrings(a []string) { Sort(StringSlice(a)) }
|
func Strings(a []string) { Sort(StringSlice(a)) }
|
||||||
|
|
||||||
|
|
||||||
// IntsAreSorted tests whether a slice of ints is sorted in increasing order.
|
// IntsAreSorted tests whether a slice of ints is sorted in increasing order.
|
||||||
|
|
|
||||||
|
|
@ -46,27 +46,27 @@ func TestSortStringSlice(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortInts(t *testing.T) {
|
func TestInts(t *testing.T) {
|
||||||
data := ints
|
data := ints
|
||||||
SortInts(data[0:])
|
Ints(data[0:])
|
||||||
if !IntsAreSorted(data[0:]) {
|
if !IntsAreSorted(data[0:]) {
|
||||||
t.Errorf("sorted %v", ints)
|
t.Errorf("sorted %v", ints)
|
||||||
t.Errorf(" got %v", data)
|
t.Errorf(" got %v", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortFloat64s(t *testing.T) {
|
func TestFloat64s(t *testing.T) {
|
||||||
data := float64s
|
data := float64s
|
||||||
SortFloat64s(data[0:])
|
Float64s(data[0:])
|
||||||
if !Float64sAreSorted(data[0:]) {
|
if !Float64sAreSorted(data[0:]) {
|
||||||
t.Errorf("sorted %v", float64s)
|
t.Errorf("sorted %v", float64s)
|
||||||
t.Errorf(" got %v", data)
|
t.Errorf(" got %v", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortStrings(t *testing.T) {
|
func TestStrings(t *testing.T) {
|
||||||
data := strings
|
data := strings
|
||||||
SortStrings(data[0:])
|
Strings(data[0:])
|
||||||
if !StringsAreSorted(data[0:]) {
|
if !StringsAreSorted(data[0:]) {
|
||||||
t.Errorf("sorted %v", strings)
|
t.Errorf("sorted %v", strings)
|
||||||
t.Errorf(" got %v", data)
|
t.Errorf(" got %v", data)
|
||||||
|
|
@ -85,7 +85,7 @@ func TestSortLarge_Random(t *testing.T) {
|
||||||
if IntsAreSorted(data) {
|
if IntsAreSorted(data) {
|
||||||
t.Fatalf("terrible rand.rand")
|
t.Fatalf("terrible rand.rand")
|
||||||
}
|
}
|
||||||
SortInts(data)
|
Ints(data)
|
||||||
if !IntsAreSorted(data) {
|
if !IntsAreSorted(data) {
|
||||||
t.Errorf("sort didn't sort - 1M ints")
|
t.Errorf("sort didn't sort - 1M ints")
|
||||||
}
|
}
|
||||||
|
|
@ -99,7 +99,7 @@ func BenchmarkSortString1K(b *testing.B) {
|
||||||
data[i] = strconv.Itoa(i ^ 0x2cc)
|
data[i] = strconv.Itoa(i ^ 0x2cc)
|
||||||
}
|
}
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
SortStrings(data)
|
Strings(data)
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +112,7 @@ func BenchmarkSortInt1K(b *testing.B) {
|
||||||
data[i] = i ^ 0x2cc
|
data[i] = i ^ 0x2cc
|
||||||
}
|
}
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
SortInts(data)
|
Ints(data)
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +125,7 @@ func BenchmarkSortInt64K(b *testing.B) {
|
||||||
data[i] = i ^ 0xcccc
|
data[i] = i ^ 0xcccc
|
||||||
}
|
}
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
SortInts(data)
|
Ints(data)
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -241,9 +241,9 @@ func TestBentleyMcIlroy(t *testing.T) {
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
mdata[i] = data[i]
|
mdata[i] = data[i]
|
||||||
}
|
}
|
||||||
// SortInts is known to be correct
|
// Ints is known to be correct
|
||||||
// because mode Sort runs after mode _Copy.
|
// because mode Sort runs after mode _Copy.
|
||||||
SortInts(mdata)
|
Ints(mdata)
|
||||||
case _Dither:
|
case _Dither:
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
mdata[i] = data[i] + i%5
|
mdata[i] = data[i] + i%5
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,7 @@ func testAfterQueuing(t *testing.T) os.Error {
|
||||||
for _, slot := range slots {
|
for _, slot := range slots {
|
||||||
go await(slot, result, After(int64(slot)*Delta))
|
go await(slot, result, After(int64(slot)*Delta))
|
||||||
}
|
}
|
||||||
sort.SortInts(slots)
|
sort.Ints(slots)
|
||||||
for _, slot := range slots {
|
for _, slot := range slots {
|
||||||
r := <-result
|
r := <-result
|
||||||
if r.slot != slot {
|
if r.slot != slot {
|
||||||
|
|
|
||||||
|
|
@ -1042,7 +1042,7 @@ func printCasefold() {
|
||||||
if orb == nil {
|
if orb == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sort.SortInts(orb)
|
sort.Ints(orb)
|
||||||
c := orb[len(orb)-1]
|
c := orb[len(orb)-1]
|
||||||
for _, d := range orb {
|
for _, d := range orb {
|
||||||
chars[c].caseOrbit = d
|
chars[c].caseOrbit = d
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue