Completed
Push — data_structures ( b3e6cb )
by Doug
18:44
created

ItemList::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem)
4
 * @package BoxPacker
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
namespace DVDoug\BoxPacker;
9
10
use ArrayIterator, Countable, IteratorAggregate, Traversable;
11
12
/**
13
 * List of items to be packed, ordered by volume
14
 * @author Doug Wright
15
 * @package BoxPacker
16
 */
17
class ItemList implements Countable, IteratorAggregate
18
{
19
    /**
20
     * List containing items
21
     * @var Item[]
22
     */
23
    private $list = [];
24
25
    /**
26
     * Has this list already been sorted?
27
     * @var bool
28
     */
29
    private $isSorted = false;
30
31
    /**
32
     * @param Item $item
33
     */
34
    public function insert(Item $item)
35
    {
36
        $this->list[] = $item;
37
    }
38
39
    /**
40
     * @return Item
41
     */
42 View Code Duplication
    public function extract(): Item
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        if (!$this->isSorted) {
45
            usort($this->list, [$this, 'compare']);
46
            $this->isSorted = true;
47
        }
48
        return array_shift($this->list);
49
    }
50
51
    /**
52
     * @return Item
53
     */
54 View Code Duplication
    public function top(): Item
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if (!$this->isSorted) {
57
            usort($this->list, [$this, 'compare']);
58
            $this->isSorted = true;
59
        }
60
        $temp = $this->list;
61
        return reset($temp);
62
    }
63
64
    /**
65
     * @return Traversable
66
     */
67 View Code Duplication
    public function getIterator(): Traversable
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (!$this->isSorted) {
70
            usort($this->list, [$this, 'compare']);
71
            $this->isSorted = true;
72
        }
73
        return new ArrayIterator($this->list);
74
    }
75
76
    /**
77
     * Number of items in list
78
     * @return int
79
     */
80
    public function count(): int
81
    {
82
        return count($this->list);
83
    }
84
85
    /**
86
     * @param Item $itemA
87
     * @param Item $itemB
88
     *
89
     * @return int
90
     */
91
    private function compare(Item $itemA, Item $itemB): int
92
    {
93
        $itemAVolume = $itemA->getWidth() * $itemA->getLength() * $itemA->getDepth();
94
        $itemBVolume = $itemB->getWidth() * $itemB->getLength() * $itemB->getDepth();
95
        return ($itemBVolume <=> $itemAVolume) ?: ($itemB->getWeight() - $itemA->getWeight());
96
    }
97
98
}
99