-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuint16Slices.go
158 lines (140 loc) · 3.91 KB
/
uint16Slices.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Code generated by yasupGen; DO NOT EDIT.
package yasup
import (
crypto "crypto/rand"
"math/big"
"math/rand"
)
var zeroValueUint16 uint16
//Uint16Insert will append elem at the position i. Might return ErrIndexOutOfBounds.
func Uint16Insert(sl *[]uint16, elem uint16, i int) error {
if i < 0 || i > len(*sl) {
return ErrIndexOutOfBounds
}
*sl = append(*sl, elem)
copy((*sl)[i+1:], (*sl)[i:])
(*sl)[i] = elem
return nil
}
//Uint16Delete delete the element at the position i. Might return ErrIndexOutOfBounds.
func Uint16Delete(sl *[]uint16, i int) error {
if i < 0 || i >= len(*sl) {
return ErrIndexOutOfBounds
}
*sl = append((*sl)[:i], (*sl)[i+1:]...)
return nil
}
//Uint16Contains will return true if elem is present in the slice and false otherwise.
func Uint16Contains(sl []uint16, elem uint16) bool {
for i := range sl {
if sl[i] == elem {
return true
}
}
return false
}
//Uint16Index returns the index of the first instance of elem, or -1 if elem is not present.
func Uint16Index(sl []uint16, elem uint16) int {
for i := range sl {
if sl[i] == elem {
return i
}
}
return -1
}
//Uint16LastIndex returns the index of the last instance of elem in the slice, or -1 if elem is not present.
func Uint16LastIndex(sl []uint16, elem uint16) int {
for i := len(sl) - 1; i >= 0; i-- {
if sl[i] == elem {
return i
}
}
return -1
}
//Uint16Count will return an int representing the amount of times that elem is present in the slice.
func Uint16Count(sl []uint16, elem uint16) int {
var n int
for i := range sl {
if sl[i] == elem {
n++
}
}
return n
}
//Uint16Push is equivalent to Uint16Insert with index len(*sl).
func Uint16Push(sl *[]uint16, elem uint16) {
Uint16Insert(sl, elem, len(*sl))
}
//Uint16FrontPush is equivalent to Uint16Insert with index 0.
func Uint16FrontPush(sl *[]uint16, elem uint16) {
Uint16Insert(sl, elem, 0)
}
//Uint16Pop is equivalent to getting and removing the last element of the slice. Might return ErrEmptySlice.
func Uint16Pop(sl *[]uint16) (uint16, error) {
if len(*sl) == 0 {
return zeroValueUint16, ErrEmptySlice
}
last := len(*sl) - 1
ret := (*sl)[last]
Uint16Delete(sl, last)
return ret, nil
}
//Uint16Pop is equivalent to getting and removing the first element of the slice. Might return ErrEmptySlice.
func Uint16FrontPop(sl *[]uint16) (uint16, error) {
if len(*sl) == 0 {
return zeroValueUint16, ErrEmptySlice
}
ret := (*sl)[0]
Uint16Delete(sl, 0)
return ret, nil
}
//Uint16Replace modifies the slice with the first n non-overlapping instances of old replaced by new. If n equals -1, there is no limit on the number of replacements.
func Uint16Replace(sl []uint16, old, new uint16, n int) (replacements int) {
left := n
for i := range sl {
if left == 0 {
break // no replacements left
}
if sl[i] == old {
sl[i] = new
left--
}
}
return n - left
}
//Uint16ReplaceAll is equivalent to Uint16Replace with n = -1.
func Uint16ReplaceAll(sl []uint16, old, new uint16) (replacements int) {
return Uint16Replace(sl, old, new, -1)
}
//Uint16Equals compares two uint16 slices. Returns true if their elements are equal.
func Uint16Equals(a, b []uint16) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
//Uint16FastShuffle will randomly swap the uint16 elements of a slice using math/rand (fast but not cryptographycally secure).
func Uint16FastShuffle(sp []uint16) {
rand.Shuffle(len(sp), func(i, j int) {
sp[i], sp[j] = sp[j], sp[i]
})
}
//Uint16SecureShuffle will randomly swap the uint16 elements of a slice using crypto/rand (resource intensive but cryptographically secure).
func Uint16SecureShuffle(sp []uint16) error {
var i int64
size := int64(len(sp)) - 1
for i = 0; i < size+1; i++ {
bigRandI, err := crypto.Int(crypto.Reader, big.NewInt(size))
if err != nil {
return err
}
randI := bigRandI.Int64()
sp[size-i], sp[randI] = sp[randI], sp[size-i]
}
return nil
}