Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Optimize aggregate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Levin committed Jun 20, 2015
1 parent 5832c52 commit 3ae8a3c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
@@ -1,11 +1,16 @@
3.2.1 (20/6/15)
===============
- Update join generator classes such that they dont perform unnecessary inner loop computations for every iteration.
- Optimize aggregate functions in `Traversable`

3.2.0 (20/6/15)
===============
- Fix return by reference issue in generator scheme for PHP versions >=5.6.8
- Refactor `Analysis\PhpTypeSystem::getTypeFromValue` method logic into new static method `Analysis\TypeId::fromValue`
- Fix bug when attempting to parse function with a magic scope parameter type hint (eg `function (self $param) { ... }`.
- Implement `Analysis\TolerantExpressionAnalyser` which will convert analysis exceptions into the *mixed* type.
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::contains` to check whether the collection contains a parameter.
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::remove` to remove a previously added parameter.\
- Add `Providers\DSL\Compilation\Parameters\ParameterCollection::remove` to remove a previously added parameter.
- Introduce `Analysis\INativeType::TYPE_NUMERIC` acting as a union type for `Analysis\INativeType::TYPE_INT` and `Analysis\INativeType::TYPE_DOUBLE`
- Fix bug when joining to the same `ITraversable` instance with an `->indexBy(...)` only returning the first element.

Expand Down
65 changes: 40 additions & 25 deletions Source/Traversable.php
Expand Up @@ -517,7 +517,7 @@ public function aggregate(callable $function)
$hasValue = false;
$aggregateValue = null;

foreach ($this->asArray() as $value) {
foreach ($this->elements as $value) {
if (!$hasValue) {
$aggregateValue = $value;
$hasValue = true;
Expand All @@ -530,21 +530,6 @@ public function aggregate(callable $function)
return $aggregateValue;
}

private function mapArray(callable $function = null)
{
if ($function === null) {
return $this->asArray();
} else {
return $this->scheme->toArray(
$this->scheme->projectionIterator(
$this->asOrderedMap(),
null,
$function
)
);
}
}

private function mapIterator(callable $function = null)
{
if ($function === null) {
Expand All @@ -560,30 +545,54 @@ private function mapIterator(callable $function = null)

public function maximum(callable $function = null)
{
$array = $this->mapArray($function);
$max = null;

foreach ($this->mapIterator($function) as $value) {
if ($value > $max) {
$max = $value;
}
}

return empty($array) ? null : max($array);
return $max;
}

public function minimum(callable $function = null)
{
$array = $this->mapArray($function);
$min = null;
$first = true;

foreach ($this->mapIterator($function) as $value) {
if ($value < $min || $first) {
$min = $value;
$first = false;
}
}

return empty($array) ? null : min($array);
return $min;
}

public function sum(callable $function = null)
{
$array = $this->mapArray($function);
$sum = null;

return empty($array) ? null : array_sum($array);
foreach ($this->mapIterator($function) as $value) {
$sum += $value;
}

return $sum;
}

public function average(callable $function = null)
{
$array = $this->mapArray($function);
$sum = null;
$count = 0;

return empty($array) ? null : array_sum($array) / count($array);
foreach ($this->mapIterator($function) as $value) {
$sum += $value;
$count++;
}

return $count === 0 ? null : $sum / $count;
}

public function all(callable $function = null)
Expand All @@ -610,7 +619,13 @@ public function any(callable $function = null)

public function implode($delimiter, callable $function = null)
{
return implode($delimiter, $this->mapArray($function));
$string = '';

foreach ($this->mapIterator($function) as $value) {
$string .= $delimiter . $value;
}

return $string === '' ? '' : substr($string, strlen($delimiter));
}

// </editor-fold>
Expand Down

0 comments on commit 3ae8a3c

Please sign in to comment.