line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Array::Sorted::Lazy; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2147
|
use base 'Tie::Array'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
105
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
6
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
860
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Tie::Array::Sorted::Lazy - An array which is kept sorted |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Tie::Array::Sorted::Lazy; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
tie @a, "Tie::Array::Sorted::Lazy", sub { $_[0] <=> $_[1] }; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
push @a, 10, 4, 7, 3, 4; |
19
|
|
|
|
|
|
|
print "@a"; # "3 4 4 7 10" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This is a version Tie::Array::Sorted optimised for arrays which are |
24
|
|
|
|
|
|
|
stored to more often than fetching. In this case the array is resorted |
25
|
|
|
|
|
|
|
on retrieval, rather than insertion. (It only re-sorts if data has been |
26
|
|
|
|
|
|
|
modified since the last sort). |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
tie @a, "Tie::Array::Sorted::Lazy", sub { -s $_[0] <=> -s $_[1] }; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub TIEARRAY { |
33
|
1
|
|
|
1
|
|
2603
|
my ($class, $comparator) = @_; |
34
|
|
|
|
|
|
|
bless { |
35
|
|
|
|
|
|
|
array => [], |
36
|
0
|
|
|
0
|
|
0
|
comp => (defined $comparator ? $comparator : sub { $_[0] cmp $_[1] }) |
37
|
1
|
50
|
|
|
|
12
|
}, $class; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub STORE { |
41
|
0
|
|
|
0
|
|
0
|
my ($self, $index, $elem) = @_; |
42
|
0
|
|
|
|
|
0
|
splice @{ $self->{array} }, $index, 0; |
|
0
|
|
|
|
|
0
|
|
43
|
0
|
|
|
|
|
0
|
$self->PUSH($elem); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub PUSH { |
47
|
6
|
|
|
6
|
|
1244
|
my $self = shift; |
48
|
6
|
|
|
|
|
11
|
$self->{dirty} = 1; |
49
|
6
|
|
|
|
|
8
|
push @{ $self->{array} }, @_; |
|
6
|
|
|
|
|
24
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub UNSHIFT { |
53
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
54
|
0
|
|
|
|
|
0
|
$self->{dirty} = 1; |
55
|
0
|
|
|
|
|
0
|
push @{ $self->{array} }, @_; |
|
0
|
|
|
|
|
0
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _fixup { |
59
|
6
|
|
|
6
|
|
9
|
my $self = shift; |
60
|
6
|
|
|
|
|
8
|
$self->{array} = [ sort { $self->{comp}->($a, $b) } @{ $self->{array} } ]; |
|
26
|
|
|
|
|
86
|
|
|
6
|
|
|
|
|
25
|
|
61
|
6
|
|
|
|
|
32
|
$self->{dirty} = 0; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub FETCH { |
65
|
27
|
100
|
|
27
|
|
6211
|
$_[0]->_fixup if $_[0]->{dirty}; |
66
|
27
|
|
|
|
|
151
|
$_[0]->{array}->[ $_[1] ]; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub FETCHSIZE { |
70
|
4
|
|
|
4
|
|
2901
|
scalar @{ $_[0]->{array} } |
|
4
|
|
|
|
|
24
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub STORESIZE { |
74
|
0
|
0
|
|
0
|
|
0
|
$_[0]->_fixup if $_[0]->{dirty}; |
75
|
0
|
|
|
|
|
0
|
$#{ $_[0]->{array} } = $_[1] - 1; |
|
0
|
|
|
|
|
0
|
|
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub POP { |
79
|
1
|
50
|
|
1
|
|
594
|
$_[0]->_fixup if $_[0]->{dirty}; |
80
|
1
|
|
|
|
|
24
|
pop(@{ $_[0]->{array} }); |
|
1
|
|
|
|
|
239
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub SHIFT { |
84
|
0
|
0
|
|
0
|
|
0
|
$_[0]->_fixup if $_[0]->{dirty}; |
85
|
0
|
|
|
|
|
0
|
shift(@{ $_[0]->{array} }); |
|
0
|
|
|
|
|
0
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub EXISTS { |
89
|
0
|
0
|
|
0
|
|
0
|
$_[0]->_fixup if $_[0]->{dirty}; |
90
|
0
|
|
|
|
|
0
|
exists $_[0]->{array}->[ $_[1] ]; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub DELETE { |
94
|
0
|
0
|
|
0
|
|
0
|
$_[0]->_fixup if $_[0]->{dirty}; |
95
|
0
|
|
|
|
|
0
|
delete $_[0]->{array}->[ $_[1] ]; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub CLEAR { |
99
|
1
|
|
|
1
|
|
6
|
@{ $_[0]->{array} } = () |
|
1
|
|
|
|
|
17
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|