Completed
Pull Request — master (#2)
by Pol
04:12
created

Greedy   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A fillPartitions() 0 17 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpartition;
6
7
use drupol\phpartition\Partition\Partition;
8
use drupol\phpartition\Partitions\Partitions;
9
10
/**
11
 * Class Greedy.
12
 */
13
class Greedy extends Partitioner
14
{
15
    /**
16
     * @param \drupol\phpartition\Partitions\Partitions $partitions
17
     * @param \drupol\phpartition\Partition\Partition $dataset
18
     * @param int $chunks
19
     */
20
    protected function fillPartitions(Partitions $partitions, Partition $dataset, int $chunks): void
21
    {
22
        $partitionItemFactory = $this->getPartitionItemFactory();
23
24
        // Greedy needs a dataset sorted DESC.
25
        $dataset->uasort(static function ($a, $b) use ($partitionItemFactory) {
26
            $left = $partitionItemFactory::create($a);
27
            $right = $partitionItemFactory::create($b);
28
29
            return $right->getWeight() <=> $left->getWeight();
30
        });
31
32
        foreach ($dataset as $data) {
33
            $partitions
34
                ->sort()
35
                ->partition(0)
36
                ->append($data);
37
        }
38
    }
39
}
40