1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Bankiru\Api\Doctrine\Utility; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CommitOrderCalculator implements topological sorting, which is an ordering |
24
|
|
|
* algorithm for directed graphs (DG) and/or directed acyclic graphs (DAG) by |
25
|
|
|
* using a depth-first searching (DFS) to traverse the graph built in memory. |
26
|
|
|
* This algorithm have a linear running time based on nodes (V) and dependency |
27
|
|
|
* between the nodes (E), resulting in a computational complexity of O(V + E). |
28
|
|
|
* |
29
|
|
|
* @since 2.0 |
30
|
|
|
* @author Guilherme Blanco <[email protected]> |
31
|
|
|
* @author Roman Borschel <[email protected]> |
32
|
|
|
*/ |
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) |
70
|
|
|
{ |
71
|
4 |
|
return isset($this->nodeList[$hash]); |
72
|
|
|
} |
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) |
82
|
|
|
{ |
83
|
4 |
|
$vertex = new \stdClass(); |
84
|
4 |
|
$vertex->hash = $hash; |
85
|
4 |
|
$vertex->state = self::NOT_VISITED; |
86
|
4 |
|
$vertex->value = $node; |
87
|
4 |
|
$vertex->dependencyList = array(); |
88
|
4 |
|
$this->nodeList[$hash] = $vertex; |
89
|
4 |
|
} |
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) |
100
|
|
|
{ |
101
|
4 |
|
$vertex = $this->nodeList[$fromHash]; |
102
|
4 |
|
$edge = new \stdClass(); |
103
|
4 |
|
$edge->from = $fromHash; |
104
|
4 |
|
$edge->to = $toHash; |
105
|
4 |
|
$edge->weight = $weight; |
106
|
4 |
|
$vertex->dependencyList[$toHash] = $edge; |
107
|
4 |
|
} |
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() |
117
|
|
|
{ |
118
|
4 |
|
foreach ($this->nodeList as $vertex) { |
119
|
4 |
|
if ($vertex->state !== self::NOT_VISITED) { |
120
|
1 |
|
continue; |
121
|
|
|
} |
122
|
4 |
|
$this->visit($vertex); |
123
|
4 |
|
} |
124
|
4 |
|
$sortedList = $this->sortedNodeList; |
125
|
4 |
|
$this->nodeList = array(); |
126
|
4 |
|
$this->sortedNodeList = array(); |
127
|
4 |
|
return array_reverse($sortedList); |
128
|
|
|
} |
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) |
137
|
|
|
{ |
138
|
4 |
|
$vertex->state = self::IN_PROGRESS; |
139
|
4 |
|
foreach ($vertex->dependencyList as $edge) { |
140
|
4 |
|
$adjacentVertex = $this->nodeList[$edge->to]; |
141
|
4 |
|
switch ($adjacentVertex->state) { |
142
|
4 |
|
case self::VISITED: |
143
|
|
|
// Do nothing, since node was already visited |
144
|
4 |
|
break; |
145
|
4 |
|
case self::IN_PROGRESS: |
146
|
4 |
|
if (isset($adjacentVertex->dependencyList[$vertex->hash]) && |
147
|
4 |
|
$adjacentVertex->dependencyList[$vertex->hash]->weight < $edge->weight) { |
148
|
|
|
$adjacentVertex->state = self::VISITED; |
149
|
|
|
$this->sortedNodeList[] = $adjacentVertex->value; |
150
|
|
|
} |
151
|
4 |
|
break; |
152
|
1 |
|
case self::NOT_VISITED: |
153
|
1 |
|
$this->visit($adjacentVertex); |
154
|
4 |
|
} |
155
|
4 |
|
} |
156
|
4 |
|
if ($vertex->state !== self::VISITED) { |
157
|
4 |
|
$vertex->state = self::VISITED; |
158
|
4 |
|
$this->sortedNodeList[] = $vertex->value; |
159
|
4 |
|
} |
160
|
4 |
|
} |
161
|
|
|
} |
162
|
|
|
|