| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Tie::Array::Iterable::BackwardIterator; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
#============================================================================= |
|
6
|
|
|
|
|
|
|
# |
|
7
|
|
|
|
|
|
|
# $Id: BackwardIterator.pm,v 0.03 2001/11/16 02:27:58 mneylon Exp $ |
|
8
|
|
|
|
|
|
|
# $Revision: 0.03 $ |
|
9
|
|
|
|
|
|
|
# $Author: mneylon $ |
|
10
|
|
|
|
|
|
|
# $Date: 2001/11/16 02:27:58 $ |
|
11
|
|
|
|
|
|
|
# $Log: BackwardIterator.pm,v $ |
|
12
|
|
|
|
|
|
|
# Revision 0.03 2001/11/16 02:27:58 mneylon |
|
13
|
|
|
|
|
|
|
# Fixed packing version variables |
|
14
|
|
|
|
|
|
|
# |
|
15
|
|
|
|
|
|
|
# Revision 0.01.01.2 2001/11/16 02:12:16 mneylon |
|
16
|
|
|
|
|
|
|
# Added code to clean up iterators after use |
|
17
|
|
|
|
|
|
|
# clear_iterators() now not needed, simply returns 1; |
|
18
|
|
|
|
|
|
|
# |
|
19
|
|
|
|
|
|
|
# Revision 0.01.01.1 2001/11/15 01:41:21 mneylon |
|
20
|
|
|
|
|
|
|
# Branch from 0.01 for new features |
|
21
|
|
|
|
|
|
|
# |
|
22
|
|
|
|
|
|
|
# Revision 0.01 2001/11/11 18:36:14 mneylon |
|
23
|
|
|
|
|
|
|
# Initial Release |
|
24
|
|
|
|
|
|
|
# |
|
25
|
|
|
|
|
|
|
# |
|
26
|
|
|
|
|
|
|
#============================================================================= |
|
27
|
|
|
|
|
|
|
|
|
28
|
2
|
|
|
2
|
|
63
|
use 5.006; |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
88
|
|
|
29
|
2
|
|
|
2
|
|
12
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
114
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $BACKWARDID; |
|
32
|
|
|
|
|
|
|
my %BACKWARDITERS; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
BEGIN { |
|
35
|
2
|
|
|
2
|
|
12
|
use Exporter (); |
|
|
2
|
|
|
|
|
15
|
|
|
|
2
|
|
|
|
|
602
|
|
|
36
|
2
|
|
|
2
|
|
13
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
434
|
|
|
37
|
2
|
|
|
2
|
|
13
|
( $VERSION ) = '$Revision: 0.03 $ ' =~ /\$Revision:\s+([^\s]+)/; |
|
38
|
2
|
|
|
|
|
101
|
@ISA = qw( Exporter ); |
|
39
|
2
|
|
|
|
|
5
|
@EXPORT = qw( ); |
|
40
|
2
|
|
|
|
|
3
|
@EXPORT_OK = qw( ); |
|
41
|
2
|
|
|
|
|
2317
|
%EXPORT_TAGS = ( ); |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
|
45
|
1
|
|
|
1
|
0
|
2
|
my $class = shift; |
|
46
|
1
|
|
|
|
|
2
|
my $iterarray = shift; |
|
47
|
1
|
|
50
|
|
|
4
|
my $pos = shift || scalar @$iterarray; |
|
48
|
1
|
50
|
|
|
|
36
|
warn "Must be created from a Tie::Array::Iterable" |
|
49
|
|
|
|
|
|
|
unless ( UNIVERSAL::isa( $iterarray, "Tie::Array::Iterable" ) ); |
|
50
|
1
|
|
|
|
|
6
|
my %data = ( |
|
51
|
|
|
|
|
|
|
array => $iterarray, |
|
52
|
|
|
|
|
|
|
pos => $pos, |
|
53
|
|
|
|
|
|
|
id => ++$BACKWARDID ); |
|
54
|
1
|
|
|
|
|
4
|
$BACKWARDITERS{ $data{ id } } = \%data; |
|
55
|
1
|
|
|
|
|
5
|
return bless \%data, $class; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub DESTORY { |
|
59
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
|
60
|
0
|
|
|
|
|
0
|
$self->{ array }->_remove_backward_iterator( $self->{ id } ); |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub at_start { |
|
64
|
33
|
|
|
33
|
0
|
36
|
my $self = shift; |
|
65
|
33
|
100
|
|
|
|
45
|
if ( $self->{ pos } >= scalar @{ $self->{ array } } ) { |
|
|
33
|
|
|
|
|
93
|
|
|
66
|
17
|
|
|
|
|
49
|
return 1; |
|
67
|
|
|
|
|
|
|
} else { |
|
68
|
16
|
|
|
|
|
44
|
return 0; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub at_end { |
|
73
|
109
|
|
|
109
|
0
|
105
|
my $self = shift; |
|
74
|
109
|
100
|
|
|
|
219
|
if ( $self->{ pos } <= 0 ) { |
|
75
|
23
|
|
|
|
|
63
|
return 1; |
|
76
|
|
|
|
|
|
|
} else { |
|
77
|
86
|
|
|
|
|
218
|
return 0; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub to_start { |
|
82
|
2
|
|
|
2
|
0
|
4
|
my $self = shift; |
|
83
|
2
|
|
|
|
|
3
|
$self->{ pos } = scalar @{ $self->{ array } }; |
|
|
2
|
|
|
|
|
7
|
|
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub to_end { |
|
87
|
2
|
|
|
2
|
0
|
5
|
my $self = shift; |
|
88
|
2
|
|
|
|
|
4
|
$self->{ pos } = 0; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub value { |
|
92
|
71
|
|
|
71
|
0
|
126
|
my $self = shift; |
|
93
|
71
|
100
|
|
|
|
112
|
if ( $self->at_end() ) { return undef }; |
|
|
7
|
|
|
|
|
23
|
|
|
94
|
64
|
|
|
|
|
252
|
return $self->{ array }->[ $self->{ pos } - 1 ]; |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub set_value { |
|
98
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
|
99
|
0
|
|
|
|
|
0
|
my $value = shift; |
|
100
|
0
|
0
|
|
|
|
0
|
if ( $self->at_end() ) { return undef; }; |
|
|
0
|
|
|
|
|
0
|
|
|
101
|
0
|
|
|
|
|
0
|
return ( $self->{ array }->[ $self->{ pos } - 1 ] = $value ); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub index { |
|
105
|
56
|
|
|
56
|
0
|
78
|
my $self = shift; |
|
106
|
56
|
|
|
|
|
231
|
return $self->{ pos }; |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub set_index { |
|
110
|
10
|
|
|
10
|
0
|
12
|
my $self = shift; |
|
111
|
10
|
|
|
|
|
12
|
my $index = shift(); |
|
112
|
10
|
50
|
|
|
|
19
|
if ( $index < 0 ) { $index = 1; } |
|
|
0
|
|
|
|
|
0
|
|
|
113
|
10
|
50
|
|
|
|
16
|
if ( $index > scalar @{ $self->{ array } } ) |
|
|
10
|
|
|
|
|
33
|
|
|
114
|
0
|
|
|
|
|
0
|
{ $index = scalar @{ $self->{ array } }; } |
|
|
0
|
|
|
|
|
0
|
|
|
115
|
10
|
|
|
|
|
43
|
$self->{ pos } = $index; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub next { |
|
119
|
34
|
|
|
34
|
0
|
43
|
my $self = shift; |
|
120
|
34
|
100
|
|
|
|
43
|
if ( $self->at_end() ) { |
|
121
|
13
|
|
|
|
|
37
|
return undef; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
21
|
|
|
|
|
26
|
$self->{ pos }--; |
|
124
|
21
|
|
|
|
|
35
|
return $self->value(); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub prev { |
|
128
|
29
|
|
|
29
|
0
|
34
|
my $self = shift; |
|
129
|
29
|
100
|
|
|
|
42
|
if ( $self->at_start() ) { |
|
130
|
14
|
|
|
|
|
41
|
return undef; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
15
|
|
|
|
|
22
|
$self->{ pos }++; |
|
133
|
15
|
|
|
|
|
27
|
return $self->value(); |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub forward { |
|
137
|
5
|
|
|
5
|
0
|
16
|
my $self = shift; |
|
138
|
5
|
|
|
|
|
6
|
my $steps = shift; |
|
139
|
5
|
50
|
|
|
|
15
|
die "Number of steps must be non-negative" if $steps < 0; |
|
140
|
5
|
50
|
33
|
|
|
16
|
$steps = 1 if ( !$steps && $steps ne "0" ); |
|
141
|
5
|
|
|
|
|
11
|
my $value = $self->value(); |
|
142
|
5
|
|
|
|
|
51
|
$value = $self->next() for ( 1..$steps ); |
|
143
|
5
|
|
|
|
|
12
|
return $value; |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub backward { |
|
147
|
3
|
|
|
3
|
0
|
8
|
my $self = shift; |
|
148
|
3
|
|
|
|
|
3
|
my $steps = shift; |
|
149
|
3
|
50
|
|
|
|
8
|
die "Number of steps must be non-negative" if $steps < 0; |
|
150
|
3
|
50
|
33
|
|
|
12
|
$steps = 1 if ( !$steps && $steps ne "0" ); |
|
151
|
3
|
|
|
|
|
7
|
my $value = $self->value(); |
|
152
|
3
|
|
|
|
|
12
|
$value = $self->prev() for ( 1..$steps ); |
|
153
|
3
|
|
|
|
|
8
|
return $value; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _lookup ($) { |
|
157
|
16
|
|
|
16
|
|
54
|
return $BACKWARDITERS{ +shift }; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub _id { |
|
161
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
|
162
|
1
|
|
|
|
|
11
|
return $self->{ id }; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
__END__ |