Skip to content

Commit a54b91f

Browse files
committed
remove use of swap_remove and compress the list as we go instead
1 parent b247402 commit a54b91f

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/librustc_data_structures/transitive_relation.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ impl<T:Debug+PartialEq> TransitiveRelation<T> {
267267
/// there exists an earlier element i<j such that i -> j. That is,
268268
/// after you run `pare_down`, you know that for all elements that
269269
/// remain in candidates, they cannot reach any of the elements that
270-
/// come after them. (Note that it may permute the ordering in some
271-
/// cases.)
270+
/// come after them.
272271
///
273272
/// Examples follow. Assume that a -> b -> c and x -> y -> z.
274273
///
@@ -278,24 +277,24 @@ impl<T:Debug+PartialEq> TransitiveRelation<T> {
278277
fn pare_down(candidates: &mut Vec<usize>, closure: &BitMatrix) {
279278
let mut i = 0;
280279
while i < candidates.len() {
281-
let candidate = candidates[i];
280+
let candidate_i = candidates[i];
282281
i += 1;
283282

284283
let mut j = i;
284+
let mut dead = 0;
285285
while j < candidates.len() {
286-
if closure.contains(candidate, candidates[j]) {
287-
// If `i` can reach `j`, then we can remove `j`. Given
288-
// how careful this algorithm is about ordering, it
289-
// may seem odd to use swap-remove. The reason it is
290-
// ok is that we are swapping two elements (`j` and
291-
// `max`) that are both to the right of our cursor
292-
// `i`, and the invariant that we are establishing
293-
// continues to hold for everything left of `i`.
294-
candidates.swap_remove(j);
286+
let candidate_j = candidates[j];
287+
if closure.contains(candidate_i, candidate_j) {
288+
// If `i` can reach `j`, then we can remove `j`. So just
289+
// mark it as dead and move on; subsequent indices will be
290+
// shifted into its place.
291+
dead += 1;
295292
} else {
296-
j += 1;
293+
candidates[j - dead] = candidate_j;
297294
}
295+
j += 1;
298296
}
297+
candidates.truncate(j - dead);
299298
}
300299
}
301300

0 commit comments

Comments
 (0)