-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
list.dart
116 lines (99 loc) · 3.02 KB
/
list.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
import 'package:dart_kollection/dart_kollection.dart';
import 'package:dart_kollection/src/collection/iterator.dart';
import 'package:dart_kollection/src/extension/collection_extension_mixin.dart';
import 'package:dart_kollection/src/extension/iterable_extension_mixin.dart';
import 'package:dart_kollection/src/extension/list_extension_mixin.dart';
import 'package:dart_kollection/src/util/hash.dart';
/**
* [KList] based on a dart [List]
*/
class DartList<T>
with
KIterableExtensionsMixin<T>,
KCollectionExtensionMixin<T>,
KListExtensionsMixin<T>
implements KList<T> {
DartList([Iterable<T> iterable = const []])
:
// copy list to prevent external modification
_list = List.from(iterable, growable: false),
super();
final List<T> _list;
int _hashCode;
@override
Iterable<T> get iter => _list;
@override
List<T> get list => _list;
@override
bool contains(T element) => _list.contains(element);
@override
bool containsAll(KCollection<T> elements) {
assert(() {
if (elements == null) throw ArgumentError("elements can't be null");
return true;
}());
return elements.all(_list.contains);
}
@override
T get(int index) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
if (index < 0 || index >= size) {
throw IndexOutOfBoundsException("index: $index, size: $size");
}
return _list[index];
}
@override
T operator [](int index) => get(index);
@override
int indexOf(T element) => _list.indexOf(element);
@override
bool isEmpty() => _list.isEmpty;
@override
KIterator<T> iterator() => InterOpKIterator(_list.iterator);
@override
int lastIndexOf(T element) => _list.lastIndexOf(element);
@override
KListIterator<T> listIterator([int index = 0]) {
assert(() {
if (index == null) throw ArgumentError("index can't be null");
return true;
}());
return InterOpKListIterator(_list, index);
}
@override
int get size => _list.length;
@override
KList<T> subList(int fromIndex, int toIndex) {
assert(() {
if (fromIndex == null) throw ArgumentError("fromIndex can't be null");
if (toIndex == null) throw ArgumentError("toIndex can't be null");
if (fromIndex > toIndex)
throw ArgumentError("fromIndex: $fromIndex > toIndex: $toIndex");
return true;
}());
if (fromIndex < 0 || toIndex > size) {
throw IndexOutOfBoundsException(
"fromIndex: $fromIndex, toIndex: $toIndex, size: $size");
}
return DartList(_list.sublist(fromIndex, toIndex));
}
@override
int get hashCode {
_hashCode ??= 1 + hashObjects(_list);
return _hashCode;
}
@override
bool operator ==(dynamic other) {
if (identical(other, this)) return true;
if (other is! KList) return false;
if (other.size != size) return false;
if (other.hashCode != hashCode) return false;
for (var i = 0; i != size; ++i) {
if (other[i] != this[i]) return false;
}
return true;
}
}