-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.go
30 lines (25 loc) · 953 Bytes
/
sort.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
package sparseset
import (
"golang.org/x/exp/slices"
)
// SortStableFunc sorts the Set according to 'compare'. The 'compare' function
// receives the 'left-hand-side' ID and Value and the 'right-hand-side' ID and
// Value. The 'compare' function should call methods on the Set (e.g.,
// Set.Get()) since SortStableFunc modifies the Set.
func SortStableFunc[T any](set *Set[T], compare func(int, *T, int, *T) int) {
slices.SortStableFunc(set.dense, func(i, j int) int {
iPos := set.index.Get(i)
jPos := set.index.Get(j)
return compare(i, &set.store[iPos], j, &set.store[jPos])
})
for i := 0; i < len(set.dense); i++ {
for pos, next := i, set.index.Get(set.dense[i]); pos != next; {
pos1 := set.index.Get(set.dense[pos])
pos2 := set.index.Get(set.dense[next])
set.store[pos1], set.store[pos2] = set.store[pos2], set.store[pos1]
set.index.Set(set.dense[pos], pos)
pos = next
next = set.index.Get(set.dense[pos])
}
}
}