-
Notifications
You must be signed in to change notification settings - Fork 1
/
attr.go
167 lines (144 loc) · 3.07 KB
/
attr.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package test
import (
"fmt"
)
type Attr[A any] interface {
Add(other A)
Adds(other A, scale float32)
Scale(amount float32)
Get() A
}
type BaseAttr[A any] struct{}
func (a *BaseAttr[A]) Adds(other A, scale float32) {}
func (a *BaseAttr[A]) Add(other A) { a.Adds(other, 1) }
func (a *BaseAttr[A]) Scale(amount float32) { a.Adds(a.Get(), amount-1) }
func (a BaseAttr[A]) Get() A { var b A; return b }
type Vec2 struct {
BaseAttr[Vec2]
X float32
Y float32
}
var _ Attr[Vec2] = &Vec2{}
func (v *Vec2) Adds(other Vec2, scale float32) {
v.X += other.X * scale
v.Y += other.Y * scale
}
func (v Vec2) Get() Vec2 {
return v
}
func (a Vec2) String() string {
return fmt.Sprintf("{%v, %v}", a.X, a.Y)
}
/*
type History[V any] struct {
Values []TimedValue[V]
Min int
Max int
}
type TimedValue[V any] struct {
Value V
Time int64
}
func NewHistory[V any](capacity int) *History[V] {
return &History{
Values: make([]TimedValue[V], capacity),
Min: 0,
Max: 0,
}
}
func (h History[V]) wrapIndex(i int) int {
n := len(h.Values)
return (i + n) % n
}
func (h History[V]) maxIndex() int {
return h.wrapIndex(h.Max - 1)
}
func (h History[V]) beforeIndex(time int64) int {
if h.Empty() || time < h.Values[h.Min].Time {
return -1
}
max := h.maxIndex()
if time >= h.Values[max].Time {
return -1
}
before := h.wrapIndex(h.Min - 1)
for i := max; i != before; i = h.wrapIndex(i - 1) {
if h.Values[i].Time <= time {
return i
}
}
return -1
}
func (h History[V]) Empty() bool {
return h.Max == h.Min
}
func (h History[V]) Duration() int64 {
if h.Empty() {
return 0
}
return h.Values[h.maxIndex()].Time - h.Values[h.Min].Time
}
func (h History[V]) Before(time int64) *TimedValue {
i := h.beforeIndex(time)
if i == -1 {
return nil
}
return &h.Values[i]
}
func (h History[V]) Around(time int64) (*TimedValue, *TimedValue) {
i := h.beforeIndex(time)
n := h.wrapIndex(i + 1)
if i == -1 || n == h.Max {
return nil, nil
}
return &h.Values[i], &h.Values[n]
}
func (h History[V]) Interpolate(time int64, inter func(a V, b V, delta float32) V) *V {
before, after := h.Around(time)
if before == nil || after == nil {
return nil
}
delta := float32(time-before.Time) / float32(after.Time-before.Time)
interpolated := inter(before.Value, after.Value, delta)
return &interpolated
}
func (h History[V]) Closest(time int64) *TimedValue {
var value V
if h.Min == h.Max {
return nil, -1
}
if time <= h.Times[h.Min] {
return h.Values[h.Min]
}
max := h.maxIndex()
if time >= h.Times[max] {
return h.Values[max]
}
before, after := h.Around(time)
if before == nil || after == nil {
return value, -1
}
if time-before.Time < after.Time-time {
return before
} else {
return after
}
}
func (h *History[V]) Clear() {
h.Max = 0
h.Min = 0
}
func (h *History[V]) Add(value V, time int64) bool {
if h.Empty() || time > h.Values[h.maxIndex()].Time {
i := h.Max
h.Max = h.wrapIndex(i + 1)
tv := &h.Values[i]
tv.Time = time
tv.Value = value
if h.Min == i {
h.Min = h.Max
}
}
b := h.beforeIndex(time)
}
*/