Ref/Role/WithDoublyLinkedList.pm

filter: Statementstmt 83.0% 137 / 165 Branchbran 73.8% 31 / 42 Conditioncond 50.0% 2 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 77.2% 17 / 22 Podpod n/a totaltotal 80.0% 188 / 235 CC 32 SCAR 13.2
CC
32
Cov
81%
_mru_history_unshift
6.0
_push
6.0
__ANON__
5.0
CRAP 3.7
« Ref/CAR.pm ↑ Summary Ref/CAR/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.04';
7 }
8 3 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 parameter name => (
11 isa => "Str",
12 required => 1,
13 );
14  
15 1 parameter [qw(value_offset next_offset prev_offset)] => (
16 isa => "Int",
17 required => 1,
18 );
19  
20 1 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 6 );
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 lru: 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 43496 ▶ method $_->{shift_method} => sub {
Subroutine _mru_history_pop: called CC=3 SCAR=11.0
Subroutine remove_mru: called CC=3 SCAR=11.0
71 43496 my $self = shift;
72  
73 43496 ▶ my $node = $self->$attr or return;
Branch: 2 branches
BranchTrueFalseTotal
my $node = $self->$attr or return 43496 0 43496
my $node = $self->$attr or return 1773 5 1778
74  
75 43496 ▶ if ( my $neighbor = $node->[$next] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $neighbor = $node->[$next]) { } 43223 273 43496
if (my $neighbor = $node->[$next]) { } 1769 4 1773
76 43223 $self->$attr($neighbor);
77 43223 $neighbor->[$prev] = undef;
78 } else {
79 # list is empty, clear both attrs
80 273 $self->$attr(undef);
81 273 $self->$rev_attr(undef);
82 }
83  
84 43496 return $node->[$value];
85 8 };
86  
87 0 ▶ method $_->{unshift_method} => sub {
Subroutine _mru_history_unshift: 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 1774 1782
106  
107 0 ▶ return ( @ret == 1 ? $ret[0] : reverse @ret );
Branch: 2 branches
BranchTrueFalseTotal
@ret == 1 ? : 0 0 0
@ret == 1 ? : 1778 4 1782
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 8 ▶ method "${name}_clear" => sub {
Subroutine _mru_history_clear: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=2 SCAR=6.9
116 8 my $self = shift;
117  
118 8 my $cur = $self->$head_attr;
119  
120 8 ▶ while ( $cur ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur) 18 8 26
if ($cur) 8 6 14
121 18 my $next = $cur->[$next_offset];
122 18 $cur = (); # FIXME not so general purpose
123 #@{$cur}[$next_offset, $prev_offset] = (); # more general purpose? don't care...
124 18 $cur = $next;
125 }
126  
127 8 $self->$head_attr(undef);
128 8 $self->$tail_attr(undef);
129 4 };
130  
131 0 ▶ method "${name}_set_next" => sub {
Subroutine _mru_history_set_next: not called CC=1 SCAR=1.2
Subroutine _set_next: called CC=1 SCAR=1.2
132 0 my ( $self, $node, $next ) = @_;
133 0 $node->[$next_offset] = $next;
134 4 };
135  
136 0 ▶ method "${name}_set_prev" => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _set_prev: called CC=1 SCAR=1.2
137 0 my ( $self, $node, $prev ) = @_;
138 0 $node->[$prev_offset] = $prev;
139 4 };
140  
141 0 ▶ method "${name}_link_sequence" => sub {
Subroutine __ANON__: not called CC=3 SCAR=14.9
Subroutine _link_sequence: called CC=3 SCAR=14.9
142 0 my ( $self, @nodes ) = @_;
143  
144 0 ▶ return unless @nodes;
Branch: 2 branches
BranchTrueFalseTotal
unless @nodes 0 0 0
unless @nodes 0 1265 1265
145  
146 0 my $prev = shift @nodes;
147  
148 0 delete $prev->[$prev_offset];
149  
150 0 foreach my $node ( @nodes ) {
151 0 $prev->[$next_offset] = $node; # $prev->next($l)
152 0 $node->[$prev_offset] = $prev;
153 0 $prev = $node;
154 }
155  
156 0 delete $prev->[$next_offset];
157  
158 0 return;
159 4 };
160  
161 6076 ▶ method "${name}_splice" => sub {
Subroutine _mfu_history_splice: called CC=5 SCAR=16.1
Subroutine _splice: called CC=5 SCAR=16.1
Subroutine __ANON__: called CC=5 SCAR=16.1
162 6076 my ( $self, @nodes ) = @_;
163  
164 6076 ▶ return unless @nodes;
Branch: 3 branches
BranchTrueFalseTotal
unless @nodes 0 6076 6076
unless @nodes 0 1255 1255
unless @nodes 0 14 14
165  
166 6076 foreach my $node ( @nodes ) {
167 # detach node from its current place in the list
168 6076 ▶ if ( $node->[$prev_offset] ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->[$prev_offset]) { } 4964 1112 6076
if ($node->[$prev_offset]) { } 1025 231 1256
if ($node->[$prev_offset]) { } 14 4 18
169 4964 $node->[$prev_offset][$next_offset] = $node->[$next_offset];
170 } else {
171 # $node is currently head, so unmark it as such
172 1112 $self->$head_attr($node->[$next_offset]);
173 }
174  
175 6076 ▶ if ( $node->[$next_offset] ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->[$next_offset]) { } 4190 1886 6076
if ($node->[$next_offset]) { } 986 270 1256
if ($node->[$next_offset]) { } 8 10 18
176 4190 $node->[$next_offset][$prev_offset] = $node->[$prev_offset];
177 } else {
178 # $node is currently tail, so unmark it as such
179 1886 $self->$tail_attr($node->[$prev_offset]);
180 }
181  
182 6076 delete @{ $node }[$prev_offset, $next_offset];
183 }
184  
185 return
186 4 };
187 1 };
188  
189 # ex: set sw=4 et:
190  
191 1 __PACKAGE__;
192  
193  
194 __END__
« Ref/CAR.pm ↑ Summary Ref/CAR/Base.pm »