Map/Tube/Route.pm

filter: Statementstmt 29.2% 26 / 89 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 31.8% 35 / 110 CC 12 SCAR 44.5
CC
12
Cov
26%
preferred
156.0
as_string
2.0
BEGIN
1.0
CRAP 85.6
↑ Summary Test/Map/Tube.pm »
1 package Map::Tube::Route;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = qv('v5.1.0');
8 our $AUTHORITY = 'cpan:MANWAR';
9  
10 =head1 NAME
11  
12 Map::Tube::Route - Class to represent the route in the map.
13  
14 =head1 VERSION
15  
16 Version v5.1.0
17  
18 =cut
19  
20 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Map::Tube::Types qw(Node Nodes);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Map::Tube::Utils qw(filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use overload q{""} => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 has [ qw(from to) ] => (is => 'ro', isa => Node, required => 1);
30 has nodes => (is => 'ro', isa => Nodes, required => 1);
31 has link_lines => (is => 'ro', default => sub { {} });
32 with 'Map::Tube::Plugin::Formatter';
33  
34 =head1 SYNOPSIS
35  
36 use strict; use warnings;
37 use Map::Tube::London;
38  
39 my $map = Map::Tube::London->new;
40 my $route = $map->get_shortest_route('Baker Street', 'Euston Square');
41  
42 print "Route Starts:", $route->from, "\n";
43 print "Route Ends:", $route->to, "\n";
44 print "Route:", $route, "\n";
45 print "Route Preferred:", $route->preferred, "\n";
46  
47 =head1 DESCRIPTION
48  
49 It provides simple interface to the C<route> of the map.
50  
51 =head1 METHODS
52  
53 =head2 from()
54  
55 Returns an object of type L<Map::Tube::Node> representing the start station.
56  
57 =head2 to()
58  
59 Returns an object of type L<Map::Tube::Node> representing the end station.
60  
61 =head2 nodes()
62  
63 Returns ref to a list of objects of type L<Map::Tube::Node> representing the path
64 from C<start> to C<end> station.
65  
66 =head2 preferred()
67  
68 Returns an object of type L<Map::Tube::Route> as preferred route.
69  
70 =cut
71  
72 0 ▶sub preferred {
Subroutine preferred: not called CC=12 SCAR=50.5
73 0 my ($self) = @_;
74  
75 0 my $lines_object = {};
76 0 foreach my $node (@{$self->nodes}) {
77 0 foreach my $line (@{$node->{line}}) {
78 0 $lines_object->{$line->id} = $line;
79 }
80 }
81  
82 0 my @all_nodes = @{$self->nodes};
83 0 my $n = scalar @all_nodes;
84  
85 # Forward pass: compute the active line set at each position.
86 # Starts with all lines at the first node; at each hop, keep only
87 # the lines that continue from the previous active set. If none
88 # continue, it is a line change and we reset to all lines at that node.
89 0 my @active;
90 {
91 0 my %cur = map { $_->id => 1 } @{$all_nodes[0]->{line}};
92 0 $active[0] = { %cur };
93 0 for my $i (1 .. $n - 1) {
94 0 my %here = map { $_->id => 1 } @{$all_nodes[$i]->{line}};
95  
96 # If this specific edge (previous node -> this node) is
97 # restricted to a subset of lines, only those lines are
98 # eligible here, matching _get_shortest_route's cost logic.
99 0 my $_node_restrictions = $self->link_lines->{uc($all_nodes[$i - 1]->id)};
100 0 ▶ my $_restriction = defined($_node_restrictions) ? $_node_restrictions->{uc($all_nodes[$i]->id)} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_node_restrictions ? : 0 0 0
101 0 ▶ if (defined $_restriction) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_restriction) 0 0 0
102 0 my %_allowed = map { uc($_) => 1 } @$_restriction;
103 0 %here = map { $_ => 1 } grep { $_allowed{uc($_)} } keys %here;
104 }
105  
106 0 my %cont = map { $_ => 1 } grep { $cur{$_} } keys %here;
107 0 ▶ %cur = %cont ? %cont : %here;
Branch: 1 branch
BranchTrueFalseTotal
%cont ? : 0 0 0
108 0 $active[$i] = { %cur };
109 }
110 }
111  
112 # Backward pass: walk back from the end, intersecting each node's
113 # active set with the segment lines accumulated so far.
114 # At a change point, the junction node shows lines from BOTH segments.
115 0 my @display;
116 0 $display[$n - 1] = [ sort keys %{$active[$n - 1]} ];
117 0 my $seg_lines = { %{$active[$n - 1]} };
118  
119 0 for my $i (reverse 0 .. $n - 2) {
120 0 my %isect = map { $_ => 1 } grep { $seg_lines->{$_} } keys %{$active[$i]};
121  
122 0 ▶ if (%isect) {
Branch: 1 branch
BranchTrueFalseTotal
if (%isect) { } 0 0 0
123 0 $seg_lines = \%isect;
124 0 $display[$i] = [ sort keys %isect ];
125 }
126 else {
127 # Change point: show both arriving and departing lines here.
128 0 $display[$i] = [ sort keys %{ { %{$active[$i]}, %$seg_lines } } ];
129 0 $seg_lines = $active[$i];
130 }
131 }
132  
133 # Rebuild nodes with the narrowed line lists.
134 0 my $_nodes = [];
135 0 for my $i (0 .. $n - 1) {
136 0 my $node = $all_nodes[$i];
137 0 my $_node = {
138 id => $node->id,
139 name => $node->name,
140 link => $node->link,
141 };
142 0 for my $id (@{$display[$i]}) {
143 0 push @{$_node->{line}}, $lines_object->{$id}
144 0 ▶ if exists $lines_object->{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $lines_object->{$id} 0 0 0
145 }
146  
147 0 push @$_nodes, Map::Tube::Node->new($_node);
148 }
149  
150 0 return Map::Tube::Route->new({
151 from => $self->from,
152 to => $self->to,
153 nodes => $_nodes });
154 }
155  
156 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
157 0 my ($self) = @_;
158  
159 0 return join ", ", @{$self->nodes};
160 }
161  
162 =head1 AUTHOR
163  
164 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
165  
166 =head1 REPOSITORY
167  
168 L<https://github.com/manwar/Map-Tube>
169  
170 =head1 BUGS
171  
172 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube/issues>.
173 I will be notified and then you'll automatically be notified of progress on your
174 bug as I make changes.
175  
176 =head1 SUPPORT
177  
178 You can find documentation for this module with the perldoc command.
179  
180 perldoc Map::Tube::Route
181  
182 You can also look for information at:
183  
184 =over 4
185  
186 =item * BUG Report
187  
188 L<https://github.com/manwar/Map-Tube/issues>
189  
190 =item * CPAN Ratings
191  
192 L<http://cpanratings.perl.org/d/Map-Tube>
193  
194 =item * Search MetaCPAN
195  
196 L<https://metacpan.org/dist/Map-Tube/>
197  
198 =back
199  
200 =head1 LICENSE AND COPYRIGHT
201  
202 Copyright (C) 2010 - 2026 Mohammad Sajid Anwar.
203  
204 This program is free software; you can redistribute it and/or modify it under
205 the terms of the the Artistic License (2.0). You may obtain a copy of the full
206 license at:
207  
208 L<http://www.perlfoundation.org/artistic_license_2_0>
209  
210 Any use, modification, and distribution of the Standard or Modified Versions is
211 governed by this Artistic License.By using, modifying or distributing the Package,
212 you accept this license. Do not use, modify, or distribute the Package, if you do
213 not accept this license.
214  
215 If your Modified Version has been derived from a Modified Version made by someone
216 other than you,you are nevertheless required to ensure that your Modified Version
217 complies with the requirements of this license.
218  
219 This license does not grant you the right to use any trademark, service mark,
220 tradename, or logo of the Copyright Holder.
221  
222 This license includes the non-exclusive, worldwide, free-of-charge patent license
223 to make, have made, use, offer to sell, sell, import and otherwise transfer the
224 Package with respect to any patent claims licensable by the Copyright Holder that
225 are necessarily infringed by the Package. If you institute patent litigation
226 (including a cross-claim or counterclaim) against any party alleging that the
227 Package constitutes direct or contributory patent infringement,then this Artistic
228 License to you shall terminate on the date that such litigation is filed.
229  
230 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
231 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
232 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
233 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
234 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
235 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
236 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237  
238 =cut
239  
240 1; # End of Map::Tube::Route
↑ Summary Test/Map/Tube.pm »