-
Notifications
You must be signed in to change notification settings - Fork 6
/
construct.go
82 lines (68 loc) · 1.39 KB
/
construct.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
package functions
import "golang.org/x/exp/constraints"
type Number interface {
constraints.Float | constraints.Integer
}
func Zeros_Go[T constraints.Float](x []T, n int) {
Repeat_Go(x, 0, n)
}
func Ones_Go[T constraints.Float](x []T, n int) {
Repeat_Go(x, 1, n)
}
func Repeat_Go[T constraints.Float](x []T, a T, n int) {
for i := 0; i < n; i++ {
x[i] = a
}
}
func Range_Go[T constraints.Float](x []T, a T, n int) {
for i := 0; i < n; i++ {
x[i] = a
a += 1
}
}
func Gather_Go[T constraints.Float](dst, x []T, idx []int) {
for i := 0; i < len(idx); i++ {
dst[i] = x[idx[i]]
}
}
func Scatter_Go[T constraints.Float](dst, x []T, idx []int) {
for i := 0; i < len(idx); i++ {
dst[idx[i]] = x[i]
}
}
func Clone_Go[T constraints.Float](x, y []T) {
copy(x, y)
}
func Clone_Go_Loop[T constraints.Float](x, y []T) {
for i := 0; i < len(x); i++ {
x[i] = y[i]
}
}
func FromNumber_Go[T constraints.Float, F Number](x []T, y []F) {
for i := 0; i < len(y); i++ {
x[i] = T(y[i])
}
}
func ToNumber_Go[T Number, F constraints.Float](x []T, y []F) {
for i := 0; i < len(y); i++ {
x[i] = T(y[i])
}
}
func FromBool_Go[T constraints.Float](x []T, y []bool) {
for i := 0; i < len(y); i++ {
if y[i] {
x[i] = 1
} else {
x[i] = 0
}
}
}
func ToBool_Go[F constraints.Float](x []bool, y []F) {
for i := 0; i < len(y); i++ {
if y[i] != 0 {
x[i] = true
} else {
x[i] = false
}
}
}