blib/lib/Devel/MAT/Graph.pm

filter: Statementstmt 86.9% 40 / 46 Branchbran 60.0% 6 / 10 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 94.7% 18 / 19 Podpod 100% 7 / 7 totaltotal 84.5% 82 / 97 CC 10 SCAR 7.1
CC
10
Cov
82%
edges_out
4.1
has_sv
3.1
edges_in
3.0
CRAP 2.0
« blib/lib/Devel/MAT/Tool/Stack.pm ↑ Summary blib/lib/Devel/MAT/Context.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2014-2026 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Graph 0.56;
7  
8 10 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Struct::Dumb 0.07 'readonly_struct';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<Devel::MAT::Graph> - a set of references between related SVs
19  
20 =head1 DESCRIPTION
21  
22 Instances of this class represent an entire graph of references between
23 related SVs, as a helper method for return values from various L<Devel::MAT>
24 methods, which might be used for some sort of screen layout or other analysis
25 tasks.
26  
27 =cut
28  
29 =head1 CONSTRUCTOR
30  
31 =for highlighter language=perl
32  
33 =cut
34  
35 =head2 new
36  
37 $graph = Devel::MAT::Graph->new( $dumpfile );
38  
39 Constructs a new C<Devel::MAT::Graph> instance backed by the given dumpfile
40 (which is only actually used to make the C<< $node->sv >> method work).
41  
42 =cut
43  
44 2 ▶sub new ( $class, $df )
Subroutine new: called CC=1 SCAR=0.0
Pod documented
45 {
46 2 bless {
47 df => $df,
48  
49 edges_from => {},
50 edges_to => {},
51  
52 roots_from => {},
53 }, $class;
54 }
55  
56 =head1 MUTATION METHODS
57  
58 =cut
59  
60 =head2 add_sv
61  
62 $graph->add_sv( $sv );
63  
64 Makes the graph aware of the given L<Devel::MAT::SV>. This is not strictly
65 necessary before calling C<add_ref> or C<add_root>, but ensures that C<has_sv>
66 will return true immediately after it, and so can be used as a sentinel for
67 recursion control.
68  
69 =cut
70  
71 7711 ▶sub add_sv ( $self, $sv )
Subroutine add_sv: called CC=2 SCAR=6.9
Pod documented
72 {
73 7711 ▶ $self->{edges_from}{$sv->addr} ||= [];
Condition100%2/2
$self->{"edges_from"}{$sv->addr} ||= []
l!l
38543857
MC/DC100%1/1
$self->{"edges_from"}{$sv->addr} ||= []
$self->{"edges_from"}{$sv->addr}
Truth table100%2/2
A
A= $self->{"edges_from"}{$sv->addr}
Aresult
11
00
74  
75 7711 return $self;
76 }
77  
78 =head2 add_ref
79  
80 $graph->add_ref( $from_sv, $to_sv, $desc );
81  
82 Adds an edge to the graph, from and to the given SVs, with the given
83 description.
84  
85 =cut
86  
87 9634 ▶sub add_ref ( $self, $from_sv, $to_sv, $desc )
Subroutine add_ref: called CC=1 SCAR=0.0
Pod documented
88 {
89 9634 my $from_addr = $from_sv->addr;
90 9634 my $to_addr = $to_sv->addr;
91  
92 9634 push $self->{edges_from}{$from_addr}->@*, [ $to_addr, $desc ];
93 9634 push $self->{edges_to} {$to_addr} ->@*, [ $from_addr, $desc ];
94  
95 9634 return $self;
96 }
97  
98 =head2 add_root
99  
100 $graph->add_root( $from_sv, $desc );
101  
102 Adds a root edge to the graph, at the given SV with the given description.
103  
104 =cut
105  
106 1921 ▶sub add_root ( $self, $from_sv, $desc )
Subroutine add_root: called CC=1 SCAR=0.0
Pod documented
107 {
108 1921 push $self->{roots_from}{$from_sv->addr}->@*, $desc;
109  
110 1921 return $self;
111 }
112  
113 =head1 QUERY METHODS
114  
115 =cut
116  
117 =head2 has_sv
118  
119 $bool = $graph->has_sv( $sv );
120  
121 Returns true if the graph has edges or roots for the given SV, or it has at
122 least been given to C<add_sv>.
123  
124 =cut
125  
126 9634 ▶sub has_sv ( $self, $sv )
Subroutine has_sv: called CC=3 SCAR=11.4
Pod documented
127 {
128 9634 my $addr = $sv->addr;
129  
130 return !!( $self->{edges_from}{$addr} ||
131 $self->{edges_to} {$addr} ||
132 9634 ▶ $self->{roots_from}{$addr} );
Condition66%4/6
$self->{'edges_from'}{$addr} || $self->{'edges_to'}{$addr} || $self->{'roots_from'}{$addr}
l!l&&r!l&&!r
577903855
$self->{"edges_from"}{$addr} or $self->{"edges_to"}{$addr}
l!l&&r!l&&!r
577903855
MC/DC33%1/3
$self->{'edges_from'}{$addr} || $self->{'edges_to'}{$addr} || $self->{'roots_from'}{$addr}
$self->{"edges_from"}{$addr} $self->{"edges_to"}{$addr} $self->{'roots_from'}{$addr}
Truth table50%2/4
A or B || C
A= $self->{"edges_from"}{$addr}B= $self->{"edges_to"}{$addr}C= $self->{'roots_from'}{$addr}
ABCresult
1XX1
01X1
0011
0000
133 }
134  
135 =head2 get_sv_node
136  
137 $node = $graph->get_sv_node( $sv );
138  
139 Returns a C<Node> object for the given SV.
140  
141 =cut
142  
143 3857 ▶sub get_sv_node ( $self, $sv )
Subroutine get_sv_node: called CC=2 SCAR=7.2
Pod documented
144 {
145 3857 ▶ my $addr = ref $sv ? $sv->addr : $sv;
Branch: 1 branch
BranchTrueFalseTotal
ref $sv ? : 3857 0 3857
146  
147 3857 return Devel::MAT::Graph::Node->new(
148 graph => $self,
149 addr => $addr,
150 );
151 }
152  
153 =head2 get_root_nodes
154  
155 @desc_nodes = $graph->get_root_nodes;
156  
157 Returns an even-sized list of pairs, containing root descriptions and the
158 nodes having those roots, in no particular order.
159  
160 =cut
161  
162 0 ▶sub get_root_nodes ( $self )
Subroutine get_root_nodes: not called CC=1 SCAR=6.9
Pod documented
163 {
164 return map {
165 0 my $node = $self->get_sv_node( $_ );
166 0 map { $_, $node } $self->{roots_from}{$_}->@*
167 0 } keys $self->{roots_from}->%*;
168 }
169  
170 package Devel::MAT::Graph::Node 0.56;
171  
172 =head1 NODE OBJECTS
173  
174 The values returned by C<get_sv_node> respond to the following methods:
175  
176 =cut
177  
178 3887 ▶sub new ( $class, %args ) { bless { %args }, $class }
Subroutine new: called CC=1 SCAR=0.0
179  
180 =head2 graph
181  
182 $graph = $node->graph;
183  
184 Returns the containing C<Devel::MAT::Graph> instance.
185  
186 =head2 addr
187  
188 $addr = $node->addr;
189  
190 Returns the address of the SV represented by this node.
191  
192 =cut
193  
194 55 ▶sub graph ( $self ) { $self->{graph} }
Subroutine graph: called CC=1 SCAR=0.0
195 34 ▶sub addr ( $self ) { $self->{addr} }
Subroutine addr: called CC=1 SCAR=0.0
196  
197 =head2 sv
198  
199 $sv = $node->sv;
200  
201 Returns the SV object itself, as taken from the dumpfile instance.
202  
203 =cut
204  
205 6 ▶sub sv ( $self ) { $self->graph->{df}->sv_at( $self->addr ) }
Subroutine sv: called CC=1 SCAR=0.0
206  
207 =head2 roots
208  
209 @roots = $node->roots;
210  
211 Returns any root descriptions given (by calls to C<< $graph->add_root >> for
212 the SV at this node.
213  
214 $graph->add_root( $sv, $desc );
215  
216 ( $desc, ... ) = $graph->get_sv_node( $sv )->roots;
217  
218 =cut
219  
220 8 ▶sub roots ( $self )
Subroutine roots: called CC=2 SCAR=6.9
221 {
222 8 ▶ return ( $self->graph->{roots_from}{$self->addr} // [] )->@*;
Condition100%2/2
$self->graph->{"roots_from"}{$self->addr} // []
l!l
62
MC/DC100%1/1
$self->graph->{"roots_from"}{$self->addr} // []
$self->graph->{"roots_from"}{$self->addr}
Truth table100%2/2
A
A= $self->graph->{"roots_from"}{$self->addr}
Aresult
11
00
223 }
224  
225 =head2 edges_out
226  
227 @edges = $node->edges_out;
228  
229 Returns an even-sized list of any edge descriptions and more C<Node> objects
230 given as references (by calls to C<< $graph->add_ref >>) from the SV at this
231 node.
232  
233 $graph->add_ref( $from_sv, $to_sv, $desc );
234  
235 ( $desc, $to_edge, ... ) = $graph->get_sv_node( $from_sv )->edges_out;
236  
237 =head2 edges_out (scalar)
238  
239 $n_edges = $node->edges_out;
240  
241 In scalar context, returns the I<number of edges> that exist; i.e. half the
242 size of the pairlist that would be returned in list context.
243  
244 =cut
245  
246 4 ▶sub edges_out ( $self )
Subroutine edges_out: called CC=3 SCAR=14.2
247 {
248 4 ▶ return unless my $edges = $self->graph->{edges_from}{$self->addr};
Branch: 1 branch
BranchTrueFalseTotal
unless my $edges = $self->graph->{"edges_from"}{$self->addr} 0 4 4
249 4 ▶ return scalar @$edges unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 0 4
250 return map {
251 0 $_->[1], ( ref $self )->new( graph => $self->graph, addr => $_->[0] )
252 } @$edges;
253 }
254  
255 =head2 edges_in
256  
257 @edges = $node->edges_in;
258  
259 Similar to C<edges_out>, but returns edges in the opposite direction; i.e.
260 edges of references to this node.
261  
262 $graph->add_ref( $from_sv, $to_sv, $desc );
263  
264 ( $desc, $from_edge, ... ) = $graph->get_sv_node( $to_sv )->edges_in;
265  
266 =head2 edges_in (scalar)
267  
268 $n_edges = $node->edges_out;
269  
270 In scalar context, returns the I<number of edges> that exist; i.e. half the
271 size of the pairlist that would be returned in list context.
272  
273 =cut
274  
275 7 ▶sub edges_in ( $self )
Subroutine edges_in: called CC=3 SCAR=11.0
276 {
277 7 ▶ return unless my $edges = $self->graph->{edges_to}{$self->addr};
Branch: 1 branch
BranchTrueFalseTotal
unless my $edges = $self->graph->{"edges_to"}{$self->addr} 1 6 7
278 6 ▶ return scalar @$edges unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 6 6
279 return map {
280 6 $_->[1], ( ref $self )->new( graph => $self->graph, addr => $_->[0] )
281 } @$edges;
282 }
283  
284 =head1 AUTHOR
285  
286 Paul Evans <leonerd@leonerd.org.uk>
287  
288 =cut
289  
290 10 0x55AA;
« blib/lib/Devel/MAT/Tool/Stack.pm ↑ Summary blib/lib/Devel/MAT/Context.pm »