1 | <?php namespace Mbh\Traits; |
||
2 | |||
3 | /** |
||
4 | * MBHFramework |
||
5 | * |
||
6 | * @link https://github.com/MBHFramework/mbh-framework |
||
7 | * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos |
||
8 | * @license https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License) |
||
9 | */ |
||
10 | |||
11 | trait Capacity |
||
12 | { |
||
13 | /** |
||
14 | * @var integer internal capacity |
||
15 | */ |
||
16 | protected $capacity = self::MIN_CAPACITY; |
||
17 | |||
18 | /** |
||
19 | * @inheritDoc |
||
20 | */ |
||
21 | public function capacity(): int |
||
22 | { |
||
23 | return $this->capacity; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @inheritDoc |
||
28 | */ |
||
29 | public function allocate(int $capacity) |
||
30 | { |
||
31 | $this->capacity = max($capacity, $this->capacity); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return float The structures growth factor. |
||
36 | */ |
||
37 | protected function getGrowthFactor(): float |
||
38 | { |
||
39 | return 2; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return float to multiply by when decreasing capacity. |
||
44 | */ |
||
45 | protected function getDecayFactor(): float |
||
46 | { |
||
47 | return 0.5; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return float The ratio between size and capacity when capacity should be |
||
52 | * decreased. |
||
53 | */ |
||
54 | protected function getTruncateThreshold(): float |
||
55 | { |
||
56 | return 0.25; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Checks and adjusts capacity if required. |
||
61 | */ |
||
62 | protected function checkCapacity() |
||
63 | { |
||
64 | if ($this->shouldIncreaseCapacity()) { |
||
65 | $this->increaseCapacity(); |
||
66 | } else { |
||
67 | if ($this->shouldDecreaseCapacity()) { |
||
68 | $this->decreaseCapacity(); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Called when capacity should be increased to accommodate new values. |
||
75 | */ |
||
76 | protected function increaseCapacity() |
||
77 | { |
||
78 | $this->capacity = max($this->count(), $this->capacity * $this->getGrowthFactor()); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Called when capacity should be decrease if it drops below a threshold. |
||
83 | */ |
||
84 | protected function decreaseCapacity() |
||
85 | { |
||
86 | $this->capacity = max(self::MIN_CAPACITY, $this->capacity * $this->getDecayFactor()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return bool whether capacity should be increased. |
||
91 | */ |
||
92 | protected function shouldDecreaseCapacity(): bool |
||
93 | { |
||
94 | return $this->count() <= $this->capacity * $this->getTruncateThreshold(); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return bool whether capacity should be increased. |
||
99 | */ |
||
100 | protected function shouldIncreaseCapacity(): bool |
||
101 | { |
||
102 | return $this->count() >= $this->capacity; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Gets the size of the array. |
||
107 | * |
||
108 | * @return int |
||
109 | */ |
||
110 | abstract protected function count(): int; |
||
111 | } |
||
112 |