-
-
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.
#4: Convert the Permutations object into a PHP Generator.
- Loading branch information
Pol Dellaiera
authored and
Pol Dellaiera
committed
Mar 31, 2017
1 parent
7039ca1
commit 2ef9a2c
Showing
3 changed files
with
70 additions
and
408 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 |
---|---|---|
|
@@ -2,16 +2,36 @@ | |
|
||
namespace drupol\phpermutations\Generators; | ||
|
||
use drupol\phpermutations\Permutations as PermutationsClass; | ||
use drupol\phpermutations\Combinatorics; | ||
|
||
/** | ||
* Class Permutations. | ||
* | ||
* @package drupol\phpermutations\Generators | ||
* | ||
* @author Mark Wilson <[email protected]> | ||
* Inspired by the work of Mark Wilson <[email protected]> | ||
*/ | ||
class Permutations extends PermutationsClass { | ||
class Permutations extends Combinatorics { | ||
|
||
/** | ||
* The combinations generator. | ||
* | ||
* @var \drupol\phpermutations\Generators\Combinations | ||
*/ | ||
public $combinations; | ||
|
||
/** | ||
* Combinatorics constructor. | ||
* | ||
* @param array $dataset | ||
* The dataset. | ||
* @param int|null $length | ||
* The length. | ||
*/ | ||
public function __construct(array $dataset = array(), $length = NULL) { | ||
parent::__construct($dataset, $length); | ||
$this->combinations = new Combinations($dataset, $this->getLength()); | ||
} | ||
|
||
/** | ||
* Alias of the get() method. | ||
|
@@ -20,11 +40,31 @@ class Permutations extends PermutationsClass { | |
* The prime generator. | ||
*/ | ||
public function generator() { | ||
return $this->get($this->getDataset()); | ||
return $this->get($this->getDataset(), $this->getLength()); | ||
} | ||
|
||
/** | ||
* The combination generator. | ||
* | ||
* @param array $dataset | ||
* The dataset. | ||
* @param int $length | ||
* The length. | ||
* | ||
* @codingStandardsIgnoreStart | ||
* @return \Generator | ||
* @codingStandardsIgnoreEnd | ||
*/ | ||
protected function get(array $dataset, $length) { | ||
foreach ($this->combinations->generator() as $combination) { | ||
foreach ($this->getPermutations($combination) as $current) { | ||
yield $current; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* The generator. | ||
* The permutations generator. | ||
* | ||
* @param array $dataset | ||
* The dataset. | ||
|
@@ -33,19 +73,42 @@ public function generator() { | |
* @return \Generator | ||
* @codingStandardsIgnoreEnd | ||
*/ | ||
protected function get(array $dataset) { | ||
protected function getPermutations(array $dataset) { | ||
foreach ($dataset as $key => $firstItem) { | ||
$remaining = $dataset; | ||
array_splice($remaining, $key, 1); | ||
if (count($remaining) === 0) { | ||
yield [$firstItem]; | ||
continue; | ||
} | ||
foreach ($this->get($remaining) as $permutation) { | ||
foreach ($this->getPermutations($remaining) as $permutation) { | ||
array_unshift($permutation, $firstItem); | ||
yield $permutation; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Convert the generator into an array. | ||
* | ||
* @return array | ||
* The elements. | ||
*/ | ||
public function toArray() { | ||
$data = array(); | ||
|
||
foreach ($this->generator() as $value) { | ||
$data[] = $value; | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() { | ||
return $this->combinations->count() * $this->fact($this->getLength()); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.