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
|
|
|
* @var \ArrayObject |
17
|
|
|
*/ |
18
|
|
|
protected $dataset; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Greedy constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->dataset = new \ArrayObject(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function add(...$values) |
32
|
|
|
{ |
33
|
|
|
foreach ($values as $value) { |
34
|
|
|
$this->dataset->append($value); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getDataset() |
44
|
|
|
{ |
45
|
|
|
$dataset = $this->dataset; |
46
|
|
|
$partitionItemFactory = $this->getPartitionItemFactory(); |
47
|
|
|
|
48
|
|
|
// Greedy needs a dataset sorted DESC. |
49
|
|
|
$copy = $dataset->getArrayCopy(); |
50
|
|
|
|
51
|
|
|
\uasort( |
52
|
|
|
$copy, |
53
|
|
|
static function ($a, $b) use ($partitionItemFactory) { |
54
|
|
|
$left = $partitionItemFactory::create($a); |
55
|
|
|
$right = $partitionItemFactory::create($b); |
56
|
|
|
|
57
|
|
|
return $left->getWeight() <=> $right->getWeight(); |
58
|
|
|
} |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$dataset->exchangeArray(\array_reverse($copy)); |
62
|
|
|
|
63
|
|
|
return $dataset; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \drupol\phpartition\Partitions\Partitions $partitions |
68
|
|
|
* @param array $dataset |
69
|
|
|
* @param int $chunks |
70
|
|
|
*/ |
71
|
|
|
protected function fillPartitions(Partitions $partitions, array $dataset, int $chunks): void |
72
|
|
|
{ |
73
|
|
|
foreach ($dataset as $data) { |
74
|
|
|
$this->findPartition($data, null, $dataset, $partitions) |
75
|
|
|
->append($data); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
private function findPartition($value, $key, array $dataset, Partitions $partitions): Partition |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return $partitions |
85
|
|
|
->sort() |
86
|
|
|
->partition(0); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.