Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
NodeMap | |
100.00% |
9 / 9 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
initialize | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setNode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getParentId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getNodeMapArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
5 | */ |
6 | |
7 | declare(strict_types=1); |
8 | |
9 | namespace pvc\struct\treesearch; |
10 | |
11 | |
12 | use pvc\interfaces\struct\treesearch\NodeMapInterface; |
13 | use pvc\interfaces\struct\treesearch\NodeVisitableInterface; |
14 | |
15 | /** |
16 | * Class NodeMap |
17 | * |
18 | * This is generated as a by-product of a depth first search and as a result of iteration. It is initialized in the |
19 | * rewind method of the search. The start node of the search is at depth = 0 |
20 | * |
21 | * @phpstan-import-type NodeMapRow from NodeMapInterface |
22 | * @template NodeType of NodeVisitableInterface |
23 | * @implements NodeMapInterface<NodeType> |
24 | */ |
25 | class NodeMap implements NodeMapInterface |
26 | { |
27 | /** |
28 | * @var array<NodeMapRow> |
29 | */ |
30 | protected array $nodes = []; |
31 | |
32 | /** |
33 | * initialize by putting the start node of the search into the map |
34 | */ |
35 | public function initialize(NodeVisitableInterface $node): void |
36 | { |
37 | $this->nodes = []; |
38 | $this->setNode($node, null); |
39 | } |
40 | |
41 | /** |
42 | * setNode |
43 | * @param NodeType $node |
44 | * @param non-negative-int|null $parentId |
45 | */ |
46 | public function setNode(NodeVisitableInterface $node, ?int $parentId): void |
47 | { |
48 | $this->nodes[$node->getNodeId()] = ['parentId' => $parentId, 'node' => $node]; |
49 | } |
50 | |
51 | /** |
52 | * getParent |
53 | * @param int $nodeId |
54 | * @return NodeType|null |
55 | */ |
56 | public function getParent(int $nodeId): ?NodeVisitableInterface |
57 | { |
58 | return $this->getNode($this->getParentId($nodeId)); |
59 | } |
60 | |
61 | /** |
62 | * getNode |
63 | * @param ?int $nodeId |
64 | * @return NodeType|null |
65 | */ |
66 | public function getNode(?int $nodeId): ?NodeVisitableInterface |
67 | { |
68 | $array = $this->nodes[$nodeId] ?? []; |
69 | return $array['node'] ?? null; |
70 | } |
71 | |
72 | /** |
73 | * getParentId |
74 | * @param int $nodeId |
75 | * @return non-negative-int|null |
76 | */ |
77 | public function getParentId(int $nodeId): ?int |
78 | { |
79 | $array = $this->nodes[$nodeId] ?? []; |
80 | return $array['parentId'] ?? null; |
81 | } |
82 | |
83 | /** |
84 | * @return array<NodeMapRow> |
85 | */ |
86 | public function getNodeMapArray(): array |
87 | { |
88 | /** @phpstan-ignore-next-line */ |
89 | return $this->nodes; |
90 | } |
91 | } |