PartitionSearch   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 5
eloc 11
c 2
b 0
f 2
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A partitionAdd() 0 12 3
A find() 0 6 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KDTree\Search;
6
7
use KDTree\Exceptions\InvalidDimensionsCount;
8
use KDTree\Interfaces\{KDTreeInterface,
9
    NodeInterface,
10
    PartitionInterface,
11
    PartitionSearchInterface,
12
    PointInterface,
13
    PointsListInterface};
14
use KDTree\Structure\PointsList;
15
16
/**
17
 * Class PartitionSearch
18
 *
19
 * @package KDTree\Search
20
 */
21
final class PartitionSearch implements PartitionSearchInterface
22
{
23
    /**
24
     * @var KDTreeInterface
25
     */
26
    private $tree;
27
28
    /**
29
     * @param KDTreeInterface $tree
30
     */
31
    public function __construct(KDTreeInterface $tree)
32
    {
33
        $this->tree = $tree;
34
    }
35
36
    /**
37
     * @param PartitionInterface $partition
38
     *
39
     * @return PointsListInterface<PointInterface>
40
     * @throws InvalidDimensionsCount
41
     */
42
    public function find(PartitionInterface $partition): PointsListInterface
43
    {
44
        $pointsList = new PointsList($this->tree->getDimensions());
45
        $this->partitionAdd($this->tree->getRoot(), $partition, $pointsList);
46
47
        return $pointsList;
48
    }
49
50
    /**
51
     * @param NodeInterface|null                  $node
52
     * @param PartitionInterface                  $partition
53
     * @param PointsListInterface<PointInterface> $pointsList
54
     */
55
    private function partitionAdd(
56
        ?NodeInterface $node,
57
        PartitionInterface $partition,
58
        PointsListInterface $pointsList
59
    ): void {
60
        if (null !== $node) {
61
            if ($partition->contains($node->getPoint())) {
62
                $pointsList->addPoint($node->getPoint());
63
            }
64
65
            $this->partitionAdd($node->getLeft(), $partition, $pointsList);
66
            $this->partitionAdd($node->getRight(), $partition, $pointsList);
67
        }
68
    }
69
}
70