1
|
|
|
<?php |
2
|
|
|
namespace Ds\Traits; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Common to structures that deal with an internal capacity. While none of the |
6
|
|
|
* PHP implementations actually make use of a capacity, it's important to keep |
7
|
|
|
* consistent with the extension. |
8
|
|
|
*/ |
9
|
|
|
trait Capacity |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var integer internal capacity |
13
|
|
|
*/ |
14
|
|
|
private $capacity = self::MIN_CAPACITY; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Returns the current capacity. |
18
|
|
|
* |
19
|
|
|
* @return int |
20
|
|
|
*/ |
21
|
|
|
public function capacity(): int |
22
|
|
|
{ |
23
|
|
|
return $this->capacity; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Ensures that enough memory is allocated for a specified capacity. This |
28
|
|
|
* potentially reduces the number of reallocations as the size increases. |
29
|
|
|
* |
30
|
|
|
* @param int $capacity The number of values for which capacity should be |
31
|
|
|
* allocated. Capacity will stay the same if this value |
32
|
|
|
* is less than or equal to the current capacity. |
33
|
|
|
*/ |
34
|
|
|
public function allocate(int $capacity) |
35
|
|
|
{ |
36
|
|
|
$this->capacity = max($capacity, $this->capacity); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return the structures growth factor. |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
protected function getGrowthFactor(): float |
43
|
|
|
{ |
44
|
|
|
return 2; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return float to multiply by when decreasing capacity. |
49
|
|
|
*/ |
50
|
|
|
protected function getDecayFactor(): float |
51
|
|
|
{ |
52
|
|
|
return 0.5; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return float the ratio between size and capacity when capacity should be |
57
|
|
|
* decreased. |
58
|
|
|
*/ |
59
|
|
|
protected function getTruncateThreshold(): float |
60
|
|
|
{ |
61
|
|
|
return 0.25; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks and adjusts capacity if required. |
66
|
|
|
*/ |
67
|
|
|
protected function checkCapacity() |
68
|
|
|
{ |
69
|
|
|
if ($this->shouldIncreaseCapacity()) { |
70
|
|
|
$this->increaseCapacity(); |
71
|
|
|
} else { |
72
|
|
|
if ($this->shouldDecreaseCapacity()) { |
73
|
|
|
$this->decreaseCapacity(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Called when capacity should be increased to accommodate new values. |
80
|
|
|
*/ |
81
|
|
|
protected function increaseCapacity() |
82
|
|
|
{ |
83
|
|
|
$this->capacity = max($this->count(), $this->capacity * $this->getGrowthFactor()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Called when capacity should be decrease if it drops below a threshold. |
88
|
|
|
*/ |
89
|
|
|
protected function decreaseCapacity() |
90
|
|
|
{ |
91
|
|
|
$this->capacity = max(self::MIN_CAPACITY, $this->capacity * $this->getDecayFactor()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return whether capacity should be increased. |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
protected function shouldDecreaseCapacity(): bool |
98
|
|
|
{ |
99
|
|
|
return count($this) <= $this->capacity * $this->getTruncateThreshold(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return whether capacity should be increased. |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
protected function shouldIncreaseCapacity(): bool |
106
|
|
|
{ |
107
|
|
|
return count($this) >= $this->capacity; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.