Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| SearchBreadthFirst | |
0.00% |
0 / 13 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
| rewind | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| next | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| 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 | use pvc\interfaces\struct\treesearch\NodeSearchableCollectionInterface; |
| 12 | use pvc\interfaces\struct\treesearch\NodeSearchableInterface; |
| 13 | use pvc\struct\treesearch\err\StartNodeUnsetException; |
| 14 | |
| 15 | /** |
| 16 | * Class SearchBreadthFirst |
| 17 | * |
| 18 | * @template NodeIdType of array-key |
| 19 | * @template NodeType of NodeSearchableInterface |
| 20 | * @extends SearchAbstract<NodeIdType, NodeType, NodeSearchableCollectionInterface> |
| 21 | */ |
| 22 | class SearchBreadthFirst extends SearchAbstract |
| 23 | { |
| 24 | /** |
| 25 | * nodes in the "current level" of the tree. |
| 26 | * |
| 27 | * @var NodeSearchableCollectionInterface<NodeIdType, NodeType> |
| 28 | */ |
| 29 | private NodeSearchableCollectionInterface $currentLevelNodes; |
| 30 | |
| 31 | /** |
| 32 | * rewind |
| 33 | * |
| 34 | * @throws StartNodeUnsetException |
| 35 | */ |
| 36 | public function rewind(): void |
| 37 | { |
| 38 | parent::rewind(); |
| 39 | $this->currentLevelNodes->initialize(); |
| 40 | $this->currentLevelNodes->add($this->getStartNode()); |
| 41 | $this->next(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * next |
| 46 | */ |
| 47 | public function next(): void |
| 48 | { |
| 49 | /** |
| 50 | * If there are no nodes at the current level, or we are at the maximum level |
| 51 | * permitted in the search and there are no more nodes at this level to |
| 52 | * process, set valid to false and return |
| 53 | */ |
| 54 | if (empty($this->currentLevelNodes) || $this->atMaxLevels()) { |
| 55 | $this->invalidate(); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Move to the next node in the current level. Check valid and if not valid, |
| 61 | * replenish currentLevelNodes with the next level of nodes, otherwise, |
| 62 | * et |
| 63 | */ |
| 64 | $this->currentLevelNodes->next(); |
| 65 | |
| 66 | if (!$this->currentLevelNodes->valid()) { |
| 67 | $this->currentLevelNodes = $this->currentLevelNodes->getChildren(); |
| 68 | $this->setCurrentLevel(-Direction::MOVE_DOWN->value); |
| 69 | $this->next(); |
| 70 | } else { |
| 71 | $this->setCurrent($this->currentLevelNodes->current()); |
| 72 | } |
| 73 | } |
| 74 | } |