Passed
Push — 3.x ( e58d28...ec426a )
by Doug
01:41
created

ItemList::insert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use function array_key_last;
12
use function array_pop;
13
use function array_reverse;
14
use function array_slice;
15
use ArrayIterator;
16
use function count;
17
use Countable;
18
use function current;
19
use function end;
20
use IteratorAggregate;
21
use function key;
22
use const PHP_VERSION_ID;
23
use function prev;
24
use Traversable;
25
use function usort;
26
27
/**
28
 * List of items to be packed, ordered by volume.
29
 *
30
 * @author Doug Wright
31
 */
32
class ItemList implements Countable, IteratorAggregate
33
{
34
    /**
35
     * List containing items.
36
     *
37
     * @var Item[]
38
     */
39
    private $list = [];
40
41
    /**
42
     * Has this list already been sorted?
43
     *
44
     * @var bool
45
     */
46
    private $isSorted = false;
47
48
    /**
49
     * Does this list contain constrained items?
50
     *
51
     * @var bool
52
     */
53
    private $hasConstrainedItems;
54
55
    /**
56
     * Do a bulk create.
57
     *
58
     * @param  Item[]   $items
59
     * @return ItemList
60
     */
61 60
    public static function fromArray(array $items, bool $preSorted = false): self
62
    {
63 60
        $list = new static();
64 60
        $list->list = array_reverse($items); // internal sort is largest at the end
65 60
        $list->isSorted = $preSorted;
66
67 60
        return $list;
68
    }
69
70 86
    public function insert(Item $item, int $qty = 1): void
71
    {
72 86
        for ($i = 0; $i < $qty; ++$i) {
73 86
            $this->list[] = $item;
74
        }
75 86
        $this->isSorted = false;
76 86
        $this->hasConstrainedItems = $this->hasConstrainedItems || $item instanceof ConstrainedPlacementItem;
77 86
    }
78
79
    /**
80
     * Remove item from list.
81
     */
82
    public function remove(Item $item): void
83
    {
84
        if (!$this->isSorted) {
85
            usort($this->list, [$this, 'compare']);
86
            $this->isSorted = true;
87
        }
88
89
        end($this->list);
90
        do {
91
            if (current($this->list) === $item) {
92
                unset($this->list[key($this->list)]);
93
94
                return;
95
            }
96
        } while (prev($this->list) !== false);
97
    }
98
99 60
    public function removePackedItems(PackedItemList $packedItemList): void
100
    {
101 60
        foreach ($packedItemList as $packedItem) {
102 60
            end($this->list);
103
            do {
104 60
                if (current($this->list) === $packedItem->getItem()) {
105 60
                    unset($this->list[key($this->list)]);
106
107 60
                    break;
108
                }
109 12
            } while (prev($this->list) !== false);
110
        }
111 60
    }
112
113
    /**
114
     * @internal
115
     */
116 74
    public function extract(): Item
117
    {
118 74
        if (!$this->isSorted) {
119 34
            usort($this->list, [$this, 'compare']);
120 34
            $this->isSorted = true;
121
        }
122
123 74
        return array_pop($this->list);
124
    }
125
126
    /**
127
     * @internal
128
     */
129 52
    public function top(): Item
130
    {
131 52
        if (!$this->isSorted) {
132 2
            usort($this->list, [$this, 'compare']);
133 2
            $this->isSorted = true;
134
        }
135
136 52
        if (PHP_VERSION_ID < 70300) {
137
            return array_slice($this->list, -1, 1)[0];
138
        }
139
140 52
        return $this->list[array_key_last($this->list)];
141
    }
142
143
    /**
144
     * @internal
145
     * @return ItemList
146
     */
147 36
    public function topN(int $n): self
148
    {
149 36
        if (!$this->isSorted) {
150 2
            usort($this->list, [$this, 'compare']);
151 2
            $this->isSorted = true;
152
        }
153
154 36
        $topNList = new self();
155 36
        $topNList->list = array_slice($this->list, -$n, $n);
156 36
        $topNList->isSorted = true;
157
158 36
        return $topNList;
159
    }
160
161
    /**
162
     * @return Traversable|Item[]
163
     */
164 74
    public function getIterator(): Traversable
165
    {
166 74
        if (!$this->isSorted) {
167 46
            usort($this->list, [$this, 'compare']);
168 46
            $this->isSorted = true;
169
        }
170
171 74
        return new ArrayIterator(array_reverse($this->list));
172
    }
173
174
    /**
175
     * Number of items in list.
176
     */
177 78
    public function count(): int
178
    {
179 78
        return count($this->list);
180
    }
181
182
    /**
183
     * Does this list contain items with constrained placement criteria.
184
     */
185 70
    public function hasConstrainedItems(): bool
186
    {
187 70
        if (!isset($this->hasConstrainedItems)) {
188 34
            $this->hasConstrainedItems = false;
189 34
            foreach ($this->list as $item) {
190 34
                if ($item instanceof ConstrainedPlacementItem) {
191 2
                    $this->hasConstrainedItems = true;
192 2
                    break;
193
                }
194
            }
195
        }
196
197 70
        return $this->hasConstrainedItems;
198
    }
199
200 78
    private static function compare(Item $itemA, Item $itemB): int
201
    {
202 78
        $volumeDecider = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth() <=> $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
203 78
        if ($volumeDecider !== 0) {
204 38
            return $volumeDecider;
205
        }
206 68
        $weightDecider = $itemA->getWeight() - $itemB->getWeight();
207 68
        if ($weightDecider !== 0) {
208 2
            return $weightDecider;
209
        }
210
211 66
        return $itemB->getDescription() <=> $itemA->getDescription();
212
    }
213
}
214