Simplified processing nested events to a single pass.
This commit is contained in:
1 parent
a9484ed4ee
commit
2c074c1cea
1 file changed
+13
-22
@@ -8,39 +8,30 @@ class Recursive_Event_Iterator extends \ArrayIterator implements \RecursiveItera
|
|||||||
|
|
||||||
public function __construct( array $events, $flags = 0 ) {
|
public function __construct( array $events, $flags = 0 ) {
|
||||||
|
|
||||||
|
usort( $events, [ $this, 'sort_origin' ] );
|
||||||
|
$end = 0;
|
||||||
|
|
||||||
foreach ( $events as $key => $event ) {
|
foreach ( $events as $key => $event ) {
|
||||||
|
|
||||||
foreach ( $events as $check_key => $check_event ) {
|
if ( $event['origin'] < $end ) {
|
||||||
|
unset( $events[ $key ] );
|
||||||
if ( $check_key <= $key ) {
|
$this->children[] = $event;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
if ( $this->overlaps( $event, $check_event ) ) {
|
|
||||||
|
|
||||||
unset( $events[ $check_key ] );
|
|
||||||
$this->children[] = $check_event;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$end = $event['origin'] + $event['duration'];
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct( $events, $flags );
|
parent::__construct( $events, $flags );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function sort_origin( $event_a, $event_b ) {
|
||||||
* @param array $event_a
|
|
||||||
* @param array $event_b
|
|
||||||
*
|
|
||||||
* @return bool True if event B starts after A and before it finishes.
|
|
||||||
*/
|
|
||||||
protected function overlaps( $event_a, $event_b ) {
|
|
||||||
|
|
||||||
if ( $event_a['origin'] > $event_b['origin'] ) {
|
if ( $event_a['origin'] === $event_b['origin'] ) {
|
||||||
return false;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $event_b['origin'] < ( $event_a['origin'] + $event_a['duration'] );
|
return ( $event_a['origin'] < $event_b['origin'] ) ? - 1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasChildren() {
|
public function hasChildren() {
|
||||||
|
|||||||
Reference in new issue
Block a user