Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cross-count.js #395

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/order/cross-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var zipObject = require("../util").zipObject;
let zipObject = require("../util").zipObject;

module.exports = crossCount;

Expand All @@ -21,8 +21,8 @@ module.exports = crossCount;
* This algorithm is derived from Barth, et al., "Bilayer Cross Counting."
*/
function crossCount(g, layering) {
var cc = 0;
for (var i = 1; i < layering.length; ++i) {
let cc = 0;
for (let i = 1; i < layering.length; ++i) {
cc += twoLayerCrossCount(g, layering[i-1], layering[i]);
}
return cc;
Expand All @@ -32,26 +32,26 @@ function twoLayerCrossCount(g, northLayer, southLayer) {
// Sort all of the edges between the north and south layers by their position
// in the north layer and then the south. Map these edges to the position of
// their head in the south layer.
var southPos = zipObject(southLayer, southLayer.map((v, i) => i));
var southEntries = northLayer.flatMap(v => {
let southPos = zipObject(southLayer, southLayer.map((v, i) => i));
let southEntries = northLayer.flatMap(v => {
return g.outEdges(v).map(e => {
return { pos: southPos[e.w], weight: g.edge(e).weight };
}).sort((a, b) => a.pos - b.pos);
});

// Build the accumulator tree
var firstIndex = 1;
let firstIndex = 1;
while (firstIndex < southLayer.length) firstIndex <<= 1;
var treeSize = 2 * firstIndex - 1;
let treeSize = 2 * firstIndex - 1;
firstIndex -= 1;
var tree = new Array(treeSize).fill(0);
let tree = new Array(treeSize).fill(0);

// Calculate the weighted crossings
var cc = 0;
let cc = 0;
southEntries.forEach(entry => {
var index = entry.pos + firstIndex;
let index = entry.pos + firstIndex;
tree[index] += entry.weight;
var weightSum = 0;
let weightSum = 0;
while (index > 0) {
if (index % 2) {
weightSum += tree[index + 1];
Expand Down