Skip to content

Commit

Permalink
Fix Date/NightIntervalDataSet optimized methods again
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Jul 20, 2021
1 parent db6975a commit 3ef7676
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/Time/IntervalData/DateIntervalDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ public function modifyDataByStream(iterable $inputs, callable $mapper, callable
switch ($intersect) {
case IntersectResult::BEFORE_START:
case IntersectResult::TOUCHES_START:
// skip result for all following inputs
$startIndex++;
// skip input
continue 3;
case IntersectResult::AFTER_END:
case IntersectResult::TOUCHES_END:
Expand Down
3 changes: 1 addition & 2 deletions src/Time/IntervalData/NightIntervalDataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ public function modifyDataByStream(iterable $inputs, callable $mapper, callable
switch ($intersect) {
case IntersectResult::BEFORE_START:
case IntersectResult::TOUCHES_START:
// skip result for all following inputs
$startIndex++;
// skip input
continue 3;
case IntersectResult::AFTER_END:
case IntersectResult::TOUCHES_END:
Expand Down
67 changes: 42 additions & 25 deletions tests/src/Time/IntervalData/DateIntervalDataSet.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Dogma\Time\Interval\DateInterval;
use Dogma\Time\Interval\DateIntervalSet;
use Dogma\Time\IntervalData\DateIntervalData;
use Dogma\Time\IntervalData\DateIntervalDataSet;
use function explode;

require_once __DIR__ . '/../../bootstrap.php';

Expand Down Expand Up @@ -102,29 +103,31 @@ Assert::equal($set->map(static function (DateIntervalData $interval) use ($i) {


collect:
// todo


collectData:
// todo


modifyData:
$reducer = static function (int $state, $data): int {
return $state + $data;
};


modifyData:
Call::withArgs(static function ($orig, $input, $output, $i) use ($ds, $reducer): void {
$orig = $ds($orig);
$input = $ds($input);
$output = $ds($output);
Assert::equal($orig->modifyData($input, $reducer)->normalize(), $output, (string) $i);
}, [
['10-15', '20-25', '10-15'], // no match
// data after "/", default 1
['10-15', '20-25', '10-15/1'], // no match
['10-15', '10-15', '10-15/2'], // same
['10-15', '10-12', '10-12/2, 13-15'], // same start
['10-15', '13-15', '10-12, 13-15/2'], // same end
['10-15', ' 5-12', '10-12/2, 13-15'], // overlaps start
['10-15', '13-20', '10-12, 13-15/2'], // overlaps end
['10-15', '12-13', '10-11, 12-13/2, 14-15'], // in middle
['10-15', '10-12', '10-12/2, 13-15/1'], // same start
['10-15', '13-15', '10-12/1, 13-15/2'], // same end
['10-15', ' 5-12', '10-12/2, 13-15/1'], // overlaps start
['10-15', '13-20', '10-12/1, 13-15/2'], // overlaps end
['10-15', '12-13', '10-11/1, 12-13/2, 14-15/1'], // in middle
['10-15', ' 5-20', '10-15/2'], // overlaps whole

['10-15, 20-25', '10-25', '10-15/2, 20-25/2'], // envelope
Expand All @@ -136,35 +139,49 @@ Call::withArgs(static function ($orig, $input, $output, $i) use ($ds, $reducer):
['10-15, 20-25', ' 5-30', '10-15/2, 20-25/2'], // overlaps whole
]);


modifyDataByStream:
$data = static function (string $days) use ($i): array {
$intervals = [];
foreach (explode(',', $days) as $startEnd) {
[$startEnd, $data] = explode('/', $startEnd . '/');
$intervals[] = [$i($startEnd), $data ? (int) $data : 1];
}
return $intervals;
};
$mapper = static function ($data): array {
return $data[0]->getStartEnd();
};
$reducer = static function (int $state, $data): int {
return $state + $data[1];
};


modifyDataByStream:
Call::withArgs(static function ($orig, $input, $output, $i) use ($ds, $di, $mapper, $reducer): void {
Call::withArgs(static function ($orig, $input, $output, $i) use ($ds, $data, $mapper, $reducer): void {
$orig = $ds($orig);
$input = $di($input);
$input = $data($input);
$output = $ds($output);
Assert::equal($orig->modifyDataByStream([[$input, 1]], $mapper, $reducer)->normalize(), $output, (string) $i);
Assert::equal($orig->modifyDataByStream($input, $mapper, $reducer)->normalize(), $output, (string) $i);
}, [
['10-15', '20-25', '10-15'], // no match
// data after "/", default 1
['10-15', '20-25', '10-15/1'], // no match
['10-15', '10-15', '10-15/2'], // same
['10-15', '10-12', '10-12/2, 13-15'], // same start
['10-15', '13-15', '10-12, 13-15/2'], // same end
['10-15', ' 5-12', '10-12/2, 13-15'], // overlaps start
['10-15', '13-20', '10-12, 13-15/2'], // overlaps end
['10-15', '12-13', '10-11, 12-13/2, 14-15'], // in middle
['10-15', '10-12', '10-12/2, 13-15/1'], // same start
['10-15', '13-15', '10-12/1, 13-15/2'], // same end
['10-15', ' 5-12', '10-12/2, 13-15/1'], // overlaps start
['10-15', '13-20', '10-12/1, 13-15/2'], // overlaps end
['10-15', '12-13', '10-11/1, 12-13/2, 14-15/1'], // in middle
['10-15', ' 5-20', '10-15/2'], // overlaps whole

['10-15, 20-25', '10-25', '10-15/2, 20-25/2'], // envelope
['10-15, 20-25', '10-22', '10-15/2, 20-22/2, 23-25'], // same start
['10-15, 20-25', '13-25', '10-12, 13-15/2, 20-25/2'], // same end
['10-15, 20-25', ' 5-22', '10-15/2, 20-22/2, 23-25'], // overlaps start
['10-15, 20-25', '13-25', '10-12, 13-15/2, 20-25/2'], // overlaps end
['10-15, 20-25', '13-22', '10-12, 13-15/2, 20-22/2, 23-25'], // in middle
['10-15, 20-25', '10-22', '10-15/2, 20-22/2, 23-25/1'], // same start
['10-15, 20-25', '13-25', '10-12/1, 13-15/2, 20-25/2'], // same end
['10-15, 20-25', ' 5-22', '10-15/2, 20-22/2, 23-25/1'], // overlaps start
['10-15, 20-25', '13-25', '10-12/1, 13-15/2, 20-25/2'], // overlaps end
['10-15, 20-25', '13-22', '10-12/1, 13-15/2, 20-22/2, 23-25/1'], // in middle
['10-15, 20-25', ' 5-30', '10-15/2, 20-25/2'], // overlaps whole

// overlaps in inputs
['1-30', '5-25, 10-20', '1-4/1, 5-9/2, 10-20/3, 21-25/2, 26-30/1'],
['1-30', '5-20, 10-25', '1-4/1, 5-9/2, 10-20/3, 21-25/2, 26-30/1'],
['1-30', '1-30, 4-6, 5-7, 14-15, 20-26, 27-30', '1-3/2, 4-4/3, 5-6/4, 7-7/3, 8-13/2, 14-15/3, 16-19/2, 20-30/3'],
]);

0 comments on commit 3ef7676

Please sign in to comment.