source: node_modules/d3-array/dist/d3-array.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 38.6 KB
Line 
1// https://d3js.org/d3-array/ v3.2.4 Copyright 2010-2023 Mike Bostock
2(function (global, factory) {
3typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4typeof define === 'function' && define.amd ? define(['exports'], factory) :
5(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
6})(this, (function (exports) { 'use strict';
7
8function ascending(a, b) {
9 return a == null || b == null ? NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
10}
11
12function descending(a, b) {
13 return a == null || b == null ? NaN
14 : b < a ? -1
15 : b > a ? 1
16 : b >= a ? 0
17 : NaN;
18}
19
20function bisector(f) {
21 let compare1, compare2, delta;
22
23 // If an accessor is specified, promote it to a comparator. In this case we
24 // can test whether the search value is (self-) comparable. We can’t do this
25 // for a comparator (except for specific, known comparators) because we can’t
26 // tell if the comparator is symmetric, and an asymmetric comparator can’t be
27 // used to test whether a single value is comparable.
28 if (f.length !== 2) {
29 compare1 = ascending;
30 compare2 = (d, x) => ascending(f(d), x);
31 delta = (d, x) => f(d) - x;
32 } else {
33 compare1 = f === ascending || f === descending ? f : zero;
34 compare2 = f;
35 delta = f;
36 }
37
38 function left(a, x, lo = 0, hi = a.length) {
39 if (lo < hi) {
40 if (compare1(x, x) !== 0) return hi;
41 do {
42 const mid = (lo + hi) >>> 1;
43 if (compare2(a[mid], x) < 0) lo = mid + 1;
44 else hi = mid;
45 } while (lo < hi);
46 }
47 return lo;
48 }
49
50 function right(a, x, lo = 0, hi = a.length) {
51 if (lo < hi) {
52 if (compare1(x, x) !== 0) return hi;
53 do {
54 const mid = (lo + hi) >>> 1;
55 if (compare2(a[mid], x) <= 0) lo = mid + 1;
56 else hi = mid;
57 } while (lo < hi);
58 }
59 return lo;
60 }
61
62 function center(a, x, lo = 0, hi = a.length) {
63 const i = left(a, x, lo, hi - 1);
64 return i > lo && delta(a[i - 1], x) > -delta(a[i], x) ? i - 1 : i;
65 }
66
67 return {left, center, right};
68}
69
70function zero() {
71 return 0;
72}
73
74function number(x) {
75 return x === null ? NaN : +x;
76}
77
78function* numbers(values, valueof) {
79 if (valueof === undefined) {
80 for (let value of values) {
81 if (value != null && (value = +value) >= value) {
82 yield value;
83 }
84 }
85 } else {
86 let index = -1;
87 for (let value of values) {
88 if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
89 yield value;
90 }
91 }
92 }
93}
94
95const ascendingBisect = bisector(ascending);
96const bisectRight = ascendingBisect.right;
97const bisectLeft = ascendingBisect.left;
98const bisectCenter = bisector(number).center;
99var bisect = bisectRight;
100
101function blur(values, r) {
102 if (!((r = +r) >= 0)) throw new RangeError("invalid r");
103 let length = values.length;
104 if (!((length = Math.floor(length)) >= 0)) throw new RangeError("invalid length");
105 if (!length || !r) return values;
106 const blur = blurf(r);
107 const temp = values.slice();
108 blur(values, temp, 0, length, 1);
109 blur(temp, values, 0, length, 1);
110 blur(values, temp, 0, length, 1);
111 return values;
112}
113
114const blur2 = Blur2(blurf);
115
116const blurImage = Blur2(blurfImage);
117
118function Blur2(blur) {
119 return function(data, rx, ry = rx) {
120 if (!((rx = +rx) >= 0)) throw new RangeError("invalid rx");
121 if (!((ry = +ry) >= 0)) throw new RangeError("invalid ry");
122 let {data: values, width, height} = data;
123 if (!((width = Math.floor(width)) >= 0)) throw new RangeError("invalid width");
124 if (!((height = Math.floor(height !== undefined ? height : values.length / width)) >= 0)) throw new RangeError("invalid height");
125 if (!width || !height || (!rx && !ry)) return data;
126 const blurx = rx && blur(rx);
127 const blury = ry && blur(ry);
128 const temp = values.slice();
129 if (blurx && blury) {
130 blurh(blurx, temp, values, width, height);
131 blurh(blurx, values, temp, width, height);
132 blurh(blurx, temp, values, width, height);
133 blurv(blury, values, temp, width, height);
134 blurv(blury, temp, values, width, height);
135 blurv(blury, values, temp, width, height);
136 } else if (blurx) {
137 blurh(blurx, values, temp, width, height);
138 blurh(blurx, temp, values, width, height);
139 blurh(blurx, values, temp, width, height);
140 } else if (blury) {
141 blurv(blury, values, temp, width, height);
142 blurv(blury, temp, values, width, height);
143 blurv(blury, values, temp, width, height);
144 }
145 return data;
146 };
147}
148
149function blurh(blur, T, S, w, h) {
150 for (let y = 0, n = w * h; y < n;) {
151 blur(T, S, y, y += w, 1);
152 }
153}
154
155function blurv(blur, T, S, w, h) {
156 for (let x = 0, n = w * h; x < w; ++x) {
157 blur(T, S, x, x + n, w);
158 }
159}
160
161function blurfImage(radius) {
162 const blur = blurf(radius);
163 return (T, S, start, stop, step) => {
164 start <<= 2, stop <<= 2, step <<= 2;
165 blur(T, S, start + 0, stop + 0, step);
166 blur(T, S, start + 1, stop + 1, step);
167 blur(T, S, start + 2, stop + 2, step);
168 blur(T, S, start + 3, stop + 3, step);
169 };
170}
171
172// Given a target array T, a source array S, sets each value T[i] to the average
173// of {S[i - r], …, S[i], …, S[i + r]}, where r = ⌊radius⌋, start <= i < stop,
174// for each i, i + step, i + 2 * step, etc., and where S[j] is clamped between
175// S[start] (inclusive) and S[stop] (exclusive). If the given radius is not an
176// integer, S[i - r - 1] and S[i + r + 1] are added to the sum, each weighted
177// according to r - ⌊radius⌋.
178function blurf(radius) {
179 const radius0 = Math.floor(radius);
180 if (radius0 === radius) return bluri(radius);
181 const t = radius - radius0;
182 const w = 2 * radius + 1;
183 return (T, S, start, stop, step) => { // stop must be aligned!
184 if (!((stop -= step) >= start)) return; // inclusive stop
185 let sum = radius0 * S[start];
186 const s0 = step * radius0;
187 const s1 = s0 + step;
188 for (let i = start, j = start + s0; i < j; i += step) {
189 sum += S[Math.min(stop, i)];
190 }
191 for (let i = start, j = stop; i <= j; i += step) {
192 sum += S[Math.min(stop, i + s0)];
193 T[i] = (sum + t * (S[Math.max(start, i - s1)] + S[Math.min(stop, i + s1)])) / w;
194 sum -= S[Math.max(start, i - s0)];
195 }
196 };
197}
198
199// Like blurf, but optimized for integer radius.
200function bluri(radius) {
201 const w = 2 * radius + 1;
202 return (T, S, start, stop, step) => { // stop must be aligned!
203 if (!((stop -= step) >= start)) return; // inclusive stop
204 let sum = radius * S[start];
205 const s = step * radius;
206 for (let i = start, j = start + s; i < j; i += step) {
207 sum += S[Math.min(stop, i)];
208 }
209 for (let i = start, j = stop; i <= j; i += step) {
210 sum += S[Math.min(stop, i + s)];
211 T[i] = sum / w;
212 sum -= S[Math.max(start, i - s)];
213 }
214 };
215}
216
217function count(values, valueof) {
218 let count = 0;
219 if (valueof === undefined) {
220 for (let value of values) {
221 if (value != null && (value = +value) >= value) {
222 ++count;
223 }
224 }
225 } else {
226 let index = -1;
227 for (let value of values) {
228 if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
229 ++count;
230 }
231 }
232 }
233 return count;
234}
235
236function length$1(array) {
237 return array.length | 0;
238}
239
240function empty(length) {
241 return !(length > 0);
242}
243
244function arrayify(values) {
245 return typeof values !== "object" || "length" in values ? values : Array.from(values);
246}
247
248function reducer(reduce) {
249 return values => reduce(...values);
250}
251
252function cross(...values) {
253 const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
254 values = values.map(arrayify);
255 const lengths = values.map(length$1);
256 const j = values.length - 1;
257 const index = new Array(j + 1).fill(0);
258 const product = [];
259 if (j < 0 || lengths.some(empty)) return product;
260 while (true) {
261 product.push(index.map((j, i) => values[i][j]));
262 let i = j;
263 while (++index[i] === lengths[i]) {
264 if (i === 0) return reduce ? product.map(reduce) : product;
265 index[i--] = 0;
266 }
267 }
268}
269
270function cumsum(values, valueof) {
271 var sum = 0, index = 0;
272 return Float64Array.from(values, valueof === undefined
273 ? v => (sum += +v || 0)
274 : v => (sum += +valueof(v, index++, values) || 0));
275}
276
277function variance(values, valueof) {
278 let count = 0;
279 let delta;
280 let mean = 0;
281 let sum = 0;
282 if (valueof === undefined) {
283 for (let value of values) {
284 if (value != null && (value = +value) >= value) {
285 delta = value - mean;
286 mean += delta / ++count;
287 sum += delta * (value - mean);
288 }
289 }
290 } else {
291 let index = -1;
292 for (let value of values) {
293 if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
294 delta = value - mean;
295 mean += delta / ++count;
296 sum += delta * (value - mean);
297 }
298 }
299 }
300 if (count > 1) return sum / (count - 1);
301}
302
303function deviation(values, valueof) {
304 const v = variance(values, valueof);
305 return v ? Math.sqrt(v) : v;
306}
307
308function extent(values, valueof) {
309 let min;
310 let max;
311 if (valueof === undefined) {
312 for (const value of values) {
313 if (value != null) {
314 if (min === undefined) {
315 if (value >= value) min = max = value;
316 } else {
317 if (min > value) min = value;
318 if (max < value) max = value;
319 }
320 }
321 }
322 } else {
323 let index = -1;
324 for (let value of values) {
325 if ((value = valueof(value, ++index, values)) != null) {
326 if (min === undefined) {
327 if (value >= value) min = max = value;
328 } else {
329 if (min > value) min = value;
330 if (max < value) max = value;
331 }
332 }
333 }
334 }
335 return [min, max];
336}
337
338// https://github.com/python/cpython/blob/a74eea238f5baba15797e2e8b570d153bc8690a7/Modules/mathmodule.c#L1423
339class Adder {
340 constructor() {
341 this._partials = new Float64Array(32);
342 this._n = 0;
343 }
344 add(x) {
345 const p = this._partials;
346 let i = 0;
347 for (let j = 0; j < this._n && j < 32; j++) {
348 const y = p[j],
349 hi = x + y,
350 lo = Math.abs(x) < Math.abs(y) ? x - (hi - y) : y - (hi - x);
351 if (lo) p[i++] = lo;
352 x = hi;
353 }
354 p[i] = x;
355 this._n = i + 1;
356 return this;
357 }
358 valueOf() {
359 const p = this._partials;
360 let n = this._n, x, y, lo, hi = 0;
361 if (n > 0) {
362 hi = p[--n];
363 while (n > 0) {
364 x = hi;
365 y = p[--n];
366 hi = x + y;
367 lo = y - (hi - x);
368 if (lo) break;
369 }
370 if (n > 0 && ((lo < 0 && p[n - 1] < 0) || (lo > 0 && p[n - 1] > 0))) {
371 y = lo * 2;
372 x = hi + y;
373 if (y == x - hi) hi = x;
374 }
375 }
376 return hi;
377 }
378}
379
380function fsum(values, valueof) {
381 const adder = new Adder();
382 if (valueof === undefined) {
383 for (let value of values) {
384 if (value = +value) {
385 adder.add(value);
386 }
387 }
388 } else {
389 let index = -1;
390 for (let value of values) {
391 if (value = +valueof(value, ++index, values)) {
392 adder.add(value);
393 }
394 }
395 }
396 return +adder;
397}
398
399function fcumsum(values, valueof) {
400 const adder = new Adder();
401 let index = -1;
402 return Float64Array.from(values, valueof === undefined
403 ? v => adder.add(+v || 0)
404 : v => adder.add(+valueof(v, ++index, values) || 0)
405 );
406}
407
408class InternMap extends Map {
409 constructor(entries, key = keyof) {
410 super();
411 Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
412 if (entries != null) for (const [key, value] of entries) this.set(key, value);
413 }
414 get(key) {
415 return super.get(intern_get(this, key));
416 }
417 has(key) {
418 return super.has(intern_get(this, key));
419 }
420 set(key, value) {
421 return super.set(intern_set(this, key), value);
422 }
423 delete(key) {
424 return super.delete(intern_delete(this, key));
425 }
426}
427
428class InternSet extends Set {
429 constructor(values, key = keyof) {
430 super();
431 Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
432 if (values != null) for (const value of values) this.add(value);
433 }
434 has(value) {
435 return super.has(intern_get(this, value));
436 }
437 add(value) {
438 return super.add(intern_set(this, value));
439 }
440 delete(value) {
441 return super.delete(intern_delete(this, value));
442 }
443}
444
445function intern_get({_intern, _key}, value) {
446 const key = _key(value);
447 return _intern.has(key) ? _intern.get(key) : value;
448}
449
450function intern_set({_intern, _key}, value) {
451 const key = _key(value);
452 if (_intern.has(key)) return _intern.get(key);
453 _intern.set(key, value);
454 return value;
455}
456
457function intern_delete({_intern, _key}, value) {
458 const key = _key(value);
459 if (_intern.has(key)) {
460 value = _intern.get(key);
461 _intern.delete(key);
462 }
463 return value;
464}
465
466function keyof(value) {
467 return value !== null && typeof value === "object" ? value.valueOf() : value;
468}
469
470function identity(x) {
471 return x;
472}
473
474function group(values, ...keys) {
475 return nest(values, identity, identity, keys);
476}
477
478function groups(values, ...keys) {
479 return nest(values, Array.from, identity, keys);
480}
481
482function flatten$1(groups, keys) {
483 for (let i = 1, n = keys.length; i < n; ++i) {
484 groups = groups.flatMap(g => g.pop().map(([key, value]) => [...g, key, value]));
485 }
486 return groups;
487}
488
489function flatGroup(values, ...keys) {
490 return flatten$1(groups(values, ...keys), keys);
491}
492
493function flatRollup(values, reduce, ...keys) {
494 return flatten$1(rollups(values, reduce, ...keys), keys);
495}
496
497function rollup(values, reduce, ...keys) {
498 return nest(values, identity, reduce, keys);
499}
500
501function rollups(values, reduce, ...keys) {
502 return nest(values, Array.from, reduce, keys);
503}
504
505function index(values, ...keys) {
506 return nest(values, identity, unique, keys);
507}
508
509function indexes(values, ...keys) {
510 return nest(values, Array.from, unique, keys);
511}
512
513function unique(values) {
514 if (values.length !== 1) throw new Error("duplicate key");
515 return values[0];
516}
517
518function nest(values, map, reduce, keys) {
519 return (function regroup(values, i) {
520 if (i >= keys.length) return reduce(values);
521 const groups = new InternMap();
522 const keyof = keys[i++];
523 let index = -1;
524 for (const value of values) {
525 const key = keyof(value, ++index, values);
526 const group = groups.get(key);
527 if (group) group.push(value);
528 else groups.set(key, [value]);
529 }
530 for (const [key, values] of groups) {
531 groups.set(key, regroup(values, i));
532 }
533 return map(groups);
534 })(values, 0);
535}
536
537function permute(source, keys) {
538 return Array.from(keys, key => source[key]);
539}
540
541function sort(values, ...F) {
542 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
543 values = Array.from(values);
544 let [f] = F;
545 if ((f && f.length !== 2) || F.length > 1) {
546 const index = Uint32Array.from(values, (d, i) => i);
547 if (F.length > 1) {
548 F = F.map(f => values.map(f));
549 index.sort((i, j) => {
550 for (const f of F) {
551 const c = ascendingDefined(f[i], f[j]);
552 if (c) return c;
553 }
554 });
555 } else {
556 f = values.map(f);
557 index.sort((i, j) => ascendingDefined(f[i], f[j]));
558 }
559 return permute(values, index);
560 }
561 return values.sort(compareDefined(f));
562}
563
564function compareDefined(compare = ascending) {
565 if (compare === ascending) return ascendingDefined;
566 if (typeof compare !== "function") throw new TypeError("compare is not a function");
567 return (a, b) => {
568 const x = compare(a, b);
569 if (x || x === 0) return x;
570 return (compare(b, b) === 0) - (compare(a, a) === 0);
571 };
572}
573
574function ascendingDefined(a, b) {
575 return (a == null || !(a >= a)) - (b == null || !(b >= b)) || (a < b ? -1 : a > b ? 1 : 0);
576}
577
578function groupSort(values, reduce, key) {
579 return (reduce.length !== 2
580 ? sort(rollup(values, reduce, key), (([ak, av], [bk, bv]) => ascending(av, bv) || ascending(ak, bk)))
581 : sort(group(values, key), (([ak, av], [bk, bv]) => reduce(av, bv) || ascending(ak, bk))))
582 .map(([key]) => key);
583}
584
585var array = Array.prototype;
586
587var slice = array.slice;
588
589function constant(x) {
590 return () => x;
591}
592
593const e10 = Math.sqrt(50),
594 e5 = Math.sqrt(10),
595 e2 = Math.sqrt(2);
596
597function tickSpec(start, stop, count) {
598 const step = (stop - start) / Math.max(0, count),
599 power = Math.floor(Math.log10(step)),
600 error = step / Math.pow(10, power),
601 factor = error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1;
602 let i1, i2, inc;
603 if (power < 0) {
604 inc = Math.pow(10, -power) / factor;
605 i1 = Math.round(start * inc);
606 i2 = Math.round(stop * inc);
607 if (i1 / inc < start) ++i1;
608 if (i2 / inc > stop) --i2;
609 inc = -inc;
610 } else {
611 inc = Math.pow(10, power) * factor;
612 i1 = Math.round(start / inc);
613 i2 = Math.round(stop / inc);
614 if (i1 * inc < start) ++i1;
615 if (i2 * inc > stop) --i2;
616 }
617 if (i2 < i1 && 0.5 <= count && count < 2) return tickSpec(start, stop, count * 2);
618 return [i1, i2, inc];
619}
620
621function ticks(start, stop, count) {
622 stop = +stop, start = +start, count = +count;
623 if (!(count > 0)) return [];
624 if (start === stop) return [start];
625 const reverse = stop < start, [i1, i2, inc] = reverse ? tickSpec(stop, start, count) : tickSpec(start, stop, count);
626 if (!(i2 >= i1)) return [];
627 const n = i2 - i1 + 1, ticks = new Array(n);
628 if (reverse) {
629 if (inc < 0) for (let i = 0; i < n; ++i) ticks[i] = (i2 - i) / -inc;
630 else for (let i = 0; i < n; ++i) ticks[i] = (i2 - i) * inc;
631 } else {
632 if (inc < 0) for (let i = 0; i < n; ++i) ticks[i] = (i1 + i) / -inc;
633 else for (let i = 0; i < n; ++i) ticks[i] = (i1 + i) * inc;
634 }
635 return ticks;
636}
637
638function tickIncrement(start, stop, count) {
639 stop = +stop, start = +start, count = +count;
640 return tickSpec(start, stop, count)[2];
641}
642
643function tickStep(start, stop, count) {
644 stop = +stop, start = +start, count = +count;
645 const reverse = stop < start, inc = reverse ? tickIncrement(stop, start, count) : tickIncrement(start, stop, count);
646 return (reverse ? -1 : 1) * (inc < 0 ? 1 / -inc : inc);
647}
648
649function nice(start, stop, count) {
650 let prestep;
651 while (true) {
652 const step = tickIncrement(start, stop, count);
653 if (step === prestep || step === 0 || !isFinite(step)) {
654 return [start, stop];
655 } else if (step > 0) {
656 start = Math.floor(start / step) * step;
657 stop = Math.ceil(stop / step) * step;
658 } else if (step < 0) {
659 start = Math.ceil(start * step) / step;
660 stop = Math.floor(stop * step) / step;
661 }
662 prestep = step;
663 }
664}
665
666function thresholdSturges(values) {
667 return Math.max(1, Math.ceil(Math.log(count(values)) / Math.LN2) + 1);
668}
669
670function bin() {
671 var value = identity,
672 domain = extent,
673 threshold = thresholdSturges;
674
675 function histogram(data) {
676 if (!Array.isArray(data)) data = Array.from(data);
677
678 var i,
679 n = data.length,
680 x,
681 step,
682 values = new Array(n);
683
684 for (i = 0; i < n; ++i) {
685 values[i] = value(data[i], i, data);
686 }
687
688 var xz = domain(values),
689 x0 = xz[0],
690 x1 = xz[1],
691 tz = threshold(values, x0, x1);
692
693 // Convert number of thresholds into uniform thresholds, and nice the
694 // default domain accordingly.
695 if (!Array.isArray(tz)) {
696 const max = x1, tn = +tz;
697 if (domain === extent) [x0, x1] = nice(x0, x1, tn);
698 tz = ticks(x0, x1, tn);
699
700 // If the domain is aligned with the first tick (which it will by
701 // default), then we can use quantization rather than bisection to bin
702 // values, which is substantially faster.
703 if (tz[0] <= x0) step = tickIncrement(x0, x1, tn);
704
705 // If the last threshold is coincident with the domain’s upper bound, the
706 // last bin will be zero-width. If the default domain is used, and this
707 // last threshold is coincident with the maximum input value, we can
708 // extend the niced upper bound by one tick to ensure uniform bin widths;
709 // otherwise, we simply remove the last threshold. Note that we don’t
710 // coerce values or the domain to numbers, and thus must be careful to
711 // compare order (>=) rather than strict equality (===)!
712 if (tz[tz.length - 1] >= x1) {
713 if (max >= x1 && domain === extent) {
714 const step = tickIncrement(x0, x1, tn);
715 if (isFinite(step)) {
716 if (step > 0) {
717 x1 = (Math.floor(x1 / step) + 1) * step;
718 } else if (step < 0) {
719 x1 = (Math.ceil(x1 * -step) + 1) / -step;
720 }
721 }
722 } else {
723 tz.pop();
724 }
725 }
726 }
727
728 // Remove any thresholds outside the domain.
729 // Be careful not to mutate an array owned by the user!
730 var m = tz.length, a = 0, b = m;
731 while (tz[a] <= x0) ++a;
732 while (tz[b - 1] > x1) --b;
733 if (a || b < m) tz = tz.slice(a, b), m = b - a;
734
735 var bins = new Array(m + 1),
736 bin;
737
738 // Initialize bins.
739 for (i = 0; i <= m; ++i) {
740 bin = bins[i] = [];
741 bin.x0 = i > 0 ? tz[i - 1] : x0;
742 bin.x1 = i < m ? tz[i] : x1;
743 }
744
745 // Assign data to bins by value, ignoring any outside the domain.
746 if (isFinite(step)) {
747 if (step > 0) {
748 for (i = 0; i < n; ++i) {
749 if ((x = values[i]) != null && x0 <= x && x <= x1) {
750 bins[Math.min(m, Math.floor((x - x0) / step))].push(data[i]);
751 }
752 }
753 } else if (step < 0) {
754 for (i = 0; i < n; ++i) {
755 if ((x = values[i]) != null && x0 <= x && x <= x1) {
756 const j = Math.floor((x0 - x) * step);
757 bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); // handle off-by-one due to rounding
758 }
759 }
760 }
761 } else {
762 for (i = 0; i < n; ++i) {
763 if ((x = values[i]) != null && x0 <= x && x <= x1) {
764 bins[bisect(tz, x, 0, m)].push(data[i]);
765 }
766 }
767 }
768
769 return bins;
770 }
771
772 histogram.value = function(_) {
773 return arguments.length ? (value = typeof _ === "function" ? _ : constant(_), histogram) : value;
774 };
775
776 histogram.domain = function(_) {
777 return arguments.length ? (domain = typeof _ === "function" ? _ : constant([_[0], _[1]]), histogram) : domain;
778 };
779
780 histogram.thresholds = function(_) {
781 return arguments.length ? (threshold = typeof _ === "function" ? _ : constant(Array.isArray(_) ? slice.call(_) : _), histogram) : threshold;
782 };
783
784 return histogram;
785}
786
787function max(values, valueof) {
788 let max;
789 if (valueof === undefined) {
790 for (const value of values) {
791 if (value != null
792 && (max < value || (max === undefined && value >= value))) {
793 max = value;
794 }
795 }
796 } else {
797 let index = -1;
798 for (let value of values) {
799 if ((value = valueof(value, ++index, values)) != null
800 && (max < value || (max === undefined && value >= value))) {
801 max = value;
802 }
803 }
804 }
805 return max;
806}
807
808function maxIndex(values, valueof) {
809 let max;
810 let maxIndex = -1;
811 let index = -1;
812 if (valueof === undefined) {
813 for (const value of values) {
814 ++index;
815 if (value != null
816 && (max < value || (max === undefined && value >= value))) {
817 max = value, maxIndex = index;
818 }
819 }
820 } else {
821 for (let value of values) {
822 if ((value = valueof(value, ++index, values)) != null
823 && (max < value || (max === undefined && value >= value))) {
824 max = value, maxIndex = index;
825 }
826 }
827 }
828 return maxIndex;
829}
830
831function min(values, valueof) {
832 let min;
833 if (valueof === undefined) {
834 for (const value of values) {
835 if (value != null
836 && (min > value || (min === undefined && value >= value))) {
837 min = value;
838 }
839 }
840 } else {
841 let index = -1;
842 for (let value of values) {
843 if ((value = valueof(value, ++index, values)) != null
844 && (min > value || (min === undefined && value >= value))) {
845 min = value;
846 }
847 }
848 }
849 return min;
850}
851
852function minIndex(values, valueof) {
853 let min;
854 let minIndex = -1;
855 let index = -1;
856 if (valueof === undefined) {
857 for (const value of values) {
858 ++index;
859 if (value != null
860 && (min > value || (min === undefined && value >= value))) {
861 min = value, minIndex = index;
862 }
863 }
864 } else {
865 for (let value of values) {
866 if ((value = valueof(value, ++index, values)) != null
867 && (min > value || (min === undefined && value >= value))) {
868 min = value, minIndex = index;
869 }
870 }
871 }
872 return minIndex;
873}
874
875// Based on https://github.com/mourner/quickselect
876// ISC license, Copyright 2018 Vladimir Agafonkin.
877function quickselect(array, k, left = 0, right = Infinity, compare) {
878 k = Math.floor(k);
879 left = Math.floor(Math.max(0, left));
880 right = Math.floor(Math.min(array.length - 1, right));
881
882 if (!(left <= k && k <= right)) return array;
883
884 compare = compare === undefined ? ascendingDefined : compareDefined(compare);
885
886 while (right > left) {
887 if (right - left > 600) {
888 const n = right - left + 1;
889 const m = k - left + 1;
890 const z = Math.log(n);
891 const s = 0.5 * Math.exp(2 * z / 3);
892 const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
893 const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
894 const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
895 quickselect(array, k, newLeft, newRight, compare);
896 }
897
898 const t = array[k];
899 let i = left;
900 let j = right;
901
902 swap(array, left, k);
903 if (compare(array[right], t) > 0) swap(array, left, right);
904
905 while (i < j) {
906 swap(array, i, j), ++i, --j;
907 while (compare(array[i], t) < 0) ++i;
908 while (compare(array[j], t) > 0) --j;
909 }
910
911 if (compare(array[left], t) === 0) swap(array, left, j);
912 else ++j, swap(array, j, right);
913
914 if (j <= k) left = j + 1;
915 if (k <= j) right = j - 1;
916 }
917
918 return array;
919}
920
921function swap(array, i, j) {
922 const t = array[i];
923 array[i] = array[j];
924 array[j] = t;
925}
926
927function greatest(values, compare = ascending) {
928 let max;
929 let defined = false;
930 if (compare.length === 1) {
931 let maxValue;
932 for (const element of values) {
933 const value = compare(element);
934 if (defined
935 ? ascending(value, maxValue) > 0
936 : ascending(value, value) === 0) {
937 max = element;
938 maxValue = value;
939 defined = true;
940 }
941 }
942 } else {
943 for (const value of values) {
944 if (defined
945 ? compare(value, max) > 0
946 : compare(value, value) === 0) {
947 max = value;
948 defined = true;
949 }
950 }
951 }
952 return max;
953}
954
955function quantile(values, p, valueof) {
956 values = Float64Array.from(numbers(values, valueof));
957 if (!(n = values.length) || isNaN(p = +p)) return;
958 if (p <= 0 || n < 2) return min(values);
959 if (p >= 1) return max(values);
960 var n,
961 i = (n - 1) * p,
962 i0 = Math.floor(i),
963 value0 = max(quickselect(values, i0).subarray(0, i0 + 1)),
964 value1 = min(values.subarray(i0 + 1));
965 return value0 + (value1 - value0) * (i - i0);
966}
967
968function quantileSorted(values, p, valueof = number) {
969 if (!(n = values.length) || isNaN(p = +p)) return;
970 if (p <= 0 || n < 2) return +valueof(values[0], 0, values);
971 if (p >= 1) return +valueof(values[n - 1], n - 1, values);
972 var n,
973 i = (n - 1) * p,
974 i0 = Math.floor(i),
975 value0 = +valueof(values[i0], i0, values),
976 value1 = +valueof(values[i0 + 1], i0 + 1, values);
977 return value0 + (value1 - value0) * (i - i0);
978}
979
980function quantileIndex(values, p, valueof = number) {
981 if (isNaN(p = +p)) return;
982 numbers = Float64Array.from(values, (_, i) => number(valueof(values[i], i, values)));
983 if (p <= 0) return minIndex(numbers);
984 if (p >= 1) return maxIndex(numbers);
985 var numbers,
986 index = Uint32Array.from(values, (_, i) => i),
987 j = numbers.length - 1,
988 i = Math.floor(j * p);
989 quickselect(index, i, 0, j, (i, j) => ascendingDefined(numbers[i], numbers[j]));
990 i = greatest(index.subarray(0, i + 1), (i) => numbers[i]);
991 return i >= 0 ? i : -1;
992}
993
994function thresholdFreedmanDiaconis(values, min, max) {
995 const c = count(values), d = quantile(values, 0.75) - quantile(values, 0.25);
996 return c && d ? Math.ceil((max - min) / (2 * d * Math.pow(c, -1 / 3))) : 1;
997}
998
999function thresholdScott(values, min, max) {
1000 const c = count(values), d = deviation(values);
1001 return c && d ? Math.ceil((max - min) * Math.cbrt(c) / (3.49 * d)) : 1;
1002}
1003
1004function mean(values, valueof) {
1005 let count = 0;
1006 let sum = 0;
1007 if (valueof === undefined) {
1008 for (let value of values) {
1009 if (value != null && (value = +value) >= value) {
1010 ++count, sum += value;
1011 }
1012 }
1013 } else {
1014 let index = -1;
1015 for (let value of values) {
1016 if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
1017 ++count, sum += value;
1018 }
1019 }
1020 }
1021 if (count) return sum / count;
1022}
1023
1024function median(values, valueof) {
1025 return quantile(values, 0.5, valueof);
1026}
1027
1028function medianIndex(values, valueof) {
1029 return quantileIndex(values, 0.5, valueof);
1030}
1031
1032function* flatten(arrays) {
1033 for (const array of arrays) {
1034 yield* array;
1035 }
1036}
1037
1038function merge(arrays) {
1039 return Array.from(flatten(arrays));
1040}
1041
1042function mode(values, valueof) {
1043 const counts = new InternMap();
1044 if (valueof === undefined) {
1045 for (let value of values) {
1046 if (value != null && value >= value) {
1047 counts.set(value, (counts.get(value) || 0) + 1);
1048 }
1049 }
1050 } else {
1051 let index = -1;
1052 for (let value of values) {
1053 if ((value = valueof(value, ++index, values)) != null && value >= value) {
1054 counts.set(value, (counts.get(value) || 0) + 1);
1055 }
1056 }
1057 }
1058 let modeValue;
1059 let modeCount = 0;
1060 for (const [value, count] of counts) {
1061 if (count > modeCount) {
1062 modeCount = count;
1063 modeValue = value;
1064 }
1065 }
1066 return modeValue;
1067}
1068
1069function pairs(values, pairof = pair) {
1070 const pairs = [];
1071 let previous;
1072 let first = false;
1073 for (const value of values) {
1074 if (first) pairs.push(pairof(previous, value));
1075 previous = value;
1076 first = true;
1077 }
1078 return pairs;
1079}
1080
1081function pair(a, b) {
1082 return [a, b];
1083}
1084
1085function range(start, stop, step) {
1086 start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
1087
1088 var i = -1,
1089 n = Math.max(0, Math.ceil((stop - start) / step)) | 0,
1090 range = new Array(n);
1091
1092 while (++i < n) {
1093 range[i] = start + i * step;
1094 }
1095
1096 return range;
1097}
1098
1099function rank(values, valueof = ascending) {
1100 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
1101 let V = Array.from(values);
1102 const R = new Float64Array(V.length);
1103 if (valueof.length !== 2) V = V.map(valueof), valueof = ascending;
1104 const compareIndex = (i, j) => valueof(V[i], V[j]);
1105 let k, r;
1106 values = Uint32Array.from(V, (_, i) => i);
1107 // Risky chaining due to Safari 14 https://github.com/d3/d3-array/issues/123
1108 values.sort(valueof === ascending ? (i, j) => ascendingDefined(V[i], V[j]) : compareDefined(compareIndex));
1109 values.forEach((j, i) => {
1110 const c = compareIndex(j, k === undefined ? j : k);
1111 if (c >= 0) {
1112 if (k === undefined || c > 0) k = j, r = i;
1113 R[j] = r;
1114 } else {
1115 R[j] = NaN;
1116 }
1117 });
1118 return R;
1119}
1120
1121function least(values, compare = ascending) {
1122 let min;
1123 let defined = false;
1124 if (compare.length === 1) {
1125 let minValue;
1126 for (const element of values) {
1127 const value = compare(element);
1128 if (defined
1129 ? ascending(value, minValue) < 0
1130 : ascending(value, value) === 0) {
1131 min = element;
1132 minValue = value;
1133 defined = true;
1134 }
1135 }
1136 } else {
1137 for (const value of values) {
1138 if (defined
1139 ? compare(value, min) < 0
1140 : compare(value, value) === 0) {
1141 min = value;
1142 defined = true;
1143 }
1144 }
1145 }
1146 return min;
1147}
1148
1149function leastIndex(values, compare = ascending) {
1150 if (compare.length === 1) return minIndex(values, compare);
1151 let minValue;
1152 let min = -1;
1153 let index = -1;
1154 for (const value of values) {
1155 ++index;
1156 if (min < 0
1157 ? compare(value, value) === 0
1158 : compare(value, minValue) < 0) {
1159 minValue = value;
1160 min = index;
1161 }
1162 }
1163 return min;
1164}
1165
1166function greatestIndex(values, compare = ascending) {
1167 if (compare.length === 1) return maxIndex(values, compare);
1168 let maxValue;
1169 let max = -1;
1170 let index = -1;
1171 for (const value of values) {
1172 ++index;
1173 if (max < 0
1174 ? compare(value, value) === 0
1175 : compare(value, maxValue) > 0) {
1176 maxValue = value;
1177 max = index;
1178 }
1179 }
1180 return max;
1181}
1182
1183function scan(values, compare) {
1184 const index = leastIndex(values, compare);
1185 return index < 0 ? undefined : index;
1186}
1187
1188var shuffle = shuffler(Math.random);
1189
1190function shuffler(random) {
1191 return function shuffle(array, i0 = 0, i1 = array.length) {
1192 let m = i1 - (i0 = +i0);
1193 while (m) {
1194 const i = random() * m-- | 0, t = array[m + i0];
1195 array[m + i0] = array[i + i0];
1196 array[i + i0] = t;
1197 }
1198 return array;
1199 };
1200}
1201
1202function sum(values, valueof) {
1203 let sum = 0;
1204 if (valueof === undefined) {
1205 for (let value of values) {
1206 if (value = +value) {
1207 sum += value;
1208 }
1209 }
1210 } else {
1211 let index = -1;
1212 for (let value of values) {
1213 if (value = +valueof(value, ++index, values)) {
1214 sum += value;
1215 }
1216 }
1217 }
1218 return sum;
1219}
1220
1221function transpose(matrix) {
1222 if (!(n = matrix.length)) return [];
1223 for (var i = -1, m = min(matrix, length), transpose = new Array(m); ++i < m;) {
1224 for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n;) {
1225 row[j] = matrix[j][i];
1226 }
1227 }
1228 return transpose;
1229}
1230
1231function length(d) {
1232 return d.length;
1233}
1234
1235function zip() {
1236 return transpose(arguments);
1237}
1238
1239function every(values, test) {
1240 if (typeof test !== "function") throw new TypeError("test is not a function");
1241 let index = -1;
1242 for (const value of values) {
1243 if (!test(value, ++index, values)) {
1244 return false;
1245 }
1246 }
1247 return true;
1248}
1249
1250function some(values, test) {
1251 if (typeof test !== "function") throw new TypeError("test is not a function");
1252 let index = -1;
1253 for (const value of values) {
1254 if (test(value, ++index, values)) {
1255 return true;
1256 }
1257 }
1258 return false;
1259}
1260
1261function filter(values, test) {
1262 if (typeof test !== "function") throw new TypeError("test is not a function");
1263 const array = [];
1264 let index = -1;
1265 for (const value of values) {
1266 if (test(value, ++index, values)) {
1267 array.push(value);
1268 }
1269 }
1270 return array;
1271}
1272
1273function map(values, mapper) {
1274 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
1275 if (typeof mapper !== "function") throw new TypeError("mapper is not a function");
1276 return Array.from(values, (value, index) => mapper(value, index, values));
1277}
1278
1279function reduce(values, reducer, value) {
1280 if (typeof reducer !== "function") throw new TypeError("reducer is not a function");
1281 const iterator = values[Symbol.iterator]();
1282 let done, next, index = -1;
1283 if (arguments.length < 3) {
1284 ({done, value} = iterator.next());
1285 if (done) return;
1286 ++index;
1287 }
1288 while (({done, value: next} = iterator.next()), !done) {
1289 value = reducer(value, next, ++index, values);
1290 }
1291 return value;
1292}
1293
1294function reverse(values) {
1295 if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
1296 return Array.from(values).reverse();
1297}
1298
1299function difference(values, ...others) {
1300 values = new InternSet(values);
1301 for (const other of others) {
1302 for (const value of other) {
1303 values.delete(value);
1304 }
1305 }
1306 return values;
1307}
1308
1309function disjoint(values, other) {
1310 const iterator = other[Symbol.iterator](), set = new InternSet();
1311 for (const v of values) {
1312 if (set.has(v)) return false;
1313 let value, done;
1314 while (({value, done} = iterator.next())) {
1315 if (done) break;
1316 if (Object.is(v, value)) return false;
1317 set.add(value);
1318 }
1319 }
1320 return true;
1321}
1322
1323function intersection(values, ...others) {
1324 values = new InternSet(values);
1325 others = others.map(set);
1326 out: for (const value of values) {
1327 for (const other of others) {
1328 if (!other.has(value)) {
1329 values.delete(value);
1330 continue out;
1331 }
1332 }
1333 }
1334 return values;
1335}
1336
1337function set(values) {
1338 return values instanceof InternSet ? values : new InternSet(values);
1339}
1340
1341function superset(values, other) {
1342 const iterator = values[Symbol.iterator](), set = new Set();
1343 for (const o of other) {
1344 const io = intern(o);
1345 if (set.has(io)) continue;
1346 let value, done;
1347 while (({value, done} = iterator.next())) {
1348 if (done) return false;
1349 const ivalue = intern(value);
1350 set.add(ivalue);
1351 if (Object.is(io, ivalue)) break;
1352 }
1353 }
1354 return true;
1355}
1356
1357function intern(value) {
1358 return value !== null && typeof value === "object" ? value.valueOf() : value;
1359}
1360
1361function subset(values, other) {
1362 return superset(other, values);
1363}
1364
1365function union(...others) {
1366 const set = new InternSet();
1367 for (const other of others) {
1368 for (const o of other) {
1369 set.add(o);
1370 }
1371 }
1372 return set;
1373}
1374
1375exports.Adder = Adder;
1376exports.InternMap = InternMap;
1377exports.InternSet = InternSet;
1378exports.ascending = ascending;
1379exports.bin = bin;
1380exports.bisect = bisect;
1381exports.bisectCenter = bisectCenter;
1382exports.bisectLeft = bisectLeft;
1383exports.bisectRight = bisectRight;
1384exports.bisector = bisector;
1385exports.blur = blur;
1386exports.blur2 = blur2;
1387exports.blurImage = blurImage;
1388exports.count = count;
1389exports.cross = cross;
1390exports.cumsum = cumsum;
1391exports.descending = descending;
1392exports.deviation = deviation;
1393exports.difference = difference;
1394exports.disjoint = disjoint;
1395exports.every = every;
1396exports.extent = extent;
1397exports.fcumsum = fcumsum;
1398exports.filter = filter;
1399exports.flatGroup = flatGroup;
1400exports.flatRollup = flatRollup;
1401exports.fsum = fsum;
1402exports.greatest = greatest;
1403exports.greatestIndex = greatestIndex;
1404exports.group = group;
1405exports.groupSort = groupSort;
1406exports.groups = groups;
1407exports.histogram = bin;
1408exports.index = index;
1409exports.indexes = indexes;
1410exports.intersection = intersection;
1411exports.least = least;
1412exports.leastIndex = leastIndex;
1413exports.map = map;
1414exports.max = max;
1415exports.maxIndex = maxIndex;
1416exports.mean = mean;
1417exports.median = median;
1418exports.medianIndex = medianIndex;
1419exports.merge = merge;
1420exports.min = min;
1421exports.minIndex = minIndex;
1422exports.mode = mode;
1423exports.nice = nice;
1424exports.pairs = pairs;
1425exports.permute = permute;
1426exports.quantile = quantile;
1427exports.quantileIndex = quantileIndex;
1428exports.quantileSorted = quantileSorted;
1429exports.quickselect = quickselect;
1430exports.range = range;
1431exports.rank = rank;
1432exports.reduce = reduce;
1433exports.reverse = reverse;
1434exports.rollup = rollup;
1435exports.rollups = rollups;
1436exports.scan = scan;
1437exports.shuffle = shuffle;
1438exports.shuffler = shuffler;
1439exports.some = some;
1440exports.sort = sort;
1441exports.subset = subset;
1442exports.sum = sum;
1443exports.superset = superset;
1444exports.thresholdFreedmanDiaconis = thresholdFreedmanDiaconis;
1445exports.thresholdScott = thresholdScott;
1446exports.thresholdSturges = thresholdSturges;
1447exports.tickIncrement = tickIncrement;
1448exports.tickStep = tickStep;
1449exports.ticks = ticks;
1450exports.transpose = transpose;
1451exports.union = union;
1452exports.variance = variance;
1453exports.zip = zip;
1454
1455}));
Note: See TracBrowser for help on using the repository browser.