]> Cypherpunks repositories - gostls13.git/commitdiff
go/*: use sort.Slice to simplify some code
authorDaniel Martí <mvdan@mvdan.cc>
Tue, 12 Sep 2017 14:32:33 +0000 (16:32 +0200)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 12 Sep 2017 14:59:39 +0000 (14:59 +0000)
Skip the ones that have multiple uses for now. Also had to rename the
importComment variable as it shadowed the top-level func by the same
name.

Change-Id: I796285aa7b4fdf2c39e652666390427d37b063ee
Reviewed-on: https://go-review.googlesource.com/63150
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/go/ast/import.go
src/go/doc/example.go
src/go/types/methodset.go

index 6b27fe822e5c3742313b66c97accf60d1ff952a6..be23c7fc43207f87af33626b9b676a6450736a66 100644 (file)
@@ -123,14 +123,14 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
        comments := f.Comments[cstart:cend]
 
        // Assign each comment to the import spec preceding it.
-       importComment := map[*ImportSpec][]*CommentGroup{}
+       importComments := map[*ImportSpec][]*CommentGroup{}
        specIndex := 0
        for _, g := range comments {
                for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
                        specIndex++
                }
                s := specs[specIndex].(*ImportSpec)
-               importComment[s] = append(importComment[s], g)
+               importComments[s] = append(importComments[s], g)
        }
 
        // Sort the import specs by import path.
@@ -138,7 +138,19 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
        // Reassign the import paths to have the same position sequence.
        // Reassign each comment to abut the end of its spec.
        // Sort the comments by new position.
-       sort.Sort(byImportSpec(specs))
+       sort.Slice(specs, func(i, j int) bool {
+               ipath := importPath(specs[i])
+               jpath := importPath(specs[j])
+               if ipath != jpath {
+                       return ipath < jpath
+               }
+               iname := importName(specs[i])
+               jname := importName(specs[j])
+               if iname != jname {
+                       return iname < jname
+               }
+               return importComment(specs[i]) < importComment(specs[j])
+       })
 
        // Dedup. Thanks to our sorting, we can just consider
        // adjacent pairs of imports.
@@ -161,38 +173,16 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
                }
                s.Path.ValuePos = pos[i].Start
                s.EndPos = pos[i].End
-               for _, g := range importComment[s] {
+               for _, g := range importComments[s] {
                        for _, c := range g.List {
                                c.Slash = pos[i].End
                        }
                }
        }
 
-       sort.Sort(byCommentPos(comments))
+       sort.Slice(comments, func(i, j int) bool {
+               return comments[i].Pos() < comments[j].Pos()
+       })
 
        return specs
 }
-
-type byImportSpec []Spec // slice of *ImportSpec
-
-func (x byImportSpec) Len() int      { return len(x) }
-func (x byImportSpec) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
-func (x byImportSpec) Less(i, j int) bool {
-       ipath := importPath(x[i])
-       jpath := importPath(x[j])
-       if ipath != jpath {
-               return ipath < jpath
-       }
-       iname := importName(x[i])
-       jname := importName(x[j])
-       if iname != jname {
-               return iname < jname
-       }
-       return importComment(x[i]) < importComment(x[j])
-}
-
-type byCommentPos []*CommentGroup
-
-func (x byCommentPos) Len() int           { return len(x) }
-func (x byCommentPos) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
-func (x byCommentPos) Less(i, j int) bool { return x[i].Pos() < x[j].Pos() }
index bbf8096ce29a38e047aa4af5dd09d4b841e56ef8..a89f29b40f47cd24d7d9a67f346322b68f98a54a 100644 (file)
@@ -94,7 +94,10 @@ func Examples(files ...*ast.File) []*Example {
                }
                list = append(list, flist...)
        }
-       sort.Sort(exampleByName(list))
+       // sort by name
+       sort.Slice(list, func(i, j int) bool {
+               return list[i].Name < list[j].Name
+       })
        return list
 }
 
@@ -135,12 +138,6 @@ func isTest(name, prefix string) bool {
        return !unicode.IsLower(rune)
 }
 
-type exampleByName []*Example
-
-func (s exampleByName) Len() int           { return len(s) }
-func (s exampleByName) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
-func (s exampleByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
-
 // playExample synthesizes a new *ast.File based on the provided
 // file with the provided function body as the body of main.
 func playExample(file *ast.File, body *ast.BlockStmt) *ast.File {
index 4f791d9d51c64e2bda2bec460c7708b2a5054803..2a8b1c24f7a2d977e3c1452c96ca4e21fac37ff5 100644 (file)
@@ -190,7 +190,10 @@ func NewMethodSet(T Type) *MethodSet {
                        list = append(list, m)
                }
        }
-       sort.Sort(byUniqueName(list))
+       // sort by unique name
+       sort.Slice(list, func(i, j int) bool {
+               return list[i].obj.Id() < list[j].obj.Id()
+       })
        return &MethodSet{list}
 }
 
@@ -257,10 +260,3 @@ func ptrRecv(f *Func) bool {
        _, isPtr := deref(f.typ.(*Signature).recv.typ)
        return isPtr
 }
-
-// byUniqueName function lists can be sorted by their unique names.
-type byUniqueName []*Selection
-
-func (a byUniqueName) Len() int           { return len(a) }
-func (a byUniqueName) Less(i, j int) bool { return a[i].obj.Id() < a[j].obj.Id() }
-func (a byUniqueName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }