nc-photos/app/lib/list_extension.dart

42 lines
1 KiB
Dart
Raw Normal View History

2022-01-01 18:25:50 +01:00
import 'dart:math' as math;
2022-06-06 10:33:56 +02:00
import 'package:collection/collection.dart';
2022-06-06 12:06:15 +02:00
import 'package:nc_photos/int_extension.dart';
2022-06-06 10:33:56 +02:00
2021-04-10 06:28:12 +02:00
extension ListExtension<T> on List<T> {
Iterable<T> takeIndex(List<int> indexes) => indexes.map((e) => this[e]);
2022-01-01 18:25:50 +01:00
List<T> slice(int start, [int? stop]) {
if (start < 0) {
start = math.max(length + start, 0);
}
if (stop != null && stop < 0) {
stop = math.max(length + stop, 0);
}
if (start >= length) {
return [];
} else if (stop == null) {
return sublist(start);
} else if (start >= stop) {
return [];
} else {
return sublist(start, math.min(stop, length));
}
}
2022-06-06 10:33:56 +02:00
void stableSort([int Function(T a, T b)? compare]) {
mergeSort(this, compare: compare);
}
2022-06-06 12:06:15 +02:00
/// In-place transform and return this
///
/// Since the elements are in-place transformed, they have to share the same
/// type
List<T> transform(T Function(T element) fn) {
for (final i in 0.until(length)) {
this[i] = fn(this[i]);
}
return this;
}
2021-04-10 06:28:12 +02:00
}