2021-11-17 20:38:04 +01:00
|
|
|
import 'package:nc_photos/override_comparator.dart';
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
extension ListExtension<T> on List<T> {
|
|
|
|
/// Return a new list with only distinct elements
|
|
|
|
List<T> distinct() {
|
2021-09-15 08:58:06 +02:00
|
|
|
final s = <T>{};
|
|
|
|
return where((element) => s.add(element)).toList();
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:34:04 +02:00
|
|
|
/// Return a new list with only distinct elements determined by [equalFn]
|
|
|
|
List<T> distinctIf(
|
|
|
|
bool Function(T a, T b) equalFn, int Function(T a) hashCodeFn) {
|
2021-11-17 20:38:04 +01:00
|
|
|
final s = <OverrideComparator<T>>{};
|
2021-09-15 08:58:06 +02:00
|
|
|
return where((element) =>
|
2021-11-17 20:38:04 +01:00
|
|
|
s.add(OverrideComparator<T>(element, equalFn, hashCodeFn))).toList();
|
2021-04-18 13:34:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
Iterable<T> takeIndex(List<int> indexes) => indexes.map((e) => this[e]);
|
|
|
|
}
|