graph: replace custom reverse logic with slices.Reverse

This commit is contained in:
Jonathan Bluett-Duncan
2024-02-03 16:20:43 +00:00
committed by Dan Kortschak
parent 1f5b2b5b54
commit b94e4828e9
12 changed files with 33 additions and 38 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"slices"
"sort" "sort"
"testing" "testing"
@@ -608,7 +609,7 @@ func TestLouvainDirectedMultiplex(t *testing.T) {
} }
// Recovery of Q values is reversed. // Recovery of Q values is reversed.
if reverse(qs); !sort.Float64sAreSorted(qs) { if slices.Reverse(qs); !sort.Float64sAreSorted(qs) {
t.Errorf("Q values not monotonically increasing: %.5v", qs) t.Errorf("Q values not monotonically increasing: %.5v", qs)
} }
} }

View File

@@ -7,6 +7,7 @@ package community
import ( import (
"math" "math"
"reflect" "reflect"
"slices"
"sort" "sort"
"testing" "testing"
@@ -631,7 +632,7 @@ func testModularizeDirected(t *testing.T, test communityDirectedQTest, g graph.D
} }
// Recovery of Q values is reversed. // Recovery of Q values is reversed.
if reverse(qs); !sort.Float64sAreSorted(qs) { if slices.Reverse(qs); !sort.Float64sAreSorted(qs) {
t.Errorf("Q values not monotonically increasing: %.5v", qs) t.Errorf("Q values not monotonically increasing: %.5v", qs)
} }
} }

View File

@@ -214,12 +214,6 @@ type moveStructures struct {
tol float64 tol float64
} }
func reverse(f []float64) {
for i, j := 0, len(f)-1; i < j; i, j = i+1, j-1 {
f[i], f[j] = f[j], f[i]
}
}
func hasNegative(f []float64) bool { func hasNegative(f []float64) bool {
for _, v := range f { for _, v := range f {
if v < 0 { if v < 0 {

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"slices"
"sort" "sort"
"testing" "testing"
@@ -577,7 +578,7 @@ func TestLouvainMultiplex(t *testing.T) {
} }
// Recovery of Q values is reversed. // Recovery of Q values is reversed.
if reverse(qs); !sort.Float64sAreSorted(qs) { if slices.Reverse(qs); !sort.Float64sAreSorted(qs) {
t.Errorf("Q values not monotonically increasing: %.5v", qs) t.Errorf("Q values not monotonically increasing: %.5v", qs)
} }
} }

View File

@@ -7,6 +7,7 @@ package community
import ( import (
"math" "math"
"reflect" "reflect"
"slices"
"sort" "sort"
"testing" "testing"
@@ -694,7 +695,7 @@ func testModularizeUndirected(t *testing.T, test communityUndirectedQTest, g gra
} }
// Recovery of Q values is reversed. // Recovery of Q values is reversed.
if reverse(qs); !sort.Float64sAreSorted(qs) { if slices.Reverse(qs); !sort.Float64sAreSorted(qs) {
t.Errorf("Q values not monotonically increasing: %.5v", qs) t.Errorf("Q values not monotonically increasing: %.5v", qs)
} }
} }

View File

@@ -71,10 +71,3 @@ func LinesByIDs(n []graph.Line) {
return n[i].ID() < n[j].ID() return n[i].ID() < n[j].ID()
}) })
} }
// Reverse reverses the order of nodes.
func Reverse(nodes []graph.Node) {
for i, j := 0, len(nodes)-1; i < j; i, j = i+1, j-1 {
nodes[i], nodes[j] = nodes[j], nodes[i]
}
}

View File

@@ -6,12 +6,12 @@ package path
import ( import (
"math" "math"
"slices"
"golang.org/x/exp/rand" "golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats/scalar" "gonum.org/v1/gonum/floats/scalar"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/internal/ordered"
"gonum.org/v1/gonum/graph/internal/set" "gonum.org/v1/gonum/graph/internal/set"
"gonum.org/v1/gonum/mat" "gonum.org/v1/gonum/mat"
) )
@@ -184,7 +184,7 @@ func (p Shortest) To(vid int64) (path []graph.Node, weight float64) {
n-- n--
} }
} }
ordered.Reverse(path) slices.Reverse(path)
return path, math.Min(weight, p.dist[p.indexOf[vid]]) return path, math.Min(weight, p.dist[p.indexOf[vid]])
} }
@@ -397,7 +397,7 @@ func (p ShortestAlts) To(vid int64) (path []graph.Node, weight float64, unique b
weight = p.dist[p.indexOf[vid]] weight = p.dist[p.indexOf[vid]]
} }
ordered.Reverse(path) slices.Reverse(path)
return path, weight, unique return path, weight, unique
} }
@@ -460,9 +460,9 @@ func (p ShortestAlts) allTo(from, to int, seen []bool, path []graph.Node, fn fun
if path == nil { if path == nil {
return return
} }
ordered.Reverse(path) slices.Reverse(path)
fn(path) fn(path)
ordered.Reverse(path) slices.Reverse(path)
return return
} }
first := true first := true
@@ -676,7 +676,7 @@ func (p AllShortest) Between(uid, vid int64) (path []graph.Node, weight float64,
} }
} }
if !p.forward { if !p.forward {
ordered.Reverse(path) slices.Reverse(path)
} }
return path, weight, unique return path, weight, unique
@@ -765,11 +765,11 @@ func (p AllShortest) allBetween(from, to int, seen []bool, path []graph.Node, fn
return return
} }
if !p.forward { if !p.forward {
ordered.Reverse(path) slices.Reverse(path)
} }
fn(path) fn(path)
if !p.forward { if !p.forward {
ordered.Reverse(path) slices.Reverse(path)
} }
return return
} }

