-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
52 lines (46 loc) · 922 Bytes
/
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
package main
import (
"image/color"
"math/rand"
)
func Clip(n int, m int, M int) int {
if n < m {
n = m
}
if n > M {
n = M
}
return n
}
func sqDiffUInt8(x, y uint8) uint64 {
d := uint64(x) - uint64(y)
return d * d
}
func RandomSampleGene(g []Gene, n int) []Gene {
idx := rand.Perm(n)
sample := make([]Gene, n)
for i, id := range idx {
sample[i] = g[id]
}
return sample
}
func RemoveGene(s []Gene, i int) []Gene {
s[i] = s[len(s)-1]
// We do not need to put s[i] at the end, as it will be discarded anyway
return s[:len(s)-1]
}
func DeepCopy(g []Gene) []Gene {
var tmpCenter *[2]int
var tmpColor *color.RGBA
genome := make([]Gene, len(g))
for i, gene := range g {
genome[i].Radius = gene.Radius
tmpCenter = new([2]int)
*tmpCenter = *&gene.Center
tmpColor = new(color.RGBA)
*tmpColor = *&gene.Color
genome[i].Center = *tmpCenter
genome[i].Color = *tmpColor
}
return genome
}