-
Notifications
You must be signed in to change notification settings - Fork 0
/
out.js
284 lines (277 loc) · 7.83 KB
/
out.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(() => {
// node_modules/d3-array/src/ascending.js
function ascending(a, b) {
return a == null || b == null ? NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
}
// node_modules/d3-array/src/descending.js
function descending(a, b) {
return a == null || b == null ? NaN : b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
}
// node_modules/d3-array/src/bisector.js
function bisector(f) {
let compare1, compare2, delta;
if (f.length !== 2) {
compare1 = ascending;
compare2 = (d, x) => ascending(f(d), x);
delta = (d, x) => f(d) - x;
} else {
compare1 = f === ascending || f === descending ? f : zero;
compare2 = f;
delta = f;
}
function left(a, x, lo = 0, hi = a.length) {
if (lo < hi) {
if (compare1(x, x) !== 0)
return hi;
do {
const mid = lo + hi >>> 1;
if (compare2(a[mid], x) < 0)
lo = mid + 1;
else
hi = mid;
} while (lo < hi);
}
return lo;
}
function right(a, x, lo = 0, hi = a.length) {
if (lo < hi) {
if (compare1(x, x) !== 0)
return hi;
do {
const mid = lo + hi >>> 1;
if (compare2(a[mid], x) <= 0)
lo = mid + 1;
else
hi = mid;
} while (lo < hi);
}
return lo;
}
function center(a, x, lo = 0, hi = a.length) {
const i = left(a, x, lo, hi - 1);
return i > lo && delta(a[i - 1], x) > -delta(a[i], x) ? i - 1 : i;
}
return { left, center, right };
}
function zero() {
return 0;
}
// node_modules/d3-array/src/number.js
function number(x) {
return x === null ? NaN : +x;
}
// node_modules/d3-array/src/bisect.js
var ascendingBisect = bisector(ascending);
var bisectRight = ascendingBisect.right;
var bisectLeft = ascendingBisect.left;
var bisectCenter = bisector(number).center;
// node_modules/internmap/src/index.js
var InternMap = class extends Map {
constructor(entries, key = keyof) {
super();
Object.defineProperties(this, { _intern: { value: /* @__PURE__ */ new Map() }, _key: { value: key } });
if (entries != null)
for (const [key2, value] of entries)
this.set(key2, value);
}
get(key) {
return super.get(intern_get(this, key));
}
has(key) {
return super.has(intern_get(this, key));
}
set(key, value) {
return super.set(intern_set(this, key), value);
}
delete(key) {
return super.delete(intern_delete(this, key));
}
};
function intern_get({ _intern, _key }, value) {
const key = _key(value);
return _intern.has(key) ? _intern.get(key) : value;
}
function intern_set({ _intern, _key }, value) {
const key = _key(value);
if (_intern.has(key))
return _intern.get(key);
_intern.set(key, value);
return value;
}
function intern_delete({ _intern, _key }, value) {
const key = _key(value);
if (_intern.has(key)) {
value = _intern.get(key);
_intern.delete(key);
}
return value;
}
function keyof(value) {
return value !== null && typeof value === "object" ? value.valueOf() : value;
}
// src/trace.js
function keyof2(value) {
return value !== null && typeof value === "object" ? value.valueOf() : value;
}
var Trace = class {
constructor(entries, defaultValue) {
this.map = new InternMap(entries);
this.list = [...this.map.keys()].sort((a, b) => keyof2(a) - keyof2(b));
this.defaultValue = defaultValue;
}
get = (key) => {
if (this.map.has(key)) {
return this.map.get(key);
} else {
const i = bisectRight(this.list, key);
if (i === 0) {
return this.defaultValue;
} else {
const previousKey = this.list[i - 1];
return this.map.get(previousKey);
}
}
};
set = (key, value) => {
if (!this.map.has(key)) {
this.list.splice(bisectRight(this.list, key), 0, key);
}
return this.map.set(key, value);
};
has = (key) => {
return this.map.has(key);
};
delete = (key) => {
const hadKey = this.map.delete(key);
if (hadKey) {
this.list.splice(bisectRight(this.list, key) - 1, 1);
}
};
entries() {
return [...this];
}
*[Symbol.iterator]() {
for (let key of this.list) {
yield [key, this.map.get(key)];
}
}
compact() {
let result = new Trace([], this.defaultValue);
let previousValue = this.defaultValue;
for (let [t, value] of this) {
if (value !== previousValue) {
result.set(t, value);
}
previousValue = value;
}
return result;
}
get size() {
return this.map.size;
}
distribution({
start = void 0,
end = void 0,
normalize = true,
durationFunction = (a, b) => b - a
} = {}) {
let result = /* @__PURE__ */ new Map();
let total = 0;
for (let [t0, t1, value] of this.intervals({ start, end })) {
let duration = durationFunction(t0, t1);
total += duration;
result.set(value, (result.get(value) ?? 0) + duration);
}
if (normalize) {
for (let v of result.keys()) {
result.set(v, result.get(v) / total);
}
}
return result;
}
*intervals({ start = void 0, end = void 0 } = {}) {
const left = start ?? this.list[0];
const right = end ?? this.list[this.list.length - 1];
const start_index = bisectRight(this.list, left);
const end_index = bisectRight(this.list, right);
let start_value;
if (start_index > 0) {
start_value = this.get(this.list[start_index - 1]);
} else {
start_value = this.defaultValue;
}
let interval_t0 = left;
let interval_value = start_value;
for (let index = start_index; index < end_index; index++) {
let interval_t1 = this.list[index];
yield [interval_t0, interval_t1, interval_value];
interval_t0 = interval_t1;
interval_value = this.get(interval_t0);
}
if (interval_t0 < end) {
yield [interval_t0, end, interval_value];
}
}
firstKey() {
return this.list[0];
}
lastKey() {
return this.list[this.list.length - 1];
}
firstValue() {
return this.get(this.firstKey());
}
lastValue() {
return this.get(this.lastKey());
}
firstEntry() {
let key = this.firstKey();
return [key, this.get(key)];
}
lastEntry() {
let key = this.lastKey();
return [key, this.get(key)];
}
getInterval(t) {
const index = bisectRight(this.list, t);
const t0 = this.list[index - 1] ?? -Infinity;
const t1 = this.list[index] ?? Infinity;
return [t0, t1, this.get(t0)];
}
nextInterval(t) {
const index = bisectRight(this.list, t);
if (index === this.list.length) {
return void 0;
} else {
return this.getInterval(this.list[index]);
}
}
previousInterval(t) {
const index = bisectRight(this.list, t);
const t0 = this.list[index - 2] ?? -Infinity;
if (index === 0) {
return void 0;
} else {
return this.getInterval(t0);
}
}
slice({ start = void 0, end = void 0 } = {}) {
let result = new Trace([], this.defaultValue);
let t0, t1, value;
for ([t0, t1, value] of this.intervals({ start, end })) {
result.set(t0, value);
}
result.set(t1, this.get(t1));
return result;
}
};
// src/merge.js
function merge(traceList, operation = (d) => d) {
let keys = new Set(traceList.map((d) => d.list).flat());
let result = new Trace([], operation(traceList.map((d) => d.defaultValue)));
for (let k of keys) {
result.set(k, operation(traceList.map((d) => d.get(k))));
}
return result;
}
})();