Skip to content

Commit 1e0c0e4

Browse files
authored
Merge pull request #34 from samsonasik/faster-splfixedarray
Make faster search on Finder::last() on SplFixedArray data
2 parents 0ed57b8 + 1111ff8 commit 1e0c0e4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Finder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ArrayIterator;
88
use ArrayLookup\Assert\Filter;
99
use ArrayObject;
10+
use SplFixedArray;
1011
use Traversable;
1112
use Webmozart\Assert\Assert;
1213

@@ -51,6 +52,10 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a
5152
return $traversable->getArrayCopy();
5253
}
5354

55+
if ($traversable instanceof SplFixedArray) {
56+
return $traversable->toArray();
57+
}
58+
5459
return iterator_to_array($traversable);
5560
}
5661

tests/FinderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Iterator;
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\TestCase;
17+
use SplFixedArray;
1718
use stdClass;
1819

1920
use function current;
@@ -248,6 +249,16 @@ public static function lastReturnKeyDataProvider(): Iterator
248249
static fn(string $datum, int $key): bool => str_contains($datum, 'test') && $key === 1,
249250
null,
250251
];
252+
yield [
253+
SplFixedArray::fromArray([6, 7, 8, 9]),
254+
static fn($datum): bool => $datum > 5,
255+
3,
256+
];
257+
yield [
258+
SplFixedArray::fromArray([6, 7, 8, 9]),
259+
static fn($datum): bool => $datum < 5,
260+
null,
261+
];
251262
}
252263

253264
/**

0 commit comments

Comments
 (0)