aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/slices/shuffle.go
blob: 5df9b33c3cffb0fc89b06652e88f39216983b243 (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package slices

import (
	"math/rand"
)

// Shuffle shuffles values in slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
func Shuffle[E any](a []E, src rand.Source) []E {
	if len(a) < 2 {
		return a
	}
	shuffle(src)(len(a), func(i, j int) {
		a[i], a[j] = a[j], a[i]
	})
	return a
}

// ShuffleStrings shuffles values in string slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleStrings = Shuffle[string]

// ShuffleInts shuffles values in int slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleInts = Shuffle[int]

// ShuffleInt8s shuffles values in int8 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleInt8s = Shuffle[int8]

// ShuffleInt16s shuffles values in int16 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleInt16s = Shuffle[int16]

// ShuffleInt32s shuffles values in int32 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleInt32s = Shuffle[int32]

// ShuffleInt64s shuffles values in int64 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleInt64s = Shuffle[int64]

// ShuffleUints shuffles values in uint slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleUints = Shuffle[uint]

// ShuffleUint8s shuffles values in uint8 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleUint8s = Shuffle[uint8]

// ShuffleUint16s shuffles values in uint16 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleUint16s = Shuffle[uint16]

// ShuffleUint32s shuffles values in uint32 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleUint32s = Shuffle[uint32]

// ShuffleUint64s shuffles values in uint64 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleUint64s = Shuffle[uint64]

// ShuffleFloat32s shuffles values in float32 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleFloat32s = Shuffle[float32]

// ShuffleFloat64s shuffles values in float64 slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleFloat64s = Shuffle[float64]

// ShuffleBools shuffles values in bool slice using given or pseudo-random source.
// It will alter original non-empty slice, consider copy it beforehand.
// Deprecated: use Shuffle instead.
var ShuffleBools = Shuffle[bool]

func shuffle(src rand.Source) func(n int, swap func(i, j int)) {
	shuf := rand.Shuffle
	if src != nil {
		shuf = rand.New(src).Shuffle
	}
	return shuf
}