| @@ 33-43 (lines=11) @@ | ||
| 30 | * Compare elements in order to place them correctly in the heap while sifting up. |
|
| 31 | * @see \SplMinHeap::compare() |
|
| 32 | */ |
|
| 33 | public function compare($boxA, $boxB) |
|
| 34 | { |
|
| 35 | $choice = $boxA->getItems()->count() - $boxB->getItems()->count(); |
|
| 36 | if ($choice === 0) { |
|
| 37 | $choice = $boxB->getBox()->getInnerVolume() - $boxA->getBox()->getInnerVolume(); |
|
| 38 | } |
|
| 39 | if ($choice === 0) { |
|
| 40 | $choice = $boxA->getWeight() - $boxB->getWeight(); |
|
| 41 | } |
|
| 42 | return $choice; |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * Reversed version of compare |
|
| @@ 49-59 (lines=11) @@ | ||
| 46 | * Reversed version of compare |
|
| 47 | * @return int |
|
| 48 | */ |
|
| 49 | public function reverseCompare($boxA, $boxB) |
|
| 50 | { |
|
| 51 | $choice = $boxB->getItems()->count() - $boxA->getItems()->count(); |
|
| 52 | if ($choice === 0) { |
|
| 53 | $choice = $boxA->getBox()->getInnerVolume() - $boxB->getBox()->getInnerVolume(); |
|
| 54 | } |
|
| 55 | if ($choice === 0) { |
|
| 56 | $choice = $boxB->getWeight() - $boxA->getWeight(); |
|
| 57 | } |
|
| 58 | return $choice; |
|
| 59 | } |
|
| 60 | ||
| 61 | /** |
|
| 62 | * Calculate the average (mean) weight of the boxes |
|