1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
4
|
|
|
* |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace DVDoug\BoxPacker; |
10
|
|
|
|
11
|
|
|
use ArrayIterator; |
12
|
|
|
use Countable; |
13
|
|
|
use IteratorAggregate; |
14
|
|
|
use Traversable; |
15
|
|
|
|
16
|
|
|
use function array_map; |
17
|
|
|
use function count; |
18
|
|
|
use function usort; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* List of packed items, ordered by volume. |
22
|
|
|
*/ |
23
|
|
|
class PackedItemList implements Countable, IteratorAggregate |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var PackedItem[] |
27
|
|
|
*/ |
28
|
|
|
private array $list = []; |
29
|
|
|
|
30
|
|
|
private int $weight = 0; |
31
|
|
|
|
32
|
|
|
private bool $isSorted = false; |
33
|
|
|
|
34
|
93 |
|
public function insert(PackedItem $item): void |
35
|
|
|
{ |
36
|
93 |
|
$this->list[] = $item; |
37
|
93 |
|
$this->weight += $item->getItem()->getWeight(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Traversable<PackedItem> |
42
|
|
|
*/ |
43
|
96 |
|
public function getIterator(): Traversable |
44
|
|
|
{ |
45
|
96 |
|
if (!$this->isSorted) { |
46
|
96 |
|
usort($this->list, $this->compare(...)); |
47
|
96 |
|
$this->isSorted = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
96 |
|
return new ArrayIterator($this->list); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Number of items in list. |
55
|
|
|
*/ |
56
|
96 |
|
public function count(): int |
57
|
|
|
{ |
58
|
96 |
|
return count($this->list); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get copy of this list as a standard PHP array. |
63
|
|
|
* |
64
|
|
|
* @internal |
65
|
|
|
* |
66
|
|
|
* @return Item[] |
67
|
|
|
*/ |
68
|
8 |
|
public function asItemArray(): array |
69
|
|
|
{ |
70
|
8 |
|
return array_map(fn (PackedItem $packedItem) => $packedItem->getItem(), $this->list); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get total volume of these items. |
75
|
|
|
*/ |
76
|
96 |
|
public function getVolume(): int |
77
|
|
|
{ |
78
|
96 |
|
$volume = 0; |
79
|
|
|
|
80
|
96 |
|
foreach ($this->list as $item) { |
81
|
93 |
|
$volume += $item->getVolume(); |
82
|
|
|
} |
83
|
|
|
|
84
|
96 |
|
return $volume; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get total weight of these items. |
89
|
|
|
*/ |
90
|
96 |
|
public function getWeight(): int |
91
|
|
|
{ |
92
|
96 |
|
return $this->weight; |
93
|
|
|
} |
94
|
|
|
|
95
|
81 |
|
private function compare(PackedItem $itemA, PackedItem $itemB): int |
|
|
|
|
96
|
|
|
{ |
97
|
81 |
|
$itemAVolume = $itemA->getItem()->getWidth() * $itemA->getItem()->getLength() * $itemA->getItem()->getDepth(); |
98
|
81 |
|
$itemBVolume = $itemB->getItem()->getWidth() * $itemB->getItem()->getLength() * $itemB->getItem()->getDepth(); |
99
|
|
|
|
100
|
81 |
|
return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getItem()->getWeight() <=> $itemA->getItem()->getWeight()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.