-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval_with_value.go
65 lines (58 loc) · 1.4 KB
/
interval_with_value.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
package main
import (
"golang.org/x/exp/constraints"
)
type IntervalWithValue[T constraints.Ordered] struct {
init T
end T
value any
}
func GenerateIntervalWithValue[T constraints.Ordered](init, end T, value any) (IntervalWithValue[T], error) {
interval := IntervalWithValue[T]{init: init, end: end, value: value}
if err := interval.validate(); err != nil {
return IntervalWithValue[T]{}, err
}
return interval, nil
}
func (receiver *IntervalWithValue[T]) validate() error {
if receiver.init > receiver.end {
return errorInvalidInterval[T]{receiver.init, receiver.end}
}
return nil
}
func (receiver *IntervalWithValue[T]) Compare(other IntervalWithValue[T]) int {
if receiver.init == other.init && receiver.end == other.end {
return 0
}
if receiver.init > other.init {
return 1
}
if receiver.end < other.end {
return -1
}
return 0
}
func (receiver *IntervalWithValue[T]) IsOverlapped(other IntervalWithValue[T]) bool {
return !(receiver.init >= other.end || receiver.end <= other.init)
}
func (receiver *IntervalWithValue[T]) Evaluate(toEvaluate T, includeInit, includeEnd bool) string {
if includeInit {
if toEvaluate < receiver.init {
return "Under"
}
} else {
if toEvaluate <= receiver.init {
return "Under"
}
}
if includeEnd {
if toEvaluate > receiver.end {
return "Upper"
}
} else {
if toEvaluate >= receiver.end {
return "Upper"
}
}
return "Contained"
}