笛卡尔运算 迭代器内存优化版

2022-02-15 06:12:01   工作备份

  个人常用函数  

  1. class CartesianService
  2. {
  3. public static function cartezianIterator($inputArray)
  4. {
  5. $maximumPosition = array_map('count', $inputArray);
  6. $position = array_pad([], count($inputArray), 0);
  7. while (false !== ($item = self::buildItemAtPosition($inputArray, $position))) {
  8. yield $item;
  9. $position = self::incrementPosition($position, $maximumPosition);
  10. }
  11. }
  12. private static function buildItemAtPosition($inputArray, $positions)
  13. {
  14. if ($positions[0] >= count($inputArray[0])) {
  15. return false;
  16. }
  17. $item = [];
  18. foreach ($inputArray as $rowIndex => $row) {
  19. $position = $positions[$rowIndex];
  20. $item[] = $row[$position];
  21. }
  22. return $item;
  23. }
  24. private static function incrementPosition($position, $maximumPosition)
  25. {
  26. $digitToIncrement = count($position) - 1;
  27. do {
  28. ++$position[$digitToIncrement];
  29. if ($position[$digitToIncrement] < $maximumPosition[$digitToIncrement] || 0 === $digitToIncrement) {
  30. //no overflow
  31. break;
  32. }
  33. //overflow, reset to zero and increment parent digit
  34. $position[$digitToIncrement] = 0;
  35. --$digitToIncrement;
  36. } while ($digitToIncrement >= 0);
  37. return $position;
  38. }
  39. }

使用

  1. foreach (CartesianService::cartezianIterator(array_values($cartesian_arr)) as $item) {
  2. var_dump($item);
  3. }