Skip to content

Commit

Permalink
Don't abandon a packing as soon as an item doesn't fit (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Mar 29, 2014
1 parent b6ee136 commit 007dd5f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,21 @@ public function doVolumePacking() {

//Find best box of iteration, and remove packed items from unpacked list
$bestBox = $packedBoxesIteration->top();
for ($i = 0; $i < $bestBox->getItems()->count(); $i++) {
$this->items->extract();
$bestBoxItems = $bestBox->getItems()->asArray();
$unpackedItems = $this->items->asArray();

foreach ($bestBoxItems as $bestBoxItem) {
foreach ($unpackedItems as $key => $unpackedItem) {
if ($bestBoxItem == $unpackedItem) {
unset($unpackedItems[$key]);
break;
}
}
}

$this->items = new ItemList;
foreach ($unpackedItems as $item) {
$this->items->insert($item);
}
$packedBoxes->insert($bestBox);

Expand Down Expand Up @@ -251,7 +264,8 @@ public function packBox(Box $aBox, ItemList $aItems) {
$itemToPack = $aItems->top();

if ($itemToPack->getDepth() > ($layerDepth ?: $remainingDepth) || $itemToPack->getWeight() > $remainingWeight) {
break;
$aItems->extract(); //ignore, move on
continue;
}

$this->logger->log(LogLevel::DEBUG, "evaluating item {$itemToPack->getDescription()}");
Expand Down

0 comments on commit 007dd5f

Please sign in to comment.