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