Skip to content

Commit 188e7b3

Browse files
♻️ refactor(blossomLeaves): Use a generator.
1 parent 3749be6 commit 188e7b3

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

src/core/blossom/blossom.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
209209
bestedge[b] = -1;
210210
if (t === 1) {
211211
// B became an S-vertex/blossom; add it(s vertices) to the queue.
212-
blossomLeaves(nvertex, blossomchilds, b, function (v) {
212+
for (const v of blossomLeaves(nvertex, blossomchilds, b)) {
213213
queue.push(v);
214-
});
214+
}
215+
215216
console.debug('DEBUG: PUSH ' + queue);
216217
} else if (t === 2) {
217218
// B became a T-vertex/blossom; assign label S to its mate.
@@ -363,15 +364,15 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
363364
// Set dual variable to zero.
364365
dualvar[b] = 0;
365366
// Relabel vertices.
366-
blossomLeaves(nvertex, blossomchilds, b, function (v) {
367+
for (const v of blossomLeaves(nvertex, blossomchilds, b)) {
367368
if (label[inblossom[v]] === 2) {
368369
// This T-vertex now turns into an S-vertex because it becomes
369370
// part of an S-blossom; add it to the queue.
370371
queue.push(v);
371372
}
372373

373374
inblossom[v] = b;
374-
});
375+
}
375376

376377
// Compute blossombestedges[b].
377378

@@ -387,7 +388,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
387388
// This subblossom does not have a list of least-slack edges;
388389
// get the information from the vertices.
389390
nblists = [];
390-
blossomLeaves(nvertex, blossomchilds, bv, function (v) {
391+
for (const v of blossomLeaves(nvertex, blossomchilds, bv)) {
391392
j = neighbend[v].length;
392393
temporary_ = new Array(j);
393394
while (j--) {
@@ -396,7 +397,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
396397
}
397398

398399
nblists.push(temporary_);
399-
});
400+
}
400401
} else {
401402
// Walk this subblossom's least-slack edges.
402403
nblists = [blossombestedges[bv]];
@@ -483,9 +484,9 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
483484
// Recursively expand this sub-blossom.
484485
expandBlossom(s, endstage);
485486
} else {
486-
blossomLeaves(nvertex, blossomchilds, s, function (v) {
487+
for (const v of blossomLeaves(nvertex, blossomchilds, s)) {
487488
inblossom[v] = s;
488-
});
489+
}
489490
}
490491
}
491492

@@ -553,7 +554,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
553554
continue;
554555
}
555556

556-
blossomLeaves(nvertex, blossomchilds, bv, function (v) {
557+
for (const v of blossomLeaves(nvertex, blossomchilds, bv)) {
557558
if (label[v] !== 0) {
558559
// If the sub-blossom contains a reachable vertex, assign
559560
// label T to the sub-blossom.
@@ -562,9 +563,9 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
562563
label[v] = 0;
563564
label[endpoint[mate[blossombase[bv]]]] = 0;
564565
assignLabel(v, 2, labelend[v]);
565-
return true;
566+
break;
566567
}
567-
});
568+
}
568569

569570
j += jstep;
570571
}

src/core/blossom/blossomLeaves.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/**
22
* Generate the leaf vertices of a blossom.
33
*/
4-
const blossomLeaves = (nvertex, nodes, b, fn) => {
5-
if (b < nvertex) {
6-
if (fn(b)) return true;
7-
} else {
8-
const length_ = nodes[b].length;
9-
for (let i = 0; i < length_; ++i) {
10-
const t = nodes[b][i];
11-
if (t < nvertex) {
12-
if (fn(t)) return true;
13-
} else if (blossomLeaves(nvertex, nodes, t, fn)) return true;
4+
export default function* blossomLeaves(nvertex, nodes, b) {
5+
if (b < nvertex) yield b;
6+
else {
7+
for (const t of nodes[b]) {
8+
if (t < nvertex) yield t;
9+
else yield* blossomLeaves(nvertex, nodes, t);
1410
}
1511
}
16-
};
17-
18-
export default blossomLeaves;
12+
}

src/core/blossom/checkDelta3.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const checkDelta3 = ({
2020
let tbd = null;
2121
for (let b = 0; b < 2 * nvertex; ++b) {
2222
if (blossomparent[b] === -1 && label[b] === 1) {
23-
blossomLeaves(nvertex, blossomchilds, b, function (v) {
23+
for (const v of blossomLeaves(nvertex, blossomchilds, b)) {
2424
for (let x = 0; x < neighbend[v].length; ++x) {
2525
const p = neighbend[v][x];
2626
const k = Math.floor(p / 2);
@@ -33,7 +33,7 @@ const checkDelta3 = ({
3333
}
3434
}
3535
}
36-
});
36+
}
3737

3838
if (bestedge[b] !== -1) {
3939
const i = edges[bestedge[b]][0];

0 commit comments

Comments
 (0)