@@ -267,8 +267,7 @@ impl<T:Debug+PartialEq> TransitiveRelation<T> {
267
267
/// there exists an earlier element i<j such that i -> j. That is,
268
268
/// after you run `pare_down`, you know that for all elements that
269
269
/// 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.
272
271
///
273
272
/// Examples follow. Assume that a -> b -> c and x -> y -> z.
274
273
///
@@ -278,24 +277,24 @@ impl<T:Debug+PartialEq> TransitiveRelation<T> {
278
277
fn pare_down ( candidates : & mut Vec < usize > , closure : & BitMatrix ) {
279
278
let mut i = 0 ;
280
279
while i < candidates. len ( ) {
281
- let candidate = candidates[ i] ;
280
+ let candidate_i = candidates[ i] ;
282
281
i += 1 ;
283
282
284
283
let mut j = i;
284
+ let mut dead = 0 ;
285
285
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 ;
295
292
} else {
296
- j += 1 ;
293
+ candidates [ j - dead ] = candidate_j ;
297
294
}
295
+ j += 1 ;
298
296
}
297
+ candidates. truncate ( j - dead) ;
299
298
}
300
299
}
301
300
0 commit comments