Skip to content

Commit 78731bb

Browse files
committed
Improve performance.
1 parent f2e0875 commit 78731bb

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

benchmarks/AbstractBench.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ abstract class AbstractBench
1111
*/
1212
public function getData()
1313
{
14-
return \range(1, 100);
14+
return \range(1, 1000);
1515
}
1616
}

spec/drupol/phptree/Node/NodeSpec.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public function it_can_remove()
3434
$this
3535
->remove($node2);
3636

37-
$this->children()[0]->shouldReturn($node1);
38-
3937
$this
4038
->remove($node1);
4139

42-
$this->children()->shouldReturn([]);
40+
$this
41+
->count()
42+
->shouldReturn(0);
4343
}
4444

4545
public function it_can_get_the_size()
@@ -83,15 +83,19 @@ public function it_can_get_its_children()
8383
{
8484
$this
8585
->children()
86-
->shouldReturn([]);
86+
->shouldBeAnInstanceOf(\Generator::class);
87+
88+
$this
89+
->children()
90+
->shouldYield(new \ArrayIterator([]));
8791

8892
$node = new Node();
8993

9094
$this
9195
->add($node)
9296
->add($node)
9397
->children()
94-
->shouldReturn([$node, $node]);
98+
->shouldYield(new \ArrayIterator([$node, $node]));
9599
}
96100

97101
public function it_can_get_the_last_children()
@@ -209,12 +213,12 @@ public function it_can_use_withChildren()
209213
$this
210214
->withChildren($child)
211215
->children()
212-
->shouldReturn([$child]);
216+
->shouldYield(new \ArrayIterator([$child]));
213217

214218
$this
215219
->withChildren()
216220
->children()
217-
->shouldReturn([]);
221+
->shouldYield(new \ArrayIterator([]));
218222
}
219223

220224
public function it_can_get_its_depth()

spec/drupol/phptree/Node/ValueNodeSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function it_is_initializable()
1313
{
1414
$this->shouldHaveType(ValueNode::class);
1515

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

1919
public function it_can_be_set_with_a_value()

src/Node/Node.php

+15-13
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public function getParent(): ?NodeInterface
7777
/**
7878
* {@inheritdoc}
7979
*/
80-
public function children(): array
80+
public function children(): \Traversable
8181
{
82-
return $this->storage['children'];
82+
yield from new \ArrayIterator($this->storage['children']);
8383
}
8484

8585
/**
@@ -109,32 +109,33 @@ public function getAncestors(): \Traversable
109109
*/
110110
public function isLeaf(): bool
111111
{
112-
return [] === $this->children();
112+
return [] === $this->storage['children'];
113113
}
114114

115115
/**
116116
* {@inheritdoc}
117117
*/
118118
public function isRoot(): bool
119119
{
120-
return null === $this->getParent();
120+
return null === $this->storage['parent'];
121121
}
122122

123123
/**
124124
* {@inheritdoc}
125125
*/
126126
public function getSibblings(): \Traversable
127127
{
128-
$parent = $this->getParent();
128+
$parent = $this->storage['parent'];
129129

130130
if (null === $parent) {
131-
return [];
131+
return new \ArrayIterator([]);
132132
}
133133

134134
foreach ($parent->children() as $child) {
135135
if ($child === $this) {
136136
continue;
137137
}
138+
138139
yield $child;
139140
}
140141
}
@@ -152,13 +153,14 @@ public function degree(): int
152153
*/
153154
public function count(): int
154155
{
155-
return \array_reduce(
156-
$this->children(),
157-
function ($carry, $node) {
158-
return 1 + $carry + $node->count();
159-
},
160-
0
161-
);
156+
$count = 0;
157+
158+
/** @var \drupol\phptree\Node\NodeInterface $child */
159+
foreach ($this->children() as $child) {
160+
$count += 1 + $child->count();
161+
}
162+
163+
return $count;
162164
}
163165

164166
/**

src/Node/NodeInterface.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ public function remove(NodeInterface ...$node): NodeInterface;
5353
/**
5454
* Get the children.
5555
*
56-
* @return \drupol\phptree\Node\NodeInterface[]
56+
* @return \Traversable<\drupol\phptree\Node\NodeInterface>
5757
* The children.
5858
*/
59-
public function children(): array;
59+
public function children(): \Traversable;
6060

6161
/**
62-
* Check if the node is a leaf.
62+
* Check if the node has children, then it's a leaf.
6363
*
6464
* @return bool
65-
* True if it's a leaf, false otherwise.
65+
* True if it has children, false otherwise.
6666
*/
6767
public function isLeaf(): bool;
6868

6969
/**
70-
* Check if the node is the root node.
70+
* Check if the node is the root node (Node parent is null)
7171
*
7272
* @return bool
7373
* True if it's a root node, false otherwise.
@@ -78,20 +78,20 @@ public function isRoot(): bool;
7878
* Get the ancestors of a node.
7979
*
8080
* @return \Traversable
81-
* The array of ancestors.
81+
* The ancestors.
8282
*/
8383
public function getAncestors(): \Traversable;
8484

8585
/**
8686
* Get the node's sibblings.
8787
*
8888
* @return \Traversable
89-
* The array of sibblings.
89+
* The sibblings.
9090
*/
9191
public function getSibblings(): \Traversable;
9292

9393
/**
94-
* The number of children a node has.
94+
* The amount of children a node has.
9595
*
9696
* @return int
9797
* The amount of children.
@@ -107,7 +107,7 @@ public function degree(): int;
107107
public function depth(): int;
108108

109109
/**
110-
* Get a clone of the object without any children.
110+
* Get a clone of the object with or without children.
111111
*
112112
* @param \drupol\phptree\Node\NodeInterface ...$nodes
113113
* The children that the clone will have.

0 commit comments

Comments
 (0)