line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package List::Indexed; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
$VERSION = "1.0"; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
################################################################################ |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# PUBLIC INTERFACE |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
################################################################################ |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new |
15
|
|
|
|
|
|
|
{ |
16
|
8
|
|
|
8
|
1
|
2089
|
my $class = shift(); |
17
|
|
|
|
|
|
|
|
18
|
8
|
|
|
|
|
18
|
my $obj = { }; |
19
|
8
|
|
|
|
|
24
|
bless($obj, $class); |
20
|
|
|
|
|
|
|
|
21
|
8
|
|
|
|
|
18
|
$obj->clear(); |
22
|
8
|
|
|
|
|
17
|
return $obj; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub find |
27
|
|
|
|
|
|
|
{ |
28
|
10
|
|
|
10
|
1
|
29
|
my $obj = shift(); |
29
|
10
|
|
|
|
|
15
|
my ($key) = @_; |
30
|
|
|
|
|
|
|
|
31
|
10
|
|
|
|
|
12
|
my $position = 0; |
32
|
10
|
|
|
|
|
13
|
foreach my $k (@{$obj->{'keys'}}) |
|
10
|
|
|
|
|
26
|
|
33
|
|
|
|
|
|
|
{ |
34
|
24
|
100
|
|
|
|
48
|
if ($k eq $key) { |
35
|
8
|
|
|
|
|
19
|
return $position; |
36
|
|
|
|
|
|
|
} |
37
|
16
|
|
|
|
|
21
|
$position++; |
38
|
|
|
|
|
|
|
} |
39
|
2
|
|
|
|
|
5
|
return undef; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub read |
44
|
|
|
|
|
|
|
{ |
45
|
19
|
|
|
19
|
1
|
89
|
my $obj = shift(); |
46
|
19
|
|
|
|
|
32
|
my ($key) = @_; |
47
|
|
|
|
|
|
|
|
48
|
19
|
|
|
|
|
21
|
my $element; |
49
|
19
|
100
|
|
|
|
34
|
if (defined $key) |
50
|
|
|
|
|
|
|
{ |
51
|
2
|
|
|
|
|
5
|
my $position = $obj->find($key); |
52
|
2
|
100
|
|
|
|
7
|
if (defined $position) { |
53
|
1
|
|
|
|
|
2
|
$element = $obj->{'elements'}->[$position]; |
54
|
|
|
|
|
|
|
} |
55
|
2
|
|
|
|
|
7
|
return $element; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else |
58
|
|
|
|
|
|
|
{ |
59
|
17
|
|
|
|
|
24
|
my $position = $obj->{'pointer'}; |
60
|
17
|
100
|
|
|
|
39
|
if (defined $obj->{'keys'}->[$position]) { |
61
|
16
|
|
|
|
|
35
|
($key, $element) = ($obj->{'keys'}->[$position], $obj->{'elements'}->[$position]); |
62
|
16
|
|
|
|
|
23
|
$obj->{'pointer'}++; |
63
|
|
|
|
|
|
|
} |
64
|
17
|
|
|
|
|
65
|
return ($key, $element); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub read_at |
70
|
|
|
|
|
|
|
{ |
71
|
2
|
|
|
2
|
1
|
9
|
my $obj = shift(); |
72
|
2
|
|
|
|
|
4
|
my ($position) = @_; |
73
|
|
|
|
|
|
|
|
74
|
2
|
100
|
66
|
|
|
20
|
if (defined $position && |
75
|
|
|
|
|
|
|
defined $obj->{'keys'}->[$position]) |
76
|
|
|
|
|
|
|
{ |
77
|
1
|
|
|
|
|
6
|
return ($obj->{'keys'}->[$position], $obj->{'elements'}->[$position]); |
78
|
|
|
|
|
|
|
} |
79
|
1
|
|
|
|
|
3
|
return (undef, undef); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub add |
84
|
|
|
|
|
|
|
{ |
85
|
24
|
|
|
24
|
1
|
78
|
my $obj = shift(); |
86
|
24
|
|
|
|
|
41
|
my ($key, $element) = @_; |
87
|
|
|
|
|
|
|
|
88
|
24
|
50
|
33
|
|
|
57
|
if (defined $key || |
89
|
|
|
|
|
|
|
defined $element) |
90
|
|
|
|
|
|
|
{ |
91
|
24
|
|
|
|
|
26
|
push(@{$obj->{'keys'}}, $key); |
|
24
|
|
|
|
|
47
|
|
92
|
24
|
|
|
|
|
28
|
push(@{$obj->{'elements'}}, $element); |
|
24
|
|
|
|
|
41
|
|
93
|
24
|
|
|
|
|
52
|
return 1; |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
0
|
return 0; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub insert_at |
100
|
|
|
|
|
|
|
{ |
101
|
1
|
|
|
1
|
1
|
6
|
my $obj = shift(); |
102
|
1
|
|
|
|
|
2
|
my ($position, $key, $element) = @_; |
103
|
|
|
|
|
|
|
|
104
|
1
|
50
|
33
|
|
|
6
|
if (defined $position && |
|
|
|
33
|
|
|
|
|
105
|
|
|
|
|
|
|
(defined $key || defined $element)) |
106
|
|
|
|
|
|
|
{ |
107
|
1
|
|
|
|
|
4
|
$position = $obj->_limit_position($position); |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
|
|
1
|
splice(@{$obj->{'keys'}}, $position, 0, $key); |
|
1
|
|
|
|
|
4
|
|
110
|
1
|
|
|
|
|
2
|
splice(@{$obj->{'elements'}}, $position, 0, $element); |
|
1
|
|
|
|
|
2
|
|
111
|
1
|
|
|
|
|
2
|
return 1; |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
0
|
return 0; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub insert_after |
118
|
|
|
|
|
|
|
{ |
119
|
2
|
|
|
2
|
1
|
8
|
my $obj = shift(); |
120
|
2
|
|
|
|
|
4
|
my ($position, $key, $element) = @_; |
121
|
|
|
|
|
|
|
|
122
|
2
|
50
|
33
|
|
|
9
|
if (defined $position && |
|
|
|
33
|
|
|
|
|
123
|
|
|
|
|
|
|
(defined $key || defined $element)) |
124
|
|
|
|
|
|
|
{ |
125
|
2
|
|
|
|
|
5
|
$position = $obj->_limit_position($position + 1); |
126
|
|
|
|
|
|
|
|
127
|
2
|
|
|
|
|
3
|
splice(@{$obj->{'keys'}}, $position, 0, $key); |
|
2
|
|
|
|
|
6
|
|
128
|
2
|
|
|
|
|
3
|
splice(@{$obj->{'elements'}}, $position, 0, $element); |
|
2
|
|
|
|
|
5
|
|
129
|
2
|
|
|
|
|
4
|
return 1; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
|
|
|
|
0
|
return 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub remove |
136
|
|
|
|
|
|
|
{ |
137
|
9
|
|
|
9
|
1
|
45
|
my $obj = shift(); |
138
|
9
|
|
|
|
|
11
|
my ($key) = @_; |
139
|
|
|
|
|
|
|
|
140
|
9
|
|
|
|
|
9
|
my $element; |
141
|
9
|
100
|
|
|
|
43
|
if (defined $key) |
142
|
|
|
|
|
|
|
{ |
143
|
1
|
|
|
|
|
3
|
my $position = $obj->find($key); |
144
|
1
|
50
|
|
|
|
4
|
if (defined $position) { |
145
|
1
|
|
|
|
|
3
|
$element = $obj->{'elements'}->[$position]; |
146
|
1
|
|
|
|
|
4
|
$obj->_remove_position($position); |
147
|
|
|
|
|
|
|
} |
148
|
1
|
|
|
|
|
4
|
return $element; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
else { |
151
|
8
|
|
|
|
|
11
|
($key, $element) = (shift(@{$obj->{'keys'}}), shift(@{$obj->{'elements'}})); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
13
|
|
152
|
8
|
|
|
|
|
24
|
return ($key, $element); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub remove_at |
158
|
|
|
|
|
|
|
{ |
159
|
1
|
|
|
1
|
1
|
7
|
my $obj = shift(); |
160
|
1
|
|
|
|
|
2
|
my ($position) = @_; |
161
|
|
|
|
|
|
|
|
162
|
1
|
50
|
33
|
|
|
17
|
if (defined $position && |
163
|
|
|
|
|
|
|
defined $obj->{'keys'}->[$position]) |
164
|
|
|
|
|
|
|
{ |
165
|
1
|
|
|
|
|
4
|
my ($key, $element) = ($obj->{'keys'}->[$position], $obj->{'elements'}->[$position]); |
166
|
1
|
|
|
|
|
10
|
$obj->_remove_position($position); |
167
|
1
|
|
|
|
|
4
|
return ($key, $element); |
168
|
|
|
|
|
|
|
} |
169
|
0
|
|
|
|
|
0
|
return (undef, undef); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub replace |
174
|
|
|
|
|
|
|
{ |
175
|
1
|
|
|
1
|
1
|
10
|
my $obj = shift(); |
176
|
1
|
|
|
|
|
3
|
my ($key, $element) = @_; |
177
|
|
|
|
|
|
|
|
178
|
1
|
50
|
|
|
|
19
|
if (defined $key) |
179
|
|
|
|
|
|
|
{ |
180
|
1
|
|
|
|
|
3
|
my $position = $obj->find($key); |
181
|
1
|
50
|
|
|
|
4
|
if (defined $position) { |
182
|
1
|
|
|
|
|
2
|
$obj->{'elements'}->[$position] = $element; |
183
|
1
|
|
|
|
|
3
|
return 1; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
0
|
|
|
|
|
0
|
return 0; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub replace_at |
191
|
|
|
|
|
|
|
{ |
192
|
1
|
|
|
1
|
1
|
4
|
my $obj = shift(); |
193
|
1
|
|
|
|
|
2
|
my ($position, $element) = @_; |
194
|
|
|
|
|
|
|
|
195
|
1
|
50
|
33
|
|
|
16
|
if (defined $position && |
196
|
|
|
|
|
|
|
defined $obj->{'keys'}->[$position]) |
197
|
|
|
|
|
|
|
{ |
198
|
1
|
|
|
|
|
3
|
$obj->{'elements'}->[$position] = $element; |
199
|
1
|
|
|
|
|
2
|
return 1; |
200
|
|
|
|
|
|
|
} |
201
|
0
|
|
|
|
|
0
|
return 0; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub reset |
206
|
|
|
|
|
|
|
{ |
207
|
1
|
|
|
1
|
1
|
6
|
my $obj = shift(); |
208
|
|
|
|
|
|
|
|
209
|
1
|
|
|
|
|
3
|
$obj->{'pointer'} = 0; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub size |
214
|
|
|
|
|
|
|
{ |
215
|
5
|
|
|
5
|
1
|
20
|
my $obj = shift(); |
216
|
|
|
|
|
|
|
|
217
|
5
|
|
|
|
|
6
|
return @{$obj->{'keys'}}; |
|
5
|
|
|
|
|
14
|
|
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub empty |
222
|
|
|
|
|
|
|
{ |
223
|
1
|
|
|
1
|
1
|
4
|
my $obj = shift(); |
224
|
|
|
|
|
|
|
|
225
|
1
|
|
|
|
|
2
|
return (@{$obj->{'keys'}} == 0); |
|
1
|
|
|
|
|
3
|
|
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub clear |
230
|
|
|
|
|
|
|
{ |
231
|
8
|
|
|
8
|
1
|
25
|
my $obj = shift(); |
232
|
|
|
|
|
|
|
|
233
|
8
|
|
|
|
|
23
|
$obj->{'keys'} = []; |
234
|
8
|
|
|
|
|
17
|
$obj->{'elements'} = []; |
235
|
8
|
|
|
|
|
15
|
$obj->{'pointer'} = 0; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
################################################################################ |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# PRIVATE IMPLEMENTATION |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
################################################################################ |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub _remove_position |
247
|
|
|
|
|
|
|
{ |
248
|
2
|
|
|
2
|
|
5
|
my $obj = shift(); |
249
|
2
|
|
|
|
|
3
|
my $position = shift(); |
250
|
|
|
|
|
|
|
|
251
|
2
|
|
|
|
|
4
|
splice(@{$obj->{'keys'}}, $position, 1); |
|
2
|
|
|
|
|
22
|
|
252
|
2
|
|
|
|
|
4
|
splice(@{$obj->{'elements'}}, $position, 1); |
|
2
|
|
|
|
|
5
|
|
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub _limit_position |
257
|
|
|
|
|
|
|
{ |
258
|
3
|
|
|
3
|
|
5
|
my $obj = shift(); |
259
|
3
|
|
|
|
|
4
|
my $position = shift(); |
260
|
|
|
|
|
|
|
|
261
|
3
|
50
|
|
|
|
6
|
if ($position < 0) { |
|
3
|
100
|
|
|
|
8
|
|
262
|
0
|
|
|
|
|
0
|
return 0; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
elsif ($position > @{$obj->{'keys'}}) { |
265
|
1
|
|
|
|
|
3
|
return scalar(@{$obj->{'keys'}}); |
|
1
|
|
|
|
|
2
|
|
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
else { |
268
|
2
|
|
|
|
|
4
|
return $position; |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
1; |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
__END__ |