<commit_before><?php
/**
 * Box packing (3D bin packing, knapsack problem)
 * @package BoxPacker
 * @author <NAME>
 */
  namespace DVDoug\BoxPacker;

  /**
   * Actual packer
   * @author <NAME>
   * @package BoxPacker
   */
  class Packer  {

    /**
     * List of items to be packed
     * @var ItemList
     */
    protected $items;

    /**
     * List of box sizes available to pack items into
     * @var Box[]
     */
    protected $boxes;

    public function __construct() {
      $this->items = new ItemList;
      $this->boxes = new BoxList;
    }

    /**
     * Add item to be packed
     * @param Item $aItem
     * @param int  $aQty
     */
    public function addItem(Item $aItem, $aQty = 1) {
      for ($i = 0; $i < $aQty; $i++) {
        $this->items->insert($aItem);
      }
    }

    /**
     * Add box size
     * @param Box $aBox
     */
    public function addBox(Box $aBox) {
      $this->boxes->insert($aBox);
    }

    /**
     * Pack items into boxes
     * @return PackedBox[]
     */
    public function pack() {
      if (!sizeof($this->items)) {
        throw new \RuntimeException('Please specify at least 1 item to be packed');
      }

      if (!sizeof($this->boxes)) {
        throw new \RuntimeException('Please specify at least 1 size of box to pack items into');
      }

      $packedBoxes = [];
      $unpackedItems = $this->items;
      $boxesToEvaluate = clone $this->boxes;

      while (!$boxesToEvaluate->isEmpty()) {
        $packedItems = $this->packBox($boxesToEvaluate->top(), $unpackedItems);
        if ($packedItems->count() == 0) {
          $boxesToEvaluate->extract();
          continue;
        }
        $packedBoxes[] = new PackedBox($boxesToEvaluate->extract(), $packedItems);

        /*
         * Have we found a single box that contains everything?
         */
        if ($unpackedItems->count() == 0) {
          return $packedBoxes;
        }
      }
      throw new \RuntimeException('Could not find a single box large enough');
    }


    /**
     * Pack as many items as possible into specific given box
     * XXX for now, simply stacks items on top of each other, no side-by-side evaluation performed yet
     * @param Box      $aBox
     * @param ItemList $aItems
     * @return ItemList items packed into box
     */
    public function packBox(Box $aBox, ItemList $aItems) {

      $packedItems = new ItemList;
      $remainingDepth = $aBox->getInnerDepth();
      $remainingWeight = $aBox->getMaxWeight() - $aBox->getEmptyWeight();
      while(!$aItems->isEmpty() && $aItems->top()->getDepth() <= $remainingDepth && $aItems->top()->getWeight() <= $remainingWeight) {
        $itemD1 = $aItems->top()->getWidth();
        $itemD2 = $aItems->top()->getLength();
        $boxD1 = $aBox->getInnerWidth();
        $boxD2 = $aBox->getInnerLength();

        if (($itemD1 <= $boxD1 && $itemD2 <= $boxD2) || ($itemD2 <= $boxD1 && $itemD1 <= $boxD2)) { //check 2D rotation
          $itemToPack = $aItems->extract();
          $remainingDepth -= $itemToPack->getDepth();
          $remainingWeight -= $itemToPack->getWeight();
          $packedItems->insert($itemToPack);
        }
        else {
          break;
        }
      }
      return $packedItems;
    }

  }
<commit_msg>Skip empty packings<commit_after><?php
/**
 * Box packing (3D bin packing, knapsack problem)
 * @package BoxPacker
 * @author <NAME>
 */
  namespace DVDoug\BoxPacker;

  /**
   * Actual packer
   * @author <NAME>
   * @package BoxPacker
   */
  class Packer  {

    /**
     * List of items to be packed
     * @var ItemList
     */
    protected $items;

    /**
     * List of box sizes available to pack items into
     * @var Box[]
     */
    protected $boxes;

    public function __construct() {
      $this->items = new ItemList;
      $this->boxes = new BoxList;
    }

    /**
     * Add item to be packed
     * @param Item $aItem
     * @param int  $aQty
     */
    public function addItem(Item $aItem, $aQty = 1) {
      for ($i = 0; $i < $aQty; $i++) {
        $this->items->insert($aItem);
      }
    }

    /**
     * Add box size
     * @param Box $aBox
     */
    public function addBox(Box $aBox) {
      $this->boxes->insert($aBox);
    }

    /**
     * Pack items into boxes
     * @return PackedBox[]
     */
    public function pack() {
      if (!sizeof($this->items)) {
        throw new \RuntimeException('Please specify at least 1 item to be packed');
      }

      if (!sizeof($this->boxes)) {
        throw new \RuntimeException('Please specify at least 1 size of box to pack items into');
      }

      $packedBoxes = [];
      $unpackedItems = $this->items;
      $boxesToEvaluate = clone $this->boxes;

      while (!$boxesToEvaluate->isEmpty()) {
        $box = $boxesToEvaluate->extract();
        $packedItems = $this->packBox($box, $unpackedItems);
        if ($packedItems->count()) {
          $packedBoxes[] = new PackedBox($box, $packedItems);
          /*
           * Have we found a single box that contains everything?
           */
          if ($unpackedItems->count() == 0) {
            return $packedBoxes;
          }
        }
      }
      throw new \RuntimeException('Could not find a single box large enough');
    }


    /**
     * Pack as many items as possible into specific given box
     * XXX for now, simply stacks items on top of each other, no side-by-side evaluation performed yet
     * @param Box      $aBox
     * @param ItemList $aItems
     * @return ItemList items packed into box
     */
    public function packBox(Box $aBox, ItemList $aItems) {

      $packedItems = new ItemList;
      $remainingDepth = $aBox->getInnerDepth();
      $remainingWeight = $aBox->getMaxWeight() - $aBox->getEmptyWeight();
      while(!$aItems->isEmpty() && $aItems->top()->getDepth() <= $remainingDepth && $aItems->top()->getWeight() <= $remainingWeight) {
        $itemD1 = $aItems->top()->getWidth();
        $itemD2 = $aItems->top()->getLength();
        $boxD1 = $aBox->getInnerWidth();
        $boxD2 = $aBox->getInnerLength();

        if (($itemD1 <= $boxD1 && $itemD2 <= $boxD2) || ($itemD2 <= $boxD1 && $itemD1 <= $boxD2)) { //check 2D rotation
          $itemToPack = $aItems->extract();
          $remainingDepth -= $itemToPack->getDepth();
          $remainingWeight -= $itemToPack->getWeight();
          $packedItems->insert($itemToPack);
        }
        else {
          break;
        }
      }
      return $packedItems;
    }

  }

{'fraction_non_alphanumeric': 0.14767280037111488, 'fraction_numerical': 0.006030616978506263, 'mean_word_length': 2.1505114466634194, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5118611', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1969, 'n_words': 519}