-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimedList.ck
executable file
·77 lines (59 loc) · 1.82 KB
/
TimedList.ck
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
public class TimedList {
dur timelist[];
float valuelist[];
0 => int interp;
time then;
fun int size() {
return valuelist.size();
}
fun void append (dur timy,float value) {
timelist.size(size()+1);
valuelist.size(size()+1);
timy => timelist[size()-1];
value => valuelist[size()-1];
}
fun void appenddur (dur dury,float value) {
timelist.size(size()+1);
valuelist.size(size()+1);
timelist[size()-2] + dury => timelist[size()-1];
value => valuelist[size()-1];
}
fun void setall(dur timy[],float values[]) {
values @=> valuelist;
timy @=> timelist;
now => then;
}
fun void setall(dur timy[],float values[]) {
timelist.size(timy.size());
0::second => dur current;
for (int i;i<timy.size();i++) {
current => timelist[i];
values[i] => valuelist[i];
timy[i] +=> current;
}
now => then;
}
fun void init() {
now => then;
}
fun int lookup() {
int x;
now - then => dur passed;
for (int i;i<timelist.size();i++) {
if (timelist[i] > passed) break;
else x++;
}
return x-1;
}
fun float linterp(dur nu,dur to,dur ti,float yo,float yi) {
return (((nu-to)*yi) + ((ti-nu)*yo))/(ti-to);
}
fun float next() {
if (interp == 0) return valuelist[lookup()];
else if (interp == 1) {
lookup() => int index;
if (index > (size()-2)) return valuelist[index];
return linterp((now-then),timelist[index],timelist[index+1],valuelist[index],valuelist[index+1]);
}
}
}