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