| @@ 98-145 (lines=48) @@ | ||
| 95 | /** |
|
| 96 | * @return void |
|
| 97 | */ |
|
| 98 | private function prepareForProcessing() |
|
| 99 | { |
|
| 100 | $dependents = []; |
|
| 101 | $dependencies = []; |
|
| 102 | $dependencyCount = []; |
|
| 103 | foreach ($this->nodes as $nodeIndex => $nodes) { |
|
| 104 | if ( ! array_key_exists($nodeIndex, $dependents)) { |
|
| 105 | $dependents[$nodeIndex] = []; |
|
| 106 | } |
|
| 107 | ||
| 108 | $nodeDependencies = array_splice($nodes, 1); |
|
| 109 | foreach ($nodeDependencies as $dependency) { |
|
| 110 | $dependents[$dependency][] = $nodeIndex; |
|
| 111 | } |
|
| 112 | $dependencies[$nodeIndex] = array_flip($nodeDependencies); |
|
| 113 | $dependencyCount[$nodeIndex] = count($nodeDependencies); |
|
| 114 | } |
|
| 115 | ||
| 116 | while ( ! empty($dependencyCount)) { |
|
| 117 | // Order the nodes by their number of unresolved dependencies so that we may |
|
| 118 | // indicate that the nodes which have no unresolved dependencies left are ready to be processed. |
|
| 119 | asort($dependencyCount); |
|
| 120 | ||
| 121 | if (reset($dependencyCount) !== 0) { |
|
| 122 | throw new RuntimeException('Cannot create a sequence of execution for this graph.'); |
|
| 123 | } |
|
| 124 | ||
| 125 | $iteration =& $this->processIteration[]; |
|
| 126 | foreach ($dependencyCount as $nodeIndex => $count) { |
|
| 127 | if ($count !== 0) { |
|
| 128 | break; |
|
| 129 | } |
|
| 130 | ||
| 131 | assert(empty($dependencies[$nodeIndex])); |
|
| 132 | assert($dependencyCount[$nodeIndex] === 0); |
|
| 133 | ||
| 134 | $iteration[] = $nodeIndex; |
|
| 135 | ||
| 136 | unset($dependencies[$nodeIndex]); |
|
| 137 | unset($dependencyCount[$nodeIndex]); |
|
| 138 | foreach ($dependents[$nodeIndex] as $dependent) { |
|
| 139 | unset($dependencies[$dependent][$nodeIndex]); |
|
| 140 | --$dependencyCount[$dependent]; |
|
| 141 | } |
|
| 142 | unset($dependents[$nodeIndex]); |
|
| 143 | } |
|
| 144 | } |
|
| 145 | } |
|
| 146 | ||
| 147 | /** |
|
| 148 | * @param array $arguments |
|
| @@ 77-124 (lines=48) @@ | ||
| 74 | /** |
|
| 75 | * @return void |
|
| 76 | */ |
|
| 77 | private function prepareForProcessing() |
|
| 78 | { |
|
| 79 | $dependents = []; |
|
| 80 | $dependencies = []; |
|
| 81 | $dependencyCount = []; |
|
| 82 | foreach ($this->nodes as $nodeIndex => $nodes) { |
|
| 83 | if ( ! array_key_exists($nodeIndex, $dependents)) { |
|
| 84 | $dependents[$nodeIndex] = []; |
|
| 85 | } |
|
| 86 | ||
| 87 | $nodeDependencies = array_splice($nodes, 1); |
|
| 88 | foreach ($nodeDependencies as $dependency) { |
|
| 89 | $dependents[$dependency][] = $nodeIndex; |
|
| 90 | } |
|
| 91 | $dependencies[$nodeIndex] = array_flip($nodeDependencies); |
|
| 92 | $dependencyCount[$nodeIndex] = count($nodeDependencies); |
|
| 93 | } |
|
| 94 | ||
| 95 | while ( ! empty($dependencyCount)) { |
|
| 96 | // Order the nodes by their number of unresolved dependencies so that we may |
|
| 97 | // indicate that the nodes which have no unresolved dependencies left are ready to be processed. |
|
| 98 | asort($dependencyCount); |
|
| 99 | ||
| 100 | if (reset($dependencyCount) !== 0) { |
|
| 101 | throw new RuntimeException('Cannot create a sequence of execution for this graph.'); |
|
| 102 | } |
|
| 103 | ||
| 104 | $iteration =& $this->processIteration[]; |
|
| 105 | foreach ($dependencyCount as $nodeIndex => $count) { |
|
| 106 | if ($count !== 0) { |
|
| 107 | break; |
|
| 108 | } |
|
| 109 | ||
| 110 | assert(empty($dependencies[$nodeIndex])); |
|
| 111 | assert($dependencyCount[$nodeIndex] === 0); |
|
| 112 | ||
| 113 | $iteration[] = $nodeIndex; |
|
| 114 | ||
| 115 | unset($dependencies[$nodeIndex]); |
|
| 116 | unset($dependencyCount[$nodeIndex]); |
|
| 117 | foreach ($dependents[$nodeIndex] as $dependent) { |
|
| 118 | unset($dependencies[$dependent][$nodeIndex]); |
|
| 119 | --$dependencyCount[$dependent]; |
|
| 120 | } |
|
| 121 | unset($dependents[$nodeIndex]); |
|
| 122 | } |
|
| 123 | } |
|
| 124 | } |
|
| 125 | ||
| 126 | /** |
|
| 127 | * @param array $arguments |
|