Skip to content

Commit

Permalink
all: make more use of slices.SortFunc
Browse files Browse the repository at this point in the history
Generics makes these bits of code a little bit easier to maintain.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I6cc4c8310754102561405f1bc302719d39d4a376
Dispatch-Trailer: {"type":"trybot","CL":1206380,"patchset":3,"ref":"refs/changes/80/1206380/3","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Jan 9, 2025
1 parent f390c1a commit 0311760
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
7 changes: 4 additions & 3 deletions internal/mod/modresolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package modresolve

import (
"cmp"
"crypto/sha256"
_ "embed"
"fmt"
"net"
"net/netip"
"path"
"sort"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -379,8 +380,8 @@ func (r *resolver) initHosts() error {
Insecure: insecure,
})
}
sort.Slice(allHosts, func(i, j int) bool {
return allHosts[i].Name < allHosts[j].Name
slices.SortFunc(allHosts, func(a, b Host) int {
return cmp.Compare(a.Name, b.Name)
})
r.allHosts = allHosts
return nil
Expand Down
11 changes: 5 additions & 6 deletions internal/mod/mvs/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package mvs

import (
"cmp"
"fmt"
"slices"
"sort"
)

// Versions is an interface that should be provided by implementations
Expand Down Expand Up @@ -157,12 +157,11 @@ func (g *Graph[V]) BuildList() []V {
}

func (g *Graph[V]) sortVersions(vs []V) {
sort.Slice(vs, func(i, j int) bool {
v0, v1 := vs[i], vs[j]
if p0, p1 := g.v.Path(v0), g.v.Path(v1); p0 != p1 {
return p0 < p1
slices.SortFunc(vs, func(a, b V) int {
if c := cmp.Compare(g.v.Path(a), g.v.Path(b)); c != 0 {
return c
}
return g.cmp(g.v.Version(v0), g.v.Version(v1)) < 0
return g.cmp(g.v.Version(a), g.v.Version(b))
})
}

Expand Down
6 changes: 3 additions & 3 deletions internal/mod/mvs/mvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package mvs

import (
"cmp"
"fmt"
"slices"
"sort"
"sync"

"cuelang.org/go/internal/par"
Expand Down Expand Up @@ -268,8 +268,8 @@ func Req[V comparable](mainModule V, base []string, reqs Reqs[V]) ([]V, error) {
walk(m)
}
}
sort.Slice(min, func(i, j int) bool {
return reqs.Path(min[i]) < reqs.Path(min[j])
slices.SortFunc(min, func(a, b V) int {
return cmp.Compare(reqs.Path(a), reqs.Path(b))
})
return min, nil
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"os"
"path/filepath"
"slices"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -276,12 +275,11 @@ func (g *generator) processGo(pkg *packages.Package) error {
obj := scope.Lookup(name)
objs = append(objs, objWithPos{obj, pkg.Fset.Position(obj.Pos())})
}
sort.Slice(objs, func(i, j int) bool {
obj1, obj2 := objs[i], objs[j]
if obj1.pos.Filename == obj2.pos.Filename {
return obj1.pos.Line < obj2.pos.Line
slices.SortFunc(objs, func(a, b objWithPos) int {
if c := cmp.Compare(a.pos.Filename, b.pos.Filename); c != 0 {
return c
}
return obj1.pos.Filename < obj2.pos.Filename
return cmp.Compare(a.pos.Line, b.pos.Line)
})

for _, obj := range objs {
Expand Down

0 comments on commit 0311760

Please sign in to comment.