Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Dec 29, 2022
1 parent 8dfa4a8 commit c2c3f87
Show file tree
Hide file tree
Showing 25 changed files with 244 additions and 682 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ foreach(xrange(1, 5, 2) as $i) {
#### Range

```php
use Smoren\Sequence\Structs\IntRange;
use Smoren\Sequence\Structs\Range;
use Smoren\Sequence\Structs\FloatRange;

/* Simple int range */
$range = new IntRange(1, 3, 2); // (from, size, step)
$range = new Range(1, 3, 2); // (from, size, step)
var_dump($range->isInfinite()); // false

foreach($range as $value) {
Expand Down Expand Up @@ -79,7 +79,7 @@ try {
}

/* Infinite int range */
$range = new IntRange(1, null, 2);
$range = new Range(1, null, 2);
var_dump($range->isInfinite()); // true

foreach($range as $i => $value) {
Expand All @@ -101,11 +101,11 @@ foreach($range as $value) {
#### Exponential

```php
use Smoren\Sequence\Structs\IntExponential;
use Smoren\Sequence\Structs\Exponential;
use Smoren\Sequence\Structs\FloatExponential;

/* Simple int exponential sequence */
$sequence = new IntExponential(1, 4, 2); // (from, size, step)
$sequence = new Exponential(1, 4, 2); // (from, size, step)
var_dump($sequence->isInfinite()); // false

foreach($sequence as $value) {
Expand Down Expand Up @@ -136,7 +136,7 @@ try {
}

/* Infinite int exponential sequence */
$sequence = new IntExponential(1, null, 2);
$sequence = new Exponential(1, null, 2);
var_dump($sequence->isInfinite()); // true

foreach($sequence as $i => $value) {
Expand Down
11 changes: 0 additions & 11 deletions src/Exceptions/BaseException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Exceptions/OutOfRangeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Smoren\Sequence\Exceptions;

class OutOfRangeException extends BaseException
class OutOfRangeException extends \OutOfRangeException
{
}
2 changes: 1 addition & 1 deletion src/Exceptions/ReadOnlyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace Smoren\Sequence\Exceptions;

class ReadOnlyException extends BaseException
class ReadOnlyException extends \Exception
{
}
6 changes: 3 additions & 3 deletions src/Interfaces/IndexedArrayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use IteratorAggregate;
use Smoren\Sequence\Exceptions\OutOfRangeException;
use Smoren\Sequence\Iterators\IndexedArrayIterator;
use Smoren\Sequence\Structs\IntRange;
use Smoren\Sequence\Structs\Range;

/**
* @template T
Expand All @@ -19,9 +19,9 @@
interface IndexedArrayInterface extends ArrayAccess, Countable, IteratorAggregate
{
/**
* @return IntRange
* @return Range<int>
*/
public function getRange(): IntRange;
public function getRange(): Range;

/**
* @return IndexedArrayIterator<T>
Expand Down
14 changes: 6 additions & 8 deletions src/Interfaces/SequenceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function isInfinite(): bool;

/**
* @param int $index
* @return mixed
* @return T
*/
public function getValueByIndex(int $index);

Expand All @@ -37,36 +37,34 @@ public function getStartValue();
* @param T $previousValue
* @return T
*/
//public function getNextValue($previousValue);
public function getNextValue($previousValue);

/**
* @param int|mixed $offset
* @param int $offset
* @return bool
*/
public function offsetExists($offset): bool;

/**
* @param int|mixed $offset
* @param int $offset
* @return T
* @throws OutOfRangeException
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset);

/**
* @param int|null|mixed $offset
* @param int|null $offset
* @param T $value
* @return void
* @throws ReadOnlyException
* @throws OutOfRangeException
*/
public function offsetSet($offset, $value): void;

/**
* @param int|mixed $offset
* @param int $offset
* @return void
* @throws ReadOnlyException
* @throws OutOfRangeException
*/
public function offsetUnset($offset): void;

Expand Down
47 changes: 0 additions & 47 deletions src/Iterators/FloatSequenceIterator.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/Iterators/IntSequenceIterator.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,55 @@

declare(strict_types=1);

namespace Smoren\Sequence\Traits;
namespace Smoren\Sequence\Iterators;

use Smoren\Sequence\Interfaces\SequenceInterface;
use Smoren\Sequence\Interfaces\SequenceIteratorInterface;

/**
* @template T
* @implements SequenceIteratorInterface<T>
* @property SequenceInterface<T> $sequence
* @property int $currentIndex
*/
trait SequenceIteratorTrait
class SequenceIterator implements SequenceIteratorInterface
{
/**
* @var SequenceInterface<T>
*/
protected SequenceInterface $sequence;
/**
* @var int
*/
protected int $currentIndex;
/**
* @var T
*/
protected $currentValue;

/**
* @param SequenceInterface<T> $sequence
*/
public function __construct(SequenceInterface $sequence)
{
$this->sequence = $sequence;
$this->currentIndex = 0;
}

/**
* {@inheritDoc}
* @return T
*/
public function current()
{
return $this->currentValue;
}

/**
* {@inheritDoc}
*/
public function next(): void
{
$this->currentIndex++;
$this->currentValue = $this->sequence->getNextValue($this->currentValue);
}

/**
Expand All @@ -46,5 +76,6 @@ public function valid(): bool
public function rewind(): void
{
$this->currentIndex = 0;
$this->currentValue = $this->sequence->getStartValue();
}
}
30 changes: 30 additions & 0 deletions src/Structs/Exponential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Smoren\Sequence\Structs;

/**
* @template T of int|float
* @extends StepSequence<T>
*/
class Exponential extends StepSequence
{
/**
* @param int $index
* @return T
*/
public function getValueByIndex(int $index)
{
return $this->start * ($this->step ** $index);
}

/**
* @param int $previousValue
* @return T
*/
public function getNextValue($previousValue)
{
return $previousValue * $this->step;
}
}
32 changes: 0 additions & 32 deletions src/Structs/FloatExponential.php

This file was deleted.

0 comments on commit c2c3f87

Please sign in to comment.