You are here

private function MemoryEfficientImplementation::length in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.php \SebastianBergmann\Diff\LCS\MemoryEfficientImplementation::length()

Parameters

array $from:

array $to:

Return value

array

1 call to MemoryEfficientImplementation::length()
MemoryEfficientImplementation::calculate in vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.php
Calculates the longest common subsequence of two arrays.

File

vendor/sebastian/diff/src/LCS/MemoryEfficientLongestCommonSubsequenceImplementation.php, line 78

Class

MemoryEfficientImplementation
Memory-efficient implementation of longest common subsequence calculation.

Namespace

SebastianBergmann\Diff\LCS

Code

private function length(array $from, array $to) {
  $current = array_fill(0, count($to) + 1, 0);
  $cFrom = count($from);
  $cTo = count($to);
  for ($i = 0; $i < $cFrom; $i++) {
    $prev = $current;
    for ($j = 0; $j < $cTo; $j++) {
      if ($from[$i] == $to[$j]) {
        $current[$j + 1] = $prev[$j] + 1;
      }
      else {
        $current[$j + 1] = max($current[$j], $prev[$j + 1]);
      }
    }
  }
  return $current;
}