Skip to content

Commit 6495c34

Browse files
committed
more inlining
1 parent d06d4bf commit 6495c34

File tree

1 file changed

+28
-39
lines changed

1 file changed

+28
-39
lines changed

src/transforms/stack.js

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function stack(x, y = one, kx, ky, {offset, order, reverse}, options) {
9494
const facetstacks = [];
9595
for (const facet of facets) {
9696
const stacks = X ? Array.from(group(facet, (i) => X[i]).values()) : [facet];
97-
if (compare) applyOrder(stacks, compare);
97+
if (compare) for (const stack of stacks) stack.sort(compare);
9898
for (const stack of stacks) {
9999
let yn = 0;
100100
let yp = 0;
@@ -228,23 +228,23 @@ function offsetCenterFacets(facetstacks, Y1, Y2) {
228228
}
229229

230230
function maybeOrder(order, offset, ky) {
231-
if (order === undefined && offset === offsetWiggle) return orderInsideOut(orderAscending);
231+
if (order === undefined && offset === offsetWiggle) return orderInsideOut(ascendingDefined);
232232
if (order == null) return;
233233
if (typeof order === "string") {
234234
const negate = order.startsWith("-");
235-
const direction = negate ? orderDescending : orderAscending;
235+
const compare = negate ? descendingDefined : ascendingDefined;
236236
switch ((negate ? order.slice(1) : order).toLowerCase()) {
237237
case "value":
238238
case ky:
239-
return orderY(direction);
239+
return orderY(compare);
240240
case "z":
241-
return orderZ(direction);
241+
return orderZ(compare);
242242
case "sum":
243-
return orderSum(direction);
243+
return orderSum(compare);
244244
case "appearance":
245-
return orderAppearance(direction);
245+
return orderAppearance(compare);
246246
case "inside-out":
247-
return orderInsideOut(direction);
247+
return orderInsideOut(compare);
248248
}
249249
return orderAccessor(field(order));
250250
}
@@ -254,18 +254,18 @@ function maybeOrder(order, offset, ky) {
254254
}
255255

256256
// by value
257-
function orderY(order) {
258-
return (data, X, Y) => order(Y);
257+
function orderY(compare) {
258+
return (data, X, Y) => (i, j) => compare(Y[i], Y[j]);
259259
}
260260

261261
// by location
262-
function orderZ(order) {
263-
return (data, X, Y, Z) => order(Z);
262+
function orderZ(compare) {
263+
return (data, X, Y, Z) => (i, j) => compare(Z[i], Z[j]);
264264
}
265265

266266
// by sum of value (a.k.a. “ascending”)
267-
function orderSum(order) {
268-
return orderZDomain(order, (data, X, Y, Z) =>
267+
function orderSum(compare) {
268+
return orderZDomain(compare, (data, X, Y, Z) =>
269269
groupSort(
270270
range(data),
271271
(I) => sum(I, (i) => Y[i]),
@@ -275,8 +275,8 @@ function orderSum(order) {
275275
}
276276

277277
// by x = argmax of value
278-
function orderAppearance(order) {
279-
return orderZDomain(order, (data, X, Y, Z) =>
278+
function orderAppearance(compare) {
279+
return orderZDomain(compare, (data, X, Y, Z) =>
280280
groupSort(
281281
range(data),
282282
(I) => X[greatest(I, (i) => Y[i])],
@@ -287,8 +287,8 @@ function orderAppearance(order) {
287287

288288
// by x = argmax of value, but rearranged inside-out by alternating series
289289
// according to the sign of a running divergence of sums
290-
function orderInsideOut(order) {
291-
return orderZDomain(order, (data, X, Y, Z) => {
290+
function orderInsideOut(compare) {
291+
return orderZDomain(compare, (data, X, Y, Z) => {
292292
const I = range(data);
293293
const K = groupSort(
294294
I,
@@ -316,39 +316,28 @@ function orderInsideOut(order) {
316316
});
317317
}
318318

319-
function orderAscending(O) {
320-
return (i, j) => ascendingDefined(O[i], O[j]);
321-
}
322-
323-
function orderDescending(O) {
324-
return (i, j) => descendingDefined(O[i], O[j]);
325-
}
326-
327319
function orderAccessor(f) {
328-
return (data) => orderAscending(valueof(data, f));
320+
return (data) => {
321+
const O = valueof(data, f);
322+
return (i, j) => ascendingDefined(O[i], O[j]);
323+
};
329324
}
330325

331326
function orderComparator(f) {
332327
return (data) => (i, j) => f(data[i], data[j]);
333328
}
334329

335330
function orderGiven(domain) {
336-
return orderZDomain(orderAscending, () => domain);
331+
return orderZDomain(ascendingDefined, () => domain);
337332
}
338333

339-
// Given an explicit ordering of distinct values in z, returns a parallel column
340-
// O that can be used with applyOrder to sort stacks. Note that this is a series
341-
// order: it will be consistent across stacks.
342-
function orderZDomain(order, domain) {
334+
// Given an ordering (domain) of distinct values in z that can be derived from
335+
// the data, returns a comparator that can be used to sort stacks. Note that
336+
// this is a series order: it will be consistent across stacks.
337+
function orderZDomain(compare, domain) {
343338
return (data, X, Y, Z) => {
344339
if (!Z) throw new Error("missing channel: z");
345340
const map = new InternMap(domain(data, X, Y, Z).map((d, i) => [d, i]));
346-
return order(Z.map((z) => map.get(z)));
341+
return (i, j) => compare(map.get(Z[i]), map.get(Z[j]));
347342
};
348343
}
349-
350-
function applyOrder(stacks, compare) {
351-
for (const stack of stacks) {
352-
stack.sort(compare);
353-
}
354-
}

0 commit comments

Comments
 (0)