/
collection.dart
150 lines (129 loc) · 3.59 KB
/
collection.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'package:collection/collection.dart';
extension IterableExtension<E> on Iterable<E> {
/// Note, elements must be unique.
Map<E, int> get asElementToIndexMap {
return {
for (var (index, element) in indexed) element: index,
};
}
/// Returns the fixed-length [List] with elements of `this`.
List<E> toFixedList() {
var result = toList(growable: false);
if (result.isEmpty) {
return const <Never>[];
}
return result;
}
Iterable<E> whereNotType<U>() {
return whereNot((element) => element is U);
}
}
extension IterableIterableExtension<T> on Iterable<Iterable<T>> {
/// Elements of each iterable in this iterable.
///
/// At the moment of writing, this method is `2.75` times faster than
/// `expand((e) => e)`, and `3.5` faster than `flattened` from
/// `package:collection`.
List<T> get flattenedToList2 {
return [
for (var elements in this) ...elements,
];
}
/// Elements of each iterable in this iterable.
Set<T> get flattenedToSet2 {
return {
for (var elements in this) ...elements,
};
}
}
extension IterableMapEntryExtension<K, V> on Iterable<MapEntry<K, V>> {
Map<K, V> get mapFromEntries => Map.fromEntries(this);
}
extension ListExtension<E> on List<E> {
Iterable<E> get withoutLast {
var length = this.length;
return length > 0 ? take(length - 1) : Iterable.empty();
}
void addIfNotNull(E? element) {
if (element != null) {
add(element);
}
}
/// Returns the element at [index].
///
/// Returns `null`, if [index] is negative or greater than the length.
E? elementAtOrNull2(int index) {
if (0 <= index && index < length) {
return this[index];
} else {
return null;
}
}
bool endsWith(List<E> expected) {
var thisIndex = length - expected.length;
if (thisIndex < 0) {
return false;
}
var expectedIndex = 0;
for (; expectedIndex < expected.length;) {
if (this[thisIndex++] != expected[expectedIndex++]) {
return false;
}
}
return true;
}
E? nextOrNull(E element) {
var index = indexOf(element);
if (index >= 0 && index < length - 1) {
return this[index + 1];
} else {
return null;
}
}
E? removeLastOrNull() {
if (isNotEmpty) {
return removeLast();
}
return null;
}
/// Returns a new list with all elements of the target, arranged such that
/// all elements for which the [predicate] specified returns `true` come
/// before those for which the [predicate] returns `false`. The partitioning
/// is stable, i.e. the relative ordering of the elements is preserved.
List<E> stablePartition(bool Function(E element) predicate) {
return [
...where(predicate),
...whereNot(predicate),
];
}
}
extension ListQueueExtension<T> on ListQueue<T> {
T? removeFirstOrNull() {
return isNotEmpty ? removeFirst() : null;
}
}
extension MapExtension<K, V> on Map<K, V> {
K? get firstKey {
return keys.firstOrNull;
}
}
extension MapOfListExtension<K, V> on Map<K, List<V>> {
void add(K key, V value) {
(this[key] ??= []).add(value);
}
/// Ensure that [key] is present in the target, maybe with the empty list.
void addKey(K key) {
this[key] ??= [];
}
}
extension SetExtension<E> on Set<E> {
void addIfNotNull(E? element) {
if (element != null) {
add(element);
}
}
}