(values, valueof = ascending)
| 2 | import {ascendingDefined, compareDefined} from "./sort.js"; |
| 3 | |
| 4 | export default function rank(values, valueof = ascending) { |
| 5 | if (typeof values[Symbol.iterator] !== "function") throw new TypeError("values is not iterable"); |
| 6 | let V = Array.from(values); |
| 7 | const R = new Float64Array(V.length); |
| 8 | if (valueof.length !== 2) V = V.map(valueof), valueof = ascending; |
| 9 | const compareIndex = (i, j) => valueof(V[i], V[j]); |
| 10 | let k, r; |
| 11 | values = Uint32Array.from(V, (_, i) => i); |
| 12 | // Risky chaining due to Safari 14 https://github.com/d3/d3-array/issues/123 |
| 13 | values.sort(valueof === ascending ? (i, j) => ascendingDefined(V[i], V[j]) : compareDefined(compareIndex)); |
| 14 | values.forEach((j, i) => { |
| 15 | const c = compareIndex(j, k === undefined ? j : k); |
| 16 | if (c >= 0) { |
| 17 | if (k === undefined || c > 0) k = j, r = i; |
| 18 | R[j] = r; |
| 19 | } else { |
| 20 | R[j] = NaN; |
| 21 | } |
| 22 | }); |
| 23 | return R; |
| 24 | } |
no test coverage detected
searching dependent graphs…