-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscale.go
57 lines (46 loc) · 1.09 KB
/
scale.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
package measure
import (
"encoding/json"
)
// Scale the cost of some other measure by a constant.
func Scale(m ByIndex, constant float64) ByIndex {
return scale{m: m, constant: constant}
}
type scale struct {
m ByIndex
constant float64
}
func (s scale) Cost(from, to int) float64 {
return s.m.Cost(from, to) * s.constant
}
func (s scale) Triangular() bool {
return IsTriangular(s.m)
}
func (s scale) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"measure": s.m,
"type": "scale",
"scale": s.constant,
})
}
// ScaleByPoint scales an underlying measure by a constant.
func ScaleByPoint(m ByPoint, constant float64) ByPoint {
return scaleByPoint{m: m, constant: constant}
}
type scaleByPoint struct {
m ByPoint
constant float64
}
func (s scaleByPoint) Cost(p1, p2 Point) float64 {
return s.m.Cost(p1, p2) * s.constant
}
func (s scaleByPoint) Triangular() bool {
return IsTriangular(s.m)
}
func (s scaleByPoint) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"measure": s.m,
"type": "scale",
"scale": s.constant,
})
}