Skip to content

Commit

Permalink
Fix variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
hytromo committed Sep 6, 2016
1 parent e722efb commit f705536
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/Collection/CollectionTrait.php
Expand Up @@ -692,43 +692,39 @@ public function cartesianProduct(callable $operation = null, callable $filter =
return new Collection([]);
}

// contains each array of the collection
$values = [];
// contains the keys of each array
$valuesKeys = [];
// contains the number of elements of each array
$counts = [];
$collectionArrays = [];
$collectionArraysKeys = [];
$collectionArraysCounts = [];

foreach ($this as $value) {
// fail if any of the values is a multidimensional array
if (count($value) !== count($value, COUNT_RECURSIVE)) {
throw new LogicException('Cannot find the cartesian product of a multidimensional array');
}

$valuesKeys[] = array_keys($value);
$counts[] = count($value);
$values[] = $value;
$collectionArraysKeys[] = array_keys($value);
$collectionArraysCounts[] = count($value);
$collectionArrays[] = $value;
}

$result = [];
$lastIndex = count($values) - 1;
$lastIndex = count($collectionArrays) - 1;
// holds the indexes of the arrays that generate the current combination
$currentIndexes = array_fill(0, $lastIndex + 1, 0);

$changeIndex = $lastIndex;

while (!($changeIndex === 0 && $currentIndexes[0] === $counts[0])) {
while (!($changeIndex === 0 && $currentIndexes[0] === $collectionArraysCounts[0])) {
$currentCombination = array_map(function ($value, $keys, $index) {
return $value[$keys[$index]];
}, $values, $valuesKeys, $currentIndexes);
}, $collectionArrays, $collectionArraysKeys, $currentIndexes);

if ($filter === null || call_user_func_array($filter, $currentCombination)) {
$result[] = ($operation === null) ? $currentCombination : call_user_func_array($operation, $currentCombination);
}

$currentIndexes[$lastIndex]++;

for ($changeIndex = $lastIndex; $currentIndexes[$changeIndex] === $counts[$changeIndex] && $changeIndex > 0; $changeIndex--) {
for ($changeIndex = $lastIndex; $currentIndexes[$changeIndex] === $collectionArraysCounts[$changeIndex] && $changeIndex > 0; $changeIndex--) {
$currentIndexes[$changeIndex] = 0;
$currentIndexes[$changeIndex - 1]++;
}
Expand Down

0 comments on commit f705536

Please sign in to comment.