Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MemoryEfficientLongestCommonSubsequenceCalculator.php

class MemoryEfficientLongestCommonSubsequenceCalculator

Hierarchy

  • class \SebastianBergmann\Diff\MemoryEfficientLongestCommonSubsequenceCalculator implements \SebastianBergmann\Diff\LongestCommonSubsequenceCalculator

Expanded class hierarchy of MemoryEfficientLongestCommonSubsequenceCalculator

File

vendor/sebastian/diff/src/MemoryEfficientLongestCommonSubsequenceCalculator.php, line 20

Namespace

SebastianBergmann\Diff
View source
final class MemoryEfficientLongestCommonSubsequenceCalculator implements LongestCommonSubsequenceCalculator {
    
    /**
     * @inheritDoc
     */
    public function calculate(array $from, array $to) : array {
        $cFrom = count($from);
        $cTo = count($to);
        if ($cFrom === 0) {
            return [];
        }
        if ($cFrom === 1) {
            if (in_array($from[0], $to, true)) {
                return [
                    $from[0],
                ];
            }
            return [];
        }
        $i = (int) ($cFrom / 2);
        $fromStart = array_slice($from, 0, $i);
        $fromEnd = array_slice($from, $i);
        $llB = $this->length($fromStart, $to);
        $llE = $this->length(array_reverse($fromEnd), array_reverse($to));
        $jMax = 0;
        $max = 0;
        for ($j = 0; $j <= $cTo; $j++) {
            $m = $llB[$j] + $llE[$cTo - $j];
            if ($m >= $max) {
                $max = $m;
                $jMax = $j;
            }
        }
        $toStart = array_slice($to, 0, $jMax);
        $toEnd = array_slice($to, $jMax);
        return array_merge($this->calculate($fromStart, $toStart), $this->calculate($fromEnd, $toEnd));
    }
    private function length(array $from, array $to) : array {
        $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 {
                    
                    /**
                     * @noinspection PhpConditionCanBeReplacedWithMinMaxCallInspection
                     *
                     * We do not use max() here to avoid the function call overhead
                     */
                    if ($current[$j] > $prev[$j + 1]) {
                        $current[$j + 1] = $current[$j];
                    }
                    else {
                        $current[$j + 1] = $prev[$j + 1];
                    }
                }
            }
        }
        return $current;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
MemoryEfficientLongestCommonSubsequenceCalculator::calculate public function @inheritDoc Overrides LongestCommonSubsequenceCalculator::calculate
MemoryEfficientLongestCommonSubsequenceCalculator::length private function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal