Completed
Push — master ( 1aac5f...cabb3c )
by Benjamin
21s queued 11s
created

Cluster::__construct()   A

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