forked from tuneinsight/lattigo
/
utils.go
299 lines (266 loc) · 6.8 KB
/
utils.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package utils
import (
"crypto/rand"
"encoding/binary"
"math/big"
"math/bits"
)
// RandUint64 return a random value between 0 and 0xFFFFFFFFFFFFFFFF.
func RandUint64() uint64 {
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
if _, err := rand.Read(b); err != nil {
panic(err)
}
return binary.BigEndian.Uint64(b)
}
// RandFloat64 returns a random float between min and max.
func RandFloat64(min, max float64) float64 {
b := []byte{0, 0, 0, 0, 0, 0, 0, 0}
if _, err := rand.Read(b); err != nil {
panic(err)
}
f := float64(binary.BigEndian.Uint64(b)) / 1.8446744073709552e+19
return min + f*(max-min)
}
// RandComplex128 returns a random complex with the real and imaginary part between min and max.
func RandComplex128(min, max float64) complex128 {
return complex(RandFloat64(min, max), RandFloat64(min, max))
}
// RandInt generates a random Int in [0, max-1].
func RandInt(max *big.Int) (n *big.Int) {
var err error
if n, err = rand.Int(rand.Reader, max); err != nil {
panic(err)
}
return
}
// EqualSliceUint64 checks the equality between two uint64 slices.
func EqualSliceUint64(a, b []uint64) (v bool) {
v = true
for i := range a {
v = v && (a[i] == b[i])
}
return
}
// EqualSliceInt64 checks the equality between two int64 slices.
func EqualSliceInt64(a, b []int64) (v bool) {
v = true
for i := range a {
v = v && (a[i] == b[i])
}
return
}
// EqualSliceUint8 checks the equality between two uint8 slices.
func EqualSliceUint8(a, b []uint8) (v bool) {
v = true
for i := range a {
v = v && (a[i] == b[i])
}
return
}
// IsInSliceUint64 checks if x is in slice.
func IsInSliceUint64(x uint64, slice []uint64) (v bool) {
for i := range slice {
v = v || (slice[i] == x)
}
return
}
// IsInSliceInt checks if x is in slice.
func IsInSliceInt(x int, slice []int) (v bool) {
for i := range slice {
v = v || (slice[i] == x)
}
return
}
// MinUint64 returns the minimum value of the input of uint64 values.
func MinUint64(a, b uint64) (r uint64) {
if a <= b {
return a
}
return b
}
// MinInt returns the minimum value of the input of int values.
func MinInt(a, b int) (r int) {
if a <= b {
return a
}
return b
}
// MaxUint64 returns the maximum value of the input slice of uint64 values.
func MaxUint64(a, b uint64) (r uint64) {
if a >= b {
return a
}
return b
}
// MaxInt returns the maximum value of the input of int values.
func MaxInt(a, b int) (r int) {
if a >= b {
return a
}
return b
}
// MaxFloat64 returns the maximum value of the input slice of float64 values.
func MaxFloat64(a, b float64) (r float64) {
if a >= b {
return a
}
return b
}
// MaxSliceUint64 returns the maximum value of the input slice of uint64 values.
func MaxSliceUint64(slice []uint64) (max uint64) {
for i := range slice {
max = MaxUint64(max, slice[i])
}
return
}
// BitReverse64 returns the bit-reverse value of the input value, within a context of 2^bitLen.
func BitReverse64(index, bitLen uint64) uint64 {
return bits.Reverse64(index) >> (64 - bitLen)
}
// HammingWeight64 returns the hammingweight if the input value.
func HammingWeight64(x uint64) uint64 {
x -= (x >> 1) & 0x5555555555555555
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
return ((x * 0x0101010101010101) & 0xffffffffffffffff) >> 56
}
// AllDistinct returns true if all elements in s are distinct, and false otherwise.
func AllDistinct(s []uint64) bool {
m := make(map[uint64]struct{}, len(s))
for _, si := range s {
if _, exists := m[si]; exists {
return false
}
m[si] = struct{}{}
}
return true
}
// GCD computes the greatest common divisor gcd(a,b) for a,b uint64 variables.
func GCD(a, b uint64) uint64 {
if a == 0 || b == 0 {
return 0
}
for b != 0 {
a, b = b, a%b
}
return a
}
// RotateUint64Slice returns a new slice corresponding to s rotated by k positions to the left.
func RotateUint64Slice(s []uint64, k int) []uint64 {
ret := make([]uint64, len(s))
RotateUint64SliceAllocFree(s, k, ret)
return ret
}
// RotateUint64SliceAllocFree rotates slice s by k positions to the left and writes the result in sout.
// without allocating new memory.
func RotateUint64SliceAllocFree(s []uint64, k int, sout []uint64) {
if len(s) != len(sout) {
panic("cannot RotateUint64SliceAllocFree: s and sout of different lengths")
}
if len(s) == 0 {
return
}
k = k % len(s)
if k < 0 {
k = k + len(s)
}
if &s[0] == &sout[0] { // checks if the two slice share the same backing array
RotateUint64SliceInPlace(s, k)
return
}
copy(sout[:len(s)-k], s[k:])
copy(sout[len(s)-k:], s[:k])
}
// RotateUint64SliceInPlace rotates slice s in place by k positions to the left.
func RotateUint64SliceInPlace(s []uint64, k int) {
n := len(s)
k = k % len(s)
if k < 0 {
k = k + len(s)
}
gcd := GCD(uint64(k), uint64(n))
for i := 0; i < int(gcd); i++ {
tmp := s[i]
j := i
for {
x := j + k
if x >= n {
x = x - n
}
if x == i {
break
}
s[j] = s[x]
j = x
}
s[j] = tmp
}
}
// RotateInt64Slice returns a new slice corresponding to s rotated by k positions to the left.
func RotateInt64Slice(s []int64, k int) []int64 {
if k == 0 || len(s) == 0 {
return s
}
r := k % len(s)
if r < 0 {
r = r + len(s)
}
ret := make([]int64, len(s))
copy(ret[:len(s)-r], s[r:])
copy(ret[len(s)-r:], s[:r])
return ret
}
// RotateUint64Slots returns a new slice corresponding to s where each half of the slice
// have been rotated by k positions to the left.
func RotateUint64Slots(s []uint64, k int) []uint64 {
ret := make([]uint64, len(s))
slots := len(s) >> 1
copy(ret[:slots], RotateUint64Slice(s[:slots], k))
copy(ret[slots:], RotateUint64Slice(s[slots:], k))
return ret
}
// RotateComplex128Slice returns a new slice corresponding to s rotated by k positions to the left.
func RotateComplex128Slice(s []complex128, k int) []complex128 {
if k == 0 || len(s) == 0 {
return s
}
r := k % len(s)
if r < 0 {
r = r + len(s)
}
ret := make([]complex128, len(s))
copy(ret[:len(s)-r], s[r:])
copy(ret[len(s)-r:], s[:r])
return ret
}
// RotateFloat64Slice returns a new slice corresponding to s rotated by k positions to the left.
func RotateFloat64Slice(s []float64, k int) []float64 {
if k == 0 || len(s) == 0 {
return s
}
r := k % len(s)
if r < 0 {
r = r + len(s)
}
ret := make([]float64, len(s))
copy(ret[:len(s)-r], s[r:])
copy(ret[len(s)-r:], s[:r])
return ret
}
// RotateSlice takes as input an interface slice and returns a new interface slice
// corresponding to s rotated by k positions to the left.
// s.(type) can be either []complex128, []float64, []uint64 or []int64.
func RotateSlice(s interface{}, k int) interface{} {
switch el := s.(type) {
case []complex128:
return RotateComplex128Slice(el, k)
case []float64:
return RotateFloat64Slice(el, k)
case []uint64:
return RotateUint64Slice(el, k)
case []int64:
return RotateInt64Slice(el, k)
}
return nil
}