-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathby_index.go
52 lines (46 loc) · 1.06 KB
/
by_index.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 measure
import (
"encoding/json"
"fmt"
)
// Indexed creates a ByIndex measure from the given ByPoint measure
// and wrapping the provided points.
func Indexed(m ByPoint, points []Point) ByIndex {
return byIndex{
points: points,
ByPoint: m,
}
}
type byIndex struct {
ByPoint
points []Point
}
func (m byIndex) Cost(from, to int) float64 {
if from < 0 {
panic(
fmt.Sprintf("byIndex.Cost: invalid `from` index %d - must be >= 0", from),
)
}
if to < 0 {
panic(fmt.Sprintf("byIndex.Cost: invalid `to` index %d - must be >= 0", to))
}
if from >= len(m.points) {
panic(fmt.Sprintf(
"byIndex.Cost: invalid `from` index %d - must be < number of points (%d)",
from, len(m.points),
))
}
if to >= len(m.points) {
panic(fmt.Sprintf(
"byIndex.Cost: invalid `to` index %d - must be < number of points (%d)",
to, len(m.points)),
)
}
return m.ByPoint.Cost(m.points[from], m.points[to])
}
func (m byIndex) Triangular() bool {
return IsTriangular(m.ByPoint)
}
func (m byIndex) MarshalJSON() ([]byte, error) {
return json.Marshal(m.ByPoint)
}