-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_int8.go
138 lines (120 loc) · 4.96 KB
/
t_int8.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package interval
// Code generated by generate/generate.go. DO NOT EDIT
import (
"fmt"
"strconv"
)
// WrapInt8 normalizes the value that "wraps around" within the [min,max) range.
// WrapInt8 always assumes that `min` is [inclusive, and `max` is exclusive).
func WrapInt8(min, max, value int8) int8 {
min, max = MinMaxInt8(min, max)
var maxLessMin = max - min
for value < min {
value += maxLessMin
}
for value >= max {
value -= maxLessMin
}
return value
//Original implementation is not working well for unsigned types:
//return ((value-min)%maxLessMin+maxLessMin)%maxLessMin + min
}
// ClampInt8 returns value capped to [min,max] range. Both ends of this range are inclusive.
func ClampInt8(min, max, value int8) int8 {
min, max = MinMaxInt8(min, max)
return MaxInt8(min, MinInt8(max, value))
}
// ValidateInt8 tests whether the value is within [(min,max)] range (depending on minExclusive and maxExclusive).
// It returns the value if it is within the range, otherwise returns 0 and error.
func ValidateInt8(min, max, value int8, minExclusive, maxExclusive bool) (int8, error) {
min, max, minExclusive, maxExclusive = MinMaxExclusiveInt8(min, max, minExclusive, maxExclusive)
if !TestInt8(min, max, value, minExclusive, maxExclusive) {
return 0, fmt.Errorf("%v is outside of range %v", value, ToStringInt8(min, max, minExclusive, maxExclusive))
}
return value, nil
}
// TestInt8 returns true if the value is within [(min,max)] range (depending on minExclusive and maxExclusive).
func TestInt8(min, max, value int8, minExclusive, maxExclusive bool) bool {
min, max, minExclusive, maxExclusive = MinMaxExclusiveInt8(min, max, minExclusive, maxExclusive)
return !(value < min || value > max || (maxExclusive && (value == max)) || (minExclusive && (value == min)))
}
// ToStringInt8 returns a string representation of the range using range notation
// (https://en.wikipedia.org/wiki/Interval_(mathematics)#Classification_of_intervals).
func ToStringInt8(min, max int8, minExclusive, maxExclusive bool) string {
min, max, minExclusive, maxExclusive = MinMaxExclusiveInt8(min, max, minExclusive, maxExclusive)
var minBracket = "["
if minExclusive {
minBracket = "("
}
var maxBracket = "]"
if maxExclusive {
maxBracket = ")"
}
return minBracket + strconv.FormatInt(int64(min), 10) + "," + strconv.FormatInt(int64(max), 10) + maxBracket
}
// MaxInt8 returns the bigger of two numbers.
func MaxInt8(x, y int8) int8 {
if x < y {
return y
}
return x
}
// MinInt8 returns the smaller of two numbers.
func MinInt8(x, y int8) int8 {
if x > y {
return y
}
return x
}
// MinMaxInt8 swaps min and max to assure that min < max.
// It is automatically called for all the rest of the functions
// that do not expect minExclusive or maxExclusive.
func MinMaxInt8(min, max int8) (int8, int8) {
if min > max {
return max, min
}
return min, max
}
// MinMaxExclusiveInt8 swaps min and max as well as minExclusive, maxExclusive correspondingly
// to assure that min < max together with the interval endings. It is automatically called for all
// the rest of the functions that expect minExclusive or maxExclusive.
func MinMaxExclusiveInt8(min, max int8, minExclusive, maxExclusive bool) (int8, int8, bool, bool) {
if min > max {
return max, min, maxExclusive, minExclusive
}
return min, max, minExclusive, maxExclusive
}
// RangeInt8 represents a struct containing all the fields defining a range.
type RangeInt8 struct {
min int8
max int8
minExclusive bool
maxExclusive bool
}
// NewRangeInt8 makes a new Range and returns its pointer. RangeInt8 can also be created with a RangeInt8{...} literal or new(RangeInt8).
func NewRangeInt8(min int8, max int8, minExclusive bool, maxExclusive bool) *RangeInt8 {
return &RangeInt8{min: min, max: max, minExclusive: minExclusive, maxExclusive: maxExclusive}
}
// Wrap does not obey minExclusive and maxExclusive and always assumes [min,max) range.
func (v RangeInt8) Wrap(value int8) int8 {
return WrapInt8(v.min, v.max, value)
}
// Clamp does not obey minExclusive and maxExclusive and always assumes [min,max] range.
func (v RangeInt8) Clamp(value int8) int8 {
return ClampInt8(v.min, v.max, value)
}
// Validate tests whether the value is within [(min,max)] range (depending on minExclusive and maxExclusive).
// It returns the value if it is within the range, otherwise returns 0 and error.
func (v RangeInt8) Validate(value int8) (int8, error) {
return ValidateInt8(v.min, v.max, value, v.minExclusive, v.maxExclusive)
}
// Test returns true if the value is within [(min,max)] range (depending on minExclusive and maxExclusive).
func (v RangeInt8) Test(value int8) bool {
return TestInt8(v.min, v.max, value, v.minExclusive, v.maxExclusive)
}
// String returns a string representation of the range using range notation
// (https://en.wikipedia.org/wiki/Interval_(mathematics)#Classification_of_intervals).
// String implements Stringer interface.
func (v RangeInt8) String() string {
return ToStringInt8(v.min, v.max, v.minExclusive, v.maxExclusive)
}