Cluster::detach()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of PHP K-Means
5
 *
6
 * Copyright (c) 2014 Benjamin Delespierre
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is furnished
13
 * to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace KMeans;
28
29
class Cluster extends Point implements \IteratorAggregate, \Countable
30
{
31
    protected $space;
32
    protected $points;
33
34
    public function __construct(Space $space, array $coordinates)
35
    {
36
        parent::__construct($space, $coordinates);
37
        $this->points = new \SplObjectStorage();
38
    }
39
40
    public function toArray(): array
41
    {
42
        $points = [];
43
        foreach ($this->points as $point) {
44
            $points[] = $point->toArray();
45
        }
46
47
        return [
48
            'centroid' => parent::toArray(),
49
            'points'   => $points,
50
        ];
51
    }
52
53
    public function attach(Point $point): Point
54
    {
55
        if ($point instanceof self) {
56
            throw new \LogicException("cannot attach a cluster to another");
57
        }
58
59
        $this->points->attach($point);
60
        return $point;
61
    }
62
63
    public function detach(Point $point): Point
64
    {
65
        $this->points->detach($point);
66
        return $point;
67
    }
68
69
    public function attachAll(\SplObjectStorage $points): void
70
    {
71
        $this->points->addAll($points);
72
    }
73
74
    public function detachAll(\SplObjectStorage $points): void
75
    {
76
        $this->points->removeAll($points);
77
    }
78
79
    public function updateCentroid(): void
80
    {
81
        if (!$count = count($this->points)) {
82
            return;
83
        }
84
85
        $centroid = $this->space->newPoint(array_fill(0, $this->dimention, 0));
86
87
        foreach ($this->points as $point) {
88 View Code Duplication
            for ($n = 0; $n < $this->dimention; $n++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
                $centroid->coordinates[$n] += $point->coordinates[$n];
90
            }
91
        }
92
93 View Code Duplication
        for ($n = 0; $n < $this->dimention; $n++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $this->coordinates[$n] = $centroid->coordinates[$n] / $count;
95
        }
96
    }
97
98
    public function getIterator(): \Iterator
99
    {
100
        return $this->points;
101
    }
102
103
    public function count(): int
104
    {
105
        return count($this->points);
106
    }
107
}
108