Skip to content

Commit 30fcf77

Browse files
authored
Merge pull request #3 from nerou42/v1.2
v1.2
2 parents 8baa03c + a838d7e commit 30fcf77

5 files changed

Lines changed: 114 additions & 2 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ You can still iterate over it as if it was a normal array.
1414

1515
One typical use case would be to load a lot of datasets from a database at once. (There are reasons to prefer this over running multiple queries.) See *Usage* below for an example for this use case using this library.
1616

17+
In general, you might want to try the following PHP-builtin solutions first:
18+
19+
- [SplFixedArray](https://www.php.net/manual/class.splfixedarray.php): By being of fixed length, this saves some memory compared to traditional `array`s. It behaves pretty much like `array`s do. Although the savings are not that huge compared to other solutions, it is probably the easiest to adopt.
20+
- [Generator](https://www.php.net/manual/language.generators.php): Instead of returning an `array` all at once, this allows to return one item after the other. Thereby a "consumer" is able to process one item after the other accordingly. One can also terminate the "generation" of further items. One of the downsides is, that a Generator can only be iterated over once. The memory consumption can be as good as constant, but this approach is quite different and therefore rather hard to adopt and not appropriate in every scenario.
21+
22+
It should be noted, that a `LargeArrayBuffer` can be converted to both of these using `toFixedArray()` and `toGenerator()` respectively.
23+
1724
## Install
1825

1926
Note: This library requires PHP 8.0+!

src/ArrayBuffer.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,36 @@ public function toArray(): array {
112112
return $this->array;
113113
}
114114
}
115+
116+
/**
117+
* @psalm-return \SplFixedArray<E>
118+
*/
119+
public function toFixedArray(): \SplFixedArray {
120+
if($this->buffer->count() > 0){
121+
return $this->buffer->toFixedArray();
122+
} else {
123+
$res = new \SplFixedArray(count($this->array));
124+
foreach($this->array as $idx => $item){
125+
$res[$idx] = $item;
126+
}
127+
return $res;
128+
}
129+
}
130+
131+
/**
132+
* @return \Generator send something other than null to terminate
133+
* @psalm-return \Generator<int, E, mixed, void>
134+
*/
135+
public function toGenerator(): \Generator {
136+
if($this->buffer->count() > 0){
137+
yield from $this->buffer->toGenerator();
138+
} else {
139+
foreach($this->array as $item){
140+
$cmd = yield $item;
141+
if($cmd !== null){
142+
break;
143+
}
144+
}
145+
}
146+
}
115147
}

src/ArrayBufferInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,15 @@ public function push(mixed $item): void;
2020
* @psalm-return list<E>
2121
*/
2222
public function toArray(): array;
23+
24+
/**
25+
* @psalm-return \SplFixedArray<E>
26+
*/
27+
public function toFixedArray(): \SplFixedArray;
28+
29+
/**
30+
* @return \Generator send something other than null to terminate
31+
* @psalm-return \Generator<int, E, mixed, void>
32+
*/
33+
public function toGenerator(): \Generator;
2334
}

src/LargeArrayBuffer.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ public function toArray(): array {
252252
}
253253
return $res;
254254
}
255+
256+
/**
257+
* @psalm-return \SplFixedArray<E>
258+
*/
259+
public function toFixedArray(): \SplFixedArray {
260+
$res = new \SplFixedArray($this->count);
261+
foreach($this as $idx => $item){
262+
$res[$idx] = $item;
263+
}
264+
return $res;
265+
}
266+
267+
/**
268+
* @return \Generator send something other than null to terminate
269+
* @psalm-return \Generator<int, E, mixed, void>
270+
*/
271+
public function toGenerator(): \Generator {
272+
foreach($this as $item){
273+
$cmd = yield $item;
274+
if($cmd !== null){
275+
break;
276+
}
277+
}
278+
}
255279

256280
public function __destruct() {
257281
/**

test/LargeArrayBufferTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
use LargeArrayBuffer\LargeArrayBuffer;
77
use PHPUnit\Framework\TestCase;
8-
use PHPUnit\Framework\Attributes\DataProvider;
98

109
/**
1110
* @author Andreas Wahlen
@@ -77,7 +76,7 @@ public function testLoop(int $serializer, int $compression): void {
7776
$count = 1500;
7877
$buf = new LargeArrayBuffer(serializer: $serializer, compression: $compression);
7978
$objs = [];
80-
for($i=0;$i<$count;$i++){
79+
for($i = 0; $i < $count; $i++){
8180
$o = new \stdClass();
8281
$o->idx = $i;
8382
$objs[] = $o;
@@ -107,4 +106,43 @@ public function testToJSON(): void {
107106
fclose($stream);
108107
$this->assertEquals(json_encode([$o, $o], JSON_THROW_ON_ERROR), $json);
109108
}
109+
110+
public function provideItems(): array {
111+
return [
112+
[['hello world!', 'just another string']]
113+
];
114+
}
115+
116+
/**
117+
* @dataProvider provideItems
118+
*/
119+
public function testToArray(array $items): void {
120+
$buf = new LargeArrayBuffer();
121+
foreach($items as $item){
122+
$buf->push($item);
123+
}
124+
$this->assertSame($items, $buf->toArray());
125+
}
126+
127+
/**
128+
* @dataProvider provideItems
129+
*/
130+
public function testToFixedArray(array $items): void {
131+
$buf = new LargeArrayBuffer();
132+
foreach($items as $item){
133+
$buf->push($item);
134+
}
135+
$this->assertSame($items, $buf->toFixedArray()->toArray());
136+
}
137+
138+
/**
139+
* @dataProvider provideItems
140+
*/
141+
public function testToGenerator(array $items): void {
142+
$buf = new LargeArrayBuffer();
143+
foreach($items as $item){
144+
$buf->push($item);
145+
}
146+
$this->assertSame($items, iterator_to_array($buf->toGenerator()));
147+
}
110148
}

0 commit comments

Comments
 (0)