d3-array.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. // https://d3js.org/d3-array/ v3.0.4 Copyright 2010-2021 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof 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. function ascending(a, b) {
  8. return a == null || b == null ? NaN : a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  9. }
  10. function bisector(f) {
  11. let delta = f;
  12. let compare1 = f;
  13. let compare2 = f;
  14. if (f.length === 1) {
  15. delta = (d, x) => f(d) - x;
  16. compare1 = ascending;
  17. compare2 = (d, x) => ascending(f(d), x);
  18. }
  19. function left(a, x, lo = 0, hi = a.length) {
  20. if (lo < hi) {
  21. if (compare1(x, x) !== 0) return hi;
  22. do {
  23. const mid = (lo + hi) >>> 1;
  24. if (compare2(a[mid], x) < 0) lo = mid + 1;
  25. else hi = mid;
  26. } while (lo < hi);
  27. }
  28. return lo;
  29. }
  30. function right(a, x, lo = 0, hi = a.length) {
  31. if (lo < hi) {
  32. if (compare1(x, x) !== 0) return hi;
  33. do {
  34. const mid = (lo + hi) >>> 1;
  35. if (compare2(a[mid], x) <= 0) lo = mid + 1;
  36. else hi = mid;
  37. } while (lo < hi);
  38. }
  39. return lo;
  40. }
  41. function center(a, x, lo = 0, hi = a.length) {
  42. const i = left(a, x, lo, hi - 1);
  43. return i > lo && delta(a[i - 1], x) > -delta(a[i], x) ? i - 1 : i;
  44. }
  45. return {left, center, right};
  46. }
  47. function number(x) {
  48. return x === null ? NaN : +x;
  49. }
  50. function* numbers(values, valueof) {
  51. if (valueof === undefined) {
  52. for (let value of values) {
  53. if (value != null && (value = +value) >= value) {
  54. yield value;
  55. }
  56. }
  57. } else {
  58. let index = -1;
  59. for (let value of values) {
  60. if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
  61. yield value;
  62. }
  63. }
  64. }
  65. }
  66. const ascendingBisect = bisector(ascending);
  67. const bisectRight = ascendingBisect.right;
  68. const bisectLeft = ascendingBisect.left;
  69. const bisectCenter = bisector(number).center;
  70. var bisect = bisectRight;
  71. function count(values, valueof) {
  72. let count = 0;
  73. if (valueof === undefined) {
  74. for (let value of values) {
  75. if (value != null && (value = +value) >= value) {
  76. ++count;
  77. }
  78. }
  79. } else {
  80. let index = -1;
  81. for (let value of values) {
  82. if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
  83. ++count;
  84. }
  85. }
  86. }
  87. return count;
  88. }
  89. function length$1(array) {
  90. return array.length | 0;
  91. }
  92. function empty(length) {
  93. return !(length > 0);
  94. }
  95. function arrayify(values) {
  96. return typeof values !== "object" || "length" in values ? values : Array.from(values);
  97. }
  98. function reducer(reduce) {
  99. return values => reduce(...values);
  100. }
  101. function cross(...values) {
  102. const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
  103. values = values.map(arrayify);
  104. const lengths = values.map(length$1);
  105. const j = values.length - 1;
  106. const index = new Array(j + 1).fill(0);
  107. const product = [];
  108. if (j < 0 || lengths.some(empty)) return product;
  109. while (true) {
  110. product.push(index.map((j, i) => values[i][j]));
  111. let i = j;
  112. while (++index[i] === lengths[i]) {
  113. if (i === 0) return reduce ? product.map(reduce) : product;
  114. index[i--] = 0;
  115. }
  116. }
  117. }
  118. function cumsum(values, valueof) {
  119. var sum = 0, index = 0;
  120. return Float64Array.from(values, valueof === undefined
  121. ? v => (sum += +v || 0)
  122. : v => (sum += +valueof(v, index++, values) || 0));
  123. }
  124. function descending(a, b) {
  125. return a == null || b == null ? NaN
  126. : b < a ? -1
  127. : b > a ? 1
  128. : b >= a ? 0
  129. : NaN;
  130. }
  131. function variance(values, valueof) {
  132. let count = 0;
  133. let delta;
  134. let mean = 0;
  135. let sum = 0;
  136. if (valueof === undefined) {
  137. for (let value of values) {
  138. if (value != null && (value = +value) >= value) {
  139. delta = value - mean;
  140. mean += delta / ++count;
  141. sum += delta * (value - mean);
  142. }
  143. }
  144. } else {
  145. let index = -1;
  146. for (let value of values) {
  147. if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
  148. delta = value - mean;
  149. mean += delta / ++count;
  150. sum += delta * (value - mean);
  151. }
  152. }
  153. }
  154. if (count > 1) return sum / (count - 1);
  155. }
  156. function deviation(values, valueof) {
  157. const v = variance(values, valueof);
  158. return v ? Math.sqrt(v) : v;
  159. }
  160. function extent(values, valueof) {
  161. let min;
  162. let max;
  163. if (valueof === undefined) {
  164. for (const value of values) {
  165. if (value != null) {
  166. if (min === undefined) {
  167. if (value >= value) min = max = value;
  168. } else {
  169. if (min > value) min = value;
  170. if (max < value) max = value;
  171. }
  172. }
  173. }
  174. } else {
  175. let index = -1;
  176. for (let value of values) {
  177. if ((value = valueof(value, ++index, values)) != null) {
  178. if (min === undefined) {
  179. if (value >= value) min = max = value;
  180. } else {
  181. if (min > value) min = value;
  182. if (max < value) max = value;
  183. }
  184. }
  185. }
  186. }
  187. return [min, max];
  188. }
  189. // https://github.com/python/cpython/blob/a74eea238f5baba15797e2e8b570d153bc8690a7/Modules/mathmodule.c#L1423
  190. class Adder {
  191. constructor() {
  192. this._partials = new Float64Array(32);
  193. this._n = 0;
  194. }
  195. add(x) {
  196. const p = this._partials;
  197. let i = 0;
  198. for (let j = 0; j < this._n && j < 32; j++) {
  199. const y = p[j],
  200. hi = x + y,
  201. lo = Math.abs(x) < Math.abs(y) ? x - (hi - y) : y - (hi - x);
  202. if (lo) p[i++] = lo;
  203. x = hi;
  204. }
  205. p[i] = x;
  206. this._n = i + 1;
  207. return this;
  208. }
  209. valueOf() {
  210. const p = this._partials;
  211. let n = this._n, x, y, lo, hi = 0;
  212. if (n > 0) {
  213. hi = p[--n];
  214. while (n > 0) {
  215. x = hi;
  216. y = p[--n];
  217. hi = x + y;
  218. lo = y - (hi - x);
  219. if (lo) break;
  220. }
  221. if (n > 0 && ((lo < 0 && p[n - 1] < 0) || (lo > 0 && p[n - 1] > 0))) {
  222. y = lo * 2;
  223. x = hi + y;
  224. if (y == x - hi) hi = x;
  225. }
  226. }
  227. return hi;
  228. }
  229. }
  230. function fsum(values, valueof) {
  231. const adder = new Adder();
  232. if (valueof === undefined) {
  233. for (let value of values) {
  234. if (value = +value) {
  235. adder.add(value);
  236. }
  237. }
  238. } else {
  239. let index = -1;
  240. for (let value of values) {
  241. if (value = +valueof(value, ++index, values)) {
  242. adder.add(value);
  243. }
  244. }
  245. }
  246. return +adder;
  247. }
  248. function fcumsum(values, valueof) {
  249. const adder = new Adder();
  250. let index = -1;
  251. return Float64Array.from(values, valueof === undefined
  252. ? v => adder.add(+v || 0)
  253. : v => adder.add(+valueof(v, ++index, values) || 0)
  254. );
  255. }
  256. class InternMap extends Map {
  257. constructor(entries, key = keyof) {
  258. super();
  259. Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
  260. if (entries != null) for (const [key, value] of entries) this.set(key, value);
  261. }
  262. get(key) {
  263. return super.get(intern_get(this, key));
  264. }
  265. has(key) {
  266. return super.has(intern_get(this, key));
  267. }
  268. set(key, value) {
  269. return super.set(intern_set(this, key), value);
  270. }
  271. delete(key) {
  272. return super.delete(intern_delete(this, key));
  273. }
  274. }
  275. class InternSet extends Set {
  276. constructor(values, key = keyof) {
  277. super();
  278. Object.defineProperties(this, {_intern: {value: new Map()}, _key: {value: key}});
  279. if (values != null) for (const value of values) this.add(value);
  280. }
  281. has(value) {
  282. return super.has(intern_get(this, value));
  283. }
  284. add(value) {
  285. return super.add(intern_set(this, value));
  286. }
  287. delete(value) {
  288. return super.delete(intern_delete(this, value));
  289. }
  290. }
  291. function intern_get({_intern, _key}, value) {
  292. const key = _key(value);
  293. return _intern.has(key) ? _intern.get(key) : value;
  294. }
  295. function intern_set({_intern, _key}, value) {
  296. const key = _key(value);
  297. if (_intern.has(key)) return _intern.get(key);
  298. _intern.set(key, value);
  299. return value;
  300. }
  301. function intern_delete({_intern, _key}, value) {
  302. const key = _key(value);
  303. if (_intern.has(key)) {
  304. value = _intern.get(key);
  305. _intern.delete(key);
  306. }
  307. return value;
  308. }
  309. function keyof(value) {
  310. return value !== null && typeof value === "object" ? value.valueOf() : value;
  311. }
  312. function identity(x) {
  313. return x;
  314. }
  315. function group(values, ...keys) {
  316. return nest(values, identity, identity, keys);
  317. }
  318. function groups(values, ...keys) {
  319. return nest(values, Array.from, identity, keys);
  320. }
  321. function flatten$1(groups, keys) {
  322. for (let i = 1, n = keys.length; i < n; ++i) {
  323. groups = groups.flatMap(g => g.pop().map(([key, value]) => [...g, key, value]));
  324. }
  325. return groups;
  326. }
  327. function flatGroup(values, ...keys) {
  328. return flatten$1(groups(values, ...keys), keys);
  329. }
  330. function flatRollup(values, reduce, ...keys) {
  331. return flatten$1(rollups(values, reduce, ...keys), keys);
  332. }
  333. function rollup(values, reduce, ...keys) {
  334. return nest(values, identity, reduce, keys);
  335. }
  336. function rollups(values, reduce, ...keys) {
  337. return nest(values, Array.from, reduce, keys);
  338. }
  339. function index(values, ...keys) {
  340. return nest(values, identity, unique, keys);
  341. }
  342. function indexes(values, ...keys) {
  343. return nest(values, Array.from, unique, keys);
  344. }
  345. function unique(values) {
  346. if (values.length !== 1) throw new Error("duplicate key");
  347. return values[0];
  348. }
  349. function nest(values, map, reduce, keys) {
  350. return (function regroup(values, i) {
  351. if (i >= keys.length) return reduce(values);
  352. const groups = new InternMap();
  353. const keyof = keys[i++];
  354. let index = -1;
  355. for (const value of values) {
  356. const key = keyof(value, ++index, values);
  357. const group = groups.get(key);
  358. if (group) group.push(value);
  359. else groups.set(key, [value]);
  360. }
  361. for (const [key, values] of groups) {
  362. groups.set(key, regroup(values, i));
  363. }
  364. return map(groups);
  365. })(values, 0);
  366. }
  367. function permute(source, keys) {
  368. return Array.from(keys, key => source[key]);
  369. }
  370. function sort(values, ...F) {
  371. if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
  372. values = Array.from(values);
  373. let [f] = F;
  374. if ((f && f.length === 1) || F.length > 1) {
  375. const index = Uint32Array.from(values, (d, i) => i);
  376. if (F.length > 1) {
  377. F = F.map(f => values.map(f));
  378. index.sort((i, j) => {
  379. for (const f of F) {
  380. const c = ascendingDefined(f[i], f[j]);
  381. if (c) return c;
  382. }
  383. });
  384. } else {
  385. f = values.map(f);
  386. index.sort((i, j) => ascendingDefined(f[i], f[j]));
  387. }
  388. return permute(values, index);
  389. }
  390. return values.sort(f === undefined ? ascendingDefined : compareDefined(f));
  391. }
  392. function compareDefined(compare) {
  393. if (typeof compare !== "function") throw new TypeError("compare is not a function");
  394. return (a, b) => {
  395. const x = compare(a, b);
  396. if (x || x === 0) return x;
  397. return (compare(b, b) === 0) - (compare(a, a) === 0);
  398. };
  399. }
  400. function ascendingDefined(a, b) {
  401. return (a == null || !(a >= a)) - (b == null || !(b >= b)) || (a < b ? -1 : a > b ? 1 : 0);
  402. }
  403. function groupSort(values, reduce, key) {
  404. return (reduce.length === 1
  405. ? sort(rollup(values, reduce, key), (([ak, av], [bk, bv]) => ascending(av, bv) || ascending(ak, bk)))
  406. : sort(group(values, key), (([ak, av], [bk, bv]) => reduce(av, bv) || ascending(ak, bk))))
  407. .map(([key]) => key);
  408. }
  409. var array = Array.prototype;
  410. var slice = array.slice;
  411. function constant(x) {
  412. return () => x;
  413. }
  414. var e10 = Math.sqrt(50),
  415. e5 = Math.sqrt(10),
  416. e2 = Math.sqrt(2);
  417. function ticks(start, stop, count) {
  418. var reverse,
  419. i = -1,
  420. n,
  421. ticks,
  422. step;
  423. stop = +stop, start = +start, count = +count;
  424. if (start === stop && count > 0) return [start];
  425. if (reverse = stop < start) n = start, start = stop, stop = n;
  426. if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) return [];
  427. if (step > 0) {
  428. let r0 = Math.round(start / step), r1 = Math.round(stop / step);
  429. if (r0 * step < start) ++r0;
  430. if (r1 * step > stop) --r1;
  431. ticks = new Array(n = r1 - r0 + 1);
  432. while (++i < n) ticks[i] = (r0 + i) * step;
  433. } else {
  434. step = -step;
  435. let r0 = Math.round(start * step), r1 = Math.round(stop * step);
  436. if (r0 / step < start) ++r0;
  437. if (r1 / step > stop) --r1;
  438. ticks = new Array(n = r1 - r0 + 1);
  439. while (++i < n) ticks[i] = (r0 + i) / step;
  440. }
  441. if (reverse) ticks.reverse();
  442. return ticks;
  443. }
  444. function tickIncrement(start, stop, count) {
  445. var step = (stop - start) / Math.max(0, count),
  446. power = Math.floor(Math.log(step) / Math.LN10),
  447. error = step / Math.pow(10, power);
  448. return power >= 0
  449. ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
  450. : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
  451. }
  452. function tickStep(start, stop, count) {
  453. var step0 = Math.abs(stop - start) / Math.max(0, count),
  454. step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)),
  455. error = step0 / step1;
  456. if (error >= e10) step1 *= 10;
  457. else if (error >= e5) step1 *= 5;
  458. else if (error >= e2) step1 *= 2;
  459. return stop < start ? -step1 : step1;
  460. }
  461. function nice(start, stop, count) {
  462. let prestep;
  463. while (true) {
  464. const step = tickIncrement(start, stop, count);
  465. if (step === prestep || step === 0 || !isFinite(step)) {
  466. return [start, stop];
  467. } else if (step > 0) {
  468. start = Math.floor(start / step) * step;
  469. stop = Math.ceil(stop / step) * step;
  470. } else if (step < 0) {
  471. start = Math.ceil(start * step) / step;
  472. stop = Math.floor(stop * step) / step;
  473. }
  474. prestep = step;
  475. }
  476. }
  477. function thresholdSturges(values) {
  478. return Math.ceil(Math.log(count(values)) / Math.LN2) + 1;
  479. }
  480. function bin() {
  481. var value = identity,
  482. domain = extent,
  483. threshold = thresholdSturges;
  484. function histogram(data) {
  485. if (!Array.isArray(data)) data = Array.from(data);
  486. var i,
  487. n = data.length,
  488. x,
  489. values = new Array(n);
  490. for (i = 0; i < n; ++i) {
  491. values[i] = value(data[i], i, data);
  492. }
  493. var xz = domain(values),
  494. x0 = xz[0],
  495. x1 = xz[1],
  496. tz = threshold(values, x0, x1);
  497. // Convert number of thresholds into uniform thresholds, and nice the
  498. // default domain accordingly.
  499. if (!Array.isArray(tz)) {
  500. const max = x1, tn = +tz;
  501. if (domain === extent) [x0, x1] = nice(x0, x1, tn);
  502. tz = ticks(x0, x1, tn);
  503. // If the last threshold is coincident with the domain’s upper bound, the
  504. // last bin will be zero-width. If the default domain is used, and this
  505. // last threshold is coincident with the maximum input value, we can
  506. // extend the niced upper bound by one tick to ensure uniform bin widths;
  507. // otherwise, we simply remove the last threshold. Note that we don’t
  508. // coerce values or the domain to numbers, and thus must be careful to
  509. // compare order (>=) rather than strict equality (===)!
  510. if (tz[tz.length - 1] >= x1) {
  511. if (max >= x1 && domain === extent) {
  512. const step = tickIncrement(x0, x1, tn);
  513. if (isFinite(step)) {
  514. if (step > 0) {
  515. x1 = (Math.floor(x1 / step) + 1) * step;
  516. } else if (step < 0) {
  517. x1 = (Math.ceil(x1 * -step) + 1) / -step;
  518. }
  519. }
  520. } else {
  521. tz.pop();
  522. }
  523. }
  524. }
  525. // Remove any thresholds outside the domain.
  526. var m = tz.length;
  527. while (tz[0] <= x0) tz.shift(), --m;
  528. while (tz[m - 1] > x1) tz.pop(), --m;
  529. var bins = new Array(m + 1),
  530. bin;
  531. // Initialize bins.
  532. for (i = 0; i <= m; ++i) {
  533. bin = bins[i] = [];
  534. bin.x0 = i > 0 ? tz[i - 1] : x0;
  535. bin.x1 = i < m ? tz[i] : x1;
  536. }
  537. // Assign data to bins by value, ignoring any outside the domain.
  538. for (i = 0; i < n; ++i) {
  539. x = values[i];
  540. if (x != null && x0 <= x && x <= x1) {
  541. bins[bisect(tz, x, 0, m)].push(data[i]);
  542. }
  543. }
  544. return bins;
  545. }
  546. histogram.value = function(_) {
  547. return arguments.length ? (value = typeof _ === "function" ? _ : constant(_), histogram) : value;
  548. };
  549. histogram.domain = function(_) {
  550. return arguments.length ? (domain = typeof _ === "function" ? _ : constant([_[0], _[1]]), histogram) : domain;
  551. };
  552. histogram.thresholds = function(_) {
  553. return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), histogram) : threshold;
  554. };
  555. return histogram;
  556. }
  557. function max(values, valueof) {
  558. let max;
  559. if (valueof === undefined) {
  560. for (const value of values) {
  561. if (value != null
  562. && (max < value || (max === undefined && value >= value))) {
  563. max = value;
  564. }
  565. }
  566. } else {
  567. let index = -1;
  568. for (let value of values) {
  569. if ((value = valueof(value, ++index, values)) != null
  570. && (max < value || (max === undefined && value >= value))) {
  571. max = value;
  572. }
  573. }
  574. }
  575. return max;
  576. }
  577. function min(values, valueof) {
  578. let min;
  579. if (valueof === undefined) {
  580. for (const value of values) {
  581. if (value != null
  582. && (min > value || (min === undefined && value >= value))) {
  583. min = value;
  584. }
  585. }
  586. } else {
  587. let index = -1;
  588. for (let value of values) {
  589. if ((value = valueof(value, ++index, values)) != null
  590. && (min > value || (min === undefined && value >= value))) {
  591. min = value;
  592. }
  593. }
  594. }
  595. return min;
  596. }
  597. // Based on https://github.com/mourner/quickselect
  598. // ISC license, Copyright 2018 Vladimir Agafonkin.
  599. function quickselect(array, k, left = 0, right = array.length - 1, compare) {
  600. compare = compare === undefined ? ascendingDefined : compareDefined(compare);
  601. while (right > left) {
  602. if (right - left > 600) {
  603. const n = right - left + 1;
  604. const m = k - left + 1;
  605. const z = Math.log(n);
  606. const s = 0.5 * Math.exp(2 * z / 3);
  607. const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
  608. const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
  609. const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
  610. quickselect(array, k, newLeft, newRight, compare);
  611. }
  612. const t = array[k];
  613. let i = left;
  614. let j = right;
  615. swap(array, left, k);
  616. if (compare(array[right], t) > 0) swap(array, left, right);
  617. while (i < j) {
  618. swap(array, i, j), ++i, --j;
  619. while (compare(array[i], t) < 0) ++i;
  620. while (compare(array[j], t) > 0) --j;
  621. }
  622. if (compare(array[left], t) === 0) swap(array, left, j);
  623. else ++j, swap(array, j, right);
  624. if (j <= k) left = j + 1;
  625. if (k <= j) right = j - 1;
  626. }
  627. return array;
  628. }
  629. function swap(array, i, j) {
  630. const t = array[i];
  631. array[i] = array[j];
  632. array[j] = t;
  633. }
  634. function quantile(values, p, valueof) {
  635. values = Float64Array.from(numbers(values, valueof));
  636. if (!(n = values.length)) return;
  637. if ((p = +p) <= 0 || n < 2) return min(values);
  638. if (p >= 1) return max(values);
  639. var n,
  640. i = (n - 1) * p,
  641. i0 = Math.floor(i),
  642. value0 = max(quickselect(values, i0).subarray(0, i0 + 1)),
  643. value1 = min(values.subarray(i0 + 1));
  644. return value0 + (value1 - value0) * (i - i0);
  645. }
  646. function quantileSorted(values, p, valueof = number) {
  647. if (!(n = values.length)) return;
  648. if ((p = +p) <= 0 || n < 2) return +valueof(values[0], 0, values);
  649. if (p >= 1) return +valueof(values[n - 1], n - 1, values);
  650. var n,
  651. i = (n - 1) * p,
  652. i0 = Math.floor(i),
  653. value0 = +valueof(values[i0], i0, values),
  654. value1 = +valueof(values[i0 + 1], i0 + 1, values);
  655. return value0 + (value1 - value0) * (i - i0);
  656. }
  657. function thresholdFreedmanDiaconis(values, min, max) {
  658. return Math.ceil((max - min) / (2 * (quantile(values, 0.75) - quantile(values, 0.25)) * Math.pow(count(values), -1 / 3)));
  659. }
  660. function thresholdScott(values, min, max) {
  661. return Math.ceil((max - min) / (3.5 * deviation(values) * Math.pow(count(values), -1 / 3)));
  662. }
  663. function maxIndex(values, valueof) {
  664. let max;
  665. let maxIndex = -1;
  666. let index = -1;
  667. if (valueof === undefined) {
  668. for (const value of values) {
  669. ++index;
  670. if (value != null
  671. && (max < value || (max === undefined && value >= value))) {
  672. max = value, maxIndex = index;
  673. }
  674. }
  675. } else {
  676. for (let value of values) {
  677. if ((value = valueof(value, ++index, values)) != null
  678. && (max < value || (max === undefined && value >= value))) {
  679. max = value, maxIndex = index;
  680. }
  681. }
  682. }
  683. return maxIndex;
  684. }
  685. function mean(values, valueof) {
  686. let count = 0;
  687. let sum = 0;
  688. if (valueof === undefined) {
  689. for (let value of values) {
  690. if (value != null && (value = +value) >= value) {
  691. ++count, sum += value;
  692. }
  693. }
  694. } else {
  695. let index = -1;
  696. for (let value of values) {
  697. if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
  698. ++count, sum += value;
  699. }
  700. }
  701. }
  702. if (count) return sum / count;
  703. }
  704. function median(values, valueof) {
  705. return quantile(values, 0.5, valueof);
  706. }
  707. function* flatten(arrays) {
  708. for (const array of arrays) {
  709. yield* array;
  710. }
  711. }
  712. function merge(arrays) {
  713. return Array.from(flatten(arrays));
  714. }
  715. function minIndex(values, valueof) {
  716. let min;
  717. let minIndex = -1;
  718. let index = -1;
  719. if (valueof === undefined) {
  720. for (const value of values) {
  721. ++index;
  722. if (value != null
  723. && (min > value || (min === undefined && value >= value))) {
  724. min = value, minIndex = index;
  725. }
  726. }
  727. } else {
  728. for (let value of values) {
  729. if ((value = valueof(value, ++index, values)) != null
  730. && (min > value || (min === undefined && value >= value))) {
  731. min = value, minIndex = index;
  732. }
  733. }
  734. }
  735. return minIndex;
  736. }
  737. function mode(values, valueof) {
  738. const counts = new InternMap();
  739. if (valueof === undefined) {
  740. for (let value of values) {
  741. if (value != null && value >= value) {
  742. counts.set(value, (counts.get(value) || 0) + 1);
  743. }
  744. }
  745. } else {
  746. let index = -1;
  747. for (let value of values) {
  748. if ((value = valueof(value, ++index, values)) != null && value >= value) {
  749. counts.set(value, (counts.get(value) || 0) + 1);
  750. }
  751. }
  752. }
  753. let modeValue;
  754. let modeCount = 0;
  755. for (const [value, count] of counts) {
  756. if (count > modeCount) {
  757. modeCount = count;
  758. modeValue = value;
  759. }
  760. }
  761. return modeValue;
  762. }
  763. function pairs(values, pairof = pair) {
  764. const pairs = [];
  765. let previous;
  766. let first = false;
  767. for (const value of values) {
  768. if (first) pairs.push(pairof(previous, value));
  769. previous = value;
  770. first = true;
  771. }
  772. return pairs;
  773. }
  774. function pair(a, b) {
  775. return [a, b];
  776. }
  777. function range(start, stop, step) {
  778. start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
  779. var i = -1,
  780. n = Math.max(0, Math.ceil((stop - start) / step)) | 0,
  781. range = new Array(n);
  782. while (++i < n) {
  783. range[i] = start + i * step;
  784. }
  785. return range;
  786. }
  787. function least(values, compare = ascending) {
  788. let min;
  789. let defined = false;
  790. if (compare.length === 1) {
  791. let minValue;
  792. for (const element of values) {
  793. const value = compare(element);
  794. if (defined
  795. ? ascending(value, minValue) < 0
  796. : ascending(value, value) === 0) {
  797. min = element;
  798. minValue = value;
  799. defined = true;
  800. }
  801. }
  802. } else {
  803. for (const value of values) {
  804. if (defined
  805. ? compare(value, min) < 0
  806. : compare(value, value) === 0) {
  807. min = value;
  808. defined = true;
  809. }
  810. }
  811. }
  812. return min;
  813. }
  814. function leastIndex(values, compare = ascending) {
  815. if (compare.length === 1) return minIndex(values, compare);
  816. let minValue;
  817. let min = -1;
  818. let index = -1;
  819. for (const value of values) {
  820. ++index;
  821. if (min < 0
  822. ? compare(value, value) === 0
  823. : compare(value, minValue) < 0) {
  824. minValue = value;
  825. min = index;
  826. }
  827. }
  828. return min;
  829. }
  830. function greatest(values, compare = ascending) {
  831. let max;
  832. let defined = false;
  833. if (compare.length === 1) {
  834. let maxValue;
  835. for (const element of values) {
  836. const value = compare(element);
  837. if (defined
  838. ? ascending(value, maxValue) > 0
  839. : ascending(value, value) === 0) {
  840. max = element;
  841. maxValue = value;
  842. defined = true;
  843. }
  844. }
  845. } else {
  846. for (const value of values) {
  847. if (defined
  848. ? compare(value, max) > 0
  849. : compare(value, value) === 0) {
  850. max = value;
  851. defined = true;
  852. }
  853. }
  854. }
  855. return max;
  856. }
  857. function greatestIndex(values, compare = ascending) {
  858. if (compare.length === 1) return maxIndex(values, compare);
  859. let maxValue;
  860. let max = -1;
  861. let index = -1;
  862. for (const value of values) {
  863. ++index;
  864. if (max < 0
  865. ? compare(value, value) === 0
  866. : compare(value, maxValue) > 0) {
  867. maxValue = value;
  868. max = index;
  869. }
  870. }
  871. return max;
  872. }
  873. function scan(values, compare) {
  874. const index = leastIndex(values, compare);
  875. return index < 0 ? undefined : index;
  876. }
  877. var shuffle = shuffler(Math.random);
  878. function shuffler(random) {
  879. return function shuffle(array, i0 = 0, i1 = array.length) {
  880. let m = i1 - (i0 = +i0);
  881. while (m) {
  882. const i = random() * m-- | 0, t = array[m + i0];
  883. array[m + i0] = array[i + i0];
  884. array[i + i0] = t;
  885. }
  886. return array;
  887. };
  888. }
  889. function sum(values, valueof) {
  890. let sum = 0;
  891. if (valueof === undefined) {
  892. for (let value of values) {
  893. if (value = +value) {
  894. sum += value;
  895. }
  896. }
  897. } else {
  898. let index = -1;
  899. for (let value of values) {
  900. if (value = +valueof(value, ++index, values)) {
  901. sum += value;
  902. }
  903. }
  904. }
  905. return sum;
  906. }
  907. function transpose(matrix) {
  908. if (!(n = matrix.length)) return [];
  909. for (var i = -1, m = min(matrix, length), transpose = new Array(m); ++i < m;) {
  910. for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n;) {
  911. row[j] = matrix[j][i];
  912. }
  913. }
  914. return transpose;
  915. }
  916. function length(d) {
  917. return d.length;
  918. }
  919. function zip() {
  920. return transpose(arguments);
  921. }
  922. function every(values, test) {
  923. if (typeof test !== "function") throw new TypeError("test is not a function");
  924. let index = -1;
  925. for (const value of values) {
  926. if (!test(value, ++index, values)) {
  927. return false;
  928. }
  929. }
  930. return true;
  931. }
  932. function some(values, test) {
  933. if (typeof test !== "function") throw new TypeError("test is not a function");
  934. let index = -1;
  935. for (const value of values) {
  936. if (test(value, ++index, values)) {
  937. return true;
  938. }
  939. }
  940. return false;
  941. }
  942. function filter(values, test) {
  943. if (typeof test !== "function") throw new TypeError("test is not a function");
  944. const array = [];
  945. let index = -1;
  946. for (const value of values) {
  947. if (test(value, ++index, values)) {
  948. array.push(value);
  949. }
  950. }
  951. return array;
  952. }
  953. function map(values, mapper) {
  954. if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
  955. if (typeof mapper !== "function") throw new TypeError("mapper is not a function");
  956. return Array.from(values, (value, index) => mapper(value, index, values));
  957. }
  958. function reduce(values, reducer, value) {
  959. if (typeof reducer !== "function") throw new TypeError("reducer is not a function");
  960. const iterator = values[Symbol.iterator]();
  961. let done, next, index = -1;
  962. if (arguments.length < 3) {
  963. ({done, value} = iterator.next());
  964. if (done) return;
  965. ++index;
  966. }
  967. while (({done, value: next} = iterator.next()), !done) {
  968. value = reducer(value, next, ++index, values);
  969. }
  970. return value;
  971. }
  972. function reverse(values) {
  973. if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable");
  974. return Array.from(values).reverse();
  975. }
  976. function difference(values, ...others) {
  977. values = new InternSet(values);
  978. for (const other of others) {
  979. for (const value of other) {
  980. values.delete(value);
  981. }
  982. }
  983. return values;
  984. }
  985. function disjoint(values, other) {
  986. const iterator = other[Symbol.iterator](), set = new InternSet();
  987. for (const v of values) {
  988. if (set.has(v)) return false;
  989. let value, done;
  990. while (({value, done} = iterator.next())) {
  991. if (done) break;
  992. if (Object.is(v, value)) return false;
  993. set.add(value);
  994. }
  995. }
  996. return true;
  997. }
  998. function intersection(values, ...others) {
  999. values = new InternSet(values);
  1000. others = others.map(set);
  1001. out: for (const value of values) {
  1002. for (const other of others) {
  1003. if (!other.has(value)) {
  1004. values.delete(value);
  1005. continue out;
  1006. }
  1007. }
  1008. }
  1009. return values;
  1010. }
  1011. function set(values) {
  1012. return values instanceof InternSet ? values : new InternSet(values);
  1013. }
  1014. function superset(values, other) {
  1015. const iterator = values[Symbol.iterator](), set = new Set();
  1016. for (const o of other) {
  1017. const io = intern(o);
  1018. if (set.has(io)) continue;
  1019. let value, done;
  1020. while (({value, done} = iterator.next())) {
  1021. if (done) return false;
  1022. const ivalue = intern(value);
  1023. set.add(ivalue);
  1024. if (Object.is(io, ivalue)) break;
  1025. }
  1026. }
  1027. return true;
  1028. }
  1029. function intern(value) {
  1030. return value !== null && typeof value === "object" ? value.valueOf() : value;
  1031. }
  1032. function subset(values, other) {
  1033. return superset(other, values);
  1034. }
  1035. function union(...others) {
  1036. const set = new InternSet();
  1037. for (const other of others) {
  1038. for (const o of other) {
  1039. set.add(o);
  1040. }
  1041. }
  1042. return set;
  1043. }
  1044. exports.Adder = Adder;
  1045. exports.InternMap = InternMap;
  1046. exports.InternSet = InternSet;
  1047. exports.ascending = ascending;
  1048. exports.bin = bin;
  1049. exports.bisect = bisect;
  1050. exports.bisectCenter = bisectCenter;
  1051. exports.bisectLeft = bisectLeft;
  1052. exports.bisectRight = bisectRight;
  1053. exports.bisector = bisector;
  1054. exports.count = count;
  1055. exports.cross = cross;
  1056. exports.cumsum = cumsum;
  1057. exports.descending = descending;
  1058. exports.deviation = deviation;
  1059. exports.difference = difference;
  1060. exports.disjoint = disjoint;
  1061. exports.every = every;
  1062. exports.extent = extent;
  1063. exports.fcumsum = fcumsum;
  1064. exports.filter = filter;
  1065. exports.flatGroup = flatGroup;
  1066. exports.flatRollup = flatRollup;
  1067. exports.fsum = fsum;
  1068. exports.greatest = greatest;
  1069. exports.greatestIndex = greatestIndex;
  1070. exports.group = group;
  1071. exports.groupSort = groupSort;
  1072. exports.groups = groups;
  1073. exports.histogram = bin;
  1074. exports.index = index;
  1075. exports.indexes = indexes;
  1076. exports.intersection = intersection;
  1077. exports.least = least;
  1078. exports.leastIndex = leastIndex;
  1079. exports.map = map;
  1080. exports.max = max;
  1081. exports.maxIndex = maxIndex;
  1082. exports.mean = mean;
  1083. exports.median = median;
  1084. exports.merge = merge;
  1085. exports.min = min;
  1086. exports.minIndex = minIndex;
  1087. exports.mode = mode;
  1088. exports.nice = nice;
  1089. exports.pairs = pairs;
  1090. exports.permute = permute;
  1091. exports.quantile = quantile;
  1092. exports.quantileSorted = quantileSorted;
  1093. exports.quickselect = quickselect;
  1094. exports.range = range;
  1095. exports.reduce = reduce;
  1096. exports.reverse = reverse;
  1097. exports.rollup = rollup;
  1098. exports.rollups = rollups;
  1099. exports.scan = scan;
  1100. exports.shuffle = shuffle;
  1101. exports.shuffler = shuffler;
  1102. exports.some = some;
  1103. exports.sort = sort;
  1104. exports.subset = subset;
  1105. exports.sum = sum;
  1106. exports.superset = superset;
  1107. exports.thresholdFreedmanDiaconis = thresholdFreedmanDiaconis;
  1108. exports.thresholdScott = thresholdScott;
  1109. exports.thresholdSturges = thresholdSturges;
  1110. exports.tickIncrement = tickIncrement;
  1111. exports.tickStep = tickStep;
  1112. exports.ticks = ticks;
  1113. exports.transpose = transpose;
  1114. exports.union = union;
  1115. exports.variance = variance;
  1116. exports.zip = zip;
  1117. Object.defineProperty(exports, '__esModule', { value: true });
  1118. })));