2626namespace doganoo \PHPAlgorithms \Datastructure \Lists \ArrayLists ;
2727
2828
29+ use function array_diff ;
30+ use function array_fill ;
31+ use function array_filter ;
32+ use const ARRAY_FILTER_USE_BOTH ;
33+ use function array_slice ;
34+ use function array_values ;
35+ use ArrayIterator ;
36+ use function count ;
2937use doganoo \PHPAlgorithms \Algorithm \Sorting \TimSort ;
3038use doganoo \PHPAlgorithms \Common \Exception \IndexOutOfBoundsException ;
3139use doganoo \PHPAlgorithms \Common \Interfaces \IComparable ;
3240use doganoo \PHPAlgorithms \Common \Util \Comparator ;
41+ use function in_array ;
42+ use IteratorAggregate ;
43+ use JsonSerializable ;
3344use Traversable ;
3445
3546/**
4758 *
4859 * @package doganoo\PHPAlgorithms\Lists\ArrayLists
4960 */
50- class ArrayList implements \ IteratorAggregate, \ JsonSerializable, IComparable {
61+ class ArrayList implements IteratorAggregate, JsonSerializable, IComparable {
5162 /**
5263 * @const DEFAULT_ARRAY_SIZE
5364 */
@@ -83,7 +94,7 @@ private function ensureCapacity(int $newCapacity): bool {
8394 }
8495
8596 $ array = $ this ->array ;
86- $ this ->array = \ array_fill (0 , $ newCapacity , null );
97+ $ this ->array = array_fill (0 , $ newCapacity , null );
8798 for ($ i = 0 ; $ i < $ this ->size (); $ i ++) {
8899 $ this ->array [$ i ] = $ array [$ i ];
89100 }
@@ -125,10 +136,10 @@ public function isEmpty(): bool {
125136
126137 public function length (): int {
127138 $ array = $ this ->array ;
128- $ array = \ array_filter ($ array , function ($ value , $ key ) {
139+ $ array = array_filter ($ array , function ($ value , $ key ) {
129140 return $ value !== null ;
130- }, \ ARRAY_FILTER_USE_BOTH );
131- return \ count ($ array );
141+ }, ARRAY_FILTER_USE_BOTH );
142+ return count ($ array );
132143 }
133144
134145 /**
@@ -206,7 +217,7 @@ public function lastIndexOf($value) {
206217 $ array [] = $ key ;
207218 }
208219 }
209- return \ count ($ array ) === 0 ? null : $ array ;
220+ return count ($ array ) === 0 ? null : $ array ;
210221 }
211222
212223 /**
@@ -231,9 +242,9 @@ public function remove($key): bool {
231242 */
232243 public function containsKey (int $ key ): bool {
233244 $ array = $ this ->array ;
234- $ array = \ array_filter ($ array , function ($ value , $ key ) {
245+ $ array = array_filter ($ array , function ($ value , $ key ) {
235246 return $ value !== null ;
236- }, \ ARRAY_FILTER_USE_BOTH );
247+ }, ARRAY_FILTER_USE_BOTH );
237248 return array_key_exists ($ key , $ array );
238249 }
239250
@@ -260,7 +271,7 @@ public function removeRange(int $start, int $end): bool {
260271 $ removed &= $ this ->remove ($ key );
261272 }
262273 }
263- $ this ->array = \ array_values ($ this ->array );
274+ $ this ->array = array_values ($ this ->array );
264275 return $ removed ;
265276 }
266277
@@ -273,7 +284,7 @@ public function removeRange(int $start, int $end): bool {
273284 public function retainAll (ArrayList $ arrayList ): bool {
274285 $ newArray = [];
275286 foreach ($ arrayList as $ value ) {
276- if (\ in_array ($ value , $ this ->array )) {
287+ if (in_array ($ value , $ this ->array )) {
277288 $ newArray [] = $ value ;
278289 }
279290 }
@@ -311,7 +322,7 @@ public function subList(int $start, int $end): ArrayList {
311322 return $ arrayList ;
312323 }
313324 //TODO preserve keys?
314- $ array = \ array_slice ($ this ->array , $ start , $ end - $ start , true );
325+ $ array = array_slice ($ this ->array , $ start , $ end - $ start , true );
315326 $ arrayList ->addAllArray ($ array );
316327 return $ arrayList ;
317328 }
@@ -350,7 +361,7 @@ public function add($item): bool {
350361 * @return bool
351362 */
352363 public function addToIndex (int $ index , $ item ): bool {
353- if (\ count ($ this ->array ) === $ this ->size ()) {
364+ if (count ($ this ->array ) === $ this ->size ()) {
354365 $ this ->ensureCapacity ($ this ->size () * 2 + 1 );
355366 }
356367 $ this ->array [$ index ] = $ item ;
@@ -385,8 +396,8 @@ public function addAll(ArrayList $arrayList): bool {
385396 * @since 5.0.0
386397 */
387398 public function getIterator () {
388- $ array = \ array_slice ($ this ->array , 0 , $ this ->length (), true );
389- return new \ ArrayIterator ($ array );
399+ $ array = array_slice ($ this ->array , 0 , $ this ->length (), true );
400+ return new ArrayIterator ($ array );
390401 }
391402
392403 /**
@@ -400,7 +411,7 @@ public function getIterator() {
400411 public function jsonSerialize () {
401412 return [
402413 "default_capacity " => ArrayList::DEFAULT_CAPACITY
403- , "size " => \ count ($ this ->array )
414+ , "size " => count ($ this ->array )
404415 , "length " => $ this ->length ()
405416 , "content " => $ this ->array ,
406417 ];
@@ -435,9 +446,9 @@ public function inList($value): bool {
435446 */
436447 public function compareTo ($ object ): int {
437448 if ($ object instanceof ArrayList) {
438- if (\ count (\ array_diff ($ this ->array , $ object ->array )) === 0 ) return 0 ;
439- if (\ count ($ this ->array ) < \ count ($ object ->array )) return -1 ;
440- if (\ count ($ this ->array ) > \ count ($ object ->array )) return 1 ;
449+ if (count (array_diff ($ this ->array , $ object ->array )) === 0 ) return 0 ;
450+ if (count ($ this ->array ) < count ($ object ->array )) return -1 ;
451+ if (count ($ this ->array ) > count ($ object ->array )) return 1 ;
441452 }
442453 return -1 ;
443454 }
@@ -446,14 +457,14 @@ public function compareTo($object): int {
446457 * @return bool
447458 */
448459 public function sort (): bool {
449- $ array = \ array_filter ($ this ->array , function ($ value , $ key ) {
460+ $ array = array_filter ($ this ->array , function ($ value , $ key ) {
450461 return $ value !== null ;
451- }, \ ARRAY_FILTER_USE_BOTH );
462+ }, ARRAY_FILTER_USE_BOTH );
452463
453464
454465 $ timSort = new TimSort ();
455466 $ array = $ timSort ->sort ($ array );
456- $ this ->array = \ array_fill (0 , self ::DEFAULT_CAPACITY , null );
467+ $ this ->array = array_fill (0 , self ::DEFAULT_CAPACITY , null );
457468 $ this ->addAllArray ($ array );
458469 return true ;
459470 }
0 commit comments