LCOV - code coverage report

Current view
top level - src/converters - lazy_in_place_list.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines34334599.4%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:math' as math;
2
3import 'package:meta/meta.dart';
4
5import 'converter.dart';
6
7@internal
8class LazyInPlaceList<E> implements List<E> {
92 LazyInPlaceList(this._data, this._cast);
10
11 final List<dynamic> _data;
12 final Cast<E> _cast;
13
141 @override
152 E get first => _get(0);
16
171 @override
183 set first(E value) => _data[0] = value;
19
201 @override
213 bool get isEmpty => _data.isEmpty;
22
231 @override
243 bool get isNotEmpty => _data.isNotEmpty;
25
261 @override
273 Iterator<E> get iterator => _iterate().iterator;
28
291 @override
304 E get last => _get(length - 1);
31
321 @override
335 set last(E value) => _data[length - 1] = value;
34
351 @override
363 int get length => _data.length;
37
381 @override
391 set length(int value) {
403 if (value > length) null as E;
413 _data.length = value;
421 }
43
441 @override
451 Iterable<E> get reversed sync* {
465 for (var i = length - 1; i >= 0; i--) {
472 yield _get(i);
48 }
491 }
50
511 @override
521 E get single {
531 if (isEmpty) throw _ListError.noElement();
544 if (length > 1) throw _ListError.tooMany();
550 return _get(0);
560 }
57
581 @override
591 List<E> operator +(List<E> other) {
604 final l = length, tl = l + other.length;
613 if (tl == l) return toList();
623 final r = List<E>.filled(tl, other[0]);
633 for (var i = 0; i < l; i++) {
643 r[i] = _get(i);
65 }
662 r.setRange(l, tl, other);
671 return r;
681 }
69
701 @override
712 E operator [](int index) => _get(index);
72
731 @override
743 void operator []=(int index, E value) => _data[index] = value;
75
761 @override
773 void add(E value) => _data.add(value);
78
791 @override
803 void addAll(Iterable<E> iterable) => _data.addAll(iterable);
81
821 @override
831 bool any(bool Function(E element) test) {
842 final l = length;
853 for (var i = 0; i < l; i++) {
863 if (test(_get(i))) return true;
87 }
881 return false;
891 }
90
911 @override
921 Map<int, E> asMap() {
933 final r = <int, E>{}, l = length;
943 for (var i = 0; i < l; i++) {
953 r[i] = _get(i);
96 }
971 return r;
981 }
99
1001 @override
1013 List<R> cast<R>() => _forceCast().cast<R>();
102
1031 @override
1043 void clear() => _data.clear();
105
1061 @override
1071 bool contains(Object? element) {
1081 final l = length;
1093 for (var i = 0; i < l; i++) {
1103 if (element == _get(i)) return true;
111 }
1121 return false;
1131 }
114
1151 @override
1162 E elementAt(int index) => _get(index);
117
1181 @override
1191 bool every(bool Function(E element) test) {
1202 final l = length;
1213 for (var i = 0; i < l; i++) {
1223 if (!test(_get(i))) return false;
123 }
1241 return true;
1251 }
126
1271 @override
1281 Iterable<U> expand<U>(Iterable<U> Function(E element) toElements) sync* {
1292 final l = length;
1303 for (var i = 0; i < l; i++) {
1313 yield* toElements(_get(i));
132 }
1331 }
134
1351 @override
1361 void fillRange(int start, int end, [E? fillValue]) =>
1373 _data.fillRange(start, end, fillValue);
138
1391 @override
1401 E firstWhere(bool Function(E element) test, {E Function()? orElse}) {
1411 final l = length;
1423 for (var i = 0; i < l; i++) {
1432 final v = _get(i);
1442 if (test(v)) return v;
145 }
1462 if (orElse != null) return orElse();
1472 throw _ListError.noElement();
1481 }
149
1501 @override
1511 U fold<U>(U initialValue, U Function(U previousValue, E element) combine) {
1522 final l = length;
1531 U res = initialValue;
1543 for (var i = 0; i < l; i++) {
1553 res = combine(res, _get(i));
156 }
1571 return res;
1581 }
159
1601 @override
1611 Iterable<E> followedBy(Iterable<E> other) sync* {
1622 final l = length;
1633 for (var i = 0; i < l; i++) {
1642 yield _get(i);
165 }
1661 yield* other;
1671 }
168
1691 @override
1701 void forEach(void Function(E element) action) {
1712 final l = length;
1723 for (var i = 0; i < l; i++) {
1733 action(_get(i));
174 }
1751 }
176
1771 @override
1781 Iterable<E> getRange(int start, int end) sync* {
1793 RangeError.checkValidRange(start, end, length);
1803 for (var i = start; i < end; i++) {
1812 yield _get(i);
182 }
1831 }
184
1851 @override
1861 int indexOf(E element, [int start = 0]) {
1872 final l = length;
1883 for (var i = start; i < l; i++) {
1893 if (_get(i) == element) return i;
190 }
1912 return -1;
1921 }
193
1941 @override
1951 int indexWhere(bool Function(E element) test, [int start = 0]) {
1962 final l = length;
1973 for (var i = start; i < l; i++) {
1983 if (test(_get(i))) return i;
199 }
2002 return -1;
2011 }
202
2031 @override
2043 void insert(int index, E element) => _data.insert(index, element);
205
2061 @override
2071 void insertAll(int index, Iterable<E> iterable) =>
2083 _data.insertAll(index, iterable);
209
2101 @override
2111 String join([String separator = ""]) {
2121 final buffer = StringBuffer();
2131 final l = length;
2142 if (l > 0) {
2154 buffer.write(_get(0).toString());
2163 for (var i = 1; i < l; i++) {
2171 buffer.write(separator);
2184 buffer.write(_get(i).toString());
219 }
220 }
2212 return buffer.toString();
2221 }
223
2241 @override
2251 int lastIndexOf(E element, [int? start]) {
2265 for (var i = start ?? (length - 1); i >= 0; i--) {
2273 if (element == _get(i)) return i;
228 }
2292 return -1;
2301 }
231
2321 @override
2331 int lastIndexWhere(bool Function(E element) test, [int? start]) {
2345 for (var i = start ?? (length - 1); i >= 0; i--) {
2353 if (test(_get(i))) return i;
236 }
2372 return -1;
2381 }
239
2401 @override
2411 E lastWhere(bool Function(E element) test, {E Function()? orElse}) {
2425 for (var i = length - 1; i >= 0; i--) {
2432 final v = _get(i);
2442 if (test(v)) return v;
245 }
2462 if (orElse != null) return orElse();
2472 throw _ListError.noElement();
2481 }
249
2501 @override
2511 Iterable<U> map<U>(U Function(E e) toElement) sync* {
2522 final l = length;
2533 for (var i = 0; i < l; i++) {
2543 yield toElement(_get(i));
255 }
2561 }
257
2581 @override
2591 E reduce(E Function(E value, E element) combine) {
2602 final l = length;
2612 if (l == 0) throw _ListError.noElement();
2622 E res = _get(0);
2633 for (var i = 1; i < l; i++) {
2643 res = combine(res, _get(i));
265 }
2661 return res;
2671 }
268
2691 @override
2703 bool remove(Object? value) => _data.remove(value);
271
2721 @override
2731 E removeAt(int index) {
2742 final v = _get(index);
2753 _data.removeAt(index);
2761 return v;
2771 }
278
2791 @override
2801 E removeLast() {
2814 final v = _get(length - 1);
2823 _data.removeLast();
2831 return v;
2841 }
285
2861 @override
2873 void removeRange(int start, int end) => _data.removeRange(start, end);
288
2891 @override
2901 void removeWhere(bool Function(E element) test) {
2913 var i = length - 1;
2922 while (i >= 0) {
2933 if (test(_get(i))) {
2941 final e = i + 1;
2951 i--;
2964 while (i >= 0 && test(_get(i))) {
2972 i--;
298 }
2992 removeRange(i + 1, e);
300 } else {
3012 i--;
302 }
303 }
3041 }
305
3061 @override
3071 void replaceRange(int start, int end, Iterable<E> replacements) =>
3083 _data.replaceRange(start, end, replacements);
309
3101 @override
3111 void retainWhere(bool Function(E element) test) =>
3124 removeWhere((e) => !test(e));
313
3141 @override
3153 void setAll(int index, Iterable<E> iterable) => _data.setAll(index, iterable);
316
3171 @override
3181 void setRange(int start, int end, Iterable<E> iterable,
319 [int skipCount = 0]) =>
3203 _data.setRange(start, end, iterable, skipCount);
321
3221 @override
3233 void shuffle([math.Random? random]) => _data.shuffle(random);
324
3251 @override
3261 E singleWhere(bool Function(E element) test, {E Function()? orElse}) {
3271 late E res;
3281 final l = length;
329 var found = false;
3303 for (var i = 0; i < l; i++) {
3312 final v = _get(i);
3322 if (test(v)) {
3332 if (found) throw _ListError.tooMany();
3341 found = true;
335 res = v;
336 }
337 }
3381 if (found) return res;
3392 if (orElse != null) return orElse();
3402 throw _ListError.noElement();
3411 }
342
3431 @override
3441 Iterable<E> skip(int count) sync* {
3452 final l = length;
3463 for (var i = count; i < l; i++) {
3472 yield _get(i);
348 }
3491 }
350
3511 @override
3521 Iterable<E> skipWhile(bool Function(E value) test) sync* {
3531 var i = 0;
3541 final l = length;
3554 while (i < l && test(_get(i))) {
3561 i++;
357 }
3582 while (i < l) {
3592 yield _get(i);
3601 i++;
361 }
3621 }
363
3641 @override
3651 void sort([int Function(E a, E b)? compare]) {
3662 _forceCast();
3671 if (compare == null) {
3683 final d = _data.cast<E>();
3692 d.sort();
370 } else {
3715 _data.sort((a, b) => compare(a as E, b as E));
372 }
3731 }
374
3751 @override
3761 List<E> sublist(int start, [int? end]) =>
3774 getRange(start, end ?? length).toList();
378
3791 @override
3801 Iterable<E> take(int count) sync* {
3813 final c = math.min(count, length);
3823 for (var i = 0; i < c; i++) {
3832 yield _get(i);
384 }
3851 }
386
3871 @override
3881 Iterable<E> takeWhile(bool Function(E value) test) sync* {
3892 final l = length;
3903 for (var i = 0; i < l; i++) {
3912 final v = _get(i);
3922 if (!test(v)) break;
3931 yield v;
394 }
3951 }
396
3971 @override
3981 List<E> toList({bool growable = true}) {
3991 final l = length;
4002 if (l == 0) return growable ? [] : const [];
4013 final res = List.filled(l, _get(0), growable: growable);
4023 for (var i = 1; i < l; i++) {
4033 res[i] = _get(i);
404 }
4051 return res;
4061 }
407
4081 @override
4091 Set<E> toSet() {
4102 final s = <E>{}, l = length;
4113 for (var i = 0; i < l; i++) {
4123 s.add(_get(i));
413 }
4141 return s;
4151 }
416
4171 @override
4181 String toString() {
4192 _forceCast();
4203 return _data.toString();
4211 }
422
4231 @override
4241 Iterable<E> where(bool Function(E element) test) sync* {
4252 final l = length;
4263 for (var i = 0; i < l; i++) {
4272 final v = _get(i);
4282 if (test(v)) yield v;
429 }
4301 }
431
4321 @override
4331 Iterable<U> whereType<U>() sync* {
4342 final l = length;
4353 for (var i = 0; i < l; i++) {
4362 final v = _get(i);
4372 if (v is U) yield v;
438 }
4391 }
440
4412 List<dynamic> _forceCast() {
4421 final l = length;
4433 for (var i = 0; i < l; i++) {
4442 _get(i);
445 }
4462 return _data;
4471 }
448
4492 E _get(int idx) {
4503 dynamic v = _data[idx];
4512 if (v != null && v is! E) {
4522 v = _cast(v);
4532 _data[idx] = v;
454 }
4551 return v;
4561 }
457
4582 Iterable<E> _iterate() sync* {
4592 final l = length;
4603 for (var i = 0; i < l; i++) {
4612 yield _get(i);
462 }
4631 }
464}
465
466// copied from dart-sdk\lib\internal\iterable.dart
467sealed class _ListError {
4683 static StateError noElement() => StateError("No element");
4693 static StateError tooMany() => StateError("Too many elements");
470}
Choose Features