Skip to content

Commit

Permalink
all: make more use of slices.Compare and slices.CompareFunc
Browse files Browse the repository at this point in the history
Sometimes their use isn't particularly obvious, particularly for code
written for previous versions of Go without generics.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I9a0a4b12f45741c704ea87e1d8cf78ca78a5db65
Dispatch-Trailer: {"type":"trybot","CL":1206379,"patchset":3,"ref":"refs/changes/79/1206379/3","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Jan 9, 2025
1 parent fddb432 commit 28b5dc2
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 39 deletions.
16 changes: 2 additions & 14 deletions cue/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,6 @@ func (p *list) Add(err Error) {
// Reset resets an List to no errors.
func (p *list) Reset() { *p = (*p)[:0] }

func comparePath(a, b []string) int {
for i, x := range a {
if i >= len(b) {
break
}
if c := cmp.Compare(x, b[i]); c != 0 {
return c
}
}
return cmp.Compare(len(a), len(b))
}

// Sanitize sorts multiple errors and removes duplicates on a best effort basis.
// If err represents a single or no error, it returns the error as is.
func Sanitize(err Error) Error {
Expand Down Expand Up @@ -407,7 +395,7 @@ func (p list) Sort() {
if c := a.Position().Compare(b.Position()); c != 0 {
return c
}
if c := comparePath(a.Path(), b.Path()); c != 0 {
if c := slices.Compare(a.Path(), b.Path()); c != 0 {
return c
}
return cmp.Compare(a.Error(), b.Error())
Expand Down Expand Up @@ -436,7 +424,7 @@ func approximateEqual(a, b Error) bool {
if aPos == token.NoPos || bPos == token.NoPos {
return a.Error() == b.Error()
}
return aPos.Compare(bPos) == 0 && comparePath(a.Path(), b.Path()) == 0
return aPos.Compare(bPos) == 0 && slices.Compare(a.Path(), b.Path()) == 0
}

// An List implements the error interface.
Expand Down
2 changes: 1 addition & 1 deletion internal/core/toposort/cycles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestElementaryCycles(t *testing.T) {
for _, cycle := range cyclesNames {
rotateToStartAt(cycle, slices.Min(cycle))
}
slices.SortFunc(cyclesNames, compareStringses)
slices.SortFunc(cyclesNames, slices.Compare)

if !slices.EqualFunc(cyclesNames, tc.expected, slices.Equal) {
t.Fatalf(`
Expand Down
14 changes: 2 additions & 12 deletions internal/core/toposort/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,22 +139,12 @@ func (index *indexComparison) compareNodeByName(a, b *Node) int {
return cmp.Compare(a.Name(index), b.Name(index))
}

func (index *indexComparison) compareNodesByNames(a, b Nodes) int {
lim := min(len(a), len(b))
for i := 0; i < lim; i++ {
if comparison := index.compareNodeByName(a[i], b[i]); comparison != 0 {
return comparison
}
}
return cmp.Compare(len(a), len(b))
}

func (index *indexComparison) compareCyclesByNames(a, b *Cycle) int {
return index.compareNodesByNames(a.Nodes, b.Nodes)
return slices.CompareFunc(a.Nodes, b.Nodes, index.compareNodeByName)
}

func (index *indexComparison) compareComponentsByNodes(a, b *StronglyConnectedComponent) int {
return index.compareNodesByNames(a.Nodes, b.Nodes)
return slices.CompareFunc(a.Nodes, b.Nodes, index.compareNodeByName)
}

func chooseCycleEntryNode(cycle *Cycle) (entryNode *Node, enabledSince, brokenEdgeCount int) {
Expand Down
11 changes: 0 additions & 11 deletions internal/core/toposort/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package toposort_test

import (
"cmp"
"fmt"
"math/rand"
"os"
Expand Down Expand Up @@ -174,16 +173,6 @@ func makeFeatures(index adt.StringIndexer, inputs [][]string) [][]adt.Feature {
return result
}

func compareStringses(a, b []string) int {
lim := min(len(a), len(b))
for i := 0; i < lim; i++ {
if comparison := cmp.Compare(a[i], b[i]); comparison != 0 {
return comparison
}
}
return cmp.Compare(len(a), len(b))
}

// Consider that names are nodes in a cycle, we want to rotate the
// slice so that it starts at the given node name. This modifies the
// names slice in-place.
Expand Down
2 changes: 1 addition & 1 deletion internal/core/toposort/scc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestStronglyConnectedComponents(t *testing.T) {
slices.Sort(names)
componentsNames[i] = names
}
slices.SortFunc(componentsNames, compareStringses)
slices.SortFunc(componentsNames, slices.Compare)

if !slices.EqualFunc(componentsNames, tc.expected, slices.Equal) {
t.Fatalf(`
Expand Down

0 comments on commit 28b5dc2

Please sign in to comment.