Skip to content

Commit

Permalink
Improving performance of Set::diff()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 29, 2010
1 parent 0abe9af commit fde10b1
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions cake/libs/set.php
Expand Up @@ -777,7 +777,8 @@ function check($data, $path = null) {
*
* @param mixed $val1 First value
* @param mixed $val2 Second value
* @return array Computed difference
* @return array Returns the key => value pairs that are not common in $val1 and $val2
* The expression for this function is ($val1 - $val2) + ($val2 - ($val1 - $val2))
* @access public
* @static
*/
Expand All @@ -788,25 +789,15 @@ function diff($val1, $val2 = null) {
if (empty($val2)) {
return (array)$val1;
}
$out = array();

foreach ($val1 as $key => $val) {
$exists = array_key_exists($key, $val2);

if ($exists && $val2[$key] != $val) {
$out[$key] = $val;
} elseif (!$exists) {
$out[$key] = $val;
$intersection = array_intersect_key($val1,$val2);
while (list($key,) = each($intersection)) {
if ($val1[$key] == $val2[$key]) {
unset($val1[$key]);
unset($val2[$key]);
}
unset($val2[$key]);
}

foreach ($val2 as $key => $val) {
if (!array_key_exists($key, $out)) {
$out[$key] = $val;
}
}
return $out;
return $val1 + $val2;
}

/**
Expand Down

0 comments on commit fde10b1

Please sign in to comment.