From d7cc67e72a3f53629104d811986acd688f104cda Mon Sep 17 00:00:00 2001 From: Antonio Gallo Date: Wed, 23 Feb 2011 21:12:47 +0000 Subject: improved diff algorithm --- h-source/Application/Include/myFunctions.php | 36 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'h-source/Application/Include/myFunctions.php') diff --git a/h-source/Application/Include/myFunctions.php b/h-source/Application/Include/myFunctions.php index d0d51bf..9e05ed2 100644 --- a/h-source/Application/Include/myFunctions.php +++ b/h-source/Application/Include/myFunctions.php @@ -128,16 +128,33 @@ function getOrderByClause($string) return $orderBy; } +//remove empty elements +function removeEmptyStrings($oArray) +{ + $nArray = array(); + foreach ($oArray as $oValue) + { + if (strcmp($oValue,'') !== 0) + { + $nArray[] = $oValue; + } + } + return $nArray; +} -function diff($old, $new){ +function diff($old, $new) +{ $maxlen = 0; - foreach($old as $oindex => $ovalue){ -// $nkeys = getNewKeys($new,$ovalue); + foreach($old as $oindex => $ovalue) + { $nkeys = array_keys($new, $ovalue); - foreach($nkeys as $nindex){ +// echo memory_get_peak_usage(true)."
"; + foreach($nkeys as $nindex) + { $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? $matrix[$oindex - 1][$nindex - 1] + 1 : 1; - if($matrix[$oindex][$nindex] > $maxlen){ + if($matrix[$oindex][$nindex] > $maxlen) + { $maxlen = $matrix[$oindex][$nindex]; $omax = $oindex + 1 - $maxlen; $nmax = $nindex + 1 - $maxlen; @@ -151,13 +168,15 @@ function diff($old, $new){ diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))); } -function htmlDiff($old, $new){ +function htmlDiff($old, $new) +{ $old = str_replace("\r\n"," \r\n ",$old); $new = str_replace("\r\n"," \r\n ",$new); $ret = null; - $diff = diff(explode(' ', $old), explode(' ', $new)); - foreach($diff as $k){ + $diff = diff(removeEmptyStrings(explode(' ', $old)),removeEmptyStrings(explode(' ', $new))); + foreach($diff as $k) + { if(is_array($k)) $ret .= (!empty($k['d'])?"".implode(' ',$k['d'])." ":''). (!empty($k['i'])?"".implode(' ',$k['i'])." ":''); @@ -167,7 +186,6 @@ function htmlDiff($old, $new){ } -//a cosa serve? function applyBreaks($values,$fields) { $fieldsArray = explode(',',$fields); -- cgit v1.2.3