Skip to content

Commit

Permalink
Improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 17, 2018
1 parent f2e0875 commit 78731bb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion benchmarks/AbstractBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ abstract class AbstractBench
*/
public function getData()
{
return \range(1, 100);
return \range(1, 1000);
}
}
18 changes: 11 additions & 7 deletions spec/drupol/phptree/Node/NodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function it_can_remove()
$this
->remove($node2);

$this->children()[0]->shouldReturn($node1);

$this
->remove($node1);

$this->children()->shouldReturn([]);
$this
->count()
->shouldReturn(0);
}

public function it_can_get_the_size()
Expand Down Expand Up @@ -83,15 +83,19 @@ public function it_can_get_its_children()
{
$this
->children()
->shouldReturn([]);
->shouldBeAnInstanceOf(\Generator::class);

$this
->children()
->shouldYield(new \ArrayIterator([]));

$node = new Node();

$this
->add($node)
->add($node)
->children()
->shouldReturn([$node, $node]);
->shouldYield(new \ArrayIterator([$node, $node]));
}

public function it_can_get_the_last_children()
Expand Down Expand Up @@ -209,12 +213,12 @@ public function it_can_use_withChildren()
$this
->withChildren($child)
->children()
->shouldReturn([$child]);
->shouldYield(new \ArrayIterator([$child]));

$this
->withChildren()
->children()
->shouldReturn([]);
->shouldYield(new \ArrayIterator([]));
}

public function it_can_get_its_depth()
Expand Down
2 changes: 1 addition & 1 deletion spec/drupol/phptree/Node/ValueNodeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function it_is_initializable()
{
$this->shouldHaveType(ValueNode::class);

$this->children()->shouldReturn([]);
$this->children()->shouldYield(new \ArrayIterator([]));
}

public function it_can_be_set_with_a_value()
Expand Down
28 changes: 15 additions & 13 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function getParent(): ?NodeInterface
/**
* {@inheritdoc}
*/
public function children(): array
public function children(): \Traversable
{
return $this->storage['children'];
yield from new \ArrayIterator($this->storage['children']);
}

/**
Expand Down Expand Up @@ -109,32 +109,33 @@ public function getAncestors(): \Traversable
*/
public function isLeaf(): bool
{
return [] === $this->children();
return [] === $this->storage['children'];
}

/**
* {@inheritdoc}
*/
public function isRoot(): bool
{
return null === $this->getParent();
return null === $this->storage['parent'];
}

/**
* {@inheritdoc}
*/
public function getSibblings(): \Traversable
{
$parent = $this->getParent();
$parent = $this->storage['parent'];

if (null === $parent) {
return [];
return new \ArrayIterator([]);
}

foreach ($parent->children() as $child) {
if ($child === $this) {
continue;
}

yield $child;
}
}
Expand All @@ -152,13 +153,14 @@ public function degree(): int
*/
public function count(): int
{
return \array_reduce(
$this->children(),
function ($carry, $node) {
return 1 + $carry + $node->count();
},
0
);
$count = 0;

/** @var \drupol\phptree\Node\NodeInterface $child */
foreach ($this->children() as $child) {
$count += 1 + $child->count();
}

return $count;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions src/Node/NodeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ public function remove(NodeInterface ...$node): NodeInterface;
/**
* Get the children.
*
* @return \drupol\phptree\Node\NodeInterface[]
* @return \Traversable<\drupol\phptree\Node\NodeInterface>
* The children.
*/
public function children(): array;
public function children(): \Traversable;

/**
* Check if the node is a leaf.
* Check if the node has children, then it's a leaf.
*
* @return bool
* True if it's a leaf, false otherwise.
* True if it has children, false otherwise.
*/
public function isLeaf(): bool;

/**
* Check if the node is the root node.
* Check if the node is the root node (Node parent is null)
*
* @return bool
* True if it's a root node, false otherwise.
Expand All @@ -78,20 +78,20 @@ public function isRoot(): bool;
* Get the ancestors of a node.
*
* @return \Traversable
* The array of ancestors.
* The ancestors.
*/
public function getAncestors(): \Traversable;

/**
* Get the node's sibblings.
*
* @return \Traversable
* The array of sibblings.
* The sibblings.
*/
public function getSibblings(): \Traversable;

/**
* The number of children a node has.
* The amount of children a node has.
*
* @return int
* The amount of children.
Expand All @@ -107,7 +107,7 @@ public function degree(): int;
public function depth(): int;

/**
* Get a clone of the object without any children.
* Get a clone of the object with or without children.
*
* @param \drupol\phptree\Node\NodeInterface ...$nodes
* The children that the clone will have.
Expand Down

0 comments on commit 78731bb

Please sign in to comment.