line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Array::LIFO; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Last-in, First-out array |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
164790
|
use Moose; |
|
2
|
|
|
|
|
1019782
|
|
|
2
|
|
|
|
|
15
|
|
9
|
2
|
|
|
2
|
|
15944
|
use namespace::autoclean; |
|
2
|
|
|
|
|
16885
|
|
|
2
|
|
|
|
|
11
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has max_size => ( |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
isa => 'Int', |
16
|
|
|
|
|
|
|
default => -1, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has sum => ( |
20
|
|
|
|
|
|
|
is => 'rw', |
21
|
|
|
|
|
|
|
isa => 'Int', |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has average => ( |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
isa => 'Num', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has queue => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
isa => 'ArrayRef[Item]', |
32
|
|
|
|
|
|
|
traits => [ 'Array' ], |
33
|
|
|
|
|
|
|
default => sub { [] }, |
34
|
|
|
|
|
|
|
handles => { |
35
|
|
|
|
|
|
|
add => 'unshift', |
36
|
|
|
|
|
|
|
remove => 'shift', |
37
|
|
|
|
|
|
|
size => 'count', |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
trigger => sub { |
40
|
|
|
|
|
|
|
my $self = shift; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
if ( $self->{max_size} > 0 ) { |
43
|
|
|
|
|
|
|
my $array = $self->{queue}; |
44
|
|
|
|
|
|
|
while ( @{ $array } > $self->{max_size} ) { |
45
|
|
|
|
|
|
|
shift @{ $array }; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $size = $self->size; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $sum = 0; |
52
|
|
|
|
|
|
|
for my $q ( @{ $self->queue } ) { |
53
|
|
|
|
|
|
|
if ( $q =~ /^-?\d+\.?\d*$/ ) { |
54
|
|
|
|
|
|
|
$sum += $q; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$self->sum($sum); |
59
|
|
|
|
|
|
|
if ( $sum > 0 ) { |
60
|
|
|
|
|
|
|
$self->average( $sum / $size ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
|
|
|
|
|
|
$self->average(0); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
}, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
around add => sub { |
69
|
|
|
|
|
|
|
my $orig = shift; |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
$self->$orig(@_); |
72
|
|
|
|
|
|
|
my $last = $self->{queue}[0]; |
73
|
|
|
|
|
|
|
$last; |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub peek { |
77
|
4
|
|
|
4
|
1
|
608
|
my $self = shift; |
78
|
4
|
|
50
|
|
|
20
|
my $element = shift || 0; |
79
|
4
|
|
|
|
|
123
|
return $self->queue->[$element]; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=pod |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=encoding UTF-8 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Array::LIFO - Last-in, First-out array |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 VERSION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
version 0.02 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use Array::LIFO; |
103
|
|
|
|
|
|
|
my $ar = Array::LIFO->new( max_size => 12 ); |
104
|
|
|
|
|
|
|
$ar->add(20); |
105
|
|
|
|
|
|
|
$ar->add(18); |
106
|
|
|
|
|
|
|
$ar->add(22); |
107
|
|
|
|
|
|
|
print $ar->size, "\n"; |
108
|
|
|
|
|
|
|
print $ar->average, "\n"; |
109
|
|
|
|
|
|
|
print $ar->sum, "\n"; |
110
|
|
|
|
|
|
|
print $ar->peek, "\n"; |
111
|
|
|
|
|
|
|
$ar->remove; |
112
|
|
|
|
|
|
|
print "@{ $ar->queue }\n"; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 DESCRIPTION |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
An C<Array::LIFO> allows the declaration of an array with last-in, first-out |
117
|
|
|
|
|
|
|
operation. That is, a "stack." |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 NAME |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Array::LIFO - Last-in, First-out array |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 METHODS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 new |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$x = Array::LIFO->new; |
128
|
|
|
|
|
|
|
$x = Array::LIFO->new( max_size => $m ); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=over 4 |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item max_size (optional) |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Numeric value of how large the array is allowed to get. When it reaches |
135
|
|
|
|
|
|
|
max_size, new items are not added. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
If no value is passed, there is no max size. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 add |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$ar->add($x); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
You can add any type of item to the array. If the element is not a number it |
146
|
|
|
|
|
|
|
will be ignored by the C<sum()> and C<average()> calculations. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 remove |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$ar->remove; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Remove the last item on the array. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 size |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$size = $ar->size; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
How many elements are in the array. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 max_size |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$max = $ar->max_size; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
The maximum size the array is allow to be. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 sum |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
$sum = $ar->sum; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The sum of all numeric elements in the array. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 average |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$avg = $ar->average; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
The average of all numeric elements in the array. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 peek |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
$last = $ar->peek; |
181
|
|
|
|
|
|
|
$element = $ar->peek($index); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Return an element of the array given by the index argument. If no index is |
184
|
|
|
|
|
|
|
provided, the last element added to the stack is returned. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head1 SEE ALSO |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
L<Array::FIFO> |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 AUTHOR |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Gene Boggs <gene@cpan.org> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
This software is copyright (c) 2015 by Gene Boggs. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
199
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |