| 1 | # A node of a double linked list. | ||||||||||
| 2 | |||||||||||
| 3 | package DataStructure::DoubleList::Node; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use feature ':5.24'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use feature 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | =pod | ||||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | DataStructure::DoubleList::Node | ||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | A single node (element) in a L<DataStructure::DoubleList>. | ||||||||||
| 23 | |||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||
| 25 | |||||||||||
| 26 | =head2 CONSTRUCTOR | ||||||||||
| 27 | |||||||||||
| 28 | You can’t build a node directly. Instead you can call one of the accessors of a | ||||||||||
| 29 | L<DataStructure::DoubleList> or some of the methods below. | ||||||||||
| 30 | |||||||||||
| 31 | Note that a B<DataStructure::DoubleList::Node> does not hold a reference on its | ||||||||||
| 32 | parent list. So a node becomes invalid when the last reference to its list is | ||||||||||
| 33 | deleted as the list itself will be destroyed. But you should also not depend on | ||||||||||
| 34 | this behavior as it might be fixed in the future. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | # The constructor is private and should be called only by this package and its | ||||||||||
| 39 | # parent. | ||||||||||
| 40 | 20 | ▶ | sub new ($class, $list, $prev, $next, $value) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 41 | 20 | my $self = bless { | |||||||||
| 42 | list => $list, | ||||||||||
| 43 | prev => $prev, | ||||||||||
| 44 | next => $next, | ||||||||||
| 45 | value => $value, | ||||||||||
| 46 | }, $class; | ||||||||||
| 47 | 20 | weaken($self->{list}); | |||||||||
| 48 | 20 | return $self; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | =pod | ||||||||||
| 52 | |||||||||||
| 53 | =head2 METHODS | ||||||||||
| 54 | |||||||||||
| 55 | All the functions below are class methods that should be called on a | ||||||||||
| 56 | C<DataStructure::DoubleList::Node> object. | ||||||||||
| 57 | |||||||||||
| 58 | =over 4 | ||||||||||
| 59 | |||||||||||
| 60 | =item value() | ||||||||||
| 61 | |||||||||||
| 62 | Returns the value held by this node. | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 122 | ▶ | sub value ($self) { | ||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 122 | return $self->{value}; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | =pod | ||||||||||
| 71 | |||||||||||
| 72 | =item prev() | ||||||||||
| 73 | |||||||||||
| 74 | Returns the previous B<DataStructure::DoubleList::Node> in this list or B<undef> | ||||||||||
| 75 | if the current object is the first node in its list. | ||||||||||
| 76 | |||||||||||
| 77 | The current node can still be used after that call. | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | sub prev ($self) { | ||||||||
|
Subroutine
prev: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 82 | 1 | return $self->{prev}; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | =pod | ||||||||||
| 86 | |||||||||||
| 87 | =item next() | ||||||||||
| 88 | |||||||||||
| 89 | Returns the next B<DataStructure::DoubleList::Node> in this list or B<undef> | ||||||||||
| 90 | if the current object is the last node in its list. | ||||||||||
| 91 | |||||||||||
| 92 | The current node can still be used after that call. | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | 119 | ▶ | sub next ($self) { | ||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 97 | 119 | return $self->{next}; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | =pod | ||||||||||
| 101 | |||||||||||
| 102 | =item insert_after($value) | ||||||||||
| 103 | |||||||||||
| 104 | Inserts a new node in the list after the current one, with the given value and | ||||||||||
| 105 | returns that new node. | ||||||||||
| 106 | |||||||||||
| 107 | The current node can still be used after that call. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 4 | ▶ | sub insert_after ($self, $value) { | ||||||||
|
Subroutine
insert_after: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 112 | 4 | my $new_node = new(ref $self, $self->{list}, $self, $self->{next}, $value); | |||||||||
| 113 | 4 | ▶ | if (defined $self->{next}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 2 | $self->{next}{prev} = $new_node; | |||||||||
| 115 | } else { | ||||||||||
| 116 | 2 | $self->{list}{last} = $new_node; | |||||||||
| 117 | } | ||||||||||
| 118 | 4 | $self->{next} = $new_node; | |||||||||
| 119 | 4 | $self->{list}{size}++; | |||||||||
| 120 | 4 | return $new_node; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | =pod | ||||||||||
| 125 | |||||||||||
| 126 | =item insert_before($value) | ||||||||||
| 127 | |||||||||||
| 128 | Inserts a new node before in the list before the current one, with the given | ||||||||||
| 129 | value and returns that new node. | ||||||||||
| 130 | |||||||||||
| 131 | The current node can still be used after that call. | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 4 | ▶ | sub insert_before ($self, $value) { | ||||||||
|
Subroutine
insert_before: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 136 | 4 | my $new_node = new(ref $self, $self->{list}, $self->{prev}, $self, $value); | |||||||||
| 137 | 4 | ▶ | if (defined $self->{prev}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 2 | $self->{prev}{next} = $new_node; | |||||||||
| 139 | } else { | ||||||||||
| 140 | 2 | $self->{list}{first} = $new_node; | |||||||||
| 141 | } | ||||||||||
| 142 | 4 | $self->{prev} = $new_node; | |||||||||
| 143 | 4 | $self->{list}{size}++; | |||||||||
| 144 | 4 | return; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | |||||||||||
| 148 | =pod | ||||||||||
| 149 | |||||||||||
| 150 | =item delete() | ||||||||||
| 151 | |||||||||||
| 152 | Removes the node from the list and returns the value that it help value. The | ||||||||||
| 153 | node becomes invalid and can no longer be used. | ||||||||||
| 154 | |||||||||||
| 155 | =over | ||||||||||
| 156 | |||||||||||
| 157 | =cut | ||||||||||
| 158 | |||||||||||
| 159 | 15 | ▶ | sub delete ($self) { | ||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 160 | 15 | my $value = $self->{value}; | |||||||||
| 161 | 15 | $self->_delete(); | |||||||||
| 162 | 15 | return $value; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | # Removes the current element without returning its value. | ||||||||||
| 166 | 15 | ▶ | sub _delete ($self) { | ||||||||
|
Subroutine
_delete: called CC=3 SCAR=11.0
| |||||||||||
| 167 | 15 | my ($prev, $next) = ($self->{prev}, $self->{next}); | |||||||||
| 168 | 15 | ▶ | if (defined $prev) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 6 | $prev->{next} = $next; | |||||||||
| 170 | } else { | ||||||||||
| 171 | 9 | $self->{list}{first} = $next; | |||||||||
| 172 | } | ||||||||||
| 173 | 15 | ▶ | if (defined $next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 7 | $next->{prev} = $prev; | |||||||||
| 175 | } else { | ||||||||||
| 176 | 8 | $self->{list}{last} = $prev; | |||||||||
| 177 | } | ||||||||||
| 178 | 15 | $self->{list}{size}--; | |||||||||
| 179 | 15 | undef %{$self}; | |||||||||
| 180 | 15 | return; | |||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | 1 | 1; | |||||||||