@@ -78,38 +78,55 @@ protected function checkCapacity()
7878 }
7979
8080 /**
81- * Called when capacity should be increased to accommodate new values.
81+ * @param int $total
8282 */
83- protected function increaseCapacity ( )
83+ protected function ensureCapacity ( int $ total )
8484 {
85- $ this ->capacity = max ($ this ->count (), $ this ->capacity * $ this ->getGrowthFactor ());
85+ if ($ total > $ this ->capacity ()) {
86+ $ this ->capacity = max ($ total , $ this ->nextCapacity ());
87+ }
8688 }
8789
8890 /**
89- * Called when capacity should be decrease if it drops below a threshold .
91+ * @return bool whether capacity should be increased .
9092 */
91- protected function decreaseCapacity ()
93+ protected function shouldIncreaseCapacity (): bool
9294 {
93- $ this ->capacity = max (self ::MIN_CAPACITY , $ this ->capacity * $ this ->getDecayFactor ());
95+ return $ this ->count () >= $ this ->capacity ();
96+ }
97+
98+ protected function nextCapacity (): int
99+ {
100+ return $ this ->capacity () * $ this ->getGrowthFactor ();
94101 }
95102
96103 /**
97- * @return bool whether capacity should be increased.
104+ * Called when capacity should be increased to accommodate new values .
98105 */
99- protected function shouldDecreaseCapacity (): bool
106+ protected function increaseCapacity ()
100107 {
101- return count ($ this ) <= $ this ->capacity * $ this ->getTruncateThreshold ();
108+ $ this ->capacity = max (
109+ $ this ->count (),
110+ $ this ->nextCapacity ()
111+ );
102112 }
103113
104114 /**
105- * @return bool whether capacity should be increased .
115+ * Called when capacity should be decrease if it drops below a threshold .
106116 */
107- protected function shouldIncreaseCapacity (): bool
117+ protected function decreaseCapacity ()
108118 {
109- if ($ this instanceof Deque) {
110- return count ($ this ) > $ this ->capacity ;
111- }
119+ $ this ->capacity = max (
120+ self ::MIN_CAPACITY ,
121+ $ this ->capacity () * $ this ->getDecayFactor ()
122+ );
123+ }
112124
113- return count ($ this ) >= $ this ->capacity ;
125+ /**
126+ * @return bool whether capacity should be increased.
127+ */
128+ protected function shouldDecreaseCapacity (): bool
129+ {
130+ return count ($ this ) <= $ this ->capacity () * $ this ->getTruncateThreshold ();
114131 }
115132}
0 commit comments