File Coverage

blib/lib/DataStructure/DoubleList.pm
Criterion Covered Total %
statement 95 95 100.0
branch 13 14 92.8
condition n/a
subroutine 19 19 100.0
pod 9 10 90.0
total 136 138 98.5


line stmt bran cond sub pod time code
1             # A double-linked list data-structure.
2              
3             package DataStructure::DoubleList;
4              
5 1     1   196158 use strict;
  1         5  
  1         29  
6 1     1   5 use warnings;
  1         2  
  1         22  
7 1     1   5 use utf8;
  1         2  
  1         6  
8 1     1   22 use feature ':5.24';
  1         2  
  1         156  
9 1     1   7 use feature 'signatures';
  1         2  
  1         26  
10 1     1   5 no warnings 'experimental::signatures';
  1         1  
  1         31  
11              
12 1     1   479 use DataStructure::DoubleList::Node;
  1         3  
  1         37  
13              
14 1     1   414 use parent qw(DataStructure::Queue DataStructure::Stack);
  1         293  
  1         9  
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 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     3 0 115278 sub new ($class) {
  3         7  
  3         5  
39 3         16 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 object. Unless documented otherwise, they run in
48             constant time.
49              
50             =over 4
51              
52             =item first()
53              
54             Returns the first L of the list, or B if
55             the list is empty.
56              
57             =cut
58              
59 33     33 1 51 sub first ($self) {
  33         48  
  33         46  
60 33         192 return $self->{first};
61             }
62              
63             =pod
64              
65             =item last()
66              
67             Returns the last L of the list, or B if
68             the list is empty.
69              
70             =cut
71              
72 5     5 1 12 sub last ($self) {
  5         9  
  5         9  
73 5         16 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     4 1 10 sub push ($self, $value) {
  4         6  
  4         7  
  4         6  
86 4         13 my $new_node = DataStructure::DoubleList::Node->new($self, $self->{last}, undef, $value);
87 4 100       15 $self->{last}{next} = $new_node if defined $self->{last};
88 4         9 $self->{last} = $new_node;
89 4 100       10 $self->{first} = $new_node unless defined $self->{first};
90 4         7 $self->{size}++;
91 4         9 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     8 1 723 sub unshift ($self, $value) {
  8         15  
  8         13  
  8         10  
105 8         34 my $new_node = DataStructure::DoubleList::Node->new($self, undef, $self->{first}, $value);
106 8 100       25 $self->{first}{prev} = $new_node if defined $self->{first};
107 8         11 $self->{first} = $new_node;
108 8 100       21 $self->{last} = $new_node unless defined $self->{last};
109 8         13 $self->{size}++;
110 8         15 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 if the
118             list is empty. Note that the method can also return B if the last node’s
119             value is B
120              
121             =cut
122              
123 8     8 1 15 sub pop ($self) {
  8         13  
  8         12  
124 8 100       33 return $self->{last}->delete if defined $self->{last};
125 1         4 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 if
134             the list is empty. Note that the method can also return B if the first
135             node’s value is B
136              
137             =cut
138              
139 10     10 1 21 sub shift ($self) {
  10         17  
  10         12  
140 10 100       41 return $self->{first}->delete if defined $self->{first};
141 2         8 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     36 1 6528 sub size ($self) {
  36         55  
  36         52  
153 36         134 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     5 1 11 sub empty ($self) {
  5         8  
  5         8  
165 5         11 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     25 1 53 sub values ($self) {
  25         39  
  25         36  
178 25 50       58 return $self->size() unless wantarray;
179 25         52 my @ret = (0) x $self->size();
180 25         43 my $i = 0;
181 25         52 my $cur = $self->first();
182 25         65 while (defined $cur) {
183 116         217 $ret[$i++] = $cur->value();
184 116         210 $cur = $cur->next();
185             }
186 25         167 return @ret;
187             }
188              
189 3     3   1108 sub DESTROY ($self) {
  3         6  
  3         4  
190 3         6 my $next = $self->{first};
191 3         8 while (defined $next) {
192 5         10 my $cur = $next;
193 5         8 $next = $cur->{next};
194 5         8 undef %{$cur};
  5         14  
195             }
196 3         11 return;
197             }
198              
199             =pod
200              
201             =back
202              
203             =head1 SEE ALSO
204              
205             L, L
206              
207             =head1 AUTHOR
208              
209             Mathias Kende
210              
211             =head1 LICENCE
212              
213             Copyright 2021 Mathias Kende
214              
215             This program is distributed under the MIT (X11) License:
216             L
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;