AssociativeModelState.fromPortableString constructor
Decodes a complete image with the expected storage geometry.
Implementation
factory AssociativeModelState.fromPortableString(String text,
{required int keyWidth, required int entryCount}) {
if (keyWidth <= 0 || entryCount <= 0) {
throw ArgumentError('Associative image geometry must be positive.');
}
final tokens = text.trim().split(RegExp(r'\s+'));
if (tokens.length != 3 + entryCount * 2 ||
tokens[0] != 'RHCL_ASSOC_STORAGE_V1' ||
tokens[1] != '$keyWidth' ||
tokens[2] != '$entryCount') {
throw const FormatException('Invalid associative storage image header.');
}
final entries = <int, AssociativeModelEntry>{};
for (var index = 0; index < entryCount; index++) {
final valid = tokens[3 + index * 2];
final key = tokens[4 + index * 2];
if (valid.length != 1 ||
key.length != keyWidth ||
!RegExp(r'^[01xX]+$').hasMatch(valid) ||
!RegExp(r'^[01xX]+$').hasMatch(key)) {
throw const FormatException('Invalid associative storage image entry.');
}
entries[index] = (
valid: LogicValue.ofString(valid.toLowerCase()),
key: LogicValue.ofString(key.toLowerCase()),
);
}
return AssociativeModelState(
keyWidth: keyWidth, entryCount: entryCount, entries: entries);
}