matches property

Iterable<Match> matches

Return list of blocks describing matching subsequences.

Implementation

Iterable<Match> get matches {
  if (_matches.isNotEmpty) return _matches;
  // Find the longest matching blocks iteratively.
  final blocks = <Match>[];
  final queue = QueueList.from([
    (
      sourceStart: 0,
      sourceEnd: _source.length,
      targetStart: 0,
      targetEnd: _target.length,
    )
  ]);
  while (queue.isNotEmpty) {
    final current = queue.removeFirst();
    final match = findLongestMatch(
        sourceStart: current.sourceStart,
        sourceEnd: current.sourceEnd,
        targetStart: current.targetStart,
        targetEnd: current.targetEnd);
    if (match.length > 0) {
      if (current.sourceStart < match.sourceStart &&
          current.targetStart < match.targetStart) {
        queue.add((
          sourceStart: current.sourceStart,
          sourceEnd: match.sourceStart,
          targetStart: current.targetStart,
          targetEnd: match.targetStart,
        ));
      }
      if (match.sourceStart + match.length < current.sourceEnd &&
          match.targetStart + match.length < current.targetEnd) {
        queue.add((
          sourceStart: match.sourceStart + match.length,
          sourceEnd: current.sourceEnd,
          targetStart: match.targetStart + match.length,
          targetEnd: current.targetEnd,
        ));
      }
      blocks.add(match);
    }
  }
  blocks.sort();
  // Collapse adjacent blocks.
  var sourceStart = 0, targetStart = 0, length = 0;
  for (final block in blocks) {
    if (sourceStart + length == block.sourceStart &&
        targetStart + length == block.targetStart) {
      length += block.length;
    } else {
      if (length > 0) {
        _matches.add(Match(
            sourceStart: sourceStart,
            targetStart: targetStart,
            length: length));
      }
      sourceStart = block.sourceStart;
      targetStart = block.targetStart;
      length = block.length;
    }
  }
  if (length > 0) {
    _matches.add(Match(
        sourceStart: sourceStart, targetStart: targetStart, length: length));
  }
  _matches.add(Match(
      sourceStart: _source.length, targetStart: _target.length, length: 0));
  return _matches;
}