diff options
author | Daniel Friesel <derf@finalrewind.org> | 2014-10-30 16:52:37 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2014-10-30 16:52:37 +0100 |
commit | 9dede9a8f5e564b6375f2fc44f7eac0c4f8e0aef (patch) | |
tree | 21c6cb8d1b924d95f8a2cfc129343aefd3869a88 /lib/Travel/Status/DE | |
parent | fdd2eb6560494f29f9f32cce3e4c156adc659738 (diff) |
Result: {additional,canceled}_stops: keep the original stop order
Diffstat (limited to 'lib/Travel/Status/DE')
-rw-r--r-- | lib/Travel/Status/DE/IRIS/Result.pm | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/lib/Travel/Status/DE/IRIS/Result.pm b/lib/Travel/Status/DE/IRIS/Result.pm index 69ed01a..68c0456 100644 --- a/lib/Travel/Status/DE/IRIS/Result.pm +++ b/lib/Travel/Status/DE/IRIS/Result.pm @@ -190,22 +190,51 @@ sub add_tl { return $self; } +# List::Compare does not keep the order of its arguments (even with unsorted). +# So we need to re-sort all stops to maintain their original order. +sub sorted_sublist { + my ( $self, $list, $sublist ) = @_; + my %pos; + + if ( not $sublist or not @{$sublist} ) { + return; + } + + for my $i ( 0 .. $#{$list} ) { + $pos{ $list->[$i] } = $i; + } + + my @sorted = sort { $pos{$a} <=> $pos{$b} } @{$sublist}; + + return @sorted; +} + sub additional_stops { my ($self) = @_; - $self->{comparator} - //= List::Compare->new( $self->{sched_route_post}, $self->{route_post} ); + $self->{comparator} //= List::Compare->new( + { + lists => [ $self->{sched_route_post}, $self->{route_post} ], + unsorted => 1, + } + ); - return $self->{comparator}->get_complement; + return $self->sorted_sublist( [ $self->{route_post} ], + [ $self->{comparator}->get_complement ] ); } sub canceled_stops { my ($self) = @_; - $self->{comparator} - //= List::Compare->new( $self->{sched_route_post}, $self->{route_post} ); + $self->{comparator} //= List::Compare->new( + { + lists => [ $self->{sched_route_post}, $self->{route_post} ], + unsorted => 1, + } + ); - return $self->{comparator}->get_unique; + return $self->sorted_sublist( [ $self->{sched_route_post} ], + [ $self->{comparator}->get_unique ] ); } sub merge_with_departure { |