Skip to content

Commit dacc61d

Browse files
♻️ refactor(blossom): Replace some while loops by native functionality.
1 parent d272171 commit dacc61d

File tree

1 file changed

+13
-35
lines changed

1 file changed

+13
-35
lines changed

src/core/blossom/blossom.js

+13-35
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
103103
// mate[v] is the remote endpoint of its matched edge, or -1 if it is single
104104
// (i.e. endpoint[mate[v]] is v's partner vertex).
105105
// Initially all vertices are single; updated during augmentation.
106-
i = nvertex;
107-
const mate = new Array(i);
108-
while (i--) mate[i] = -1;
106+
const mate = new Array(nvertex).fill(-1);
109107

110108
// If b is a top-level blossom,
111109
// label[b] is 0 if b is unlabeled (free);
@@ -116,19 +114,15 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
116114
// If v is a vertex inside a T-blossom,
117115
// label[v] is 2 iff v is reachable from an S-vertex outside the blossom.
118116
// Labels are assigned during a stage and reset after each augmentation.
119-
i = 2 * nvertex;
120-
const label = new Array(i);
121-
while (i--) label[i] = 0;
117+
const label = new Array(2 * nvertex).fill(0);
122118

123119
// If b is a labeled top-level blossom,
124120
// labelend[b] is the remote endpoint of the edge through which b obtained
125121
// its label, or -1 if b's base vertex is single.
126122
// If v is a vertex inside a T-blossom and label[v] === 2,
127123
// labelend[v] is the remote endpoint of the edge through which v is
128124
// reachable from outside the blossom.
129-
i = 2 * nvertex;
130-
const labelend = new Array(i);
131-
while (i--) labelend[i] = -1;
125+
const labelend = new Array(2 * nvertex).fill(-1);
132126

133127
// If v is a vertex,
134128
// inblossom[v] is the top-level blossom to which v belongs.
@@ -142,16 +136,12 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
142136
// If b is a sub-blossom,
143137
// blossomparent[b] is its immediate parent (sub-)blossom.
144138
// If b is a top-level blossom, blossomparent[b] is -1.
145-
i = 2 * nvertex;
146-
const blossomparent = new Array(i);
147-
while (i--) blossomparent[i] = -1;
139+
const blossomparent = new Array(2 * nvertex).fill(-1);
148140

149141
// If b is a non-trivial (sub-)blossom,
150142
// blossomchilds[b] is an ordered list of its sub-blossoms, starting with
151143
// the base and going round the blossom.
152-
i = 2 * nvertex;
153-
const blossomchilds = new Array(i);
154-
while (i--) blossomchilds[i] = null;
144+
const blossomchilds = new Array(2 * nvertex).fill(null);
155145

156146
// If b is a (sub-)blossom,
157147
// blossombase[b] is its base VERTEX (i.e. recursive sub-blossom).
@@ -164,9 +154,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
164154
// blossomendps[b] is a list of endpoints on its connecting edges,
165155
// such that blossomendps[b][i] is the local endpoint of blossomchilds[b][i]
166156
// on the edge that connects it to blossomchilds[b][wrap(i+1)].
167-
i = 2 * nvertex;
168-
const blossomendps = new Array(i);
169-
while (i--) blossomendps[i] = null;
157+
const blossomendps = new Array(2 * nvertex).fill(null);
170158

171159
// If v is a free vertex (or an unreached vertex inside a T-blossom),
172160
// bestedge[v] is the edge to an S-vertex with least slack,
@@ -175,17 +163,13 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
175163
// bestedge[b] is the least-slack edge to a different S-blossom,
176164
// or -1 if there is no such edge.
177165
// This is used for efficient computation of delta2 and delta3.
178-
i = 2 * nvertex;
179-
const bestedge = new Array(i);
180-
while (i--) bestedge[i] = -1;
166+
const bestedge = new Array(2 * nvertex).fill(-1);
181167

182168
// If b is a non-trivial top-level S-blossom,
183169
// blossombestedges[b] is a list of least-slack edges to neighbouring
184170
// S-blossoms, or null if no such list has been computed yet.
185171
// This is used for efficient computation of delta3.
186-
i = 2 * nvertex;
187-
const blossombestedges = new Array(i);
188-
while (i--) blossombestedges[i] = null;
172+
const blossombestedges = new Array(2 * nvertex).fill(null);
189173

190174
// List of currently unused blossom numbers.
191175
i = nvertex;
@@ -207,9 +191,7 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
207191
// If allowedge[k] is true, edge k has zero slack in the optimization
208192
// problem; if allowedge[k] is false, the edge's slack may or may not
209193
// be zero.
210-
i = nedge;
211-
const allowedge = new Array(i);
212-
while (i--) allowedge[i] = false;
194+
const allowedge = new Array(nedge).fill(false);
213195

214196
// Queue of newly discovered S-vertices.
215197
let queue = [];
@@ -774,19 +756,15 @@ export default function blossom(CHECK_OPTIMUM, CHECK_DELTA) {
774756
console.debug('DEBUG: STAGE ' + t);
775757

776758
// Remove labels from top-level blossoms/vertices.
777-
i = 2 * nvertex;
778-
while (i--) label[i] = 0;
759+
label.fill(0);
779760

780761
// Forget all about least-slack edges.
781-
i = 2 * nvertex;
782-
while (i--) bestedge[i] = -1;
783-
i = nvertex;
784-
while (i--) blossombestedges[nvertex + i] = null;
762+
bestedge.fill(-1);
763+
blossombestedges.fill(null, nvertex, 2 * nvertex);
785764

786765
// Loss of labeling means that we can not be sure that currently
787766
// allowable edges remain allowable througout this stage.
788-
i = nedge;
789-
while (i--) allowedge[i] = false;
767+
allowedge.fill(false);
790768

791769
// Make queue empty.
792770
queue = [];

0 commit comments

Comments
 (0)