Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modernize #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor/
.DS_Store
composer.lock
phpunit.xml
.phpunit.result.cache
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ without affecting the memory footprint.
Via Composer

``` bash
$ composer require th3n3rd/cartesian-product
$ composer require "th3n3rd/cartesian-product"
```

## Usage
Expand All @@ -28,18 +28,18 @@ use Nerd\CartesianProduct\CartesianProduct;
$cartesianProduct = new CartesianProduct();

$cartesianProduct
->appendSet(array('a', 'b', 'c'))
->appendSet(array('d', 'e'))
->appendSet(array('f', 'g', 'h'))
->appendSet(array('i', 'j'))
->appendSet(array('k', 'l'))
->appendSet(array('m', 'n'))
->appendSet(array('o'))
->appendSet(array('p'))
->appendSet(array('q', 'r', 's', 't'))
->appendSet(array('u', 'v', 'w'))
->appendSet(array('x', 'y'))
->appendSet(array('z'))
->appendSet(['a', 'b', 'c'])
->appendSet(['d', 'e'])
->appendSet(['f', 'g', 'h'])
->appendSet(['i', 'j'])
->appendSet(['k', 'l'])
->appendSet(['m', 'n'])
->appendSet(['o'])
->appendSet(['p'])
->appendSet(['q', 'r', 's', 't'])
->appendSet(['u', 'v', 'w'])
->appendSet(['x', 'y'])
->appendSet(['z'])
;

