Skip to content

Commit

Permalink
Do an upfront check that all items will fit into at least 1 box to av…
Browse files Browse the repository at this point in the history
…oid wasted time packing the others if we're going to throw anyway. Fixes #182
  • Loading branch information
dvdoug committed Dec 21, 2019
1 parent 16ff9bd commit 2dccfb6
Show file tree
Hide file tree
Showing 3 changed files with 479 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function remove(Item $item): void
do {
if (current($this->list) === $item) {
unset($this->list[key($this->list)]);

return;
}
} while (prev($this->list) !== false);
Expand Down
21 changes: 21 additions & 0 deletions src/Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public function doVolumePacking(): PackedBoxList
{
$packedBoxes = new PackedBoxList();

$this->sanityPrecheck();

//Keep going until everything packed
while ($this->items->count()) {
$packedBoxesIteration = [];
Expand Down Expand Up @@ -192,6 +194,25 @@ protected function findBestBoxFromIteration(array $packedBoxes): PackedBox
return array_shift($packedBoxes);
}

private function sanityPrecheck(): void
{
/** @var Item $item */
foreach ($this->items as $item) {
$possibleFits = 0;

/** @var Box $box */
foreach ($this->boxes as $box) {
if ($item->getWeight() <= ($box->getMaxWeight() - $box->getEmptyWeight())) {
$possibleFits += count((new OrientatedItemFactory($box))->getPossibleOrientationsInEmptyBox($item));
}
}

if ($possibleFits === 0) {
throw new ItemTooLargeException('Item ' . $item->getDescription() . ' is too large to fit into any box', $item);
}
}
}

private static function compare(PackedBox $boxA, PackedBox $boxB): int
{
$choice = $boxB->getItems()->count() <=> $boxA->getItems()->count();
Expand Down
Loading

0 comments on commit 2dccfb6

Please sign in to comment.