Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpstan/phpstan": "^0.12.91",
"phpstan/phpstan": "^1.3.3",
"vimeo/psalm": "^4.8",
"phpbench/phpbench": "^1.0",
"squizlabs/php_codesniffer": "^3.6",
Expand Down
7 changes: 4 additions & 3 deletions tests/Benchmark/CountIntersectionsBench.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Danon\IntervalTree\Tests\Benchmark;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function init(): void
$this->bruteForceList = [];

for ($i = 0; $i < self::AMOUNT_INTERVALS_IN_TREE; $i++) {
$interval = $this->generateInterval();
$interval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
$this->tree->insert($interval);
$this->bruteForceList[] = $interval;
}
Expand All @@ -45,7 +46,7 @@ public function init(): void
*/
public function benchTree(): void
{
$searchedInterval = $this->generateInterval();
$searchedInterval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
$this->tree->countIntersections($searchedInterval);
}

Expand All @@ -54,7 +55,7 @@ public function benchTree(): void
*/
public function benchBruteForce(): void
{
$searchedInterval = $this->generateInterval();
$searchedInterval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
foreach ($this->bruteForceList as $interval) {
$interval->intersect($searchedInterval);
}
Expand Down
15 changes: 9 additions & 6 deletions tests/Benchmark/GenerateIntervalTrait.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?php

declare(strict_types=1);

namespace Danon\IntervalTree\Tests\Benchmark;

use Danon\IntervalTree\Interval\NumericInterval;
use Exception;
use InvalidArgumentException;

trait GenerateIntervalTrait
{
/**
* @param int $maxHigh
* @param int $maxOffset
* @return NumericInterval
*/
private function generateInterval(): NumericInterval
private function generateInterval(int $maxHigh, int $maxOffset): NumericInterval
{
try {
$low = random_int(0, self::MAX_INTERVAL_HIGH);
$high = random_int($low, min($low + self::MAX_INTERVAL_OFFSET, self::MAX_INTERVAL_HIGH));
$low = random_int(0, $maxHigh);
$high = random_int($low, min($low + $maxOffset, $maxHigh));
} catch (Exception $exception) {
echo 'Cannot generate interval: ' . $exception->getMessage();
exit;
throw new InvalidArgumentException('Wrong interval arguments', $exception->getCode(), $exception);
}
return new NumericInterval($low, $high);
}
}
}
23 changes: 4 additions & 19 deletions tests/Benchmark/HasIntersectionBench.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

declare(strict_types=1);

namespace Danon\IntervalTree\Tests\Benchmark;

use Danon\IntervalTree\Interval\NumericInterval;
use Danon\IntervalTree\IntervalTree;
use Exception;
use PhpBench\Benchmark\Metadata\Annotations\Revs;

/**
Expand Down Expand Up @@ -35,7 +35,7 @@ public function init(): void
$this->bruteForceList = [];

for ($i = 0; $i < self::AMOUNT_INTERVALS_IN_TREE; $i++) {
$interval = $this->generateInterval();
$interval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
$this->tree->insert($interval);
$this->bruteForceList[] = $interval;
}
Expand All @@ -46,7 +46,7 @@ public function init(): void
*/
public function benchTree(): void
{
$searchedInterval = $this->generateInterval();
$searchedInterval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
$this->tree->hasIntersection($searchedInterval);
}

Expand All @@ -55,26 +55,11 @@ public function benchTree(): void
*/
public function benchBruteForce(): void
{
$searchedInterval = $this->generateInterval();
$searchedInterval = $this->generateInterval(self::MAX_INTERVAL_HIGH, self::MAX_INTERVAL_OFFSET);
foreach ($this->bruteForceList as $interval) {
if ($interval->intersect($searchedInterval)) {
break;
}
}
}

/**
* @return NumericInterval
*/
private function generateInterval(): NumericInterval
{
try {
$low = random_int(0, self::MAX_INTERVAL_HIGH);
$high = random_int($low, min($low + self::MAX_INTERVAL_OFFSET, self::MAX_INTERVAL_HIGH));
} catch (Exception $exception) {
echo 'Cannot generate interval: ' . $exception->getMessage();
exit;
}
return new NumericInterval($low, $high);
}
}