| Total Complexity | 3 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php namespace Mbh\Collection\Traits; |
||
| 14 | trait SquaredCapacity |
||
| 15 | { |
||
| 16 | use Capacity; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Rounds an integer to the next power of two if not already a power of two. |
||
| 20 | * |
||
| 21 | * @param int $capacity |
||
| 22 | * |
||
| 23 | * @return int |
||
| 24 | */ |
||
| 25 | private function square(int $capacity): int |
||
| 26 | { |
||
| 27 | return pow(2, ceil(log($capacity, 2))); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Ensures that enough memory is allocated for a specified capacity. This |
||
| 32 | * potentially reduces the number of reallocations as the size increases. |
||
| 33 | * |
||
| 34 | * @param int $capacity The number of values for which capacity should be |
||
| 35 | * allocated. Capacity will stay the same if this value |
||
| 36 | * is less than or equal to the current capacity. |
||
| 37 | */ |
||
| 38 | public function allocate(int $capacity) |
||
| 39 | { |
||
| 40 | $this->capacity = max($this->square($capacity), $this->capacity); |
||
|
|
|||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Called when capacity should be increased to accommodate new values. |
||
| 45 | */ |
||
| 46 | protected function increaseCapacity() |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Gets the size of the array. |
||
| 53 | * |
||
| 54 | * @return int |
||
| 55 | */ |
||
| 56 | abstract protected function count(): int; |
||
| 57 | } |
||
| 58 |