Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Avoid setRange with potentially incompatible types
Browse files Browse the repository at this point in the history
Fixes #317
  • Loading branch information
natebosch committed Nov 2, 2023
1 parent 1f5c234 commit 223033d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Adds `shuffled` to `IterableExtension`.
- Shuffle `IterableExtension.sample` results.
- Fix `mergeSort` when the runtime iterable generic is a subtype of the static
generic.

## 1.18.0

Expand Down
5 changes: 3 additions & 2 deletions lib/src/algorithms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ void _merge<E, K>(
}
// First list empties first. Reached by break above.
target[targetOffset++] = secondElement;
target.setRange(
targetOffset, targetOffset + (secondEnd - cursor2), secondList, cursor2);
for (var i = targetOffset; i < targetOffset + (secondEnd - cursor2); i++) {
target[i] = secondList[(i - targetOffset) + cursor2];
}
}

/// Sort [elements] using a quick-sort algorithm.
Expand Down
9 changes: 9 additions & 0 deletions test/algorithms_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,15 @@ void main() {
reverse(l, 0, 6);
expect(l, equals([2, 1, 4, 3, 6, 5]));
});

test('mergeSort works when runtime generic is a subtype of the static type',
() {
// Regression test for https://github.com/dart-lang/collection/issues/317
final length = 32; // _mergeSortLimit
// In order list, first half empties first during merge.
final list = List<int>.generate(length, (i) => i);
expect(() => mergeSort<num>(list), returnsNormally);
});
}

class C {
Expand Down

0 comments on commit 223033d

Please sign in to comment.