From bc0d5e20e874802f1fe73eeb2ab50d116ac30cee Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Mon, 12 Nov 2018 23:17:50 +0000 Subject: [PATCH] Cap lookahead recursion --- src/ItemList.php | 20 ++++++++++++++++++++ src/OrientatedItemFactory.php | 2 +- tests/data/expected.csv | 12 ++++++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/ItemList.php b/src/ItemList.php index c2a99e1d..a0047eca 100644 --- a/src/ItemList.php +++ b/src/ItemList.php @@ -14,6 +14,7 @@ use Traversable; use function array_pop; use function array_reverse; +use function array_slice; use function count; use function end; use function usort; @@ -107,6 +108,25 @@ public function top(): Item return end($temp); } + /** + * @internal + * + * @param int $n + * @return ItemList + */ + public function topN(int $n): self + { + if (!$this->isSorted) { + usort($this->list, [$this, 'compare']); + $this->isSorted = true; + } + + $topNList = new self(); + $topNList->insertFromArray(array_slice($this->list, 0, $n)); + + return $topNList; + } + /** * @return Traversable */ diff --git a/src/OrientatedItemFactory.php b/src/OrientatedItemFactory.php index c2d12a55..d296dde6 100644 --- a/src/OrientatedItemFactory.php +++ b/src/OrientatedItemFactory.php @@ -294,7 +294,7 @@ protected function calculateAdditionalItemsPackedWithThisOrientation( $packedCount = 0; // first try packing into current row - $currentRowWorkingSetItems = clone $nextItems; + $currentRowWorkingSetItems = $nextItems->topN(8); // cap lookahead as this gets recursive and slow $nextRowWorkingSetItems = new ItemList(); $widthLeft = $originalWidthLeft - $prevItem->getWidth(); $lengthLeft = $originalLengthLeft; diff --git a/tests/data/expected.csv b/tests/data/expected.csv index ce65109c..64dddc9f 100644 --- a/tests/data/expected.csv +++ b/tests/data/expected.csv @@ -393,7 +393,7 @@ 1672e9897bfb16f7f644365da8d6992c,1,0,26.4,1,0,26.4 167db38fa514a30e70f167f70d19f214,1,0,16.2,1,0,16.2 168065302d10d2f4b683e3d5e7475594,2,536556.3,32.7,1,0,19.8 -1680b70ba864c313d28c9386ce54ac25,3,12592978.7,44,3,203349.6,49.5 +1680b70ba864c313d28c9386ce54ac25,3,12592978.7,44,3,64070.2,38.5 1694662b95ac32661ee2b45826bb54f6,1,0,42.4,1,0,42.4 16a2283358cddda00bf2e47e5ad746d3,1,0,24,1,0,24 16a59c72d05be053c19e4b32ceafaef0,1,0,29.2,1,0,29.2 @@ -868,7 +868,7 @@ 3223ff53874ff3768881db6e163c96a3,2,99856,31.5,1,0,19.1 323d3a61ac8f60761edc56c34b339a1d,1,0,40.8,1,0,40.8 3242e661790ad530e5a8419a5c7c0b5b,1,0,22.8,1,0,22.8 -3247e39c4bfd94e9f525f7ef5c179317,2,366025,36.9,2,3721,29.2 +3247e39c4bfd94e9f525f7ef5c179317,2,366025,36.9,2,1,21.2 326b6af0e5d3ac4f72622ce744ee4b27,1,0,32.4,1,0,32.4 3276e0aa9afede0f2e5b0d5e3dcf7426,2,14884,23.9,1,0,7.2 328f28aadcd871aa3ade0646fda55b85,1,0,25.2,1,0,25.2 @@ -1466,7 +1466,7 @@ 547b3c46daded352fbf63c857d87921c,6,8937399.5,61.4,2,167690.3,50.8 548c715873b6223fe988a0f94c932b05,1,0,57.2,1,0,57.2 549e072fe0adbf86a9cdf96456702e6b,2,58806.3,26.5,1,0,8 -54aa5358f035a72d895437e03cf3393c,1,0,51.2,1,0,51.2 +54aa5358f035a72d895437e03cf3393c,2,121,35.2,1,0,51.2 54d00e9d39e00973e74abb7d463509df,2,3782.3,43.7,1,0,13.1 54d7241c191e5c5d30f55f86f3c2a1e2,2,25600,37.6,1,0,11.3 54ebd8d18b8e5eed18b5f0e263dc6449,2,9,36.5,1,0,10.9 @@ -2076,7 +2076,7 @@ 77ee435e81a6ed974d2d23d5ad92d758,1,0,43.8,1,0,43.8 77f36d03b8b0972966f784ede18df036,1,0,31.8,1,0,31.8 7803029edad1118c42361b90838021ff,1,0,15.5,1,0,15.5 -7808268fc06dc358047e06d04131dae6,2,121,40,2,121,40 +7808268fc06dc358047e06d04131dae6,2,34225,55,2,121,40 780e3fcb605b33a5293dc71d295e4f38,1,0,34.7,1,0,34.7 7818ebbfce480f11d9c91b9fdfeae9d7,1,0,33.9,1,0,33.9 78202b56af66040afa4b19d23164168b,1,0,18.9,1,0,18.9 @@ -2227,7 +2227,7 @@ 808255961bd2cd69ada085757150b913,1,0,38,1,0,38 808b82b9297c4ab6bbd366c4e189962c,1,0,33.3,1,0,33.3 80ad76b20162a20c96fd28c1a0fd1554,1,0,57.8,1,0,57.8 -80b35862e48198d457d850237619a1be,1,0,73.1,1,0,33.3 +80b35862e48198d457d850237619a1be,1,0,33.3,1,0,33.3 80bfcbb8cabb6d518bd218a09f72d280,1,0,26.1,1,0,26.1 80d45c7ecee2bf6045d135f0b149a218,1,0,56.3,1,0,56.3 80f0a1007c7e471ed1bfff1373ba6b53,2,756.3,47.1,2,36290.3,12.3 @@ -3553,7 +3553,7 @@ d0efe6d5522001320a4372da93769920,1,0,33.1,1,0,33.1 d10e78eff303de831aff0a556a536a0f,1,0,48.7,1,0,48.7 d11076dc6cd4720a1fe9add55d2281aa,1,0,14.9,1,0,14.9 d1239d721e49347c9804bed0a6db56ab,1,0,39.3,1,0,39.3 -d12f9cb4e8c278ab3286ae03ea30313a,2,90000,23.3,1,0,7 +d12f9cb4e8c278ab3286ae03ea30313a,2,39204,11.5,1,0,7 d130d596db644f016ca8529c8abf46cc,1,0,37.8,1,0,37.8 d138dfe2bd5abdd66b951e505e6ce634,1,0,21.3,1,0,21.3 d144251f58433c0af02fa1a09ce64d30,1,0,74.6,1,0,74.6