Ref/Role/WithDoublyLinkedList.pm

filter: Statementstmt 86.9% 93 / 107 Branchbran 73.0% 19 / 26 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 14 / 16 Podpod n/a totaltotal 83.2% 129 / 155 CC 32 SCAR 12.3
CC
32
Cov
83%
_mru_history_push
6.0
_push
6.0
__ANON__
5.0
CRAP 3.4
« Ref/CAR/Base.pm ↑ Summary Ref/CLOCK/Base.pm »
1 package Cache::Ref::Role::WithDoublyLinkedList;
2 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 $Cache::Ref::Role::WithDoublyLinkedList::AUTHORITY = 'cpan:NUFFIN';
4 }
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $Cache::Ref::Role::WithDoublyLinkedList::VERSION = '0.05'; # TRIAL
7 }
8 3 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 parameter name => (
11 isa => "Str",
12 required => 1,
13 );
14  
15 3 parameter [qw(value_offset next_offset prev_offset)] => (
16 isa => "Int",
17 required => 1,
18 );
19  
20 3 foreach my $method (qw(head tail shift pop unshift push)) {
21 parameter "${method}_method" => (
22 isa => "Str",
23 is => "ro",
24 lazy => 1,
25 14 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 14 my $p = shift;
27 14 $p->name . "_" . $method;
28 }
29 18 );
30 }
31  
32 4 ▶role {
Subroutine __ANON__: called CC=2 SCAR=7.1
33 4 my $p = shift;
34  
35 4 my $name = $p->name;
36  
37 4 my ( $head_attr, $tail_attr ) = ("${name}_head", "${name}_tail");
38  
39 # technically a doubly linked list is just the inverse of itself
40 # so really this is like including two inverted parameterized roles (the
41 # notion of next/prev is reversed) for the endian methods
42 4 for (
43 {
44 attr => $head_attr,
45 rev_attr => $tail_attr,
46 vo => $p->value_offset,
47 no => $p->next_offset,
48 po => $p->prev_offset,
49 head_method => $p->head_method,
50 shift_method => $p->shift_method,
51 unshift_method => $p->unshift_method
52 },
53 {
54 attr => $tail_attr,
55 rev_attr => $head_attr,
56 vo => $p->value_offset,
57 no => $p->prev_offset,
58 po => $p->next_offset,
59 head_method => $p->tail_method,
60 shift_method => $p->pop_method,
61 unshift_method => $p->push_method,
62 },
63 ) {
64 8 my ( $attr, $rev_attr, $value, $next, $prev ) = @{$_}{qw(attr rev_attr vo no po)};
65  
66 8 has $attr => ( is => "rw" );
67  
68 0 ▶ method $_->{head_method} => sub { (shift->$attr || return)->[$value] };
Subroutine _mfu_history_tail: not called CC=2 SCAR=8.4
Subroutine mru: called CC=2 SCAR=8.4
Condition50%2/4
(shift())->$attr || (return)
l!l
00
(shift())->$attr || (return)
l!l
3910
MC/DC0%0/1
(shift())->$attr || (return)
(shift())->$attr
MC/DC100%1/1
(shift())->$attr || (return)
(shift())->$attr
Truth table0%0/2
A
A= (shift())->$attr
Aresult
11
00
Truth table100%2/2
A
A= (shift())->$attr
Aresult
11
00
69  
70 44355 ▶ method $_->{shift_method} => sub {
Subroutine _mfu_history_shift: called CC=3 SCAR=11.0
Subroutine __ANON__: called CC=3 SCAR=11.0
71 44355 my $self = shift;
72  
73 44355 ▶ my $node = $self->$attr or return;
Branch: 2 branches
BranchTrueFalseTotal
my $node = $self->$attr or return 44355 0 44355
my $node = $self->$attr or return 1783 5 1788
74  
75 44355 ▶ if ( my $neighbor = $node->[$next] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $neighbor = $node->[$next]) { } 44013 342 44355
if (my $neighbor = $node->[$next]) { } 1779 4 1783
76 44013 $self->$attr($neighbor);
77 44013 $neighbor->[$prev] = undef;
78 } else {
79 # list is empty, clear both attrs
80 342 $self->$attr(undef);
81 342 $self->$rev_attr(undef);
82 }
83  
84 44355 return $node->[$value];
85 8 };
86  
87 0 ▶ method $_->{unshift_method} => sub {
Subroutine _mru_history_push: not called CC=4 SCAR=17.9
Subroutine _push: called CC=4 SCAR=17.9
88 0 my ( $self, @values ) = @_;
89  
90 0 my $head = $self->$attr;
91  
92 0 my @ret;
93  
94 0 foreach my $v ( reverse @values ) {
95 # cons up a new list
96 0 my $new = [];
97 0 $new->[$value] = $v;
98 0 $new->[$next] = $head;
99 0 $head->[$prev] = $new;
100 0 push @ret, $new;
101 0 $head = $new;
102 }
103  
104 0 $self->$attr($head);
105 0 ▶ $self->$rev_attr($ret[0]) unless $self->$rev_attr;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->$rev_attr 0 0 0
unless $self->$rev_attr 8 1784 1792
106  
107 0 ▶ return ( @ret == 1 ? $ret[0] : reverse @ret );
Branch: 2 branches
BranchTrueFalseTotal
@ret == 1 ? : 0 0 0
@ret == 1 ? : 1788 4 1792
108 }
109 8 }
110  
111 # these methods are per linked list
112  
113 4 my ( $next_offset, $prev_offset ) = ( $p->next_offset, $p->prev_offset );
114  
115 14 ▶ method "${name}_clear" => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
116 14 my $self = shift;
117  
118 14 my $cur = $self->$head_attr;
119  
120 14 ▶ while ( $cur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 26 14 40
121 26 my $next = $cur->[$next_offset];
122 26 $cur = (); # FIXME not so general purpose
123 #@{$cur}[$next_offset, $prev_offset] = (); # more general purpose? don't care...
124 26 $cur = $next;
125 }
126  
127 14 $self->$head_attr(undef);
128 14 $self->$tail_attr(undef);
129 4 };
130  
131 1253 ▶ method "${name}_set_next" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1253 my ( $self, $node, $next ) = @_;
133 1253 $node->[$next_offset] = $next;
134 4 };
135  
136 1253 ▶ method "${name}_set_prev" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 1253 my ( $self, $node, $prev ) = @_;
138 1253 $node->[$prev_offset] = $prev;
139 4 };
140  
141 1255 ▶ method "${name}_link_sequence" => sub {
Subroutine _mru_history_link_sequence: called CC=3 SCAR=11.0
142 1255 my ( $self, @nodes ) = @_;
143  
144 1255 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 1255 1255
145  
146 1255 my $prev = shift @nodes;
147  
148 1255 delete $prev->[$prev_offset];
149  
150 1255 foreach my $node ( @nodes ) {
151 5 $prev->[$next_offset] = $node; # $prev->next($l)
152 5 $node->[$prev_offset] = $prev;
153 5 $prev = $node;
154 }
155  
156 1255 delete $prev->[$next_offset];
157  
158 1255 return;
159 4 };
160  
161 7394 ▶ method "${name}_splice" => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
162 7394 my ( $self, @nodes ) = @_;
163  
164 7394 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 7394 7394
165  
166 7394 foreach my $node ( @nodes ) {
167 # detach node from its current place in the list
168 7399 ▶ if ( $node->[$prev_offset] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[$prev_offset]) { } 5829 1570 7399
169 5829 $node->[$prev_offset][$next_offset] = $node->[$next_offset];
170 } else {
171 # $node is currently head, so unmark it as such
172 1570 $self->$head_attr($node->[$next_offset]);
173 }
174  
175 7399 ▶ if ( $node->[$next_offset] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[$next_offset]) { } 5117 2282 7399
176 5117 $node->[$next_offset][$prev_offset] = $node->[$prev_offset];
177 } else {
178 # $node is currently tail, so unmark it as such
179 2282 $self->$tail_attr($node->[$prev_offset]);
180 }
181  
182 7399 delete @{ $node }[$prev_offset, $next_offset];
183 }
184  
185 return
186 4 };
187 3 };
188  
189 # ex: set sw=4 et:
190  
191 3 __PACKAGE__;
192  
193  
194 __END__
« Ref/CAR/Base.pm ↑ Summary Ref/CLOCK/Base.pm »