fillRange method
Overwrites a range of elements with fillValue
.
Sets the positions greater than or equal to start
and less than end
,
to fillValue
.
The provided range, given by start
and end
, must be valid.
A range from start
to end
is valid if 0 ≤ start
≤ end
≤ length.
An empty range (with end == start
) is valid.
If the element type is not nullable, the fillValue
must be provided and
must be non-null
.
Example:
final words = List.filled(5, 'old');
print(words); // [old, old, old, old, old]
words.fillRange(1, 3, 'new');
print(words); // [old, new, new, old, old]
Implementation
@override
void fillRange(int start, int end, [bool? fill]) {
RangeError.checkValidRange(start, end, length);
if (start == end) return;
final startIndex = start >> bitShift, startBit = start & bitOffset;
final endIndex = (end - 1) >> bitShift, endBit = (end - 1) & bitOffset;
if (startIndex == endIndex) {
if (fill == true) {
buffer[startIndex] |= ((1 << (endBit - startBit + 1)) - 1) << startBit;
} else {
buffer[startIndex] &= ((1 << startBit) - 1) | (bitMask << (endBit + 1));
}
} else {
if (fill == true) {
buffer[startIndex] |= bitMask << startBit;
buffer.fillRange(startIndex + 1, endIndex, bitMask);
buffer[endIndex] |= (1 << (endBit + 1)) - 1;
} else {
buffer[startIndex] &= (1 << startBit) - 1;
buffer.fillRange(startIndex + 1, endIndex, 0);
buffer[endIndex] &= bitMask << (endBit + 1);
}
}
}