-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel_expression_binary.go
97 lines (82 loc) · 1.95 KB
/
model_expression_binary.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// © 2019-present nextmv.io inc
package nextroute
import (
"fmt"
)
// BinaryFunction is a function that takes two float64 values and returns a
// float64 value.
type BinaryFunction func(float64, float64) float64
// BinaryExpression is an expression that takes two expressions as input and
// returns a value.
type BinaryExpression interface {
ModelExpression
// Left returns the left expression.
Left() ModelExpression
// Right returns the right expression.
Right() ModelExpression
}
// NewOperatorExpression returns a new BinaryExpression that uses the given
// operator function.
func NewOperatorExpression(
name string,
left ModelExpression,
right ModelExpression,
operator BinaryFunction,
) BinaryExpression {
return &binaryExpression{
index: NewModelExpressionIndex(),
left: left,
right: right,
operator: operator,
name: name,
}
}
type binaryExpression struct {
left ModelExpression
right ModelExpression
operator BinaryFunction
name string
index int
}
func (b *binaryExpression) HasNegativeValues() bool {
return b.left.HasNegativeValues() || b.right.HasNegativeValues()
}
func (b *binaryExpression) HasPositiveValues() bool {
return b.left.HasPositiveValues() || b.right.HasPositiveValues()
}
func (b *binaryExpression) Index() int {
return b.index
}
func (b *binaryExpression) String() string {
return fmt.Sprintf("Binary[%v] '%v' left: %v, right: %v",
b.index,
b.name,
b.left,
b.right,
)
}
func (b *binaryExpression) Name() string {
return fmt.Sprintf("%s(%s,%s)",
b.name,
b.left.Name(),
b.right.Name(),
)
}
func (b *binaryExpression) SetName(n string) {
b.name = n
}
func (b *binaryExpression) Left() ModelExpression {
return b.left
}
func (b *binaryExpression) Right() ModelExpression {
return b.right
}
func (b *binaryExpression) Value(
vehicle ModelVehicleType,
from, to ModelStop,
) float64 {
return b.operator(
b.left.Value(vehicle, from, to),
b.right.Value(vehicle, from, to),
)
}