foreach ($cartesianProduct as $index => $product) {
Expand All @@ -58,7 +58,7 @@ foreach ($result as $index => $product) {
## Testing

``` bash
$ phpunit
$ vendor/bin/phpunit
```

## License
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=8.0"
},
"require-dev": {
"devster/ubench": "~1.1",
"phpunit/phpunit": "~4.4"
"devster/ubench": "^2.1",
"phpunit/phpunit": "^9.6.17"
},
"autoload": {
"psr-4": {
Expand Down
26 changes: 13 additions & 13 deletions examples/iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
$cartesianProduct = new CartesianProduct();

$cartesianProduct
->appendSet(array('a', 'b', 'c'))
->appendSet(array('d', 'e'))
->appendSet(array('f', 'g', 'h'))
->appendSet(array('i', 'j'))
->appendSet(array('k', 'l'))
->appendSet(array('m', 'n'))
->appendSet(array('o'))
->appendSet(array('p'))
->appendSet(array('q', 'r', 's', 't'))
->appendSet(array('u', 'v', 'w'))
->appendSet(array('x', 'y'))
->appendSet(array('z'))
->appendSet(['a', 'b', 'c'])
->appendSet(['d', 'e'])
->appendSet(['f', 'g', 'h'])
->appendSet(['i', 'j'])
->appendSet(['k', 'l'])
->appendSet(['m', 'n'])
->appendSet(['o'])
->appendSet(['p'])
->appendSet(['q', 'r', 's', 't'])
->appendSet(['u', 'v', 'w'])
->appendSet(['x', 'y'])
->appendSet(['z'])
;

$bench->start();
Expand All @@ -38,4 +38,4 @@
$bench->end();

printf("Time elapsed: %s\n", $bench->getTime());
printf("Memory footprint: %s\n", $bench->getMemoryPeak());
printf("Memory footprint: %s\n", $bench->getMemoryPeak());
26 changes: 13 additions & 13 deletions examples/memory-usage-comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
$cartesianProduct = new CartesianProduct();

$cartesianProduct
->appendSet(array('a', 'b', 'c'))
->appendSet(array('d', 'e'))
->appendSet(array('f', 'g', 'h'))
->appendSet(array('i', 'j'))
->appendSet(array('k', 'l'))
->appendSet(array('m', 'n'))
->appendSet(array('o'))
->appendSet(array('p'))
->appendSet(array('q', 'r', 's', 't'))
->appendSet(array('u', 'v', 'w'))
->appendSet(array('x', 'y'))
->appendSet(array('z'))
->appendSet(['a', 'b', 'c'])
->appendSet(['d', 'e'])
->appendSet(['f', 'g', 'h'])
->appendSet(['i', 'j'])
->appendSet(['k', 'l'])
->appendSet(['m', 'n'])
->appendSet(['o'])
->appendSet(['p'])
->appendSet(['q', 'r', 's', 't'])
->appendSet(['u', 'v', 'w'])
->appendSet(['x', 'y'])
->appendSet(['z'])
;

$bench->start();
Expand All @@ -52,4 +52,4 @@
printf("Memory Usage Comparison: \n");
printf("Whole -> %s\n", $wholeUseCase);
printf("Iterator -> %s\n", $iteratorUseCase);
printf("Ratio: 1:%s (whole vs iterator) \n", ceil($iteratorUseCaseBytes / $wholeUseCaseBytes * 100));
printf("Ratio: 1:%s (whole vs iterator) \n", ceil($iteratorUseCaseBytes / $wholeUseCaseBytes * 100));
26 changes: 13 additions & 13 deletions examples/whole-result.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
$cartesianProduct = new CartesianProduct();

$cartesianProduct
->appendSet(array('a', 'b', 'c'))
->appendSet(array('d', 'e'))
->appendSet(array('f', 'g', 'h'))
->appendSet(array('i', 'j'))
->appendSet(array('k', 'l'))
->appendSet(array('m', 'n'))
->appendSet(array('o'))
->appendSet(array('p'))
->appendSet(array('q', 'r', 's', 't'))
->appendSet(array('u', 'v', 'w'))
->appendSet(array('x', 'y'))
->appendSet(array('z'))
->appendSet(['a', 'b', 'c'])
->appendSet(['d', 'e'])
->appendSet(['f', 'g', 'h'])
->appendSet(['i', 'j'])
->appendSet(['k', 'l'])
->appendSet(['m', 'n'])
->appendSet(['o'])
->appendSet(['p'])
->appendSet(['q', 'r', 's', 't'])
->appendSet(['u', 'v', 'w'])
->appendSet(['x', 'y'])
->appendSet(['z'])
;

$bench->start();
Expand All @@ -39,4 +39,4 @@
$bench->end();

printf("Time elapsed: %s\n", $bench->getTime());
printf("Memory footprint: %s\n", $bench->getMemoryPeak());
printf("Memory footprint: %s\n", $bench->getMemoryPeak());
17 changes: 0 additions & 17 deletions phpunit.xml.ci

This file was deleted.

28 changes: 17 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Cartesian Product Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Cartesian Product Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

</phpunit>
45 changes: 14 additions & 31 deletions src/CartesianProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,12 @@
*/
class CartesianProduct implements \Iterator
{
/**
* @var array
*/
private $sets = array();
private array $sets = [];
private \Iterator $referenceSet;

/**
* @var \Iterator
*/
private $referenceSet;
private int $cursor = 0;

/**
* @var integer
*/
private $cursor = 0;

/**
* @param array $sets
*/
public function __construct(array $sets = array())
public function __construct(array $sets = [])
{
foreach ($sets as $set) {
$this->addSet($set);
Expand All @@ -46,11 +33,9 @@ public function __construct(array $sets = array())
/**
* Adds a set.
*
* @param array|Traversable $set
*
* @throws \InvalidArgumentException
*/
private function addSet($set)
private function addSet(iterable $set): void
{
if (is_array($set)) {
$set = new \ArrayIterator($set);
Expand All @@ -66,13 +51,11 @@ private function addSet($set)
/**
* Appends the given set.
*
* @param array|Traversable $set
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function appendSet($set)
public function appendSet(iterable $set): static
{
$this->addSet($set);
$this->computeReferenceSet();
Expand All @@ -83,7 +66,7 @@ public function appendSet($set)
/**
* Computes the reference set used for iterate over the product.
*/
private function computeReferenceSet()
private function computeReferenceSet(): void
{
if (empty($this->sets)) {
return;
Expand All @@ -100,7 +83,7 @@ private function computeReferenceSet()
/**
* {@inheritdoc}
*/
public function current()
public function current(): mixed
{
$current = $this->referenceSet->current();

Expand All @@ -110,7 +93,7 @@ public function current()
/**
* {@inheritdoc}
*/
public function next()
public function next(): void
{
$this->cursor++;
$this->referenceSet->next();
Expand All @@ -119,23 +102,23 @@ public function next()
/**
* {@inheritdoc}
*/
public function key()
public function key(): mixed
{
return $this->cursor;
}

/**
* {@inheritdoc}
*/
public function valid()
public function valid(): bool
{
return $this->referenceSet->valid();
}

/**
* {@inheritdoc}
*/
public function rewind()
public function rewind(): void
{
$this->cursor = 0;
$this->referenceSet->rewind();
Expand All @@ -149,9 +132,9 @@ public function rewind()
* The recommended way to use the Cartesian Product is through its iterator interface
* which is memory efficient.
*/
public function compute()
public function compute(): array
{
$product = array();
$product = [];

$this->referenceSet->rewind();

Expand Down
Loading