Skip to content

Commit

Permalink
Seed
Browse files Browse the repository at this point in the history
  • Loading branch information
dakujem committed Jan 15, 2024
1 parent a8a0588 commit d0ab792
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/Iterator/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Dakujem\Oliva\Iterator;

use CallbackFilterIterator;
use Dakujem\Oliva\Seed;

/**
* Filter tree iterator.
Expand All @@ -24,7 +25,7 @@ public function __construct(
callable $accept,
) {
parent::__construct(
Data::iterator($input),
Seed::iterator($input),
$accept,
);
}
Expand Down
66 changes: 34 additions & 32 deletions src/Iterator/Data.php → src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,28 @@

declare(strict_types=1);

namespace Dakujem\Oliva\Iterator;
namespace Dakujem\Oliva;

use ArrayIterator;
use Dakujem\Oliva\DataNodeContract;
use Dakujem\Oliva\TreeNodeContract;
use Generator;
use Iterator;
use IteratorIterator;

/**
* Iterable data collection helper functions.
* A static tool to manage iterable data collections and iterators.
*
* Contains:
* - methods that produce or adapt iterable data and iterators
* - methods that produce filtering callables to be used with the Filter iterator
*
* @author Andrej Rypak <[email protected]>
*/
final class Data
final class Seed
{
/**
* Prepend `null` value at the beginning of the data collection.
* Use when missing root.
* Remember, the data accessor and the node factory must be aware that a null may be passed to them.
*/
public static function nullFirst(iterable $input): Generator
{
yield null;
yield from $input;
}

/**
* Create a merged iterable.
* Can be used to prepend or append data from multiple source collections.
* The data is not actually merged, but a generator is used.
* The data is not actually merged, but a generator is produced.
*/
public static function merged(iterable ...$input): Generator
{
Expand All @@ -42,13 +33,25 @@ public static function merged(iterable ...$input): Generator
}

/**
* Create a callable that omits the root node.
* To be used with `Filter` iterator as the predicate:
* @see Filter
* Returns the first element of an iterable collection.
*/
public static function omitRoot(): callable
public static function first(iterable $input): mixed
{
return fn(TreeNodeContract $node): bool => !$node->isRoot();
foreach ($input as $item) {
return $item;
}
return null;
}

/**
* Prepend `null` value at the beginning of the data collection.
* Use when missing root.
* Remember, the data accessor and the node factory must be aware that a null may be passed to them.
*/
public static function nullFirst(iterable $input): Generator
{
yield null;
yield from $input;
}

/**
Expand All @@ -62,22 +65,21 @@ public static function omitNull(): callable
}

/**
* Returns an iterator.
* Useful where en iterator is required.
* Create a filtering callable that omits the root node.
* To be used with `Filter` iterator as the predicate:
* @see Filter
*/
public static function iterator(iterable $input): Iterator
public static function omitRoot(): callable
{
return is_array($input) ? new ArrayIterator($input) : new IteratorIterator($input);
return fn(TreeNodeContract $node): bool => !$node->isRoot();
}

/**
* Returns the first element of an iterable collection.
* Accepts any iterable and returns an iterator.
* Useful where en iterator is required, but any iterable or array is provided.
*/
public static function first(iterable $input): mixed
public static function iterator(iterable $input): Iterator
{
foreach ($input as $item) {
return $item;
}
return null;
return is_array($input) ? new ArrayIterator($input) : new IteratorIterator($input);
}
}
8 changes: 4 additions & 4 deletions tests/mptree.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

use Dakujem\Oliva\Iterator\Data;
use Dakujem\Oliva\Iterator\Filter;
use Dakujem\Oliva\Iterator\PreOrderTraversal;
use Dakujem\Oliva\MaterializedPath\TreeBuilder;
use Dakujem\Oliva\Node;
use Dakujem\Oliva\Seed;
use Dakujem\Oliva\TreeNodeContract;
use Tester\Environment;

Expand Down Expand Up @@ -35,7 +35,7 @@ $data = [

$builder = new TreeBuilder();
$tree = $builder->buildTree(
input: Data::nullFirst($data),
input: Seed::nullFirst($data),
node: fn(?Item $item) => new Node($item),
vector: TreeBuilder::fixed(
3,
Expand All @@ -61,8 +61,8 @@ foreach ($it as $key => $node) {

//xdebug_break();

new Filter($it, Data::omitNull());
new Filter($it, Data::omitRoot());
new Filter($it, Seed::omitNull());
new Filter($it, Seed::omitRoot());

$item = $tree->root()?->data();

Expand Down
4 changes: 2 additions & 2 deletions tests/recursive.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Dakujem\Oliva\Iterator\Data;
use Dakujem\Oliva\Node;
use Dakujem\Oliva\Recursive\TreeBuilder;
use Dakujem\Oliva\Seed;
use Tester\Environment;

require_once __DIR__ . '/../vendor/autoload.php';
Expand Down Expand Up @@ -33,7 +33,7 @@ $data = [
$builder = new TreeBuilder();

$tree = $builder->build(
input: Data::nullFirst($data),
input: Seed::nullFirst($data),
node: fn(?Item $item) => new Node($item),
self: fn(?Item $item) => $item?->id,
parent: fn(?Item $item) => $item?->parent,
Expand Down
6 changes: 3 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# A refactor of dakujem/oliva-tree
# A ~~refactor~~ reimplementaion of dakujem/oliva-tree

... after 8+ years


- [x] filter
- [x] iterating over tree without a real data root
- [ ] recursive tree builder
- [ ] calculus
- [x] recursive tree builder
- [ ] MP tree calculus
- [ ] dox


Expand Down

0 comments on commit d0ab792

Please sign in to comment.