-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenes.go
59 lines (49 loc) · 1.25 KB
/
genes.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
package genes
import (
"errors"
"fmt"
"github.com/soypat/mu8"
)
// Gene errors.
var (
ErrMismatchedGeneType = errors.New("mu8.Gene argument in Splice or CloneFrom not same type as receiver")
errStartOutOfBounds = errors.New("start value should be contained within bounds [min,max] for Contrained types")
errBadConstraints = errors.New("min should be less than max for Constrained types and not equal for int gene types")
)
// Helper function for casting interfaces.
func castGene[T mu8.Gene](gene mu8.Gene) T {
g, ok := gene.(T)
if !ok {
panic(fmt.Errorf("%w: cast %T->%T failed", ErrMismatchedGeneType, gene, g))
}
return g
}
// Compile time check of internal interface implementation
type gene[T any] interface {
mu8.Gene
fmt.Stringer
Value() T
SetValue(v T)
}
// Compile-time checks of interface implementation.
var (
_ gene[float64] = (*ConstrainedFloat)(nil)
_ gene[float64] = (*NormalDistribution)(nil)
_ gene[float64] = (*ConstrainedNormalDistr)(nil)
_ gene[int] = (*ConstrainedInt)(nil)
)
type integer interface {
int | int64 | int32 | int16 | int8 | uint | uint64 | uint32 | uint16 | uint8
}
func max[T integer](a, b T) T {
if a > b {
return a
}
return b
}
func min[T integer](a, b T) T {
if a < b {
return a
}
return b
}