Skip to content

Commit

Permalink
Move more InfalliblePacker-specific logic into it
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Jul 4, 2021
1 parent fe0a973 commit 7c08629
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 51 deletions.
37 changes: 27 additions & 10 deletions src/InfalliblePacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace DVDoug\BoxPacker;

use function count;

/**
* A version of the packer that swallows internal exceptions.
*
Expand Down Expand Up @@ -42,16 +44,7 @@ public function getUnpackedItems(): ItemList
*/
public function pack(): PackedBoxList
{
foreach ($this->items as $item) {
foreach ($this->boxes as $box) {
if ($item->getWeight() <= ($box->getMaxWeight() - $box->getEmptyWeight()) && (new OrientatedItemFactory($box))->hasPossibleOrientationsInEmptyBox($item)) {
continue 2;
}
}
$this->unpackedItems->insert($item);
$this->items->remove($item);
}

$this->sanityPrecheck();
while (true) {
try {
return parent::pack();
Expand All @@ -61,4 +54,28 @@ public function pack(): PackedBoxList
}
}
}

private function sanityPrecheck(): void
{
$cache = [];

foreach ($this->items as $item) {
$cacheKey = $item->getWidth() .
'|' .
$item->getLength() .
'|' .
$item->getDepth() .
'|' .
($item->getKeepFlat() ? '2D' : '3D');

foreach ($this->boxes as $box) {
if ($item->getWeight() <= ($box->getMaxWeight() - $box->getEmptyWeight()) && (isset($cache[$cacheKey]) || (count((new OrientatedItemFactory($box))->getPossibleOrientations($item, null, $box->getInnerWidth(), $box->getInnerLength(), $box->getInnerDepth(), 0, 0, 0, new PackedItemList())) > 0))) {
$cache[$cacheKey] = true;
continue 2;
}
}
$this->unpackedItems->insert($item);
$this->items->remove($item);
}
}
}
41 changes: 0 additions & 41 deletions src/OrientatedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ class OrientatedItemFactory implements LoggerAwareInterface
*/
protected $singlePassMode = false;

/**
* @var bool[]
*/
protected static $emptyBoxItemOrientationCache = [];

/**
* @var bool[]
*/
Expand Down Expand Up @@ -136,42 +131,6 @@ public function getPossibleOrientations(
return $orientations;
}

public function hasPossibleOrientationsInEmptyBox(Item $item): bool
{
$cacheKey = $item->getWidth() .
'|' .
$item->getLength() .
'|' .
$item->getDepth() .
'|' .
($item->getKeepFlat() ? '2D' : '3D') .
'|' .
$this->box->getInnerWidth() .
'|' .
$this->box->getInnerLength() .
'|' .
$this->box->getInnerDepth();

if (isset(static::$emptyBoxItemOrientationCache[$cacheKey])) {
return static::$emptyBoxItemOrientationCache[$cacheKey];
}

$orientations = $this->getPossibleOrientations(
$item,
null,
$this->box->getInnerWidth(),
$this->box->getInnerLength(),
$this->box->getInnerDepth(),
0,
0,
0,
new PackedItemList()
);
static::$emptyBoxItemOrientationCache[$cacheKey] = count($orientations) > 0;

return static::$emptyBoxItemOrientationCache[$cacheKey];
}

/**
* @param OrientatedItem[] $possibleOrientations
* @return OrientatedItem[]
Expand Down

0 comments on commit 7c08629

Please sign in to comment.