/
array.dart
173 lines (139 loc) · 3.61 KB
/
array.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import 'dart:async';
import 'dart:ffi';
import 'package:cbl_ffi/cbl_ffi.dart';
import 'package:collection/collection.dart';
import '../../support/utils.dart';
import '../decoder.dart';
import '../encoder.dart';
import 'collection.dart';
import 'value.dart';
class MArray extends MCollection {
MArray()
: _array = null,
_values = [];
MArray.asCopy(MArray original, {bool? isMutable})
: _array = original._array,
_values = original._values.map((value) => value?.clone()).toList(),
super.asCopy(original, isMutable: isMutable ?? original.isMutable);
MArray.asChild(MValue slot, MCollection parent, {bool? isMutable})
:
// ignore: cast_nullable_to_non_nullable
_array = (slot.value as CollectionFLValue).value.cast(),
_values = List<MValue?>.filled(
// ignore: cast_nullable_to_non_nullable
(slot.value as CollectionFLValue).length,
null,
growable: true,
),
super.asChild(
slot,
parent,
isMutable: isMutable ?? parent.hasMutableChildren,
);
final Pointer<FLArray>? _array;
final List<MValue?> _values;
int get length => _values.length;
MValue? get(int index) {
assert(index >= 0);
if (index >= _values.length) {
return null;
}
return _values[index] ??= _loadMValue(index)!;
}
bool set(int index, Object? native) {
assert(isMutable);
assert(index >= 0);
if (index >= length) {
return false;
}
mutate();
(_values[index] ??= MValue.empty()).setNative(native, this);
return true;
}
bool insert(int index, Object? native) {
assert(isMutable);
assert(index >= 0);
final length = this.length;
if (index > length) {
return false;
} else if (index < length) {
_populateValues();
}
mutate();
_values.insert(index, MValue.empty()..setNative(native, this));
return true;
}
void append(Object? native) => insert(length, native);
bool remove(int index, [int count = 1]) {
assert(isMutable);
assert(index >= 0);
assert(count >= 0);
final end = index + count;
if (end > length) {
return false;
}
if (count == 0) {
return true;
}
if (end < length) {
_populateValues();
}
mutate();
_values.getRange(index, end).forEach((value) => value?.removeFromParent());
_values.removeRange(index, end);
return true;
}
void clear() {
assert(isMutable);
if (_values.isEmpty) {
return;
}
mutate();
_values
..forEach((value) => value?.removeFromParent())
..clear();
}
@override
FutureOr<void> performEncodeTo(FleeceEncoder encoder) {
if (!isMutated) {
encoder.writeValue(_array!.cast());
} else {
return syncOrAsync(() sync* {
encoder.beginArray(length);
var index = 0;
for (final value in _values) {
if (value == null) {
encoder.writeArrayValue(_array!, index);
} else {
yield value.encodeTo(encoder);
}
++index;
}
encoder.endArray();
}());
}
}
@override
Iterable<MValue> get values => _values.whereNotNull();
void _populateValues() {
if (_array == null) {
return;
}
var i = 0;
for (final value in _values) {
if (value == null) {
_values[i] = _loadMValue(i);
}
i++;
}
}
MValue? _loadMValue(int index) {
final array = _array;
if (array == null) {
return null;
}
return context!.decoder
.loadValueFromArray(array, index)
?.let(MValue.withValue);
}
}