This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from robations/feature/chunk-without-group
Implement chunking without use of group iterator
- Loading branch information
Showing
7 changed files
with
250 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: php | ||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
install: composer install | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Fusonic-linq. | ||
* https://github.com/fusonic/fusonic-linq | ||
* | ||
* (c) Fusonic GmbH | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Fusonic\Linq\Iterator; | ||
|
||
use Fusonic\Linq\Linq; | ||
use Iterator; | ||
|
||
/** | ||
* Iterates over an iterator, returning Linq objects of the given chunk size. | ||
*/ | ||
class ChunkIterator implements Iterator | ||
{ | ||
/** | ||
* @var Iterator | ||
*/ | ||
private $iterator; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $chunk; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $i = 0; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $chunkSize; | ||
|
||
public function __construct(Iterator $iterator, $chunkSize) | ||
{ | ||
$this->iterator = $iterator; | ||
$this->chunkSize = $chunkSize; | ||
} | ||
|
||
/** | ||
* @return Linq | ||
*/ | ||
public function current() | ||
{ | ||
return new Linq($this->chunk); | ||
} | ||
|
||
public function next() | ||
{ | ||
$this->iterator->next(); | ||
$this->chunk = $this->getNextChunk(); | ||
$this->i++; | ||
} | ||
|
||
private function getNextChunk() | ||
{ | ||
$chunk = []; | ||
while ($this->iterator->valid()) { | ||
$chunk[] = $this->iterator->current(); | ||
|
||
if (count($chunk) < $this->chunkSize) { | ||
$this->iterator->next(); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return $chunk; | ||
} | ||
|
||
public function key() | ||
{ | ||
return $this->i; | ||
} | ||
|
||
public function valid() | ||
{ | ||
return !empty($this->chunk); | ||
} | ||
|
||
public function rewind() | ||
{ | ||
$this->iterator->rewind(); | ||
$this->i = 0; | ||
$this->chunk = $this->getNextChunk(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Fusonic\Linq\Test; | ||
|
||
use Fusonic\Linq\Iterator\ChunkIterator; | ||
use Fusonic\Linq\Linq; | ||
|
||
class ChunkIteratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testEmptyIterator() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([]), 1); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(false, $x->valid()); | ||
} | ||
|
||
public function testChunkSizeLargerThanIterator() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1]), 100); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals([0, 1], $x->current()->toArray()); | ||
} | ||
|
||
public function testChunkSizeSmallerThanIterator() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2]), 2); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals([0, 1], $x->current()->toArray()); | ||
|
||
$x->next(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals([2], $x->current()->toArray()); | ||
|
||
$x->next(); | ||
$this->assertEquals(false, $x->valid()); | ||
} | ||
|
||
public function testCharElementsScenario() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator(["a", "b", "c", "d", "e"]), 2); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals(["a", "b"], $x->current()->toArray()); | ||
|
||
$x->next(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals(["c", "d"], $x->current()->toArray()); | ||
|
||
$x->next(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals(["e"], $x->current()->toArray()); | ||
|
||
$x->next(); | ||
$this->assertEquals(false, $x->valid()); | ||
} | ||
|
||
public function testDifferentChunkSize() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2, 3, 4, 5, 6, 7, 8]), 3); | ||
|
||
$arr = array_map( | ||
function (Linq $y) { | ||
return implode(",", $y->toArray()); | ||
}, | ||
iterator_to_array($x) | ||
); | ||
$this->assertEquals( | ||
["0,1,2", "3,4,5", "6,7,8"], | ||
$arr | ||
); | ||
} | ||
|
||
public function testIdempotency() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2]), 2); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals([0, 1], $x->current()->toArray()); | ||
$this->assertEquals([0, 1], $x->current()->toArray()); | ||
$x->next(); | ||
|
||
$this->assertEquals([2], $x->current()->toArray()); | ||
$this->assertEquals([2], $x->current()->toArray()); | ||
} | ||
|
||
public function testNext() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2]), 2); | ||
|
||
$x->rewind(); | ||
$x->next(); | ||
$this->assertEquals([2], $x->current()->toArray()); | ||
} | ||
|
||
public function testKey() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2]), 2); | ||
|
||
$x->rewind(); | ||
$this->assertEquals(0, $x->key()); | ||
$x->next(); | ||
$this->assertEquals(1, $x->key()); | ||
} | ||
|
||
public function testRewind() | ||
{ | ||
$x = new ChunkIterator(new \ArrayIterator([0, 1, 2]), 2); | ||
|
||
$x->rewind(); | ||
$x->next(); | ||
$x->rewind(); | ||
$this->assertEquals(true, $x->valid()); | ||
$this->assertEquals(0, $x->key()); | ||
$this->assertEquals([0, 1], $x->current()->toArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters