-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the Combinations generator based on Mark Wilson's array-subset…
…s repository.
- Loading branch information
Pol Dellaiera
authored and
Pol Dellaiera
committed
Mar 29, 2017
1 parent
cba6141
commit 31b6b60
Showing
2 changed files
with
150 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace drupol\phpermutations\Generators; | ||
|
||
use drupol\phpermutations\Combinatorics; | ||
|
||
/** | ||
* Class Combinations. | ||
* | ||
* @package drupol\phpermutations\Generators | ||
* | ||
* @author Mark Wilson <[email protected]> | ||
*/ | ||
class Combinations extends Combinatorics { | ||
|
||
/** | ||
* Alias of the get() method. | ||
* | ||
* @return \Generator | ||
* The prime generator. | ||
*/ | ||
public function generator() { | ||
return $this->get($this->getDataset(), $this->getLength()); | ||
} | ||
|
||
/** | ||
* The generator. | ||
* | ||
* @param array $dataset | ||
* The dataset. | ||
* | ||
* @codingStandardsIgnoreStart | ||
* @return \Generator | ||
* @codingStandardsIgnoreEnd | ||
*/ | ||
protected function get(array $dataset, $length) { | ||
$originalLength = count($dataset); | ||
$remainingLength = $originalLength - $length + 1; | ||
for ($i = 0; $i < $remainingLength; $i++) { | ||
$current = $dataset[$i]; | ||
if ($length === 1) { | ||
yield [$current]; | ||
} else { | ||
$remaining = array_slice($dataset, $i + 1); | ||
foreach ($this->get($remaining, $length - 1) as $permutation) { | ||
array_unshift($permutation, $current); | ||
yield $permutation; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() { | ||
return $this->fact(count($this->getDataset())) / ($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength())); | ||
} | ||
|
||
/** | ||
* Convert the iterator into an array. | ||
* | ||
* @return array | ||
* The elements. | ||
*/ | ||
public function toArray() { | ||
$data = array(); | ||
|
||
foreach ($this->generator() as $value) { | ||
$data[] = $value; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
} |
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,74 @@ | ||
<?php | ||
|
||
namespace drupol\phpermutations\Tests\Generators; | ||
|
||
use drupol\phpermutations\Generators\Combinations; | ||
use drupol\phpermutations\Permutations; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Class PermutationsTest. | ||
* | ||
* @package drupol\phpermutations\Tests | ||
*/ | ||
class CombinationsTest extends TestCase { | ||
|
||
/** | ||
* The tests. | ||
* | ||
* @dataProvider simpleValueProvider | ||
*/ | ||
public function testCombinationsClass($input, $expected) { | ||
$combinations = new Combinations($input['dataset'], $input['length']); | ||
|
||
$this->assertEquals($input['dataset'], $combinations->getDataset()); | ||
$this->assertEquals($input['length'], $combinations->getLength()); | ||
$this->assertEquals($expected['dataset'], $combinations->toArray(), "\$canonicalize = true", $delta = 0.0, $maxDepth = 10, $canonicalize = TRUE); | ||
$this->assertEquals($expected['count'], $combinations->count()); | ||
} | ||
|
||
/** | ||
* The data provider. | ||
* | ||
* @return array | ||
* The test input values and their expected output. | ||
*/ | ||
public function simpleValueProvider() { | ||
return [ | ||
[ | ||
'input' => [ | ||
'dataset' => [1, 2, 3, 4, 5], | ||
'length' => 5, | ||
], | ||
'output' => [ | ||
'dataset' => [ | ||
[1, 2, 3, 4, 5], | ||
], | ||
'count' => 1, | ||
], | ||
], | ||
[ | ||
'input' => [ | ||
'dataset' => [1, 2, 3, 4, 5], | ||
'length' => 3, | ||
], | ||
'output' => [ | ||
'dataset' => [ | ||
[1, 2, 3], | ||
[1, 2, 4], | ||
[1, 2, 5], | ||
[1, 3, 4], | ||
[1, 3, 5], | ||
[1, 4, 5], | ||
[2, 3, 4], | ||
[2, 3, 5], | ||
[2, 4, 5], | ||
[3, 4, 5], | ||
], | ||
'count' => 10, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
} |