| 1 | package List::DoubleLinked; | ||||||||||
| 2 | 1 | $List::DoubleLinked::VERSION = '0.005'; | |||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Carp qw/carp croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'weaken'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use namespace::clean 0.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | #no autovivication; | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 12 | 2 | my ($class, @items) = @_; | |||||||||
| 13 | 2 | my $self = bless { | |||||||||
| 14 | head => undef, | ||||||||||
| 15 | tail => undef, | ||||||||||
| 16 | head => { prev => undef }, | ||||||||||
| 17 | tail => { tail => undef }, | ||||||||||
| 18 | }, $class; | ||||||||||
| 19 | 2 | $self->{head}{next} = $self->{tail}; | |||||||||
| 20 | 2 | $self->{tail}{prev} = $self->{head}; | |||||||||
| 21 | 2 | $self->push(@items); | |||||||||
| 22 | 2 | return $self; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | ## no critic (Subroutines::ProhibitBuiltinHomonyms, ControlStructures::ProhibitCStyleForLoops) | ||||||||||
| 26 | |||||||||||
| 27 | 4 | ▶ | sub push { | ||||||||
|
Subroutine
push: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 28 | 4 | my ($self, @items) = @_; | |||||||||
| 29 | 4 | for my $item (@items) { | |||||||||
| 30 | my $new_tail = { | ||||||||||
| 31 | item => $item, | ||||||||||
| 32 | prev => $self->{tail}{prev}, | ||||||||||
| 33 | next => $self->{tail}, | ||||||||||
| 34 | 7 | }; | |||||||||
| 35 | 7 | $self->{tail}{prev}{next} = $new_tail; | |||||||||
| 36 | 7 | $self->{tail}{prev} = $new_tail; | |||||||||
| 37 | 7 | ▶ | $self->{head}{next} = $new_tail if $self->{head}{next} == $self->{tail}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | } | ||||||||||
| 39 | 4 | return; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | sub pop { | ||||||||
|
Subroutine
pop: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 43 | 1 | my $self = shift; | |||||||||
| 44 | 1 | ▶ | croak 'No items to pop from the list' if $self->{tail}{prev} == $self->{head}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | my $ret = $self->{tail}{prev}; | |||||||||
| 46 | 1 | $self->{tail}{prev} = $ret->{prev}; | |||||||||
| 47 | 1 | $ret->{prev}{next} = $self->{tail}; | |||||||||
| 48 | 1 | return $ret->{item}; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1 | ▶ | sub unshift { | ||||||||
|
Subroutine
unshift: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 1 | my ($self, @items) = @_; | |||||||||
| 53 | 1 | for my $item (reverse @items) { | |||||||||
| 54 | my $new_head = { | ||||||||||
| 55 | item => $item, | ||||||||||
| 56 | prev => $self->{head}, | ||||||||||
| 57 | next => $self->{head}{next}, | ||||||||||
| 58 | 1 | }; | |||||||||
| 59 | 1 | $self->{head}{next}{prev} = $new_head; | |||||||||
| 60 | 1 | $self->{head}{next} = $new_head; | |||||||||
| 61 | } | ||||||||||
| 62 | 1 | return; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 1 | ▶ | sub shift { | ||||||||
|
Subroutine
shift: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 66 | 1 | my $self = CORE::shift; | |||||||||
| 67 | 1 | ▶ | croak 'No items to shift from the list' if $self->{head}{next} == $self->{tail}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 1 | my $ret = $self->{head}{next}; | |||||||||
| 69 | 1 | $self->{head}{next} = $ret->{next}; | |||||||||
| 70 | 1 | $ret->{next}{prev} = $self->{head}; | |||||||||
| 71 | 1 | return $ret->{item}; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 8 | ▶ | sub flatten { | ||||||||
|
Subroutine
flatten: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 75 | 8 | my $self = CORE::shift; | |||||||||
| 76 | 8 | my @ret; | |||||||||
| 77 | 8 | ▶ | for (my $current = $self->{head}{next} ; $current != $self->{tail}; $current = $current->{next}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 25 | CORE::push @ret, $current->{item}; | |||||||||
| 79 | } | ||||||||||
| 80 | 8 | return @ret; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | sub front { | ||||||||
|
Subroutine
front: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 84 | 1 | my $self = CORE::shift; | |||||||||
| 85 | 1 | return $self->{head}{next}{item}; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | sub back { | ||||||||
|
Subroutine
back: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | 1 | my $self = CORE::shift; | |||||||||
| 90 | 1 | return $self->{tail}{prev}{item}; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | ▶ | sub empty { | ||||||||
|
Subroutine
empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 94 | 2 | my $self = CORE::shift; | |||||||||
| 95 | return $self->{head}{next} == $self->{tail} | ||||||||||
| 96 | 2 | } | |||||||||
| 97 | |||||||||||
| 98 | 2 | ▶ | sub size { | ||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 99 | 2 | my $self = CORE::shift; | |||||||||
| 100 | 2 | my $ret = 0; | |||||||||
| 101 | 2 | ▶ | for (my $current = $self->{head}{next} ; $current != $self->{tail}; $current = $current->{next}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 3 | $ret++; | |||||||||
| 103 | } | ||||||||||
| 104 | 2 | return $ret; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | ▶ | sub erase { | ||||||||
|
Subroutine
erase: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 108 | 1 | my ($self, $iter) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 1 | my $ret = $iter->next; | |||||||||
| 111 | 1 | my $node = $iter->[0]; | |||||||||
| 112 | |||||||||||
| 113 | 1 | $node->{prev}{next} = $node->{next}; | |||||||||
| 114 | 1 | $node->{next}{prev} = $node->{prev}; | |||||||||
| 115 | |||||||||||
| 116 | 1 | weaken $node; | |||||||||
| 117 | 1 | ▶ | carp 'Node may be leaking' if $node; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 1 | return $ret; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 2 | ▶ | sub begin { | ||||||||
|
Subroutine
begin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 123 | 2 | my $self = CORE::shift; | |||||||||
| 124 | 2 | require List::DoubleLinked::Iterator; | |||||||||
| 125 | |||||||||||
| 126 | 2 | return List::DoubleLinked::Iterator->new($self->{head}{next}); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | 7 | ▶ | sub end { | ||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 130 | 7 | my $self = CORE::shift; | |||||||||
| 131 | 7 | require List::DoubleLinked::Iterator; | |||||||||
| 132 | |||||||||||
| 133 | 7 | return List::DoubleLinked::Iterator->new($self->{tail}); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 2 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||
| 137 | 2 | my $self = CORE::shift; | |||||||||
| 138 | 2 | my $current = $self->{head}; | |||||||||
| 139 | 2 | ▶ | while ($current) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 13 | delete $current->{prev}; | |||||||||
| 141 | 13 | $current = delete $current->{next}; | |||||||||
| 142 | } | ||||||||||
| 143 | 2 | return; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | # ABSTRACT: Double Linked Lists for Perl | ||||||||||
| 147 | |||||||||||
| 148 | 1 | 1; | |||||||||
| 149 | |||||||||||
| 150 | __END__ | ||||||||||