-
-
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.
- Loading branch information
Pol Dellaiera
authored and
Pol Dellaiera
committed
Apr 10, 2017
1 parent
84546e8
commit fa4893c
Showing
38 changed files
with
1,089 additions
and
858 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
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 |
---|---|---|
|
@@ -11,17 +11,19 @@ | |
* | ||
* @author Mark Wilson <[email protected]> | ||
*/ | ||
class Combinations extends CombinationsIterator { | ||
class Combinations extends CombinationsIterator | ||
{ | ||
|
||
/** | ||
* Alias of the get() method. | ||
* | ||
* @return \Generator | ||
* The prime generator. | ||
*/ | ||
public function generator() { | ||
return $this->get($this->getDataset(), $this->getLength()); | ||
} | ||
public function generator() | ||
{ | ||
return $this->get($this->getDataset(), $this->getLength()); | ||
} | ||
|
||
/** | ||
* The generator. | ||
|
@@ -35,38 +37,38 @@ public function generator() { | |
* @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; | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Convert the generator into an array. | ||
* | ||
* @return array | ||
* The elements. | ||
*/ | ||
public function toArray() { | ||
$data = array(); | ||
|
||
foreach ($this->generator() as $value) { | ||
$data[] = $value; | ||
} | ||
public function toArray() | ||
{ | ||
$data = array(); | ||
|
||
return $data; | ||
} | ||
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
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
Oops, something went wrong.