1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpartition; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class PartitionContainer. |
7
|
|
|
* |
8
|
|
|
* @package drupol\phpartition |
9
|
|
|
*/ |
10
|
|
|
class PartitionContainer extends \SplHeap { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var BasePartitionAlgorithm |
14
|
|
|
*/ |
15
|
|
|
protected $algo; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $size; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Override compare method. |
24
|
|
|
* |
25
|
|
|
* @param Partition $a |
26
|
|
|
* @param Partition $b |
27
|
|
|
* |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
10 |
|
public function compare($a, $b) { |
|
|
|
|
31
|
10 |
|
$al = $a->getAlgo()->getPartitionWeight($a); |
|
|
|
|
32
|
10 |
|
$bl = $b->getAlgo()->getPartitionWeight($b); |
|
|
|
|
33
|
|
|
|
34
|
10 |
|
if ($al == $bl) { |
35
|
10 |
|
return 0; |
36
|
|
|
} |
37
|
10 |
|
return ($al > $bl) ? -1 : +1; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $size |
42
|
|
|
*/ |
43
|
10 |
|
public function setSize($size) { |
44
|
10 |
|
$this->size = $size; |
45
|
|
|
|
46
|
10 |
|
for ($i = 0; $i < $size; $i++) { |
47
|
10 |
|
$subset = new Partition(); |
48
|
10 |
|
$subset->setAlgo($this->getAlgo()); |
49
|
10 |
|
$this->insert($subset); |
50
|
|
|
} |
51
|
10 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
10 |
|
public function getSize() { |
57
|
10 |
|
return $this->size; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param PartitionItem[] $items |
62
|
|
|
*/ |
63
|
4 |
|
public function addItemsToPartition(array $items = array()) { |
64
|
4 |
|
foreach ($items as $item) { |
65
|
4 |
|
$this->addItemToPartition($item); |
66
|
|
|
} |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param \drupol\phpartition\PartitionItem $item |
71
|
|
|
*/ |
72
|
4 |
|
public function addItemToPartition(PartitionItem $item) { |
73
|
4 |
|
$this->top(); |
74
|
4 |
|
$subset = $this->extract(); |
75
|
4 |
|
$subset->addItem($item); |
76
|
4 |
|
$this->insert($subset); |
77
|
4 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Partition[] |
81
|
|
|
*/ |
82
|
10 |
|
public function getPartitions() { |
83
|
10 |
|
$data = array(); |
84
|
10 |
|
$clone = clone $this; |
85
|
|
|
|
86
|
10 |
|
for ($clone->top(); $clone->valid(); $clone->next()) { |
87
|
10 |
|
$data[] = $clone->current(); |
88
|
|
|
} |
89
|
|
|
|
90
|
10 |
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
10 |
|
public function getPartitionsItemsArray() { |
97
|
10 |
|
$data = array(); |
98
|
|
|
|
99
|
10 |
|
foreach ($this->getPartitions() as $subset) { |
100
|
10 |
|
$data[] = $subset->getRawItems(); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
return array_values(array_filter($data)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \drupol\phpartition\BasePartitionAlgorithm $algo |
108
|
|
|
*/ |
109
|
10 |
|
public function setAlgo(BasePartitionAlgorithm $algo) { |
110
|
10 |
|
$this->algo = $algo; |
111
|
10 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return BasePartitionAlgorithm |
115
|
|
|
*/ |
116
|
10 |
|
public function getAlgo() { |
117
|
10 |
|
return $this->algo; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.