From d662272b3111ceedf8e518f21347f03533788771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 27 Dec 2024 15:23:14 +0000 Subject: [PATCH] all: make more use of slices.SortFunc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generics makes these bits of code a little bit easier to maintain. Signed-off-by: Daniel Martí Change-Id: I6cc4c8310754102561405f1bc302719d39d4a376 Dispatch-Trailer: {"type":"trybot","CL":1206380,"patchset":2,"ref":"refs/changes/80/1206380/2","targetBranch":"master"} --- internal/mod/modresolve/resolve.go | 7 ++++--- internal/mod/mvs/graph.go | 11 +++++------ internal/mod/mvs/mvs.go | 6 +++--- pkg/gen.go | 10 ++++------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/internal/mod/modresolve/resolve.go b/internal/mod/modresolve/resolve.go index 878777029..60253eb47 100644 --- a/internal/mod/modresolve/resolve.go +++ b/internal/mod/modresolve/resolve.go @@ -15,13 +15,14 @@ package modresolve import ( + "cmp" "crypto/sha256" _ "embed" "fmt" "net" "net/netip" "path" - "sort" + "slices" "strings" "sync" @@ -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 diff --git a/internal/mod/mvs/graph.go b/internal/mod/mvs/graph.go index 6cab7c3ed..7ae9d0530 100644 --- a/internal/mod/mvs/graph.go +++ b/internal/mod/mvs/graph.go @@ -5,9 +5,9 @@ package mvs import ( + "cmp" "fmt" "slices" - "sort" ) // Versions is an interface that should be provided by implementations @@ -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)) }) } diff --git a/internal/mod/mvs/mvs.go b/internal/mod/mvs/mvs.go index 45d444f33..4003b71bd 100644 --- a/internal/mod/mvs/mvs.go +++ b/internal/mod/mvs/mvs.go @@ -7,9 +7,9 @@ package mvs import ( + "cmp" "fmt" "slices" - "sort" "sync" "cuelang.org/go/internal/par" @@ -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 } diff --git a/pkg/gen.go b/pkg/gen.go index 66f3fa08c..ff39d1fd3 100644 --- a/pkg/gen.go +++ b/pkg/gen.go @@ -43,7 +43,6 @@ import ( "os" "path/filepath" "slices" - "sort" "strings" "text/template" @@ -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 {