-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmaxvalue.go
108 lines (88 loc) · 1.96 KB
/
minmaxvalue.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
package containers
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"golang.org/x/exp/constraints"
)
type MinMaxValue[T constraints.Ordered] struct {
def T
minValue T
curValue T
maxValue T
}
func NewMinMaxValue[T constraints.Ordered](v T) *MinMaxValue[T] {
return &MinMaxValue[T]{
minValue: v,
curValue: v,
maxValue: v,
}
}
func (p *MinMaxValue[T]) Reset() {
p.minValue = p.def
p.curValue = p.def
p.maxValue = p.def
}
func (p *MinMaxValue[T]) isUntouched() bool {
return p.minValue == p.def && p.curValue == p.def && p.maxValue == p.def
}
func (p *MinMaxValue[T]) Update(v T) {
if p.isUntouched() {
p.minValue = v
p.curValue = v
p.maxValue = v
}
p.minValue = Min(p.minValue, v)
p.curValue = v
p.maxValue = Max(p.maxValue, v)
}
func (p *MinMaxValue[T]) Min() T {
return p.minValue
}
func (p *MinMaxValue[T]) Cur() T {
return p.curValue
}
func (p *MinMaxValue[T]) Max() T {
return p.maxValue
}
func (p *MinMaxValue[T]) Format(format string, a ...any) string {
return fmt.Sprintf(format, append([]any{p.Min(), p.Cur(), p.Max()}, a...)...)
}
func (p *MinMaxValue[T]) WriteTo(w io.Writer) error {
enc := gob.NewEncoder(w)
if err := enc.Encode(p.minValue); err != nil {
return err
}
if err := enc.Encode(p.curValue); err != nil {
return err
}
return enc.Encode(p.maxValue)
}
func (p *MinMaxValue[T]) GobEncode() ([]byte, error) {
buffer := bytes.NewBuffer(nil)
if err := p.WriteTo(buffer); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func (p *MinMaxValue[T]) ReadFrom(r io.Reader) error {
dec := gob.NewDecoder(r)
if err := dec.Decode(&p.minValue); err != nil {
return err
}
if err := dec.Decode(&p.curValue); err != nil {
return err
}
return dec.Decode(&p.maxValue)
}
func (p *MinMaxValue[T]) GobDecode(v []byte) error {
return p.ReadFrom(bytes.NewBuffer(v))
}
func (p *MinMaxValue[T]) ToMap() map[string]interface{} {
return map[string]interface{}{
"min": p.minValue,
"max": p.maxValue,
"cur": p.curValue,
}
}