File: order.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (60 lines) | stat: -rw-r--r-- 1,497 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright ©2024 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package order

import (
	"cmp"
	"slices"
	"sort"

	"gonum.org/v1/gonum/graph"
)

// ByID sorts a slice of graph.Node by ID.
func ByID[S ~[]E, E graph.Node](n S) {
	sort.Slice(n, func(i, j int) bool { return n[i].ID() < n[j].ID() })
}

// BySliceValues sorts a slice of []cmp.Ordered lexically by the values of
// the []cmp.Ordered.
func BySliceValues[S ~[]E, E cmp.Ordered](c []S) {
	slices.SortFunc(c, func(a, b S) int {
		l := min(len(a), len(b))
		for k, v := range a[:l] {
			if n := cmp.Compare(v, b[k]); n != 0 {
				return n
			}
		}
		return cmp.Compare(len(a), len(b))
	})
}

// BySliceIDs sorts a slice of []graph.Node lexically by the IDs of the
// []graph.Node.
func BySliceIDs(c [][]graph.Node) {
	slices.SortFunc(c, func(a, b []graph.Node) int {
		l := min(len(a), len(b))
		for k, v := range a[:l] {
			if n := cmp.Compare(v.ID(), b[k].ID()); n != 0 {
				return n
			}
		}
		return cmp.Compare(len(a), len(b))
	})
}

// LinesByIDs sort a slice of graph.LinesByIDs lexically by the From IDs,
// then by the To IDs, finally by the Line IDs.
func LinesByIDs(n []graph.Line) {
	slices.SortFunc(n, func(a, b graph.Line) int {
		if n := cmp.Compare(a.From().ID(), b.From().ID()); n != 0 {
			return n
		}
		if n := cmp.Compare(a.To().ID(), b.To().ID()); n != 0 {
			return n
		}
		return cmp.Compare(a.ID(), b.ID())
	})
}