withSet method
override
Returns a copy of this Logic with the bits starting from startIndex
up until startIndex
+ update
.width
set to update
instead
of their original value.
The return signal will be the same width. An exception will be thrown
if the position of the update
would cause an overrun past the width.
Implementation
@override
Logic withSet(int startIndex, Logic update) {
final endIndex = startIndex + update.width;
if (endIndex > width) {
throw RangeError('Width of update $update at startIndex $startIndex would'
' overrun the width of the original ($width).');
}
if (startIndex < 0) {
throw RangeError(
'Start index must be greater than zero but was $startIndex');
}
final newWithSet = clone();
var index = 0;
for (var i = 0; i < leafElements.length; i++) {
final newElement = newWithSet.leafElements[i];
final element = leafElements[i];
final elementWidth = element.width;
// if the *start* or *end* of `element` is within [startIndex, endIndex],
// then we have to include it in `matchingElements`
final elementStart = index;
final elementEnd = index + elementWidth;
final elementInRange =
((elementStart >= startIndex) && (elementStart < endIndex)) ||
((elementEnd > startIndex) && (elementEnd <= endIndex));
if (elementInRange) {
newElement <=
element.withSet(
max(startIndex - index, 0),
update.getRange(
max(index - startIndex, 0),
min(index - startIndex + elementWidth, elementWidth),
));
} else {
newElement <= element;
}
index += element.width;
}
return newWithSet;
}