Skip to content

Commit

Permalink
Update the Combinations generator based on Mark Wilson's array-subset…
Browse files Browse the repository at this point in the history
…s repository.
  • Loading branch information
Pol Dellaiera authored and Pol Dellaiera committed Mar 29, 2017
1 parent cba6141 commit 31b6b60
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Generators/Combinations.php
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;
}

}
74 changes: 74 additions & 0 deletions tests/src/Generators/CombinationsTest.php
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,
],
],
];
}

}

0 comments on commit 31b6b60

Please sign in to comment.