1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpartition; |
4
|
|
|
|
5
|
|
|
use phootwork\collection\Map; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Partition. |
9
|
|
|
* |
10
|
|
|
* @package drupol\phpartition |
11
|
|
|
*/ |
12
|
|
|
class Partition extends Map |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The algorithm in use. |
17
|
|
|
* |
18
|
|
|
* @var PartitionAlgorithmInterface |
19
|
|
|
*/ |
20
|
|
|
protected $algo; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Partition constructor. |
24
|
|
|
* |
25
|
|
|
* @param PartitionItem[] $elements |
26
|
|
|
* The list of elements. |
27
|
|
|
*/ |
28
|
14 |
|
public function __construct(array $elements = array()) |
29
|
|
|
{ |
30
|
14 |
|
parent::__construct(array()); |
31
|
14 |
|
$this->addItems($elements); |
32
|
14 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add an item to the partition. |
36
|
|
|
* |
37
|
|
|
* @param PartitionItem $item |
38
|
|
|
* The item to add to the partition. |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
* Return itself. |
42
|
|
|
*/ |
43
|
14 |
|
public function addItem(PartitionItem $item) |
44
|
|
|
{ |
45
|
14 |
|
$this->set(spl_object_hash($item), $item); |
46
|
|
|
|
47
|
14 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add items to the partition. |
52
|
|
|
* |
53
|
|
|
* @param PartitionItem[] $items |
54
|
|
|
* The items to add to the partition. |
55
|
|
|
* |
56
|
|
|
* @return $this |
57
|
|
|
* Return itself. |
58
|
|
|
*/ |
59
|
14 |
|
public function addItems(array $items = array()) |
60
|
|
|
{ |
61
|
14 |
|
foreach ($items as $item) { |
62
|
14 |
|
$this->addItem($item); |
63
|
14 |
|
} |
64
|
|
|
|
65
|
14 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get an array of original items. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
* The original items. |
73
|
|
|
*/ |
74
|
14 |
|
public function getRawItems() |
75
|
|
|
{ |
76
|
14 |
|
return array_values( |
77
|
|
|
array_map(function ($item) { |
78
|
14 |
|
return $item->getItem(); |
79
|
14 |
|
}, $this->toArray()) |
80
|
14 |
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the weight of the partition. |
85
|
|
|
* |
86
|
|
|
* @return int |
87
|
|
|
* The partition's weight. |
88
|
|
|
*/ |
89
|
11 |
|
public function getWeight() |
90
|
|
|
{ |
91
|
|
|
return array_reduce($this->toArray(), function ($sum, $item) { |
92
|
11 |
|
$sum += $item->getValue(); |
93
|
11 |
|
return $sum; |
94
|
11 |
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the algorithm to use. |
99
|
|
|
* |
100
|
|
|
* @param BasePartitionAlgorithm $algo |
101
|
|
|
* The algorithm to use. |
102
|
|
|
*/ |
103
|
14 |
|
public function setAlgo(BasePartitionAlgorithm $algo) |
104
|
|
|
{ |
105
|
14 |
|
$this->algo = $algo; |
106
|
14 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the algorithm in use. |
110
|
|
|
* |
111
|
|
|
* @return PartitionAlgorithmInterface |
112
|
|
|
* The algorithm in use. |
113
|
|
|
*/ |
114
|
14 |
|
public function getAlgo() |
115
|
|
|
{ |
116
|
14 |
|
return $this->algo; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Delete items from the partition. |
121
|
|
|
* |
122
|
|
|
* @param PartitionItem[] $items |
123
|
|
|
* The items to delete. |
124
|
|
|
*/ |
125
|
3 |
|
public function deleteItems(array $items = array()) |
126
|
|
|
{ |
127
|
3 |
|
foreach ($items as $item) { |
128
|
3 |
|
$this->delete($item); |
129
|
3 |
|
} |
130
|
3 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Delete an item from the partition. |
134
|
|
|
* |
135
|
|
|
* @param PartitionItem $item |
136
|
|
|
* The item to delete. |
137
|
|
|
*/ |
138
|
3 |
|
public function delete(PartitionItem $item) |
139
|
|
|
{ |
140
|
3 |
|
$this->remove(spl_object_hash($item)); |
141
|
3 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sort the items of the partition in a particular order. |
145
|
|
|
* |
146
|
|
|
* @param string $order |
147
|
|
|
* ASC for ascending, DESC for descending. |
148
|
|
|
* |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
11 |
|
public function sortByValue($order = 'ASC') |
152
|
|
|
{ |
153
|
11 |
|
if ('ASC' == $order) { |
154
|
|
|
$this->sort(function ($itemA, $itemB) { |
155
|
8 |
|
return $itemA->getValue() > $itemB->getValue(); |
156
|
8 |
|
}); |
157
|
8 |
|
} |
158
|
|
|
|
159
|
11 |
|
if ('DESC' == $order) { |
160
|
3 |
|
$this->sort(function ($itemA, $itemB) { |
161
|
3 |
|
return $itemA->getValue() < $itemB->getValue(); |
162
|
3 |
|
}); |
163
|
3 |
|
} |
164
|
|
|
|
165
|
11 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|