|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Base daft objects. |
|
4
|
|
|
* |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
|
10
|
|
|
|
|
11
|
|
|
class InefficientDaftNestedRebuild |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var DaftNestedWriteableObjectTree |
|
15
|
|
|
*/ |
|
16
|
|
|
private $tree; |
|
17
|
|
|
|
|
18
|
48 |
|
public function __construct(DaftNestedWriteableObjectTree $tree) |
|
19
|
|
|
{ |
|
20
|
48 |
|
$this->tree = $tree; |
|
21
|
48 |
|
} |
|
22
|
|
|
|
|
23
|
48 |
|
public function RebuildTree() : void |
|
24
|
|
|
{ |
|
25
|
|
|
$parentIdXref = [ |
|
26
|
48 |
|
(array) $this->tree->GetNestedObjectTreeRootId(), |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array<int, array<int, DaftNestedWriteableObject>> $xRefChildren |
|
31
|
|
|
*/ |
|
32
|
|
|
$xRefChildren = [ |
|
33
|
48 |
|
[], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array<int, scalar|scalar[]> $idXref |
|
38
|
|
|
*/ |
|
39
|
48 |
|
$idXref = []; |
|
40
|
|
|
|
|
41
|
48 |
|
$tree = $this->tree->RecallDaftNestedObjectFullTree(); |
|
42
|
|
|
|
|
43
|
|
|
usort($tree, function (DaftNestedWriteableObject $a, DaftNestedWriteableObject $b) : int { |
|
44
|
48 |
|
return $this->tree->CompareObjects($a, $b); |
|
45
|
48 |
|
}); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var DaftNestedWriteableObject $leaf |
|
49
|
|
|
*/ |
|
50
|
48 |
|
foreach ($tree as $i => $leaf) { |
|
51
|
48 |
|
$leafParentId = $leaf->ObtainDaftNestedObjectParentId(); |
|
52
|
48 |
|
$pos = array_search($leafParentId, $parentIdXref, true); |
|
53
|
|
|
|
|
54
|
48 |
|
if (false === $pos) { |
|
55
|
42 |
|
$parentIdXref[] = $leafParentId; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var int $pos |
|
59
|
|
|
*/ |
|
60
|
42 |
|
$pos = array_search($leafParentId, $parentIdXref, true); |
|
61
|
|
|
|
|
62
|
42 |
|
$xRefChildren[$pos] = []; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
48 |
|
if ( ! in_array($leaf, $xRefChildren[$pos], true)) { |
|
66
|
48 |
|
$xRefChildren[$pos][] = $leaf; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
48 |
|
if ( ! in_array($leaf->GetId(), $idXref, true)) { |
|
70
|
|
|
/** |
|
71
|
|
|
* @var scalar|scalar[] $leafId |
|
72
|
|
|
*/ |
|
73
|
48 |
|
$leafId = $leaf->GetId(); |
|
74
|
48 |
|
$idXref[] = $leafId; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
48 |
|
$leaf->SetIntNestedLeft(0); |
|
78
|
48 |
|
$leaf->SetIntNestedRight(0); |
|
79
|
48 |
|
$leaf->SetIntNestedLevel(0); |
|
80
|
|
|
|
|
81
|
48 |
|
$tree[$i] = $this->tree->StoreThenRetrieveFreshLeaf($leaf); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
48 |
|
$n = 0; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var DaftNestedWriteableObject $rootLeaf |
|
88
|
|
|
*/ |
|
89
|
48 |
|
foreach ($xRefChildren[0] as $rootLeaf) { |
|
90
|
48 |
|
$n = $this->InefficientRebuild( |
|
91
|
48 |
|
$rootLeaf, |
|
92
|
48 |
|
0, |
|
93
|
48 |
|
$n, |
|
94
|
48 |
|
$parentIdXref, |
|
95
|
48 |
|
$idXref, |
|
96
|
48 |
|
$xRefChildren |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
48 |
|
} |
|
100
|
|
|
|
|
101
|
48 |
|
protected function InefficientRebuild( |
|
102
|
|
|
DaftNestedWriteableObject $leaf, |
|
103
|
|
|
int $level, |
|
104
|
|
|
int $n, |
|
105
|
|
|
array $parentIds, |
|
106
|
|
|
array $ids, |
|
107
|
|
|
array $children |
|
108
|
|
|
) : int { |
|
109
|
|
|
/** |
|
110
|
|
|
* @var scalar|scalar[] $id |
|
111
|
|
|
*/ |
|
112
|
48 |
|
$id = $leaf->GetId(); |
|
113
|
|
|
|
|
114
|
48 |
|
$leaf->SetIntNestedLevel($level); |
|
115
|
48 |
|
$leaf->SetIntNestedLeft($n); |
|
116
|
|
|
|
|
117
|
48 |
|
++$n; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @var int|false $parentPos |
|
121
|
|
|
*/ |
|
122
|
48 |
|
$parentPos = array_search((array) $id, $parentIds, true); |
|
123
|
|
|
|
|
124
|
48 |
|
if (false !== $parentPos) { |
|
125
|
|
|
/** |
|
126
|
|
|
* @var DaftNestedWriteableObject $childLeaf |
|
127
|
|
|
*/ |
|
128
|
42 |
|
foreach ($children[$parentPos] as $childLeaf) { |
|
129
|
42 |
|
$n = $this->InefficientRebuild( |
|
130
|
42 |
|
$childLeaf, |
|
131
|
42 |
|
$level + 1, |
|
132
|
42 |
|
$n, |
|
133
|
42 |
|
$parentIds, |
|
134
|
42 |
|
$ids, |
|
135
|
42 |
|
$children |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
48 |
|
$leaf->SetIntNestedRight($n); |
|
141
|
|
|
|
|
142
|
48 |
|
$this->tree->StoreThenRetrieveFreshLeaf($leaf); |
|
143
|
|
|
|
|
144
|
48 |
|
return $n + 1; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|