| 1 | # A double-linked list data-structure. | ||||||||||
| 2 | |||||||||||
| 3 | package DataStructure::DoubleList; | ||||||||||
| 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 DataStructure::DoubleList::Node; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use parent qw(DataStructure::Queue DataStructure::Stack); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =pod | ||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | DataStructure::DoubleList | ||||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | A double-linked list data-structure, written in pure Perl. | ||||||||||
| 25 | |||||||||||
| 26 | See also L<DataStructure::LinkedList> for a non double-linked list version. | ||||||||||
| 27 | |||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | =head2 CONSTRUCTOR | ||||||||||
| 31 | |||||||||||
| 32 | C<< DataStructure::DoubleList->new() >> | ||||||||||
| 33 | |||||||||||
| 34 | Creates an empty list. | ||||||||||
| 35 | |||||||||||
| 36 | =cut | ||||||||||
| 37 | |||||||||||
| 38 | 3 | ▶ | sub new ($class) { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | 3 | return bless { size => 0, first => undef, last => undef}, $class; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | =pod | ||||||||||
| 43 | |||||||||||
| 44 | =head2 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | All the functions below are class methods that should be called on a | ||||||||||
| 47 | B<DataStructure::DoubleList> object. Unless documented otherwise, they run in | ||||||||||
| 48 | constant time. | ||||||||||
| 49 | |||||||||||
| 50 | =over 4 | ||||||||||
| 51 | |||||||||||
| 52 | =item first() | ||||||||||
| 53 | |||||||||||
| 54 | Returns the first L<DataStructure::DoubleList::Node> of the list, or B<undef> if | ||||||||||
| 55 | the list is empty. | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 33 | ▶ | sub first ($self) { | ||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | 33 | return $self->{first}; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =pod | ||||||||||
| 64 | |||||||||||
| 65 | =item last() | ||||||||||
| 66 | |||||||||||
| 67 | Returns the last L<DataStructure::DoubleList::Node> of the list, or B<undef> if | ||||||||||
| 68 | the list is empty. | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | 5 | ▶ | sub last ($self) { | ||||||||
|
Subroutine
last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 5 | return $self->{last}; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | =pod | ||||||||||
| 77 | |||||||||||
| 78 | =item push($value) | ||||||||||
| 79 | |||||||||||
| 80 | Adds a new node at the end of the list with the given value. Returns the newly | ||||||||||
| 81 | added node. | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | 4 | ▶ | sub push ($self, $value) { | ||||||||
|
Subroutine
push: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 86 | 4 | my $new_node = DataStructure::DoubleList::Node->new($self, $self->{last}, undef, $value); | |||||||||
| 87 | 4 | ▶ | $self->{last}{next} = $new_node if defined $self->{last}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 4 | $self->{last} = $new_node; | |||||||||
| 89 | 4 | ▶ | $self->{first} = $new_node unless defined $self->{first}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 4 | $self->{size}++; | |||||||||
| 91 | 4 | return $new_node; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | =pod | ||||||||||
| 96 | |||||||||||
| 97 | =item unshift($value) | ||||||||||
| 98 | |||||||||||
| 99 | Adds a new node at the beginning of the list with the given value. Returns the | ||||||||||
| 100 | newly added node. | ||||||||||
| 101 | |||||||||||
| 102 | =cut | ||||||||||
| 103 | |||||||||||
| 104 | 8 | ▶ | sub unshift ($self, $value) { | ||||||||
|
Subroutine
unshift: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 105 | 8 | my $new_node = DataStructure::DoubleList::Node->new($self, undef, $self->{first}, $value); | |||||||||
| 106 | 8 | ▶ | $self->{first}{prev} = $new_node if defined $self->{first}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 8 | $self->{first} = $new_node; | |||||||||
| 108 | 8 | ▶ | $self->{last} = $new_node unless defined $self->{last}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 8 | $self->{size}++; | |||||||||
| 110 | 8 | return $new_node; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | =pod | ||||||||||
| 114 | |||||||||||
| 115 | =item pop() | ||||||||||
| 116 | |||||||||||
| 117 | Removes the last node of the list and returns its value. Returns B<undef> if the | ||||||||||
| 118 | list is empty. Note that the method can also return B<undef> if the last node’s | ||||||||||
| 119 | value is B<undef> | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | 8 | ▶ | sub pop ($self) { | ||||||||
|
Subroutine
pop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 124 | 8 | ▶ | return $self->{last}->delete if defined $self->{last}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 1 | return; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | =pod | ||||||||||
| 130 | |||||||||||
| 131 | =item shift() | ||||||||||
| 132 | |||||||||||
| 133 | Removes the first node of the list and returns its value. Returns B<undef> if | ||||||||||
| 134 | the list is empty. Note that the method can also return B<undef> if the first | ||||||||||
| 135 | node’s value is B<undef> | ||||||||||
| 136 | |||||||||||
| 137 | =cut | ||||||||||
| 138 | |||||||||||
| 139 | 10 | ▶ | sub shift ($self) { | ||||||||
|
Subroutine
shift: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 140 | 10 | ▶ | return $self->{first}->delete if defined $self->{first}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 2 | return; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =pod | ||||||||||
| 145 | |||||||||||
| 146 | =item size() | ||||||||||
| 147 | |||||||||||
| 148 | Returns the number of nodes in the list. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 36 | ▶ | sub size ($self) { | ||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 153 | 36 | return $self->{size}; | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | =pod | ||||||||||
| 157 | |||||||||||
| 158 | =item empty() | ||||||||||
| 159 | |||||||||||
| 160 | Returns whether the list is empty. | ||||||||||
| 161 | |||||||||||
| 162 | =cut | ||||||||||
| 163 | |||||||||||
| 164 | 5 | ▶ | sub empty ($self) { | ||||||||
|
Subroutine
empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 165 | 5 | return $self->size() == 0; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | =pod | ||||||||||
| 169 | |||||||||||
| 170 | =item values() | ||||||||||
| 171 | |||||||||||
| 172 | Returns all the values of the list, as a normal Perl list. This runs in linear | ||||||||||
| 173 | time with the size of the list. | ||||||||||
| 174 | |||||||||||
| 175 | =cut | ||||||||||
| 176 | |||||||||||
| 177 | 25 | ▶ | sub values ($self) { | ||||||||
|
Subroutine
values: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 178 | 25 | ▶ | return $self->size() unless wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | 25 | my @ret = (0) x $self->size(); | |||||||||
| 180 | 25 | my $i = 0; | |||||||||
| 181 | 25 | my $cur = $self->first(); | |||||||||
| 182 | 25 | ▶ | while (defined $cur) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 116 | $ret[$i++] = $cur->value(); | |||||||||
| 184 | 116 | $cur = $cur->next(); | |||||||||
| 185 | } | ||||||||||
| 186 | 25 | return @ret; | |||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | 3 | ▶ | sub DESTROY ($self) { | ||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||
| 190 | 3 | my $next = $self->{first}; | |||||||||
| 191 | 3 | ▶ | while (defined $next) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 5 | my $cur = $next; | |||||||||
| 193 | 5 | $next = $cur->{next}; | |||||||||
| 194 | 5 | undef %{$cur}; | |||||||||
| 195 | } | ||||||||||
| 196 | 3 | return; | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | =pod | ||||||||||
| 200 | |||||||||||
| 201 | =back | ||||||||||
| 202 | |||||||||||
| 203 | =head1 SEE ALSO | ||||||||||
| 204 | |||||||||||
| 205 | L<DataStructure::LinkedList>, L<List::DoubleLinked> | ||||||||||
| 206 | |||||||||||
| 207 | =head1 AUTHOR | ||||||||||
| 208 | |||||||||||
| 209 | Mathias Kende <mathias@cpan.org> | ||||||||||
| 210 | |||||||||||
| 211 | =head1 LICENCE | ||||||||||
| 212 | |||||||||||
| 213 | Copyright 2021 Mathias Kende | ||||||||||
| 214 | |||||||||||
| 215 | This program is distributed under the MIT (X11) License: | ||||||||||
| 216 | L<http://www.opensource.org/licenses/mit-license.php> | ||||||||||
| 217 | |||||||||||
| 218 | Permission is hereby granted, free of charge, to any person | ||||||||||
| 219 | obtaining a copy of this software and associated documentation | ||||||||||
| 220 | files (the "Software"), to deal in the Software without | ||||||||||
| 221 | restriction, including without limitation the rights to use, | ||||||||||
| 222 | copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||
| 223 | copies of the Software, and to permit persons to whom the | ||||||||||
| 224 | Software is furnished to do so, subject to the following | ||||||||||
| 225 | conditions: | ||||||||||
| 226 | |||||||||||
| 227 | The above copyright notice and this permission notice shall be | ||||||||||
| 228 | included in all copies or substantial portions of the Software. | ||||||||||
| 229 | |||||||||||
| 230 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||||||
| 231 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||||||||||
| 232 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||||||
| 233 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||||||||||
| 234 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||||||||||
| 235 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||||||||||
| 236 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||||||||||
| 237 | OTHER DEALINGS IN THE SOFTWARE. | ||||||||||
| 238 | |||||||||||
| 239 | =cut | ||||||||||
| 240 | |||||||||||
| 241 | 1 | 1; | |||||||||