Passed
Push — 1.x-dev ( 8f914b...081a6e )
by Doug
04:31
created

ItemList::remove()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * List of items to be packed, ordered by volume.
12
 *
13
 * @author Doug Wright
14
 */
15
class ItemList extends \SplMaxHeap
16
{
17
    /**
18
     * Compare elements in order to place them correctly in the heap while sifting up.
19
     *
20
     * @see \SplMaxHeap::compare()
21
     *
22
     * @param mixed $itemA
23
     * @param mixed $itemB
24
     *
25
     * @return int
26
     */
27 22
    public function compare($itemA, $itemB)
28
    {
29 22
        if ($itemA->getVolume() > $itemB->getVolume()) {
30 11
            return 1;
31 22
        } elseif ($itemA->getVolume() < $itemB->getVolume()) {
32 10
            return -1;
33 20
        } elseif ($itemA->getWeight() !== $itemB->getWeight()) {
34 3
            return $itemA->getWeight() - $itemB->getWeight();
35 20
        } elseif ($itemA->getDescription() < $itemB->getDescription()) {
36 4
            return 1;
37
        } else {
38 18
            return -1;
39
        }
40
    }
41
42
    /**
43
     * Get copy of this list as a standard PHP array.
44
     *
45
     * @return Item[]
46
     */
47 5
    public function asArray()
48
    {
49 5
        $return = [];
50 5
        foreach (clone $this as $item) {
51 5
            $return[] = $item;
52
        }
53
54 5
        return $return;
55
    }
56
57
    /**
58
     * @internal
59
     *
60
     * @param  int      $n
61
     * @return ItemList
62
     */
63 2
    public function topN($n)
64
    {
65 2
        $workingList = clone $this;
66 2
        $topNList = new self();
67 2
        $i = 0;
68 2
        while(!$workingList->isEmpty() && $i < $n) {
69 2
            $topNList->insert($workingList->extract());
70 2
            $i++;
71
        }
72
73 2
        return $topNList;
74
    }
75
76
    /**
77
     * Remove item from list.
78
     *
79
     * @param Item $item
80
     */
81 2
    public function remove(Item $item)
82
    {
83 2
        $workingSet = [];
84 2
        while (!$this->isEmpty()) {
85 2
            $workingSet[] = $this->extract();
86
        }
87
88 2
        $removed = false; // there can be multiple identical items, ensure that only 1 is removed
89 2
        foreach ($workingSet as $workingSetItem) {
90 2
            if (!$removed && $workingSetItem === $item) {
91 2
                $removed = true;
92
            } else {
93 2
                $this->insert($workingSetItem);
94
            }
95
        }
96
97 2
    }
98
}
99