d3-array.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // https://d3js.org/d3-array/ v1.2.4 Copyright 2018 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. (factory((global.d3 = global.d3 || {})));
  6. }(this, (function (exports) { 'use strict';
  7. function ascending(a, b) {
  8. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  9. }
  10. function bisector(compare) {
  11. if (compare.length === 1) compare = ascendingComparator(compare);
  12. return {
  13. left: function(a, x, lo, hi) {
  14. if (lo == null) lo = 0;
  15. if (hi == null) hi = a.length;
  16. while (lo < hi) {
  17. var mid = lo + hi >>> 1;
  18. if (compare(a[mid], x) < 0) lo = mid + 1;
  19. else hi = mid;
  20. }
  21. return lo;
  22. },
  23. right: function(a, x, lo, hi) {
  24. if (lo == null) lo = 0;
  25. if (hi == null) hi = a.length;
  26. while (lo < hi) {
  27. var mid = lo + hi >>> 1;
  28. if (compare(a[mid], x) > 0) hi = mid;
  29. else lo = mid + 1;
  30. }
  31. return lo;
  32. }
  33. };
  34. }
  35. function ascendingComparator(f) {
  36. return function(d, x) {
  37. return ascending(f(d), x);
  38. };
  39. }
  40. var ascendingBisect = bisector(ascending);
  41. var bisectRight = ascendingBisect.right;
  42. var bisectLeft = ascendingBisect.left;
  43. function pairs(array, f) {
  44. if (f == null) f = pair;
  45. var i = 0, n = array.length - 1, p = array[0], pairs = new Array(n < 0 ? 0 : n);
  46. while (i < n) pairs[i] = f(p, p = array[++i]);
  47. return pairs;
  48. }
  49. function pair(a, b) {
  50. return [a, b];
  51. }
  52. function cross(values0, values1, reduce) {
  53. var n0 = values0.length,
  54. n1 = values1.length,
  55. values = new Array(n0 * n1),
  56. i0,
  57. i1,
  58. i,
  59. value0;
  60. if (reduce == null) reduce = pair;
  61. for (i0 = i = 0; i0 < n0; ++i0) {
  62. for (value0 = values0[i0], i1 = 0; i1 < n1; ++i1, ++i) {
  63. values[i] = reduce(value0, values1[i1]);
  64. }
  65. }
  66. return values;
  67. }
  68. function descending(a, b) {
  69. return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
  70. }
  71. function number(x) {
  72. return x === null ? NaN : +x;
  73. }
  74. function variance(values, valueof) {
  75. var n = values.length,
  76. m = 0,
  77. i = -1,
  78. mean = 0,
  79. value,
  80. delta,
  81. sum = 0;
  82. if (valueof == null) {
  83. while (++i < n) {
  84. if (!isNaN(value = number(values[i]))) {
  85. delta = value - mean;
  86. mean += delta / ++m;
  87. sum += delta * (value - mean);
  88. }
  89. }
  90. }
  91. else {
  92. while (++i < n) {
  93. if (!isNaN(value = number(valueof(values[i], i, values)))) {
  94. delta = value - mean;
  95. mean += delta / ++m;
  96. sum += delta * (value - mean);
  97. }
  98. }
  99. }
  100. if (m > 1) return sum / (m - 1);
  101. }
  102. function deviation(array, f) {
  103. var v = variance(array, f);
  104. return v ? Math.sqrt(v) : v;
  105. }
  106. function extent(values, valueof) {
  107. var n = values.length,
  108. i = -1,
  109. value,
  110. min,
  111. max;
  112. if (valueof == null) {
  113. while (++i < n) { // Find the first comparable value.
  114. if ((value = values[i]) != null && value >= value) {
  115. min = max = value;
  116. while (++i < n) { // Compare the remaining values.
  117. if ((value = values[i]) != null) {
  118. if (min > value) min = value;
  119. if (max < value) max = value;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. else {
  126. while (++i < n) { // Find the first comparable value.
  127. if ((value = valueof(values[i], i, values)) != null && value >= value) {
  128. min = max = value;
  129. while (++i < n) { // Compare the remaining values.
  130. if ((value = valueof(values[i], i, values)) != null) {
  131. if (min > value) min = value;
  132. if (max < value) max = value;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. return [min, max];
  139. }
  140. var array = Array.prototype;
  141. var slice = array.slice;
  142. var map = array.map;
  143. function constant(x) {
  144. return function() {
  145. return x;
  146. };
  147. }
  148. function identity(x) {
  149. return x;
  150. }
  151. function range(start, stop, step) {
  152. start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
  153. var i = -1,
  154. n = Math.max(0, Math.ceil((stop - start) / step)) | 0,
  155. range = new Array(n);
  156. while (++i < n) {
  157. range[i] = start + i * step;
  158. }
  159. return range;
  160. }
  161. var e10 = Math.sqrt(50),
  162. e5 = Math.sqrt(10),
  163. e2 = Math.sqrt(2);
  164. function ticks(start, stop, count) {
  165. var reverse,
  166. i = -1,
  167. n,
  168. ticks,
  169. step;
  170. stop = +stop, start = +start, count = +count;
  171. if (start === stop && count > 0) return [start];
  172. if (reverse = stop < start) n = start, start = stop, stop = n;
  173. if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) return [];
  174. if (step > 0) {
  175. start = Math.ceil(start / step);
  176. stop = Math.floor(stop / step);
  177. ticks = new Array(n = Math.ceil(stop - start + 1));
  178. while (++i < n) ticks[i] = (start + i) * step;
  179. } else {
  180. start = Math.floor(start * step);
  181. stop = Math.ceil(stop * step);
  182. ticks = new Array(n = Math.ceil(start - stop + 1));
  183. while (++i < n) ticks[i] = (start - i) / step;
  184. }
  185. if (reverse) ticks.reverse();
  186. return ticks;
  187. }
  188. function tickIncrement(start, stop, count) {
  189. var step = (stop - start) / Math.max(0, count),
  190. power = Math.floor(Math.log(step) / Math.LN10),
  191. error = step / Math.pow(10, power);
  192. return power >= 0
  193. ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
  194. : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
  195. }
  196. function tickStep(start, stop, count) {
  197. var step0 = Math.abs(stop - start) / Math.max(0, count),
  198. step1 = Math.pow(10, Math.floor(Math.log(step0) / Math.LN10)),
  199. error = step0 / step1;
  200. if (error >= e10) step1 *= 10;
  201. else if (error >= e5) step1 *= 5;
  202. else if (error >= e2) step1 *= 2;
  203. return stop < start ? -step1 : step1;
  204. }
  205. function sturges(values) {
  206. return Math.ceil(Math.log(values.length) / Math.LN2) + 1;
  207. }
  208. function histogram() {
  209. var value = identity,
  210. domain = extent,
  211. threshold = sturges;
  212. function histogram(data) {
  213. var i,
  214. n = data.length,
  215. x,
  216. values = new Array(n);
  217. for (i = 0; i < n; ++i) {
  218. values[i] = value(data[i], i, data);
  219. }
  220. var xz = domain(values),
  221. x0 = xz[0],
  222. x1 = xz[1],
  223. tz = threshold(values, x0, x1);
  224. // Convert number of thresholds into uniform thresholds.
  225. if (!Array.isArray(tz)) {
  226. tz = tickStep(x0, x1, tz);
  227. tz = range(Math.ceil(x0 / tz) * tz, x1, tz); // exclusive
  228. }
  229. // Remove any thresholds outside the domain.
  230. var m = tz.length;
  231. while (tz[0] <= x0) tz.shift(), --m;
  232. while (tz[m - 1] > x1) tz.pop(), --m;
  233. var bins = new Array(m + 1),
  234. bin;
  235. // Initialize bins.
  236. for (i = 0; i <= m; ++i) {
  237. bin = bins[i] = [];
  238. bin.x0 = i > 0 ? tz[i - 1] : x0;
  239. bin.x1 = i < m ? tz[i] : x1;
  240. }
  241. // Assign data to bins by value, ignoring any outside the domain.
  242. for (i = 0; i < n; ++i) {
  243. x = values[i];
  244. if (x0 <= x && x <= x1) {
  245. bins[bisectRight(tz, x, 0, m)].push(data[i]);
  246. }
  247. }
  248. return bins;
  249. }
  250. histogram.value = function(_) {
  251. return arguments.length ? (value = typeof _ === "function" ? _ : constant(_), histogram) : value;
  252. };
  253. histogram.domain = function(_) {
  254. return arguments.length ? (domain = typeof _ === "function" ? _ : constant([_[0], _[1]]), histogram) : domain;
  255. };
  256. histogram.thresholds = function(_) {
  257. return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), histogram) : threshold;
  258. };
  259. return histogram;
  260. }
  261. function quantile(values, p, valueof) {
  262. if (valueof == null) valueof = number;
  263. if (!(n = values.length)) return;
  264. if ((p = +p) <= 0 || n < 2) return +valueof(values[0], 0, values);
  265. if (p >= 1) return +valueof(values[n - 1], n - 1, values);
  266. var n,
  267. i = (n - 1) * p,
  268. i0 = Math.floor(i),
  269. value0 = +valueof(values[i0], i0, values),
  270. value1 = +valueof(values[i0 + 1], i0 + 1, values);
  271. return value0 + (value1 - value0) * (i - i0);
  272. }
  273. function freedmanDiaconis(values, min, max) {
  274. values = map.call(values, number).sort(ascending);
  275. return Math.ceil((max - min) / (2 * (quantile(values, 0.75) - quantile(values, 0.25)) * Math.pow(values.length, -1 / 3)));
  276. }
  277. function scott(values, min, max) {
  278. return Math.ceil((max - min) / (3.5 * deviation(values) * Math.pow(values.length, -1 / 3)));
  279. }
  280. function max(values, valueof) {
  281. var n = values.length,
  282. i = -1,
  283. value,
  284. max;
  285. if (valueof == null) {
  286. while (++i < n) { // Find the first comparable value.
  287. if ((value = values[i]) != null && value >= value) {
  288. max = value;
  289. while (++i < n) { // Compare the remaining values.
  290. if ((value = values[i]) != null && value > max) {
  291. max = value;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. else {
  298. while (++i < n) { // Find the first comparable value.
  299. if ((value = valueof(values[i], i, values)) != null && value >= value) {
  300. max = value;
  301. while (++i < n) { // Compare the remaining values.
  302. if ((value = valueof(values[i], i, values)) != null && value > max) {
  303. max = value;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return max;
  310. }
  311. function mean(values, valueof) {
  312. var n = values.length,
  313. m = n,
  314. i = -1,
  315. value,
  316. sum = 0;
  317. if (valueof == null) {
  318. while (++i < n) {
  319. if (!isNaN(value = number(values[i]))) sum += value;
  320. else --m;
  321. }
  322. }
  323. else {
  324. while (++i < n) {
  325. if (!isNaN(value = number(valueof(values[i], i, values)))) sum += value;
  326. else --m;
  327. }
  328. }
  329. if (m) return sum / m;
  330. }
  331. function median(values, valueof) {
  332. var n = values.length,
  333. i = -1,
  334. value,
  335. numbers = [];
  336. if (valueof == null) {
  337. while (++i < n) {
  338. if (!isNaN(value = number(values[i]))) {
  339. numbers.push(value);
  340. }
  341. }
  342. }
  343. else {
  344. while (++i < n) {
  345. if (!isNaN(value = number(valueof(values[i], i, values)))) {
  346. numbers.push(value);
  347. }
  348. }
  349. }
  350. return quantile(numbers.sort(ascending), 0.5);
  351. }
  352. function merge(arrays) {
  353. var n = arrays.length,
  354. m,
  355. i = -1,
  356. j = 0,
  357. merged,
  358. array;
  359. while (++i < n) j += arrays[i].length;
  360. merged = new Array(j);
  361. while (--n >= 0) {
  362. array = arrays[n];
  363. m = array.length;
  364. while (--m >= 0) {
  365. merged[--j] = array[m];
  366. }
  367. }
  368. return merged;
  369. }
  370. function min(values, valueof) {
  371. var n = values.length,
  372. i = -1,
  373. value,
  374. min;
  375. if (valueof == null) {
  376. while (++i < n) { // Find the first comparable value.
  377. if ((value = values[i]) != null && value >= value) {
  378. min = value;
  379. while (++i < n) { // Compare the remaining values.
  380. if ((value = values[i]) != null && min > value) {
  381. min = value;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. else {
  388. while (++i < n) { // Find the first comparable value.
  389. if ((value = valueof(values[i], i, values)) != null && value >= value) {
  390. min = value;
  391. while (++i < n) { // Compare the remaining values.
  392. if ((value = valueof(values[i], i, values)) != null && min > value) {
  393. min = value;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. return min;
  400. }
  401. function permute(array, indexes) {
  402. var i = indexes.length, permutes = new Array(i);
  403. while (i--) permutes[i] = array[indexes[i]];
  404. return permutes;
  405. }
  406. function scan(values, compare) {
  407. if (!(n = values.length)) return;
  408. var n,
  409. i = 0,
  410. j = 0,
  411. xi,
  412. xj = values[j];
  413. if (compare == null) compare = ascending;
  414. while (++i < n) {
  415. if (compare(xi = values[i], xj) < 0 || compare(xj, xj) !== 0) {
  416. xj = xi, j = i;
  417. }
  418. }
  419. if (compare(xj, xj) === 0) return j;
  420. }
  421. function shuffle(array, i0, i1) {
  422. var m = (i1 == null ? array.length : i1) - (i0 = i0 == null ? 0 : +i0),
  423. t,
  424. i;
  425. while (m) {
  426. i = Math.random() * m-- | 0;
  427. t = array[m + i0];
  428. array[m + i0] = array[i + i0];
  429. array[i + i0] = t;
  430. }
  431. return array;
  432. }
  433. function sum(values, valueof) {
  434. var n = values.length,
  435. i = -1,
  436. value,
  437. sum = 0;
  438. if (valueof == null) {
  439. while (++i < n) {
  440. if (value = +values[i]) sum += value; // Note: zero and null are equivalent.
  441. }
  442. }
  443. else {
  444. while (++i < n) {
  445. if (value = +valueof(values[i], i, values)) sum += value;
  446. }
  447. }
  448. return sum;
  449. }
  450. function transpose(matrix) {
  451. if (!(n = matrix.length)) return [];
  452. for (var i = -1, m = min(matrix, length), transpose = new Array(m); ++i < m;) {
  453. for (var j = -1, n, row = transpose[i] = new Array(n); ++j < n;) {
  454. row[j] = matrix[j][i];
  455. }
  456. }
  457. return transpose;
  458. }
  459. function length(d) {
  460. return d.length;
  461. }
  462. function zip() {
  463. return transpose(arguments);
  464. }
  465. exports.bisect = bisectRight;
  466. exports.bisectRight = bisectRight;
  467. exports.bisectLeft = bisectLeft;
  468. exports.ascending = ascending;
  469. exports.bisector = bisector;
  470. exports.cross = cross;
  471. exports.descending = descending;
  472. exports.deviation = deviation;
  473. exports.extent = extent;
  474. exports.histogram = histogram;
  475. exports.thresholdFreedmanDiaconis = freedmanDiaconis;
  476. exports.thresholdScott = scott;
  477. exports.thresholdSturges = sturges;
  478. exports.max = max;
  479. exports.mean = mean;
  480. exports.median = median;
  481. exports.merge = merge;
  482. exports.min = min;
  483. exports.pairs = pairs;
  484. exports.permute = permute;
  485. exports.quantile = quantile;
  486. exports.range = range;
  487. exports.scan = scan;
  488. exports.shuffle = shuffle;
  489. exports.sum = sum;
  490. exports.ticks = ticks;
  491. exports.tickIncrement = tickIncrement;
  492. exports.tickStep = tickStep;
  493. exports.transpose = transpose;
  494. exports.variance = variance;
  495. exports.zip = zip;
  496. Object.defineProperty(exports, '__esModule', { value: true });
  497. })));