|
1
|
|
|
<?php namespace Mbh\Collection\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
|
|
|
return $this->setSize($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->allocate(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->allocate(max(self::MIN_CAPACITY, $this->capacity * $this->getDecayFactor())); |
|
|
|
|
|
|
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
|
|
|
/** |
|
113
|
|
|
* Change the size of an array to the new size of size. |
|
114
|
|
|
* If size is less than the current array size, any values after the |
|
115
|
|
|
* new size will be discarded. If size is greater than the current |
|
116
|
|
|
* array size, the array will be padded with NULL values. |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $size The new array size. This should be a value between 0 |
|
119
|
|
|
* and PHP_INT_MAX. |
|
120
|
|
|
* @return bool Returns TRUE on success or FALSE on failure. |
|
121
|
|
|
*/ |
|
122
|
|
|
abstract protected function setSize(int $size): bool; |
|
123
|
|
|
} |
|
124
|
|
|
|