|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ORM\Internal; |
| 6 | + |
| 7 | +use Doctrine\ORM\Internal\TopologicalSort\CycleDetectedException; |
| 8 | + |
| 9 | +use function array_keys; |
| 10 | +use function array_reverse; |
| 11 | +use function array_unshift; |
| 12 | +use function spl_object_id; |
| 13 | + |
| 14 | +/** |
| 15 | + * TopologicalSort implements topological sorting, which is an ordering |
| 16 | + * algorithm for directed graphs (DG) using a depth-first searching (DFS) |
| 17 | + * to traverse the graph built in memory. |
| 18 | + * This algorithm has a linear running time based on nodes (V) and edges |
| 19 | + * between the nodes (E), resulting in a computational complexity of O(V + E). |
| 20 | + * |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class TopologicalSort |
| 24 | +{ |
| 25 | + private const NOT_VISITED = 1; |
| 26 | + private const IN_PROGRESS = 2; |
| 27 | + private const VISITED = 3; |
| 28 | + |
| 29 | + /** |
| 30 | + * Array of all nodes, indexed by object ids. |
| 31 | + * |
| 32 | + * @var array<int, object> |
| 33 | + */ |
| 34 | + private $nodes = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * DFS state for the different nodes, indexed by node object id and using one of |
| 38 | + * this class' constants as value. |
| 39 | + * |
| 40 | + * @var array<int, self::*> |
| 41 | + */ |
| 42 | + private $states = []; |
| 43 | + |
| 44 | + /** |
| 45 | + * Edges between the nodes. The first-level key is the object id of the outgoing |
| 46 | + * node; the second array maps the destination node by object id as key. The final |
| 47 | + * boolean value indicates whether the edge is optional or not. |
| 48 | + * |
| 49 | + * @var array<int, array<int, bool>> |
| 50 | + */ |
| 51 | + private $edges = []; |
| 52 | + |
| 53 | + /** |
| 54 | + * Builds up the result during the DFS. |
| 55 | + * |
| 56 | + * @var list<object> |
| 57 | + */ |
| 58 | + private $sortResult = []; |
| 59 | + |
| 60 | + /** @param object $node */ |
| 61 | + public function addNode($node): void |
| 62 | + { |
| 63 | + $id = spl_object_id($node); |
| 64 | + $this->nodes[$id] = $node; |
| 65 | + $this->states[$id] = self::NOT_VISITED; |
| 66 | + $this->edges[$id] = []; |
| 67 | + } |
| 68 | + |
| 69 | + /** @param object $node */ |
| 70 | + public function hasNode($node): bool |
| 71 | + { |
| 72 | + return isset($this->nodes[spl_object_id($node)]); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds a new edge between two nodes to the graph |
| 77 | + * |
| 78 | + * @param object $from |
| 79 | + * @param object $to |
| 80 | + * @param bool $optional This indicates whether the edge may be ignored during the topological sort if it is necessary to break cycles. |
| 81 | + */ |
| 82 | + public function addEdge($from, $to, bool $optional): void |
| 83 | + { |
| 84 | + $fromId = spl_object_id($from); |
| 85 | + $toId = spl_object_id($to); |
| 86 | + |
| 87 | + if (isset($this->edges[$fromId][$toId]) && $this->edges[$fromId][$toId] === false) { |
| 88 | + return; // we already know about this dependency, and it is not optional |
| 89 | + } |
| 90 | + |
| 91 | + $this->edges[$fromId][$toId] = $optional; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns a topological sort of all nodes. When we have an edge A->B between two nodes |
| 96 | + * A and B, then A will be listed before B in the result. |
| 97 | + * |
| 98 | + * @return list<object> |
| 99 | + */ |
| 100 | + public function sort(): array |
| 101 | + { |
| 102 | + /* |
| 103 | + * When possible, keep objects in the result in the same order in which they were added as nodes. |
| 104 | + * Since nodes are unshifted into $this->>sortResult (see the visit() method), that means we |
| 105 | + * need to work them in array_reverse order here. |
| 106 | + */ |
| 107 | + foreach (array_reverse(array_keys($this->nodes)) as $oid) { |
| 108 | + if ($this->states[$oid] === self::NOT_VISITED) { |
| 109 | + $this->visit($oid); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return $this->sortResult; |
| 114 | + } |
| 115 | + |
| 116 | + private function visit(int $oid): void |
| 117 | + { |
| 118 | + if ($this->states[$oid] === self::IN_PROGRESS) { |
| 119 | + // This node is already on the current DFS stack. We've found a cycle! |
| 120 | + throw new CycleDetectedException($this->nodes[$oid]); |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->states[$oid] === self::VISITED) { |
| 124 | + // We've reached a node that we've already seen, including all |
| 125 | + // other nodes that are reachable from here. We're done here, return. |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $this->states[$oid] = self::IN_PROGRESS; |
| 130 | + |
| 131 | + // Continue the DFS downwards the edge list |
| 132 | + foreach ($this->edges[$oid] as $adjacentId => $optional) { |
| 133 | + try { |
| 134 | + $this->visit($adjacentId); |
| 135 | + } catch (CycleDetectedException $exception) { |
| 136 | + if ($exception->isCycleCollected()) { |
| 137 | + // There is a complete cycle downstream of the current node. We cannot |
| 138 | + // do anything about that anymore. |
| 139 | + throw $exception; |
| 140 | + } |
| 141 | + |
| 142 | + if ($optional) { |
| 143 | + // The current edge is part of a cycle, but it is optional and the closest |
| 144 | + // such edge while backtracking. Break the cycle here by skipping the edge |
| 145 | + // and continuing with the next one. |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + // We have found a cycle and cannot break it at $edge. Best we can do |
| 150 | + // is to retreat from the current vertex, hoping that somewhere up the |
| 151 | + // stack this can be salvaged. |
| 152 | + $this->states[$oid] = self::NOT_VISITED; |
| 153 | + $exception->addToCycle($this->nodes[$oid]); |
| 154 | + |
| 155 | + throw $exception; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // We have traversed all edges and visited all other nodes reachable from here. |
| 160 | + // So we're done with this vertex as well. |
| 161 | + |
| 162 | + $this->states[$oid] = self::VISITED; |
| 163 | + array_unshift($this->sortResult, $this->nodes[$oid]); |
| 164 | + } |
| 165 | +} |
0 commit comments