fromChunks<DataType extends Object> static method

AddressedMemoryImage<DataType> fromChunks<DataType extends Object>(
  1. Iterable<AddressedMemoryImageChunk<DataType>> chunks
)

Reassembles a complete image from validated chunks.

Implementation

static AddressedMemoryImage<DataType> fromChunks<DataType extends Object>(
  Iterable<AddressedMemoryImageChunk<DataType>> chunks,
) {
  final ordered = chunks.toList()
    ..sort((left, right) => left.index.compareTo(right.index));
  if (ordered.isEmpty) {
    throw ArgumentError('At least one image chunk is required.');
  }
  final first = ordered.first;
  final entries = <int, DataType>{};
  for (var index = 0; index < ordered.length; index++) {
    final chunk = ordered[index];
    if (chunk.index != index ||
        chunk.count != ordered.length ||
        chunk.image.depth != first.image.depth ||
        chunk.image.dataWidth != first.image.dataWidth) {
      throw ArgumentError('Image chunks are incomplete or incompatible.');
    }
    for (final entry in chunk.image.entries.entries) {
      if (entries.containsKey(entry.key)) {
        throw ArgumentError(
            'Image chunks contain duplicate address ${entry.key}.');
      }
      entries[entry.key] = entry.value;
    }
  }
  return AddressedMemoryImage(
    depth: first.image.depth,
    dataWidth: first.image.dataWidth,
    entries: entries,
  );
}