Hopen/OrderedPredecessorGraph.pm

filter: Statementstmt 100% 36 / 36 Branchbran 100% 10 / 10 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 64 / 64 CC 8 SCAR 10.1
CC
8
Cov
100%
ordered_predecessors
5.0
add_edge
2.0
add_edge_by_id
2.0
CRAP 2.7
« Hopen/Util/Filename.pm ↑ Summary Hopen/G/Entity.pm »
1 # Data::Hopen::OrderedPredecessorGraph - Graph that keeps predecessors in order
2 package Data::Hopen::OrderedPredecessorGraph;
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '0.000018';
7  
8 9 ▶use parent 'Graph';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Docs {{{1
11  
12 =head1 NAME
13  
14 Data::Hopen::OrderedPredecessorGraph - Graph that tracks the order in which edges are added
15  
16 =head1 SYNOPSIS
17  
18 Just like a L<Graph> with two exceptions:
19  
20 =over
21  
22 =item *
23  
24 Every call to L</add_edge> (or other edge-adding routines) tracks the order in
25 an attribute on that edge; and
26  
27 =item *
28  
29 New routine L</ordered_predecessors> returns the predecessors sorted
30 in the order they were added.
31  
32 =back
33  
34 This is unlike L<Graph/predecessors> and L<Graph/edges_to>, which return
35 the predecessors in random order.
36  
37 =cut
38  
39 # }}}1
40  
41 # Internals
42  
43 9 ▶use constant _EDGE_ID => (__PACKAGE__ . '_edge_id'); # attribute name
Subroutine BEGIN: called CC=1 SCAR=0.0
44 9 my $_edge_id = 0; # unique ID for each edge added.
45 # INTERNAL PRECONDITION: real edge IDs are > 0.
46  
47 =head1 FUNCTIONS
48  
49 =head2 add_edge
50  
51 Add an edge. Exactly as L<Graph/add_edge> except that it also creates the
52 new edge attribute to hold the order. Returns the graph.
53  
54 L<Graph/add_edges> is implemented using C<add_edge>, so we don't need to
55 override C<add_edges>.
56  
57 =cut
58  
59 sub add_edge {
60 49 ▶ croak 'Need instance, from, to' unless @_ == 3;
Subroutine add_edge: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 1 48 49
61 48 my ($self, $from, $to) = @_;
62 48 $self->SUPER::add_edge($from, $to);
63 48 $self->set_edge_attribute($from, $to, _EDGE_ID, ++$_edge_id);
64 48 return $self;
65 } #todo()
66  
67 =head2 ordered_predecessors
68  
69 Return a list of the predecessors of the given vertex, in order that the edges
70 were added to that vertex. Exactly as L<Graph/predecessors> except for the
71 stable order.
72  
73 =cut
74  
75 sub ordered_predecessors {
76 101 ▶ croak 'Need instance, vertex' unless @_ == 2;
Subroutine ordered_predecessors: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 1 100 101
77 100 my ($self, $to) = @_;
78 100 ▶ die 'Multiedged graphs are not yet supported' if $self->multiedged;
Branch: 1 branch
BranchTrueFalseTotal
if $self->multiedged 1 99 100
79 # TODO use get_multiedge_ids to get the edge IDs, then get the
80 # attributes for each edge, then sort.
81  
82 99 my @p = $self->predecessors($to);
83 return sort {
84 99 ▶ ( $self->get_edge_attribute($a, $to, _EDGE_ID) // 0 )
Condition100%4/4
$self->get_edge_attribute($a, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id") // 0
l!l
1741
$self->get_edge_attribute($b, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id") // 0
l!l
1741
MC/DC100%1/1
$self->get_edge_attribute($a, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id") // 0
$self->get_edge_attribute($a, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id")
MC/DC100%1/1
$self->get_edge_attribute($b, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id") // 0
$self->get_edge_attribute($b, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id")
Truth table100%2/2
A
A= $self->get_edge_attribute($a, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id")
Aresult
11
00
Truth table100%2/2
A
A= $self->get_edge_attribute($b, $to, "Data::Hopen::OrderedPredecessorGraph_edge_id")
Aresult
11
00
85 <=>
86 ( $self->get_edge_attribute($b, $to, _EDGE_ID) // 0 )
87 } @p;
88 } #ordered_predecessors()
89  
90 =head2 add_edge_by_id
91  
92 Add a multiedge. Exactly as L<Graph/add_edge_by_id> except that it also
93 creates the new edge attribute to hold the order. Returns the graph.
94 Can only be used on a multiedged graph.
95  
96 =cut
97  
98 sub add_edge_by_id {
99 2 ▶ croak 'Need self, from, to, id' unless @_ == 4;
Subroutine add_edge_by_id: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 1 1 2
100 1 my ($self, $from, $to, $id) = @_;
101 1 $self->SUPER::add_edge_by_id($from, $to, $id);
102 1 $self->set_edge_attribute_by_id($from, $to, $id, _EDGE_ID, ++$_edge_id);
103 1 return $self;
104 } #add_edge_by_id()
105  
106 =head2 add_edge_get_id
107  
108 Add a multiedge. Exactly as L<Graph/add_edge_get_id> except that it also
109 creates the new edge attribute to hold the order. Returns the ID of the
110 new edge. Can only be used on a multiedged graph.
111  
112 =cut
113  
114 sub add_edge_get_id {
115 3 ▶ croak 'Need self, from, to' unless @_ == 3;
Subroutine add_edge_get_id: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 1 2 3
116 2 my ($self, $from, $to) = @_;
117 2 my $id = $self->SUPER::add_edge_get_id($from, $to);
118 2 $self->set_edge_attribute_by_id($from, $to, $id, _EDGE_ID, ++$_edge_id);
119 2 return $id;
120 } #add_edge_get_id()
121  
122 9 1;
123 __END__
« Hopen/Util/Filename.pm ↑ Summary Hopen/G/Entity.pm »