Add fetchSublist method to BinaryTable for improved data retrieval

This commit is contained in:
ImBenji
2025-09-18 15:08:57 +01:00
parent 3ae088f518
commit cf983c8d96
2 changed files with 75 additions and 2 deletions

View File

@@ -363,6 +363,34 @@ class BT_UniformArray extends BT_Reference {
addAll([value]);
}
List<dynamic> fetchSublist([int start = 0, int end = -1]) {
// Read the full array by loading the full buffer of only whats needed
BT_Type? type = elementType;
if (type == null) {
return [];
}
if (type.size == -1) {
throw Exception("Types with variable size are not supported in uniform arrays. Use a non-uniform array instead.");
}
end = end == -1 ? length : end;
int bufferStart = 1 + 4 + start * (1 + type.size);
int bufferEnd = 1 + 4 + end * (1 + type.size);
int bufferSize = bufferEnd - bufferStart;
_table._file.setPositionSync(_pointer.address + bufferStart);
List<int> buffer = _table._file.readSync(bufferSize).toList();
List<dynamic> values = [];
for (int i = 0; i < (end - start); i++) {
int offset = i * (1 + type.size);
BT_Reference itemRef = BT_Reference(_table, BT_Pointer((_pointer.address + bufferStart) + offset));
values.add(itemRef.decodeValue());
}
return values;
}
@override
int get size {
@@ -875,6 +903,12 @@ void main() {
table["int_array"].addAll([420, 69, 1337, 1738]);
table["float_array"].addAll([6.5, 7.5, 8.5]);
// Test the full read method
var intArrayFull = table["int_array"].fetchSublist(0, 3);
var floatArrayFull = table["float_array"].fetchSublist(0, 2);
print("Full read int_array (0-3): $intArrayFull");
print("Full read float_array (0-2): $floatArrayFull");
var readback1 = table["int_array"];
var readback2 = table["float_array"];
var readback3 = table["empty"];