1 | <?php |
||
33 | class CommitOrderCalculator |
||
34 | { |
||
35 | const NOT_VISITED = 0; |
||
36 | const IN_PROGRESS = 1; |
||
37 | const VISITED = 2; |
||
38 | /** |
||
39 | * Matrix of nodes (aka. vertex). |
||
40 | * Keys are provided hashes and values are the node definition objects. |
||
41 | * |
||
42 | * The node state definition contains the following properties: |
||
43 | * |
||
44 | * - <b>state</b> (integer) |
||
45 | * Whether the node is NOT_VISITED or IN_PROGRESS |
||
46 | * |
||
47 | * - <b>value</b> (object) |
||
48 | * Actual node value |
||
49 | * |
||
50 | * - <b>dependencyList</b> (array<string>) |
||
51 | * Map of node dependencies defined as hashes. |
||
52 | * |
||
53 | * @var array<stdClass> |
||
54 | */ |
||
55 | private $nodeList = array(); |
||
56 | /** |
||
57 | * Volatile variable holding calculated nodes during sorting process. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $sortedNodeList = array(); |
||
62 | /** |
||
63 | * Checks for node (vertex) existence in graph. |
||
64 | * |
||
65 | * @param string $hash |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | 4 | public function hasNode($hash) |
|
73 | /** |
||
74 | * Adds a new node (vertex) to the graph, assigning its hash and value. |
||
75 | * |
||
76 | * @param string $hash |
||
77 | * @param object $node |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | 4 | public function addNode($hash, $node) |
|
90 | /** |
||
91 | * Adds a new dependency (edge) to the graph using their hashes. |
||
92 | * |
||
93 | * @param string $fromHash |
||
94 | * @param string $toHash |
||
95 | * @param integer $weight |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | 4 | public function addDependency($fromHash, $toHash, $weight) |
|
108 | /** |
||
109 | * Return a valid order list of all current nodes. |
||
110 | * The desired topological sorting is the reverse post order of these searches. |
||
111 | * |
||
112 | * {@internal Highly performance-sensitive method.} |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 4 | public function sort() |
|
129 | /** |
||
130 | * Visit a given node definition for reordering. |
||
131 | * |
||
132 | * {@internal Highly performance-sensitive method.} |
||
133 | * |
||
134 | * @param \stdClass $vertex |
||
135 | */ |
||
136 | 4 | private function visit($vertex) |
|
161 | } |
||
162 |