DataStructure/LinkedList.pm

filter: Statementstmt 99.0% 100 / 101 Branchbran 92.8% 26 / 28 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 96.3% 26 / 27 Podpod 80.0% 8 / 10 totaltotal 96.4% 163 / 169 CC 16 SCAR 7.5
CC
16
Cov
98%
new
4.0
__ANON__
4.0
values
3.0
CRAP 2.1
« DataStructure/BTree/Node.pm ↑ Summary DataStructure.pm »
1 # A linked list data-structure.
2  
3 package DataStructure::LinkedList;
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::LinkedList::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use parent qw(DataStructure::Queue);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 package DataStructure::ReverseLinkedList {
17 1 ▶ use parent qw(DataStructure::LinkedList DataStructure::Queue DataStructure::Stack);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 }
19  
20 =pod
21  
22 =head1 NAME
23  
24 DataStructure::LinkedList
25  
26 =head1 SYNOPSIS
27  
28 A linked list data-structure, written in pure Perl.
29  
30 See also L<DataStructure::DoubleList> for a double-linked list version that
31 offers a richer interface.
32  
33 =head1 DESCRIPTION
34  
35 =head2 CONSTRUCTOR
36  
37 C<< DataStructure::LinkedList->new(%options) >>
38  
39 Creates an empty list.
40  
41 The following options are available:
42  
43 =over 4
44  
45 =item reverse
46  
47 By default this class implements the standard C<shift> and C<unshift> methods
48 that operate on the beginning of the list and the C<push> method that operates
49 on the end of the list. And C<pop> is a synonym for C<shift> (so not the
50 opposite of C<push>).
51  
52 If the C<reverse> option is set to a true value then the semantics of the list
53 is reversed and C<pop> and C<push> operate on the beginning of the list,
54 C<unshift> operates on the end of the list and C<shift> becomes a synonym for
55 C<pop>.
56  
57 =back
58  
59 =cut
60  
61 4 ▶sub new ($class, %options) {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
62 4 ▶ if ($options{reverse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"reverse"}) 1 3 4
63 1 ▶ die unless $class eq 'DataStructure::LinkedList';
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq "DataStructure::LinkedList" 0 1 1
64 1 $class = 'DataStructure::ReverseLinkedList';
65 }
66 return bless {
67 size => 0,
68 first => undef,
69 last => undef,
70 4 ▶ reverse => $options{reverse} // 0,
Condition100%2/2
$options{"reverse"} // 0
l!l
13
MC/DC100%1/1
$options{"reverse"} // 0
$options{"reverse"}
Truth table100%2/2
A
A= $options{"reverse"}
Aresult
11
00
71 }, $class;
72 }
73  
74 =pod
75  
76 =head2 METHODS
77  
78 All the functions below are class methods that should be called on a
79 B<DataStructure::LinkedList> object. Unless documented otherwise, they run in
80 constant time.
81  
82 =over 4
83  
84 =item first()
85  
86 Returns the first L<DataStructure::LinkedList::Node> of the list, or B<undef> if
87 the list is empty.
88  
89 =cut
90  
91 34 ▶sub first ($self) {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
92 34 return $self->{first};
93 }
94  
95 =pod
96  
97 =item last()
98  
99 Returns the last L<DataStructure::LinkedList::Node> of the list, or B<undef> if
100 the list is empty.
101  
102 =cut
103  
104 0 ▶sub last ($self) {
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
105 0 return $self->{last};
106 }
107  
108 # Actual unshift that always operates on the beginning of the list.
109 8 ▶sub _unshift ($self, $value) {
Subroutine _unshift: called CC=2 SCAR=6.9
110 8 my $new_node = DataStructure::LinkedList::Node->new($self, $self->{first}, $value);
111 8 $self->{first} = $new_node;
112 8 ▶ $self->{last} = $new_node unless defined $self->{last};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"last"} 4 4 8
113 8 $self->{size}++;
114 8 return $new_node;
115 }
116  
117 # Actual push that always operates on the end of the list.
118 4 ▶sub _push ($self, $value) {
Subroutine _push: called CC=2 SCAR=6.9
119 4 my $new_node = DataStructure::LinkedList::Node->new($self, undef, $value);
120 4 ▶ if (defined $self->{last}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'last'}) { } 2 2 4
121 2 $self->{last}{next} = $new_node;
122 } else {
123 2 $self->{first} = $new_node;
124 }
125 4 $self->{last} = $new_node;
126 4 $self->{size}++;
127 4 return $new_node;
128 }
129  
130 # Actual shift that always operates on the beginning of the list.
131 14 ▶sub _shift ($self) {
Subroutine _shift: called CC=3 SCAR=11.0
132 14 ▶ return unless defined $self->{first};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"first"} 6 8 14
133 8 my $old_first = $self->first();
134 8 $self->{first} = $old_first->next();
135 8 ▶ $self->{last} = undef unless defined $self->{first};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"first"} 3 5 8
136 8 return $old_first->_delete_first();
137 }
138  
139 =pod
140  
141 =item unshift($value)
142  
143 Adds a new node at the beginning of the list with the given value. Returns the
144 newly added node.
145  
146 =cut
147  
148 8 ▶sub unshift ($self, $value) {
Subroutine unshift: called CC=2 SCAR=6.9
Pod documented
149 8 ▶ return $self->_push($value) if $self->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reverse"} 2 6 8
150 6 return $self->_unshift($value);
151 }
152  
153 =pod
154  
155 =item push($value)
156  
157 Adds a new node at the end of the list with the given value. Returns the
158 newly added node.
159  
160 =cut
161  
162 4 ▶sub push ($self, $value) {
Subroutine push: called CC=2 SCAR=6.9
Pod documented
163 4 ▶ return $self->_unshift($value) if $self->{reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reverse"} 2 2 4
164 2 return $self->_push($value);
165 }
166  
167 =pod
168  
169 =item shift()
170  
171 Removes the first node of the list and returns its value. Returns B<undef> if
172 the list is empty. Note that the method can also return B<undef> if the first
173 node’s value is B<undef>
174  
175 For convenience, C<pop()> can be used as a synonym of C<shift()>.
176  
177 =cut
178  
179 9 ▶sub shift ($self) {
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
180 9 return $self->_shift();
181 }
182  
183 5 ▶sub pop ($self) {
Subroutine pop: called CC=1 SCAR=0.0
Pod undocumented
184 5 return $self->_shift();
185 }
186  
187 =pod
188  
189 =item size()
190  
191 Returns the number of nodes in the list.
192  
193 =cut
194  
195 42 ▶sub size ($self) {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
196 42 return $self->{size};
197 }
198  
199 =pod
200  
201 =item empty()
202  
203 Returns whether the list is empty.
204  
205 =cut
206  
207 3 ▶sub empty ($self) {
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
208 3 return $self->size() == 0;
209 }
210  
211 =pod
212  
213 =item values()
214  
215 Returns all the values of the list, as a normal Perl list. This runs in linear
216 time with the size of the list.
217  
218 =cut
219  
220 21 ▶sub values ($self) {
Subroutine values: called CC=3 SCAR=11.0
Pod documented
221 21 ▶ return $self->size() unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 21 21
222 21 my @ret = (0) x $self->size();
223 21 my $i = 0;
224 21 my $cur = $self->first();
225 21 ▶ while (defined $cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur) 24 21 45
226 24 $ret[$i++] = $cur->value();
227 24 $cur = $cur->next();
228 }
229 21 return @ret;
230 }
231  
232 # Runs a consistency check of the list. Assumes that tests are running with
233 # Test::More.
234 18 ▶sub _self_check ($self, $name) {
Subroutine _self_check: called CC=1 SCAR=0.0
235 1 ▶ eval { use Test2::Tools::Compare qw(is T D U); use Test2::Tools::Subtest };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
236 18 ▶ subtest_streamed $name => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
237 18 my $s = $self->{size};
238 18 is($s >= 0, T(), 'Size is non-negative');
239 18 ▶ if ($s == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0) { } 6 12 18
240 6 is($self->{first}, U(), 'No first when size is 0');
241 6 is($self->{last}, U(), 'No last when size is 0');
242 } else {
243 12 is($self->{first}, D(), 'Has first when size is not 0');
244 12 is($self->{last}, D(), 'Has last when size is not 0');
245 12 my $n = $self->{first};
246 12 my $c = 0;
247 12 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 20 12 32
248 20 $c++;
249 20 is($n->{list} == $self, T(), 'Self pointer in node');
250 20 ▶ if ($c < $s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $s) { } 8 12 20
251 8 is($n->{next}, D(), 'Node has next element');
252 } else {
253 12 is($n->{next}, U(), 'Node has no next element');
254 12 is($n == $self->{last}, T(), 'Correct last element');
255 }
256 20 $n = $n->{next};
257 }
258 12 is($c, $s, 'Correct node count');
259 }
260 18 };
261 }
262  
263 # The destructor is not strictly needed because all the nodes don’t have cyclic
264 # references. But let’s keep it.
265 4 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=2 SCAR=6.9
266 4 my $next = $self->{first};
267 4 ▶ while (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next) 4 4 8
268 4 my $cur = $next;
269 4 $next = $cur->{next};
270 4 undef %{$cur};
271 }
272 4 return;
273 }
274  
275 =pod
276  
277 =back
278  
279 =head1 SEE ALSO
280  
281 L<DataStructure::DoubleList>
282  
283 =head1 AUTHOR
284  
285 Mathias Kende <mathias@cpan.org>
286  
287 =head1 LICENCE
288  
289 Copyright 2021 Mathias Kende
290  
291 This program is distributed under the MIT (X11) License:
292 L<http://www.opensource.org/licenses/mit-license.php>
293  
294 Permission is hereby granted, free of charge, to any person
295 obtaining a copy of this software and associated documentation
296 files (the "Software"), to deal in the Software without
297 restriction, including without limitation the rights to use,
298 copy, modify, merge, publish, distribute, sublicense, and/or sell
299 copies of the Software, and to permit persons to whom the
300 Software is furnished to do so, subject to the following
301 conditions:
302  
303 The above copyright notice and this permission notice shall be
304 included in all copies or substantial portions of the Software.
305  
306 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
307 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
308 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
309 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
310 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
311 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
312 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
313 OTHER DEALINGS IN THE SOFTWARE.
314  
315 =cut
316  
317 1 1;
« DataStructure/BTree/Node.pm ↑ Summary DataStructure.pm »