graph: use slices package for sorting and reversing slices

This commit is contained in:
Jonathan Bluett-Duncan
2024-03-06 22:38:22 +00:00
committed by Dan Kortschak
parent a9b228ed6b
commit bdcda9a453
29 changed files with 224 additions and 295 deletions

View File

@@ -6,9 +6,10 @@ package dynamic
import (
"bytes"
"cmp"
"fmt"
"io"
"sort"
"slices"
"text/tabwriter"
"gonum.org/v1/gonum/graph/path/internal/testgraphs"
@@ -131,7 +132,12 @@ func (d *dumper) printEdges(format string, edges []simple.WeightedEdge) {
return
}
var buf bytes.Buffer
sort.Sort(lexically(edges))
slices.SortFunc(edges, func(a, b simple.WeightedEdge) int {
if n := cmp.Compare(a.From().ID(), b.From().ID()); n != 0 {
return n
}
return cmp.Compare(a.To().ID(), b.To().ID())
})
for i, e := range edges {
if i != 0 {
fmt.Fprint(&buf, ", ")
@@ -143,11 +149,3 @@ func (d *dumper) printEdges(format string, edges []simple.WeightedEdge) {
}
fmt.Fprintf(d.w, format, buf.Bytes())
}
type lexically []simple.WeightedEdge
func (l lexically) Len() int { return len(l) }
func (l lexically) Less(i, j int) bool {
return l[i].From().ID() < l[j].From().ID() || (l[i].From().ID() == l[j].From().ID() && l[i].To().ID() < l[j].To().ID())
}
func (l lexically) Swap(i, j int) { l[i], l[j] = l[j], l[i] }