View File

@@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"slices"
"testing" "testing"
"golang.org/x/exp/rand" "golang.org/x/exp/rand"
@@ -181,7 +182,7 @@ func (p allShortest) allBetween(from, to int, seen []bool, path []graph.Node, pa
return paths return paths
} }
if !p.forward { if !p.forward {
ordered.Reverse(path) slices.Reverse(path)
} }
return append(paths, path) return append(paths, path)
} }

View File

@@ -128,7 +128,8 @@ func BenchmarkUndirectedCyclesInGnp_100_half(b *testing.B) {
func benchmarkSort(b *testing.B, g graph.Directed) { func benchmarkSort(b *testing.B, g graph.Directed) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := Sort(g); err != nil { _, err := Sort(g)
if err != nil {
b.FailNow() b.FailNow()
} }
} }
@@ -164,7 +165,8 @@ func BenchmarkSortPath_100000(b *testing.B) {
func benchmarkSortStabilized(b *testing.B, g graph.Directed) { func benchmarkSortStabilized(b *testing.B, g graph.Directed) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := SortStabilized(g, nil); err != nil { _, err := SortStabilized(g, nil)
if err != nil {
b.FailNow() b.FailNow()
} }
} }

View File

@@ -5,8 +5,9 @@
package topo package topo
import ( import (
"slices"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/internal/ordered"
"gonum.org/v1/gonum/graph/internal/set" "gonum.org/v1/gonum/graph/internal/set"
) )
@@ -15,7 +16,7 @@ import (
func DegeneracyOrdering(g graph.Undirected) (order []graph.Node, cores [][]graph.Node) { func DegeneracyOrdering(g graph.Undirected) (order []graph.Node, cores [][]graph.Node) {
order, offsets := degeneracyOrdering(g) order, offsets := degeneracyOrdering(g)
ordered.Reverse(order) slices.Reverse(order)
cores = make([][]graph.Node, len(offsets)) cores = make([][]graph.Node, len(offsets))
offset := len(order) offset := len(order)
for i, n := range offsets { for i, n := range offsets {
@@ -145,7 +146,7 @@ func BronKerbosch(g graph.Undirected) [][]graph.Node {
x := set.NewNodes() x := set.NewNodes()
var bk bronKerbosch var bk bronKerbosch
order, _ := degeneracyOrdering(g) order, _ := degeneracyOrdering(g)
ordered.Reverse(order) slices.Reverse(order)
for _, v := range order { for _, v := range order {
neighbours := graph.NodesOf(g.From(v.ID())) neighbours := graph.NodesOf(g.From(v.ID()))
nv := set.NewNodesSize(len(neighbours)) nv := set.NewNodesSize(len(neighbours))

View File

@@ -6,6 +6,7 @@ package topo
import ( import (
"reflect" "reflect"
"slices"
"testing" "testing"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
@@ -133,7 +134,7 @@ func canonicalise(c []graph.Node) []graph.Node {
c = append(c[idx:], c[:idx]...) c = append(c[idx:], c[:idx]...)
} }
if c[len(c)-1].ID() < c[1].ID() { if c[len(c)-1].ID() < c[1].ID() {
ordered.Reverse(c[1:]) slices.Reverse(c[1:])
} }
return c return c
} }

View File

@@ -6,6 +6,7 @@ package topo
import ( import (
"fmt" "fmt"
"slices"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/internal/ordered" "gonum.org/v1/gonum/graph/internal/ordered"
@@ -70,12 +71,10 @@ func sortedFrom(sccs [][]graph.Node, order func([]graph.Node)) ([]graph.Node, er
} }
var err error var err error
if sc != nil { if sc != nil {
for i, j := 0, len(sc)-1; i < j; i, j = i+1, j-1 { slices.Reverse(sc)
sc[i], sc[j] = sc[j], sc[i]
}
err = sc err = sc
} }
ordered.Reverse(sorted) slices.Reverse(sorted)
return sorted, err return sorted, err
} }
@@ -100,12 +99,12 @@ func tarjanSCCstabilized(g graph.Directed, order func([]graph.Node)) [][]graph.N
} }
} else { } else {
order(nodes) order(nodes)
ordered.Reverse(nodes) slices.Reverse(nodes)
succ = func(id int64) []graph.Node { succ = func(id int64) []graph.Node {
to := graph.NodesOf(g.From(id)) to := graph.NodesOf(g.From(id))
order(to) order(to)
ordered.Reverse(to) slices.Reverse(to)
return to return to
} }
} }