-
Notifications
You must be signed in to change notification settings - Fork 11
/
argsort.go
43 lines (36 loc) · 1.01 KB
/
argsort.go
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
// https://github.com/gonum/floats/blob/master/floats.go
package gomxnet
import (
"sort"
)
// argsort is a helper that implements sort.Interface, as used by
// Argsort.
type argsort struct {
s []float32
inds []int
}
func (a argsort) Len() int {
return len(a.s)
}
func (a argsort) Less(i, j int) bool {
return a.s[i] > a.s[j]
}
func (a argsort) Swap(i, j int) {
a.s[i], a.s[j] = a.s[j], a.s[i]
a.inds[i], a.inds[j] = a.inds[j], a.inds[i]
}
// Argsort sorts the elements of s while tracking their original order.
// At the conclusion of Argsort, s will contain the original elements of s
// but sorted in increasing order, and inds will contain the original position
// of the elements in the slice such that dst[i] = origDst[inds[i]].
// It panics if the lengths of dst and inds do not match.
func Argsort(dst []float32, inds []int) {
if len(dst) != len(inds) {
panic("floats: length of inds does not match length of slice")
}
for i := range dst {
inds[i] = i
}
a := argsort{s: dst, inds: inds}
sort.Sort(a)
}