fillRange method

  1. @override
void fillRange(
  1. int start,
  2. int end,
  3. [bool? fill]
)
override

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 ≤ startendlength. 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);
  fill ??= false;

  var startWord = start >> _entryShift;
  var endWord = (end - 1) >> _entryShift;

  var startBit = start & _entrySignBitIndex;
  var endBit = (end - 1) & _entrySignBitIndex;

  if (startWord < endWord) {
    if (fill) {
      _data[startWord] |= -1 << startBit;
      _data.fillRange(startWord + 1, endWord, -1);
      _data[endWord] |= (1 << (endBit + 1)) - 1;
    } else {
      _data[startWord] &= (1 << startBit) - 1;
      _data.fillRange(startWord + 1, endWord, 0);
      _data[endWord] &= -1 << (endBit + 1);
    }
  } else {
    if (fill) {
      _data[startWord] |= ((1 << (endBit - startBit + 1)) - 1) << startBit;
    } else {
      _data[startWord] &= ((1 << startBit) - 1) | (-1 << (endBit + 1));
    }
  }
}