Skip to content

Commit

Permalink
#4: Convert the Permutations object into a PHP Generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pol Dellaiera authored and Pol Dellaiera committed Mar 31, 2017
1 parent 7039ca1 commit 2ef9a2c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 408 deletions.
77 changes: 70 additions & 7 deletions src/Generators/Permutations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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());
}

}
119 changes: 0 additions & 119 deletions src/Permutations.php

This file was deleted.

Loading

0 comments on commit 2ef9a2c

Please sign in to comment.