From 9dede9a8f5e564b6375f2fc44f7eac0c4f8e0aef Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 30 Oct 2014 16:52:37 +0100 Subject: Result: {additional,canceled}_stops: keep the original stop order --- lib/Travel/Status/DE/IRIS/Result.pm | 41 +++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) (limited to 'lib') 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 { -- cgit v1.2.3