Passed
Pull Request — master (#2)
by Pol
09:04
created

Greedy::fillPartitions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpartition;
6
7
use drupol\phpartition\Contract\PartitionItem;
8
use drupol\phpartition\Partition\Partition;
9
use drupol\phpartition\Partitions\Partitions;
10
11
/**
12
 * Class Greedy.
13
 */
14
class Greedy extends Partitioner
15
{
16
    /**
17
     * @param \drupol\phpartition\Partitions\Partitions $partitions
18
     * @param \drupol\phpartition\Partition\Partition $dataset
19
     * @param int $chunks
20
     */
21
    protected function fillPartitions(Partitions $partitions, Partition $dataset, int $chunks): void
22
    {
23
        // Greedy needs a dataset sorted DESC.
24
        $dataset->uasort(
25
            static function (PartitionItem $left, PartitionItem $right) {
26
                return $right->getWeight() <=> $left->getWeight();
27
            }
28
        );
29
30
        foreach ($dataset as $data) {
31
            $partitions
32
                ->sort()
33
                ->partition(0)
34
                ->append($data);
35
        }
36
    }
37
}
38